View Javadoc

1   /*
2   Copyright 2010 James Pether Sörling Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. 
3   	$Id
4   */
5   package com.hack23.cia.service.impl.common;
6   
7   import org.apache.commons.logging.Log;
8   import org.apache.commons.logging.LogFactory;
9   import org.springframework.transaction.annotation.Propagation;
10  import org.springframework.transaction.annotation.Transactional;
11  
12  import com.hack23.cia.model.application.dto.common.UserSessionDTO;
13  import com.hack23.cia.model.application.impl.common.ActionEvent;
14  import com.hack23.cia.model.application.impl.common.UserSession;
15  import com.hack23.cia.service.api.common.AbstractServiceRequest;
16  import com.hack23.cia.service.api.common.ServiceRequest;
17  import com.hack23.cia.service.api.common.ServiceResponse;
18  
19  /***
20   * The Class AbstractGenericService.
21   *
22   * @param <REQUEST> the generic type
23   */
24  @Transactional(propagation = Propagation.REQUIRED)
25  public abstract class AbstractGenericService<REQUEST extends AbstractServiceRequest>
26          implements BusinessService {
27      
28      /*** The Constant LOGGER. */
29      private static final Log LOGGER = LogFactory
30              .getLog(AbstractGenericService.class);
31  
32  
33      /*** The user session service. */
34      private final UserSessionService userSessionService;
35  
36      /***
37       * Instantiates a new abstract generic service.
38       *
39       * @param userSessionService the user session service
40       */
41      public AbstractGenericService(final UserSessionService userSessionService) {
42          super();
43          this.userSessionService = userSessionService;
44      }
45  
46      /***
47       * Creates the action event.
48       *
49       * @param service the service
50       * @param userSession the user session
51       * @return the action event
52       */
53      public abstract ActionEvent createActionEvent(REQUEST service,
54              UserSession userSession);
55  
56      /***
57       * Gets the user session service.
58       *
59       * @return the user session service
60       */
61      public final UserSessionService getUserSessionService() {
62          return userSessionService;
63      }
64  
65      /***
66       * Handle service request.
67       *
68       * @param service the service
69       * @param userSession the user session
70       * @return the service response
71       */
72      public abstract ServiceResponse handleServiceRequest(REQUEST service,
73              UserSessionDTO userSession);
74  
75      /***
76       * Lookup user session.
77       *
78       * @param request the request
79       * @return the user session
80       */
81      public abstract UserSession lookupUserSession(AbstractServiceRequest request);
82  
83      /*
84       * (non-Javadoc)
85       * 
86       * @see
87       * com.hack23.cia.service.common.ServiceHandler#processService(com.hack23
88       * .cia.service.api.ServiceRequest)
89       */
90      @Override
91  	@SuppressWarnings("unchecked")
92      public final ServiceResponse processService(
93              final ServiceRequest serviceRequest) {
94          ServiceResponse serviceResponse = null;
95          final UserSession userSession = lookupUserSession((REQUEST) serviceRequest);
96          
97          if (userSession != null) {
98              final ActionEvent actionEvent = createActionEvent((REQUEST) serviceRequest,
99                  userSession);
100             serviceResponse = handleServiceRequest(
101                 (REQUEST) serviceRequest, new UserSessionDTO(userSession));
102             LOGGER.info("ServiceResonse " + serviceResponse.getResult());
103             actionEvent.computeExutionTime();
104             this.userSessionService.addActionEvent(actionEvent, userSession);
105             LOGGER.info("Event logged : " + actionEvent);
106         } 
107         return serviceResponse;
108     }
109 }