1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
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  
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  
55  
56  
57  
58  
59  
60  
61  
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          
70          LOG.debug("Leaving massApprove of OleOrderQueueAction");
71          return mapping.findForward(RiceConstants.MAPPING_BASIC);
72      }
73  
74      
75  
76  
77  
78  
79  
80  
81  
82  
83  
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          
97          LOG.debug("Leaving assign of OleOrderQueueAction");
98          return mapping.findForward(RiceConstants.MAPPING_BASIC);
99      }
100 
101     
102 
103 
104 
105 
106 
107 
108 
109 
110 
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         
120         LOG.debug("Leaving delete of OleOrderQueueAction");
121         return mapping.findForward(RiceConstants.MAPPING_BASIC);
122     }
123 
124     
125 
126 
127 
128 
129 
130 
131 
132 
133 
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         
143         LOG.debug("Leaving totalSelectedItems of OleOrderQueueAction");
144         return mapping.findForward(RiceConstants.MAPPING_BASIC);
145     }
146 
147     
148 
149 
150 
151 
152 
153 
154 
155 
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         
172         return mapping.findForward(RiceConstants.MAPPING_BASIC);
173     }
174 
175     
176 
177 
178 
179 
180 
181 
182 
183 
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         
200         return mapping.findForward(RiceConstants.MAPPING_BASIC);
201     }
202 
203     
204 
205 
206 
207 
208 
209 
210 
211 
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         
227         return forward;
228 
229     }
230 
231     
232 
233 
234 
235 
236 
237 
238 
239 
240 
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             
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             
270             
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                 
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         
296         return mapping.findForward(RiceConstants.MAPPING_BASIC);
297     }
298 
299     
300 
301 
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         
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 }