1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.ole.pdp.web.struts;
17
18 import java.text.MessageFormat;
19 import java.util.List;
20 import java.util.Properties;
21
22 import javax.servlet.http.HttpServletRequest;
23 import javax.servlet.http.HttpServletResponse;
24
25 import org.apache.commons.lang.StringUtils;
26 import org.apache.struts.action.ActionForm;
27 import org.apache.struts.action.ActionForward;
28 import org.apache.struts.action.ActionMapping;
29 import org.kuali.ole.pdp.PdpKeyConstants;
30 import org.kuali.ole.pdp.PdpParameterConstants;
31 import org.kuali.ole.pdp.PdpPropertyConstants;
32 import org.kuali.ole.pdp.businessobject.Batch;
33 import org.kuali.ole.pdp.service.BatchMaintenanceService;
34 import org.kuali.ole.pdp.util.PdpBatchQuestionCallback;
35 import org.kuali.ole.sys.OLEConstants;
36 import org.kuali.ole.sys.context.SpringContext;
37 import org.kuali.rice.core.api.config.property.ConfigurationService;
38 import org.kuali.rice.kim.api.identity.Person;
39 import org.kuali.rice.kns.question.ConfirmationQuestion;
40 import org.kuali.rice.kns.web.struts.action.KualiAction;
41 import org.kuali.rice.krad.util.ErrorMessage;
42 import org.kuali.rice.krad.util.GlobalVariables;
43 import org.kuali.rice.krad.util.KRADConstants;
44 import org.kuali.rice.krad.util.MessageMap;
45 import org.kuali.rice.krad.util.UrlFactory;
46
47
48
49
50 public class BatchAction extends KualiAction {
51
52 private BatchMaintenanceService batchMaintenanceService;
53
54
55
56
57 public BatchAction() {
58 setBatchMaintenanceService(SpringContext.getBean(BatchMaintenanceService.class));
59 }
60
61
62
63
64
65
66
67
68
69
70
71
72 public ActionForward confirmAndCancel(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
73
74 PdpBatchQuestionCallback callback = new PdpBatchQuestionCallback() {
75 public boolean doPostQuestion(String batchIdString, String cancelNote, Person user) {
76 return performCancel(batchIdString, cancelNote, user);
77 }
78 };
79 return askQuestionWithInput(mapping, form, request, response, PdpKeyConstants.BatchConstants.Confirmation.CANCEL_BATCH_QUESTION, PdpKeyConstants.BatchConstants.Confirmation.CANCEL_BATCH_MESSAGE, PdpKeyConstants.BatchConstants.Messages.BATCH_SUCCESSFULLY_CANCELED, "confirmAndCancel", callback);
80
81 }
82
83
84
85
86
87
88
89
90 private boolean performCancel(String batchIdString, String cancelNote, Person user) {
91 try {
92 Integer batchId = Integer.parseInt(batchIdString);
93 return batchMaintenanceService.cancelPendingBatch(batchId, cancelNote, user);
94 }
95 catch (NumberFormatException e) {
96 GlobalVariables.getMessageMap().putError(PdpPropertyConstants.BatchConstants.BATCH_ID, PdpKeyConstants.BatchConstants.ErrorMessages.ERROR_BATCH_ID_IS_NOT_NUMERIC);
97 return false;
98 }
99
100 }
101
102
103
104
105
106
107
108
109
110
111
112 public ActionForward confirmAndHold(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
113
114 PdpBatchQuestionCallback callback = new PdpBatchQuestionCallback() {
115 public boolean doPostQuestion(String batchIdString, String note, Person user) {
116 return performHold(batchIdString, note, user);
117 }
118 };
119 return askQuestionWithInput(mapping, form, request, response, PdpKeyConstants.BatchConstants.Confirmation.HOLD_BATCH_QUESTION, PdpKeyConstants.BatchConstants.Confirmation.HOLD_BATCH_MESSAGE, PdpKeyConstants.BatchConstants.Messages.BATCH_SUCCESSFULLY_HOLD, "confirmAndHold", callback);
120 }
121
122
123
124
125
126
127
128
129
130 private boolean performHold(String batchIdString, String holdNote, Person user) {
131 try {
132 Integer batchId = Integer.parseInt(batchIdString);
133 return batchMaintenanceService.holdPendingBatch(batchId, holdNote, user);
134 }
135 catch (NumberFormatException e) {
136 GlobalVariables.getMessageMap().putError(PdpPropertyConstants.BatchConstants.BATCH_ID, PdpKeyConstants.BatchConstants.ErrorMessages.ERROR_BATCH_ID_IS_NOT_NUMERIC);
137 return false;
138 }
139
140 }
141
142
143
144
145
146
147
148
149
150
151
152 public ActionForward confirmAndRemoveHold(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
153 PdpBatchQuestionCallback callback = new PdpBatchQuestionCallback() {
154 public boolean doPostQuestion(String batchIdString, String note, Person user) {
155 return performRemoveHold(batchIdString, note, user);
156 }
157 };
158 return askQuestionWithInput(mapping, form, request, response, PdpKeyConstants.BatchConstants.Confirmation.REMOVE_HOLD_BATCH_QUESTION, PdpKeyConstants.BatchConstants.Confirmation.REMOVE_HOLD_BATCH_MESSAGE, PdpKeyConstants.BatchConstants.Messages.HOLD_SUCCESSFULLY_REMOVED_ON_BATCH, "confirmAndRemoveHold", callback);
159 }
160
161
162
163
164
165
166
167
168
169 private boolean performRemoveHold(String batchIdString, String changeText, Person user) {
170 try {
171 Integer batchId = Integer.parseInt(batchIdString);
172 return batchMaintenanceService.removeBatchHold(batchId, changeText, user);
173 }
174 catch (NumberFormatException e) {
175 GlobalVariables.getMessageMap().putError(PdpPropertyConstants.BatchConstants.BATCH_ID, PdpKeyConstants.BatchConstants.ErrorMessages.ERROR_BATCH_ID_IS_NOT_NUMERIC);
176 return false;
177 }
178
179 }
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195 private ActionForward askQuestionWithInput(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response, String confirmationQuestion, String confirmationText, String successMessage, String caller, PdpBatchQuestionCallback callback) throws Exception {
196 Object question = request.getParameter(KRADConstants.QUESTION_INST_ATTRIBUTE_NAME);
197 String reason = request.getParameter(KRADConstants.QUESTION_REASON_ATTRIBUTE_NAME);
198 String noteText = OLEConstants.EMPTY_STRING;
199 Person person = GlobalVariables.getUserSession().getPerson();
200 boolean actionStatus;
201 String message = OLEConstants.EMPTY_STRING;
202
203 String batchId = request.getParameter(PdpParameterConstants.BatchConstants.BATCH_ID_PARAM);
204 if (batchId == null) {
205 batchId = request.getParameter(KRADConstants.QUESTION_CONTEXT);
206 }
207
208 ConfigurationService kualiConfiguration = SpringContext.getBean(ConfigurationService.class);
209 confirmationText = kualiConfiguration.getPropertyValueAsString(confirmationText);
210 confirmationText = MessageFormat.format(confirmationText, batchId);
211
212 if (question == null) {
213
214 return this.performQuestionWithInput(mapping, form, request, response, confirmationQuestion, confirmationText, KRADConstants.CONFIRMATION_QUESTION, caller, batchId);
215 }
216 else {
217 Object buttonClicked = request.getParameter(KRADConstants.QUESTION_CLICKED_BUTTON);
218 if ((confirmationQuestion.equals(question)) && ConfirmationQuestion.NO.equals(buttonClicked)) {
219 actionStatus = false;
220 }
221 else {
222 noteText = reason;
223 int noteTextLength = (reason == null) ? 0 : noteText.length();
224 int noteTextMaxLength = PdpKeyConstants.BatchConstants.Confirmation.NOTE_TEXT_MAX_LENGTH;
225
226 if (StringUtils.isBlank(reason)) {
227
228 if (reason == null) {
229
230 reason = OLEConstants.EMPTY_STRING;
231 }
232 return this.performQuestionWithInputAgainBecauseOfErrors(mapping, form, request, response, confirmationQuestion, confirmationText, KRADConstants.CONFIRMATION_QUESTION, OLEConstants.MAPPING_BASIC, batchId, reason, PdpKeyConstants.BatchConstants.ErrorMessages.ERROR_NOTE_EMPTY, KRADConstants.QUESTION_REASON_ATTRIBUTE_NAME, "");
233 }
234 else if (noteTextLength > noteTextMaxLength) {
235 return this.performQuestionWithInputAgainBecauseOfErrors(mapping, form, request, response, confirmationQuestion, confirmationText, KRADConstants.CONFIRMATION_QUESTION, OLEConstants.MAPPING_BASIC, batchId, reason, PdpKeyConstants.BatchConstants.ErrorMessages.ERROR_NOTE_TOO_LONG, KRADConstants.QUESTION_REASON_ATTRIBUTE_NAME, "");
236 }
237
238 actionStatus = callback.doPostQuestion(batchId, noteText, person);
239 if (actionStatus) {
240 message = successMessage;
241 }
242
243 }
244 }
245
246 String returnUrl = buildUrl(batchId, actionStatus, message, buildErrorMesageKeyList());
247 return new ActionForward(returnUrl, true);
248 }
249
250
251
252
253
254
255
256
257
258 private String buildUrl(String batchId, boolean success, String message, String errorList) {
259 String basePath = SpringContext.getBean(ConfigurationService.class).getPropertyValueAsString(OLEConstants.APPLICATION_URL_KEY);
260
261 Properties parameters = new Properties();
262 parameters.put(OLEConstants.DISPATCH_REQUEST_PARAMETER, OLEConstants.SEARCH_METHOD);
263 parameters.put(OLEConstants.BACK_LOCATION, basePath + "/" + OLEConstants.MAPPING_PORTAL + ".do");
264 parameters.put(KRADConstants.DOC_FORM_KEY, "88888888");
265 parameters.put(OLEConstants.BUSINESS_OBJECT_CLASS_ATTRIBUTE, Batch.class.getName());
266 parameters.put(OLEConstants.HIDE_LOOKUP_RETURN_LINK, "true");
267 parameters.put(OLEConstants.SUPPRESS_ACTIONS, "false");
268 parameters.put(PdpPropertyConstants.BatchConstants.BATCH_ID, batchId);
269 parameters.put(PdpParameterConstants.ACTION_SUCCESSFUL_PARAM, String.valueOf(success));
270 if (message != null && !message.equalsIgnoreCase(OLEConstants.EMPTY_STRING)) {
271 parameters.put(PdpParameterConstants.MESSAGE_PARAM, message);
272 }
273
274 if (StringUtils.isNotEmpty(errorList)) {
275 parameters.put(PdpParameterConstants.ERROR_KEY_LIST_PARAM, errorList);
276 }
277
278 String lookupUrl = UrlFactory.parameterizeUrl(basePath + "/" + OLEConstants.LOOKUP_ACTION, parameters);
279
280 return lookupUrl;
281 }
282
283
284
285
286
287
288 private String buildErrorMesageKeyList() {
289 MessageMap errorMap = GlobalVariables.getMessageMap();
290 StringBuffer errorList = new StringBuffer();
291
292 for (String errorKey : (List<String>) errorMap.getPropertiesWithErrors()) {
293 for (ErrorMessage errorMessage : (List<ErrorMessage>) errorMap.getMessages(errorKey)) {
294
295 errorList.append(errorMessage.getErrorKey());
296 errorList.append(PdpParameterConstants.ERROR_KEY_LIST_SEPARATOR);
297 }
298 }
299 if (errorList.length() > 0) {
300 errorList.replace(errorList.lastIndexOf(PdpParameterConstants.ERROR_KEY_LIST_SEPARATOR), errorList.lastIndexOf(PdpParameterConstants.ERROR_KEY_LIST_SEPARATOR) + PdpParameterConstants.ERROR_KEY_LIST_SEPARATOR.length(), "");
301 }
302
303 return errorList.toString();
304 }
305
306
307
308
309
310
311 public BatchMaintenanceService getBatchMaintenanceService() {
312 return batchMaintenanceService;
313 }
314
315
316
317
318
319
320 public void setBatchMaintenanceService(BatchMaintenanceService batchMaintenanceService) {
321 this.batchMaintenanceService = batchMaintenanceService;
322 }
323
324 }
325