View Javadoc
1   /*
2    * Copyright 2008 The Kuali Foundation
3    * 
4    * Licensed under the Educational Community 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.opensource.org/licenses/ecl2.php
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  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   * This class defines actions for Batch (cancel, hold, remove hold).
49   */
50  public class BatchAction extends KualiAction {
51  
52      private BatchMaintenanceService batchMaintenanceService;
53  
54      /**
55       * Constructs a BatchAction.java.
56       */
57      public BatchAction() {
58          setBatchMaintenanceService(SpringContext.getBean(BatchMaintenanceService.class));
59      }
60  
61  
62      /**
63       * This method confirms and performs batch cancel.
64       * 
65       * @param mapping
66       * @param form
67       * @param request
68       * @param response
69       * @return
70       * @throws Exception
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       * This method cancels a batch.
85       * 
86       * @param batchIdString a string representing the batch id
87       * @param cancelNote the cancelation note entered by the user
88       * @param user the current user
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      * This method confirms and performs batch hold.
104      * 
105      * @param mapping
106      * @param form
107      * @param request
108      * @param response
109      * @return
110      * @throws Exception
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      * This method holds a batch
124      * 
125      * @param batchIdString
126      * @param holdNote
127      * @param user
128      * @throws PdpException
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      * This method confirms and peforms remove hold on batch action.
144      * 
145      * @param mapping
146      * @param form
147      * @param request
148      * @param response
149      * @return
150      * @throws Exception
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      * This method removes a batch hold.
163      * 
164      * @param batchIdString
165      * @param changeText
166      * @param user
167      * @throws PdpException
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      * This method prompts for a reason to perfomr an action on a batch (cancel, hold, remove hold).
183      * 
184      * @param mapping
185      * @param form
186      * @param request
187      * @param response
188      * @param confirmationQuestion
189      * @param confirmationText
190      * @param caller
191      * @param callback
192      * @return
193      * @throws Exception
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             // ask question if not already asked
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                         // prevent a NPE by setting the reason to a blank string
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      * This method builds the forward url.
252      * 
253      * @param batchId the batch id
254      * @param success action status: true if success, false otherwise
255      * @param message the message for the user
256      * @return the build url
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      * This method build a string list of error message keys out of the error map in GlobalVariables
285      * 
286      * @return a String representing the list of error message keys
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      * This method gets the batch maintenance service.
308      * 
309      * @return the BatchMaintenanceService
310      */
311     public BatchMaintenanceService getBatchMaintenanceService() {
312         return batchMaintenanceService;
313     }
314 
315     /**
316      * This method sets the batch maintenance service.
317      * 
318      * @param batchMaintenanceService
319      */
320     public void setBatchMaintenanceService(BatchMaintenanceService batchMaintenanceService) {
321         this.batchMaintenanceService = batchMaintenanceService;
322     }
323 
324 }
325