1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  package com.hack23.cia.service.impl.action.admin;
20  
21  import org.slf4j.Logger;
22  import org.slf4j.LoggerFactory;
23  import org.springframework.beans.factory.annotation.Autowired;
24  import org.springframework.security.access.annotation.Secured;
25  import org.springframework.stereotype.Service;
26  import org.springframework.transaction.annotation.Propagation;
27  import org.springframework.transaction.annotation.Transactional;
28  
29  import com.hack23.cia.model.internal.application.system.impl.ApplicationEventGroup;
30  import com.hack23.cia.model.internal.application.system.impl.ApplicationOperationType;
31  import com.hack23.cia.model.internal.application.user.impl.UserAccount;
32  import com.hack23.cia.service.api.action.admin.UpdateSearchIndexRequest;
33  import com.hack23.cia.service.api.action.admin.UpdateSearchIndexResponse;
34  import com.hack23.cia.service.api.action.application.CreateApplicationEventRequest;
35  import com.hack23.cia.service.api.action.application.CreateApplicationEventResponse;
36  import com.hack23.cia.service.api.action.common.ServiceResponse.ServiceResult;
37  import com.hack23.cia.service.data.api.SearchIndexer;
38  import com.hack23.cia.service.impl.action.common.AbstractBusinessServiceImpl;
39  import com.hack23.cia.service.impl.action.common.BusinessService;
40  
41  
42  
43  
44  @Service
45  @Transactional(propagation = Propagation.REQUIRED,timeout=1200)
46  public final class UpdateSearchIndexService extends
47  		AbstractBusinessServiceImpl<UpdateSearchIndexRequest, UpdateSearchIndexResponse>
48  		implements BusinessService<UpdateSearchIndexRequest, UpdateSearchIndexResponse> {
49  
50  	
51  	private static final Logger LOGGER = LoggerFactory
52  			.getLogger(UpdateSearchIndexService.class);
53  
54  	
55  	@Autowired
56  	private SearchIndexer searchIndexer;
57  
58  	
59  	@Autowired
60  	private BusinessService<CreateApplicationEventRequest, CreateApplicationEventResponse> createApplicationEventService;
61  
62  	
63  
64  
65  	public UpdateSearchIndexService() {
66  		super(UpdateSearchIndexRequest.class);
67  	}
68  
69  	@Override
70  	@Secured({ "ROLE_ADMIN" })
71  	public UpdateSearchIndexResponse processService(
72  			final UpdateSearchIndexRequest serviceRequest) {
73  
74  
75  		final CreateApplicationEventRequest eventRequest = new CreateApplicationEventRequest();
76  		eventRequest.setEventGroup(ApplicationEventGroup.ADMIN);
77  		eventRequest.setApplicationOperation(ApplicationOperationType.UPDATE);
78  		eventRequest.setActionName(UpdateSearchIndexRequest.class.getSimpleName());
79  		eventRequest.setSessionId(serviceRequest.getSessionId());
80  
81  		final UserAccount userAccount = getUserAccountFromSecurityContext();
82  
83  
84  		if (userAccount != null) {
85  			LOGGER.info("{} started:{}", serviceRequest.getClass().getSimpleName(),userAccount.getEmail());
86  
87  			eventRequest.setUserId(userAccount.getUserId());
88  		}
89  
90  		UpdateSearchIndexResponse response;
91  		try {
92  			searchIndexer.updateSearchIndex();
93  			response = new UpdateSearchIndexResponse(ServiceResult.SUCCESS);
94  		} catch (final InterruptedException e) {
95  			LOGGER.warn("Update Index failed",e);
96  		    Thread.currentThread().interrupt();
97  		    response = new UpdateSearchIndexResponse(ServiceResult.FAILURE);
98  		}
99  
100 		eventRequest.setApplicationMessage(response.getResult().toString());
101 		createApplicationEventService.processService(eventRequest);
102 
103 		return response;
104 	}
105 
106 
107 
108 }