1
2
3
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
85
86
87
88
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 }