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