1
2
3
4
5 package com.hack23.cia.web.controller.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.service.api.application.ApplicationManager;
13 import com.hack23.cia.service.api.common.AbstractServiceRequest;
14 import com.hack23.cia.service.api.common.AbstractServiceResponse;
15 import com.hack23.cia.service.api.common.ApplicationErrorRequest;
16 import com.hack23.cia.service.api.common.ErrorResponse;
17 import com.hack23.cia.service.api.common.ServiceRequest;
18 import com.hack23.cia.service.api.common.ServiceResponse;
19 import com.hack23.cia.service.api.common.ServiceResponse.ServiceResult;
20 import com.hack23.cia.web.action.common.AbstractAction;
21 import com.hack23.cia.web.action.common.ControllerAction;
22 import com.hack23.cia.web.common.UserState;
23 import com.hack23.cia.web.viewfactory.api.common.ErrorMessageModelAndView;
24 import com.hack23.cia.web.viewfactory.api.common.ModelAndView;
25 import com.hack23.cia.web.viewfactory.api.common.ViewFactoryService;
26 import com.hack23.cia.web.viewfactory.api.common.ErrorMessageModelAndView.ErrorViewSpecification;
27
28 /***
29 * The Class AbstractGenericHandler.
30 *
31 * @param <ACTION> the generic type
32 * @param <REQUEST> the generic type
33 * @param <RESPONSE> the generic type
34 */
35 @Transactional(propagation = Propagation.REQUIRED)
36 public abstract class AbstractGenericHandler<ACTION extends AbstractAction, REQUEST extends AbstractServiceRequest, RESPONSE extends AbstractServiceResponse>
37 implements ActionHandler {
38
39 /*** The Constant LOGGER. */
40 private static final Log LOGGER = LogFactory
41 .getLog(AbstractGenericHandler.class);
42
43 /*** The application manager. */
44 private final ApplicationManager applicationManager;
45
46 /*** The user state service. */
47 private UserStateService userStateService = new UserStateService() {
48
49 @Override
50 public Long getUserSessionId() {
51 return UserState.getId().get();
52 }
53
54 @Override
55 public void setUserSessionId(final Long userSessionId) {
56 UserState.getId().set(userSessionId);
57 }
58 };
59
60 /*** The view factory service. */
61 private ViewFactoryService viewFactoryService;
62
63 /***
64 * Instantiates a new abstract generic handler.
65 *
66 * @param viewFactoryService the view factory service
67 * @param applicationManager the application manager
68 */
69 public AbstractGenericHandler(final ViewFactoryService viewFactoryService,
70 final ApplicationManager applicationManager) {
71 super();
72 this.viewFactoryService = viewFactoryService;
73 this.applicationManager = applicationManager;
74 }
75
76 /***
77 * Creates the service request.
78 *
79 * @param action the action
80 * @return the rEQUEST
81 */
82 public abstract REQUEST createServiceRequest(final ACTION action);
83
84 /***
85 * Gets the application manager.
86 *
87 * @return the application manager
88 */
89 public final ApplicationManager getApplicationManager() {
90 return applicationManager;
91 }
92
93 /***
94 * Gets the user state service.
95 *
96 * @return the user state service
97 */
98 public final UserStateService getUserStateService() {
99 return this.userStateService;
100 }
101
102 /***
103 * Gets the view factory service.
104 *
105 * @return the view factory service
106 */
107 public final ViewFactoryService getViewFactoryService() {
108 return viewFactoryService;
109 }
110
111 /***
112 * Handle error response.
113 *
114 * @param action the action
115 * @param errorResponse the error response
116 * @return the model and view
117 */
118 protected final ModelAndView handleErrorResponse(
119 final ControllerAction action, final ErrorResponse errorResponse) {
120 ModelAndView modelAndView;
121 modelAndView = new ErrorMessageModelAndView(errorResponse
122 .getUserSessionDTO(), action,
123 ErrorViewSpecification.ErrorMessageView, errorResponse
124 .getErrorMessage());
125 return modelAndView;
126 }
127
128 /***
129 * Handle success response.
130 *
131 * @param action the action
132 * @param response the response
133 * @return the model and view
134 */
135 public abstract ModelAndView handleSuccessResponse(final ACTION action,
136 final RESPONSE response);
137
138
139
140
141
142
143
144
145 @Override
146 @SuppressWarnings("unchecked")
147 public final void processAction(final ControllerAction action) {
148 LOGGER.info("processAction : " + action.getActionCategory() + " :"
149 + action.getClass().getSimpleName());
150
151 ModelAndView modelAndView = null;
152
153 final ServiceRequest serviceRequest = createServiceRequest((ACTION) action);
154 ServiceResponse serviceResponse = getApplicationManager().service(
155 serviceRequest);
156
157 if (serviceResponse == null) {
158 final ApplicationErrorRequest applicationErrorRequest = new ApplicationErrorRequest(userStateService.getUserSessionId(),new StackTraceElement[0], "Missing service response: " + serviceRequest.getClass().getSimpleName());
159 serviceResponse = getApplicationManager().service(applicationErrorRequest);
160 }
161
162 getUserStateService().setUserSessionId(
163 serviceResponse.getUserSessionDTO().getUserSession().getId());
164
165 if (serviceResponse.getResult().equals(ServiceResult.SUCCESS)) {
166 modelAndView = handleSuccessResponse((ACTION) action,
167 (RESPONSE) serviceResponse);
168 } else {
169 modelAndView = handleErrorResponse(action,
170 (ErrorResponse) serviceResponse);
171 }
172
173 processModelAndView(modelAndView);
174 }
175
176 /***
177 * Process model and view.
178 *
179 * @param modelAndView the model and view
180 */
181 protected final void processModelAndView(final ModelAndView modelAndView) {
182 viewFactoryService.processView(modelAndView);
183 }
184
185
186
187
188
189
190
191
192 @Override
193 public final void setUserStateService(
194 final UserStateService userStateService) {
195 this.userStateService = userStateService;
196 }
197
198
199
200
201
202
203
204
205 @Override
206 public final void setViewFactoryService(
207 final ViewFactoryService viewFactoryService) {
208 this.viewFactoryService = viewFactoryService;
209 }
210
211 }