View Javadoc
1   /*
2    * Copyright 2010 James Pether Sörling
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *   http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   *
16   *	$Id$
17   *  $HeadURL$
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   * The Class UpdateSearchIndexService.
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  	/** The Constant LOGGER. */
51  	private static final Logger LOGGER = LoggerFactory
52  			.getLogger(UpdateSearchIndexService.class);
53  
54  	/** The search indexer. */
55  	@Autowired
56  	private SearchIndexer searchIndexer;
57  
58  	/** The create application event service. */
59  	@Autowired
60  	private BusinessService<CreateApplicationEventRequest, CreateApplicationEventResponse> createApplicationEventService;
61  
62  	/**
63  	 * Instantiates a new update search index service.
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 }