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.user;
20  
21  import java.util.ArrayList;
22  import java.util.List;
23  
24  import org.slf4j.Logger;
25  import org.slf4j.LoggerFactory;
26  import org.springframework.beans.factory.annotation.Autowired;
27  import org.springframework.security.access.annotation.Secured;
28  import org.springframework.stereotype.Service;
29  import org.springframework.transaction.annotation.Propagation;
30  import org.springframework.transaction.annotation.Transactional;
31  
32  import com.hack23.cia.model.external.riksdagen.documentcontent.impl.DocumentContentData;
33  import com.hack23.cia.model.external.riksdagen.dokumentlista.impl.DocumentElement;
34  import com.hack23.cia.model.internal.application.system.impl.ApplicationEventGroup;
35  import com.hack23.cia.model.internal.application.system.impl.ApplicationOperationType;
36  import com.hack23.cia.model.internal.application.user.impl.UserAccount;
37  import com.hack23.cia.service.api.action.application.CreateApplicationEventRequest;
38  import com.hack23.cia.service.api.action.application.CreateApplicationEventResponse;
39  import com.hack23.cia.service.api.action.common.ServiceResponse.ServiceResult;
40  import com.hack23.cia.service.api.action.user.SearchDocumentRequest;
41  import com.hack23.cia.service.api.action.user.SearchDocumentResponse;
42  import com.hack23.cia.service.data.api.DocumentContentDataDAO;
43  import com.hack23.cia.service.data.api.DocumentElementDAO;
44  import com.hack23.cia.service.impl.action.common.AbstractBusinessServiceImpl;
45  import com.hack23.cia.service.impl.action.common.BusinessService;
46  
47  /**
48   * The Class SearchDocumentService.
49   */
50  @Service
51  @Transactional(propagation = Propagation.REQUIRED,timeout=600)
52  public final class SearchDocumentService extends
53  		AbstractBusinessServiceImpl<SearchDocumentRequest, SearchDocumentResponse>
54  		implements BusinessService<SearchDocumentRequest, SearchDocumentResponse> {
55  
56  	/** The Constant LOGGER. */
57  	private static final Logger LOGGER = LoggerFactory
58  			.getLogger(SearchDocumentService.class);
59  
60  	/** The create application event service. */
61  	@Autowired
62  	private BusinessService<CreateApplicationEventRequest, CreateApplicationEventResponse> createApplicationEventService;
63  
64  	/** The document element dao. */
65  	@Autowired
66  	private DocumentElementDAO documentElementDAO;
67  
68  	@Autowired
69  	private DocumentContentDataDAO documentContentDataDAO;
70  
71  	/**
72  	 * Instantiates a new search document service.
73  	 */
74  	public SearchDocumentService() {
75  		super(SearchDocumentRequest.class);
76  	}
77  
78  
79  	@Secured({ "ROLE_USER", "ROLE_ADMIN", "ROLE_ANONYMOUS" })
80  	@Override
81  	public SearchDocumentResponse processService(
82  			final SearchDocumentRequest serviceRequest) {
83  
84  		LOGGER.info("{}:{}",serviceRequest.getClass().getSimpleName(),serviceRequest.getSearchExpression());
85  
86  
87  		final CreateApplicationEventRequest eventRequest = new CreateApplicationEventRequest();
88  		eventRequest.setEventGroup(ApplicationEventGroup.USER);
89  		eventRequest.setApplicationOperation(ApplicationOperationType.READ);
90  		eventRequest.setActionName(SearchDocumentRequest.class.getSimpleName());
91  		eventRequest.setSessionId(serviceRequest.getSessionId());
92  
93  		final UserAccount userAccount = getUserAccountFromSecurityContext();
94  
95  
96  		if (userAccount != null) {
97  
98  			eventRequest.setUserId(userAccount.getUserId());
99  		}
100 
101 		final SearchDocumentResponse response = new SearchDocumentResponse(ServiceResult.SUCCESS);
102 
103 		final List<DocumentElement> searchResultTitles = documentElementDAO.search(serviceRequest.getSearchExpression(), serviceRequest.getMaxResults(),"id", "title","subTitle");
104 		if (!searchResultTitles.isEmpty()) {
105 		 response.setResultElement(searchResultTitles);
106 		} else {
107 			final List<DocumentContentData> searchResultContent = documentContentDataDAO.search(serviceRequest.getSearchExpression(), serviceRequest.getMaxResults(), "id","content");
108 			if (!searchResultContent.isEmpty()) {
109 				final List<DocumentElement> searchResultTitlesForContent = new ArrayList<>();
110 
111 				for (final DocumentContentData documentContent : searchResultContent) {
112 
113 					searchResultTitlesForContent.add(documentElementDAO.load(documentContent.getId()));
114 				}
115 
116 				response.setResultElement(searchResultTitlesForContent);
117 
118 			}
119 		}
120 
121 		eventRequest.setApplicationMessage(response.getResult().toString());
122 		createApplicationEventService.processService(eventRequest);
123 
124 		return response;
125 	}
126 
127 
128 
129 }