View Javadoc
1   /*
2    * Copyright 2011 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.select.document.web.struts;
17  
18  import org.apache.struts.action.ActionForm;
19  import org.apache.struts.action.ActionForward;
20  import org.apache.struts.action.ActionMapping;
21  import org.kuali.ole.select.businessobject.OleRequisitionItem;
22  import org.kuali.ole.select.document.OleOrderQueueDocument;
23  import org.kuali.ole.select.document.validation.event.AssignOrderQueueEvent;
24  import org.kuali.ole.select.document.validation.event.OleAccountFilterEvent;
25  import org.kuali.ole.select.service.OleDocStoreLookupService;
26  import org.kuali.ole.sys.OLEConstants;
27  import org.kuali.ole.sys.OLEKeyConstants;
28  import org.kuali.ole.sys.context.SpringContext;
29  import org.kuali.rice.core.api.util.RiceConstants;
30  import org.kuali.rice.core.api.util.type.KualiDecimal;
31  import org.kuali.rice.kim.api.identity.principal.Principal;
32  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
33  import org.kuali.rice.kns.web.struts.action.KualiTransactionalDocumentActionBase;
34  import org.kuali.rice.kns.web.struts.form.KualiDocumentFormBase;
35  import org.kuali.rice.krad.service.KualiRuleService;
36  import org.kuali.rice.krad.util.GlobalVariables;
37  import org.kuali.rice.krad.util.KRADConstants;
38  
39  import javax.servlet.http.HttpServletRequest;
40  import javax.servlet.http.HttpServletResponse;
41  import java.util.ArrayList;
42  import java.util.Collection;
43  import java.util.List;
44  import java.util.Map;
45  
46  /**
47   * This class is the action class for Order Holding Queue.
48   */
49  public class OleOrderQueueAction extends KualiTransactionalDocumentActionBase {
50  
51      private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(OleOrderQueueAction.class);
52  
53      /**
54       * This method approves the selected requisition documents.
55       *
56       * @param mapping
57       * @param form
58       * @param request
59       * @param response
60       * @return
61       * @throws Exception
62       */
63      public ActionForward massApprove(ActionMapping mapping, ActionForm form,
64                                       HttpServletRequest request, HttpServletResponse response) throws Exception {
65          LOG.debug("Inside massApprove of OleOrderQueueAction");
66          OleOrderQueueForm orderForm = (OleOrderQueueForm) form;
67          OleOrderQueueDocument orderQueueDoc = (OleOrderQueueDocument) orderForm.getDocument();
68          orderQueueDoc.massApprove();
69          //request.setAttribute(OLEConstants.OrderQueue.CONSTANTS, new JSTLConstants(KewApiConstants.class));
70          LOG.debug("Leaving massApprove of OleOrderQueueAction");
71          return mapping.findForward(RiceConstants.MAPPING_BASIC);
72      }
73  
74      /**
75       * This method validates for selector and assigns the selected requisition documents
76       * to the selector.
77       *
78       * @param mapping
79       * @param form
80       * @param request
81       * @param response
82       * @return
83       * @throws Exception
84       */
85      public ActionForward assign(ActionMapping mapping, ActionForm form,
86                                  HttpServletRequest request, HttpServletResponse response) throws Exception {
87          LOG.debug("Inside assign of OleOrderQueueAction");
88          OleOrderQueueForm orderForm = (OleOrderQueueForm) form;
89          OleOrderQueueDocument orderQueueDoc = (OleOrderQueueDocument) orderForm.getDocument();
90  
91          boolean rulePassed = SpringContext.getBean(KualiRuleService.class).applyRules(new AssignOrderQueueEvent(OLEConstants.OrderQueue.PRINCIPAL_NAME, orderQueueDoc));
92  
93          if (rulePassed) {
94              orderQueueDoc.assign();
95          }
96          //request.setAttribute(OLEConstants.OrderQueue.CONSTANTS, new JSTLConstants(KewApiConstants.class));
97          LOG.debug("Leaving assign of OleOrderQueueAction");
98          return mapping.findForward(RiceConstants.MAPPING_BASIC);
99      }
100 
101     /**
102      * This method cancels the selected requisition documents.
103      * to the selector.
104      *
105      * @param mapping
106      * @param form
107      * @param request
108      * @param response
109      * @return
110      * @throws Exception
111      */
112     @Override
113     public ActionForward delete(ActionMapping mapping, ActionForm form,
114                                 HttpServletRequest request, HttpServletResponse response) throws Exception {
115         LOG.debug("Inside delete of OleOrderQueueAction");
116         OleOrderQueueForm orderForm = (OleOrderQueueForm) form;
117         OleOrderQueueDocument orderQueueDoc = (OleOrderQueueDocument) orderForm.getDocument();
118         orderQueueDoc.delete();
119         //request.setAttribute(OLEConstants.OrderQueue.CONSTANTS, new JSTLConstants(KewApiConstants.class));
120         LOG.debug("Leaving delete of OleOrderQueueAction");
121         return mapping.findForward(RiceConstants.MAPPING_BASIC);
122     }
123 
124     /**
125      * This method totals price on the selected requisition documents.
126      * to the selector.
127      *
128      * @param mapping
129      * @param form
130      * @param request
131      * @param response
132      * @return
133      * @throws Exception
134      */
135     public ActionForward totalSelectedItems(ActionMapping mapping, ActionForm form,
136                                             HttpServletRequest request, HttpServletResponse response) throws Exception {
137         LOG.debug("Inside totalSelectedItems of OleOrderQueueAction");
138         OleOrderQueueForm orderForm = (OleOrderQueueForm) form;
139         OleOrderQueueDocument orderQueueDoc = (OleOrderQueueDocument) orderForm.getDocument();
140         KualiDecimal totalPrice = orderQueueDoc.getTotalSelectedItemsPrice();
141         request.setAttribute(OLEConstants.OrderQueue.TOTAL_PRICE, totalPrice);
142         //request.setAttribute(OLEConstants.OrderQueue.CONSTANTS, new JSTLConstants(KewApiConstants.class));
143         LOG.debug("Leaving totalSelectedItems of OleOrderQueueAction");
144         return mapping.findForward(RiceConstants.MAPPING_BASIC);
145     }
146 
147     /**
148      * This method selects all checkboxes from all pages in the result tab.
149      *
150      * @param mapping
151      * @param form
152      * @param request
153      * @param response
154      * @return
155      * @throws Exception
156      */
157     public ActionForward selectAll(ActionMapping mapping, ActionForm form,
158                                    HttpServletRequest request, HttpServletResponse response) throws Exception {
159         LOG.debug("Inside selectAll of OleOrderQueueAction");
160         OleOrderQueueForm orderForm = (OleOrderQueueForm) form;
161         List<OleRequisitionItem> items = ((OleOrderQueueDocument) orderForm.getDocument()).getRequisitionItems();
162         List<OleRequisitionItem> refreshItems = new ArrayList<OleRequisitionItem>(0);
163 
164         for (OleRequisitionItem item : items) {
165             item.setItemAdded(true);
166             refreshItems.add(item);
167         }
168 
169         ((OleOrderQueueDocument) orderForm.getDocument()).setRequisitionItems(refreshItems);
170         LOG.debug("Leaving selectAll of OleOrderQueueAction");
171         //request.setAttribute(OLEConstants.OrderQueue.CONSTANTS, new JSTLConstants(KewApiConstants.class));
172         return mapping.findForward(RiceConstants.MAPPING_BASIC);
173     }
174 
175     /**
176      * This method deselects all checkboxes from all pages in the result tab.
177      *
178      * @param mapping
179      * @param form
180      * @param request
181      * @param response
182      * @return
183      * @throws Exception
184      */
185     public ActionForward unselectAll(ActionMapping mapping, ActionForm form,
186                                      HttpServletRequest request, HttpServletResponse response) throws Exception {
187         LOG.debug("Inside unselectAll of OleOrderQueueAction");
188         OleOrderQueueForm orderForm = (OleOrderQueueForm) form;
189         List<OleRequisitionItem> items = ((OleOrderQueueDocument) orderForm.getDocument()).getRequisitionItems();
190         List<OleRequisitionItem> refreshItems = new ArrayList<OleRequisitionItem>(0);
191 
192         for (OleRequisitionItem item : items) {
193             item.setItemAdded(false);
194             refreshItems.add(item);
195         }
196 
197         ((OleOrderQueueDocument) orderForm.getDocument()).setRequisitionItems(refreshItems);
198         LOG.debug("Leaving unselectAll of OleOrderQueueAction");
199         //request.setAttribute(OLEConstants.OrderQueue.CONSTANTS, new JSTLConstants(KewApiConstants.class));
200         return mapping.findForward(RiceConstants.MAPPING_BASIC);
201     }
202 
203     /**
204      * Takes care of storing the action form in the User session and forwarding to the prlookup action.
205      *
206      * @param mapping
207      * @param form
208      * @param request
209      * @param response
210      * @return
211      * @throws Exception
212      */
213     public ActionForward performPRLookup(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
214 
215         LOG.debug("Inside performPRLookup of OleOrderQueueAction");
216         ActionForward forward = super.performLookup(mapping, form, request, response);
217         String path = forward.getPath();
218         if (path.contains("kr/" + KRADConstants.LOOKUP_ACTION)) {
219             path = path.replace("kr/" + KRADConstants.LOOKUP_ACTION, OLEConstants.PR_LOOKUP_ACTION);
220         } else if (path.contains(KRADConstants.LOOKUP_ACTION)) {
221             path = path.replace(KRADConstants.LOOKUP_ACTION, OLEConstants.PR_LOOKUP_ACTION);
222         }
223         forward.setPath(path);
224 
225         LOG.debug("Leaving performPRLookup of OleOrderQueueAction");
226         //request.setAttribute(OLEConstants.OrderQueue.CONSTANTS, new JSTLConstants(KewApiConstants.class));
227         return forward;
228 
229     }
230 
231     /**
232      * This method returns the list of requisition items that satisfy the search criteria
233      * input from Order Holding Queue page.
234      *
235      * @param mapping
236      * @param form
237      * @param request
238      * @param response
239      * @return
240      * @throws Exception
241      */
242     public ActionForward searchRequisitions(ActionMapping mapping, ActionForm form,
243                                             HttpServletRequest request, HttpServletResponse response) throws Exception {
244         LOG.debug("Inside searchRequisitions of OleOrderQueueAction");
245         OleOrderQueueForm orderForm = (OleOrderQueueForm) form;
246         OleOrderQueueDocument orderQueueDoc = (OleOrderQueueDocument) orderForm.getDocument();
247         if (orderQueueDoc.getSelectorUserName() == null) {
248             orderQueueDoc.setSelectorUserId(null);
249         } else {
250             Principal selector = KimApiServiceLocator.getIdentityService().getPrincipalByPrincipalName(orderQueueDoc.getSelectorUserName());
251             orderQueueDoc.setSelectorUserId(selector != null ? selector.getPrincipalId() : null);
252         }
253 
254         boolean rulePassed = SpringContext.getBean(KualiRuleService.class).applyRules(new OleAccountFilterEvent(orderQueueDoc,
255                 orderQueueDoc.getChartOfAccountsCode(), orderQueueDoc.getAccountNumber(), orderQueueDoc.getObjectCode()));
256         if (rulePassed) {
257             Map<String, String> searchCriteriaMap = orderQueueDoc.populateRequisitionFields();
258 
259             // Remove fields from search criteria which do not belong to OleRequisitionItem
260             searchCriteriaMap.remove(OLEConstants.OrderQueue.selectorField);
261             searchCriteriaMap.remove(OLEConstants.OrderQueue.workflowStatusChangeDateFrom);
262             searchCriteriaMap.remove(OLEConstants.OrderQueue.workflowStatusChangeDateTo);
263 
264             Collection results = SpringContext.getBean(OleDocStoreLookupService.class).findCollectionBySearch(OleRequisitionItem.class, searchCriteriaMap);
265             if (LOG.isDebugEnabled()) {
266                 LOG.debug("Result size from OleOrderQueueAction.searchRequisitions before filtering - " + results.size());
267             }
268 
269             // Checks Whether WorkflowStatusChangeDateFrom is greater than WorkflowStatusChangeDateTo if it is then stops filtering
270             // process and display error message
271 
272             boolean isEndDateSmallerThanBeginDate = false;
273             if (orderQueueDoc.getWorkflowStatusChangeDateFrom() != null && orderQueueDoc.getWorkflowStatusChangeDateTo() != null) {
274                 if (orderQueueDoc.getWorkflowStatusChangeDateFrom().compareTo(orderQueueDoc.getWorkflowStatusChangeDateTo()) > 0) {
275                     isEndDateSmallerThanBeginDate = true;
276                     GlobalVariables.getMessageMap().putError(OLEConstants.OrderQueue.REQUISITIONS, OLEKeyConstants.WORKFLOWSTATUSCHANGEDATETO_NOT_LESSER_THAN_WORKFLOWSTATUSCHANGEDATEFROM, new String[]{});
277                 }
278             }
279             if (!isEndDateSmallerThanBeginDate) {
280 
281                 // Filter results for other search criteria that are not present in Requisition and Requisition Item related table
282 
283                 List<OleRequisitionItem> finalSearchResultList = orderQueueDoc.filterOtherSearchCriteria((List<OleRequisitionItem>) results);
284                 ((OleOrderQueueDocument) orderForm.getDocument()).setRequisitionItems(finalSearchResultList);
285 
286                 if (finalSearchResultList.size() <= 0) {
287                     GlobalVariables.getMessageMap().putInfo(OLEConstants.OrderQueue.REQUISITIONS, OLEKeyConstants.ERROR_NO_REQUISITIONS_FOUND);
288                 }
289             }
290         } else {
291             ((OleOrderQueueDocument) orderForm.getDocument()).setRequisitionItems(new ArrayList<OleRequisitionItem>());
292         }
293 
294         LOG.debug("Leaving searchRequisitions of OleOrderQueueAction");
295         //request.setAttribute(OLEConstants.OrderQueue.CONSTANTS, new JSTLConstants(KewApiConstants.class));
296         return mapping.findForward(RiceConstants.MAPPING_BASIC);
297     }
298 
299     /**
300      * @see org.kuali.rice.kns.web.struts.action.KualiDocumentActionBase#cancel(org.apache.struts.action.ActionMapping,
301      *      org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
302      */
303     @Override
304     public ActionForward cancel(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
305         KualiDocumentFormBase kualiDocumentFormBase = (KualiDocumentFormBase) form;
306         return returnToSender(request, mapping, kualiDocumentFormBase);
307     }
308 
309     public ActionForward clear(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
310         OleOrderQueueForm orderForm = (OleOrderQueueForm) form;
311         OleOrderQueueDocument orderQueueDoc = (OleOrderQueueDocument) orderForm.getDocument();
312         orderQueueDoc.setPrincipalName(null);
313         orderQueueDoc.setRequisitionDocNumber(null);
314         orderQueueDoc.setRequisitionStatusCode(null);
315         orderQueueDoc.setRequisitionStatus(null);
316         orderQueueDoc.setVendorName(null);
317         orderQueueDoc.setRequestorName(null);
318         orderQueueDoc.setFormatTypeId(null);
319         orderQueueDoc.setWorkflowStatusChangeDateFrom(null);
320         orderQueueDoc.setWorkflowStatusChangeDateTo(null);
321         orderQueueDoc.setTitle(null);
322         orderQueueDoc.setAuthor(null);
323         orderQueueDoc.setPublisher(null);
324         orderQueueDoc.setSelectorUserId(null);
325         orderQueueDoc.setSelectedUserId(null);
326         orderQueueDoc.setRequisitionItems(null);
327         orderQueueDoc.setAccountNumber(null);
328         orderQueueDoc.setObjectCode(null);
329         orderQueueDoc.setChartOfAccountsCode(null);
330         orderQueueDoc.setSelectorUserName(null);
331         orderQueueDoc.setIsbn(null);
332         //request.setAttribute(OLEConstants.OrderQueue.CONSTANTS, new JSTLConstants(KewApiConstants.class));
333         return mapping.findForward(RiceConstants.MAPPING_BASIC);
334     }
335 
336     @Override
337     public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
338         ActionForward actionForward = null;
339         if(((OleOrderQueueForm) form).getMethodToCall() != null) {
340             if(((OleOrderQueueForm) form).getMethodToCall().equalsIgnoreCase("docHandler")){
341                 ((OleOrderQueueForm) form).setDocId(null);
342             }
343         }
344         return super.execute(mapping, form, request, response);
345     }
346 }