1   package org.kuali.ole.module.purap.document.web.controller;
2   
3   import org.apache.commons.lang.StringUtils;
4   import org.apache.struts.upload.FormFile;
5   import org.kuali.ole.module.purap.PurapConstants;
6   import org.kuali.ole.module.purap.PurapPropertyConstants;
7   import org.kuali.ole.module.purap.businessobject.PurApAccountingLine;
8   import org.kuali.ole.module.purap.businessobject.PurApAccountingLineParser;
9   import org.kuali.ole.module.purap.businessobject.PurApItem;
10  import org.kuali.ole.module.purap.document.OlePurchasingAccountsPayableDocument;
11  import org.kuali.ole.module.purap.document.PurchasingAccountsPayableDocument;
12  import org.kuali.ole.module.purap.document.PurchasingAccountsPayableDocumentBase;
13  import org.kuali.ole.module.purap.document.service.impl.OLEPurchaseAccountsPayableControllerBaseServiceImpl;
14  import org.kuali.ole.module.purap.document.web.form.OlePurchasingAccountsPayableFormBase;
15  import org.kuali.ole.module.purap.document.web.form.OlePurchasingFormBase;
16  import org.kuali.ole.module.purap.service.PurapAccountingService;
17  import org.kuali.ole.sys.OLEConstants;
18  import org.kuali.ole.sys.OLEPropertyConstants;
19  import org.kuali.ole.sys.businessobject.AccountingLine;
20  import org.kuali.ole.sys.businessobject.SourceAccountingLine;
21  import org.kuali.ole.sys.context.SpringContext;
22  import org.kuali.ole.sys.document.validation.event.AddAccountingLineEvent;
23  import org.kuali.ole.sys.exception.AccountingLineParserException;
24  import org.kuali.ole.sys.web.controller.OLEKualiAccountingDocumentControllerBase;
25  import org.kuali.ole.sys.web.struts.KualiAccountingDocumentFormBase;
26  import org.kuali.rice.kew.api.WorkflowDocument;
27  import org.kuali.rice.kew.api.exception.WorkflowException;
28  import org.kuali.rice.krad.document.Copyable;
29  import org.kuali.rice.krad.service.DocumentService;
30  import org.kuali.rice.krad.service.KualiRuleService;
31  import org.kuali.rice.krad.service.PersistenceService;
32  import org.kuali.rice.krad.util.GlobalVariables;
33  import org.kuali.rice.krad.util.ObjectUtils;
34  import org.kuali.rice.krad.web.form.DocumentFormBase;
35  import org.kuali.rice.krad.web.form.TransactionalDocumentFormBase;
36  import org.kuali.rice.krad.web.form.UifFormBase;
37  import org.kuali.rice.krad.web.service.ControllerService;
38  import org.springframework.beans.factory.annotation.Autowired;
39  import org.springframework.beans.factory.annotation.Qualifier;
40  import org.springframework.stereotype.Controller;
41  import org.springframework.validation.BindingResult;
42  import org.springframework.web.bind.ServletRequestBindingException;
43  import org.springframework.web.bind.annotation.ModelAttribute;
44  import org.springframework.web.bind.annotation.RequestMapping;
45  import org.springframework.web.bind.annotation.RequestMethod;
46  import org.springframework.web.servlet.ModelAndView;
47  
48  import javax.servlet.http.HttpServletRequest;
49  import javax.servlet.http.HttpServletResponse;
50  import java.io.FileNotFoundException;
51  import java.io.IOException;
52  import java.math.BigDecimal;
53  import java.util.HashMap;
54  import java.util.Iterator;
55  import java.util.List;
56  import java.util.Map;
57  
58  
59  
60  
61  
62  @Controller
63  @RequestMapping(value = "/OlePurchasingAccountsPayableControllerBase")
64  public class OlePurchasingAccountsPayableControllerBase extends OLEKualiAccountingDocumentControllerBase {
65  
66      @Override
67      @RequestMapping(params = "methodToCall=copy")
68      public ModelAndView copy(@ModelAttribute("KualiForm") TransactionalDocumentFormBase form)  {
69  
70  
71  
72  
73          try {
74              ((Copyable) form.getDocument()).toCopy();
75          } catch (WorkflowException e) {
76              e.printStackTrace();
77          }
78  
79          OlePurchasingAccountsPayableDocument purapDocument = (OlePurchasingAccountsPayableDocument) form.getDocument();
80  
81          
82          List<PurApItem> items = purapDocument.getItems();
83  
84          for (PurApItem item : items) {
85              item.getNewSourceLine().setAccountLinePercent(new BigDecimal(0));
86              
87              for (PurApAccountingLine account : item.getSourceAccountingLines()) {
88                  account.setSequenceNumber(0);
89              }
90          }
91  
92          return getModelAndView(form);
93      }
94  
95      
96  
97  
98  
99      
100 
101 
102 
103 
104 
105 
106 
107 
108 
109 
110 
111 
112 
113 
114 
115 
116 
117 
118     
119 
120 
121 
122 
123 
124     
125 
126 
127 
128 
129 
130 
131 
132 
133 
134 
135 
136 
137 
138 
139 
140 
141 
142 
143 
144 
145 
146 
147     @RequestMapping(params = "methodToCall=refreshAccountSummary")
148     public ModelAndView refreshAccountSummary(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
149                                               HttpServletRequest request, HttpServletResponse response) throws Exception {
150         OlePurchasingAccountsPayableFormBase purapForm = (OlePurchasingAccountsPayableFormBase) form;
151         OlePurchasingAccountsPayableDocument document = (OlePurchasingAccountsPayableDocument) purapForm.getDocument();
152         SpringContext.getBean(PurapAccountingService.class).updateAccountAmounts(document);
153         purapForm.refreshAccountSummmary();
154         return getModelAndView(form);
155 
156     }
157 
158     
159 
160 
161     @Override
162     protected void uploadAccountingLines(boolean isSource, DocumentFormBase form) throws FileNotFoundException, IOException {
163         OlePurchasingAccountsPayableFormBase purapForm = (OlePurchasingAccountsPayableFormBase) form;
164         PurchasingAccountsPayableDocumentBase purapDocument = (PurchasingAccountsPayableDocumentBase) purapForm.getFinancialDocument();
165         PurApAccountingLineParser accountingLineParser = (PurApAccountingLineParser) purapDocument.getAccountingLineParser();
166         List importedLines = null;
167         String errorPathPrefix = PurapConstants.ACCOUNT_DISTRIBUTION_ERROR_KEY;
168         
169 
170         
171         try {
172             FormFile sourceFile = purapForm.getSourceFile();
173             checkUploadFile(sourceFile);
174             GlobalVariables.getMessageMap().clearErrorPath();
175             GlobalVariables.getMessageMap().addToErrorPath(errorPathPrefix);
176             importedLines = accountingLineParser.importSourceAccountingLines(sourceFile.getFileName(), sourceFile.getInputStream(), purapDocument);
177             GlobalVariables.getMessageMap().removeFromErrorPath(errorPathPrefix);
178         } catch (AccountingLineParserException e) {
179             GlobalVariables.getMessageMap().putError(errorPathPrefix, e.getErrorKey(), e.getErrorParameters());
180         }
181 
182         
183         if (importedLines != null) {
184             for (Iterator iter = importedLines.iterator(); iter.hasNext(); ) {
185                 PurApAccountingLine importedLine = (PurApAccountingLine) iter.next();
186                 
187                 
188                 
189                 SpringContext.getBean(PersistenceService.class).retrieveNonKeyFields(importedLine);
190                 ((OlePurchasingFormBase) purapForm).addAccountDistributionsourceAccountingLine(importedLine);
191                 
192             }
193         }
194     }
195 
196     
197 
198 
199 
200 
201 
202 
203 
204     @Override
205     @RequestMapping(params = "methodToCall=insertSourceLine")
206     public ModelAndView insertSourceLine(@ModelAttribute("KualiForm") DocumentFormBase form, BindingResult result,
207                                          HttpServletRequest request, HttpServletResponse response) throws Exception {
208         
209         OlePurchasingAccountsPayableFormBase purapForm = (OlePurchasingAccountsPayableFormBase) form;
210 
211         
212         int itemIndex = getSelectedLine(request);
213         PurApItem item = null;
214 
215         
216         if (processCustomInsertAccountingLine(purapForm, request) == false) {
217             String errorPrefix = null;
218             PurApAccountingLine line = null;
219             boolean rulePassed = false;
220             if (itemIndex >= 0) {
221                 item = (PurApItem) ((PurchasingAccountsPayableDocument) purapForm.getDocument()).getItem((itemIndex));
222                 line = (PurApAccountingLine) ObjectUtils.deepCopy(item.getNewSourceLine());
223                 
224                 errorPrefix = OLEPropertyConstants.DOCUMENT + "." + PurapPropertyConstants.ITEM + "[" + Integer.toString(itemIndex) + "]." + OLEConstants.NEW_SOURCE_ACCT_LINE_PROPERTY_NAME;
225                 rulePassed = SpringContext.getBean(KualiRuleService.class).applyRules(new AddAccountingLineEvent(errorPrefix, purapForm.getDocument(), (AccountingLine) line));
226             } else if (itemIndex == -2) {
227                 
228                 
229                 line = ((OlePurchasingFormBase) purapForm).getAccountDistributionnewSourceLine();
230                 
231                 errorPrefix = PurapPropertyConstants.ACCOUNT_DISTRIBUTION_NEW_SRC_LINE;
232                 rulePassed = SpringContext.getBean(KualiRuleService.class).applyRules(new AddAccountingLineEvent(errorPrefix, purapForm.getDocument(), (AccountingLine) line));
233             }
234 
235             if (rulePassed) {
236                 
237                 SpringContext.getBean(PersistenceService.class).retrieveNonKeyFields(line);
238                 if (itemIndex >= 0) {
239                     insertAccountingLine(purapForm, item, line);
240                     
241                     item.resetAccount();
242                 } else if (itemIndex == -2) {
243                     
244                     ((OlePurchasingFormBase) purapForm).addAccountDistributionsourceAccountingLine(line);
245                 }
246             }
247         }
248 
249         return getModelAndView(form);
250     }
251 
252     
253 
254 
255 
256 
257 
258 
259     protected void insertAccountingLine(OlePurchasingAccountsPayableFormBase financialDocumentForm, PurApItem item, PurApAccountingLine line) {
260         PurchasingAccountsPayableDocument preq = (PurchasingAccountsPayableDocument) financialDocumentForm.getDocument();
261 
262         Integer index = item.getSourceAccountingLines().size() + 1;
263         line.setSequenceNumber(index);
264         
265         item.getSourceAccountingLines().add(line);
266     }
267 
268     
269 
270 
271 
272 
273 
274 
275 
276     public boolean processCustomInsertAccountingLine(OlePurchasingAccountsPayableFormBase purapForm, HttpServletRequest request) {
277         return false;
278     }
279 
280     
281 
282 
283 
284 
285 
286 
287     protected void insertAccountingLine(KualiAccountingDocumentFormBase financialDocumentForm, PurApItem item, PurApAccountingLine line) {
288         
289         item.getSourceAccountingLines().add(line);
290     }
291 
292     
293 
294 
295 
296 
297 
298 
299 
300     @Override
301     @RequestMapping(params = "methodToCall=deleteSourceLine")
302     public ModelAndView deleteSourceLine(@ModelAttribute("KualiForm") DocumentFormBase form, BindingResult result,
303                                          HttpServletRequest request, HttpServletResponse response) throws Exception {
304         OlePurchasingAccountsPayableFormBase purapForm = (OlePurchasingAccountsPayableFormBase) form;
305 
306         String[] indexes = getSelectedLineForAccounts(request);
307         int itemIndex = Integer.parseInt(indexes[0]);
308         int accountIndex = Integer.parseInt(indexes[1]);
309 
310         PurApItem item = (PurApItem) ((PurchasingAccountsPayableDocument) purapForm.getDocument()).getItem((itemIndex));
311         if (itemIndex == -2) {
312             item.getSourceAccountingLines().remove(accountIndex);
313         } else {
314             item.getSourceAccountingLines().remove(accountIndex);
315             List<PurApAccountingLine> purApAccountingLineList = item.getSourceAccountingLines();
316             BigDecimal initialPercent = new BigDecimal(0);
317             for (PurApAccountingLine purApAccountingLine : purApAccountingLineList) {
318                 initialPercent = initialPercent.add(purApAccountingLine.getAccountLinePercent());
319             }
320             initialPercent = new BigDecimal(100).subtract(initialPercent);
321             if (initialPercent.intValue() > 0) {
322                 item.resetAccount(initialPercent);
323             } else {
324                 item.resetAccount(new BigDecimal(0));
325             }
326         }
327         return getModelAndView(form);
328 
329     }
330 
331 
332     
333 
334 
335 
336 
337     @Override
338     public SourceAccountingLine getSourceAccountingLine(DocumentFormBase form, HttpServletRequest request) {
339         String[] indexes = getSelectedLineForAccounts(request);
340         int itemIndex = Integer.parseInt(indexes[0]);
341         int accountIndex = Integer.parseInt(indexes[1]);
342         OlePurchasingAccountsPayableFormBase purchasingAccountsPayableForm = (OlePurchasingAccountsPayableFormBase) form;
343         SourceAccountingLine line;
344         if (itemIndex == -2) {
345             line = customAccountRetrieval(accountIndex, purchasingAccountsPayableForm);
346         } else {
347             PurApItem item = (PurApItem) ((PurchasingAccountsPayableDocument) purchasingAccountsPayableForm.getDocument()).getItem((itemIndex));
348             line = (SourceAccountingLine) ObjectUtils.deepCopy(item.getSourceAccountingLines().get(accountIndex));
349         }
350         return line;
351     }
352 
353     
354 
355 
356 
357 
358 
359 
360     protected SourceAccountingLine customAccountRetrieval(int accountIndex, OlePurchasingAccountsPayableFormBase purchasingAccountsPayableForm) {
361         
362         return null;
363     }
364 
365     
366 
367 
368 
369 
370 
371 
372 
373     protected String[] getSelectedLineForAccounts(HttpServletRequest request) {
374         String accountString = new String();
375         String parameterName = (String) request.getAttribute(OLEConstants.METHOD_TO_CALL_ATTRIBUTE);
376         if (StringUtils.isNotBlank(parameterName)) {
377             accountString = StringUtils.substringBetween(parameterName, ".line", ".");
378         }
379         String[] result = StringUtils.split(accountString, ":");
380 
381         return result;
382     }
383 
384     
385 
386 
387 
388 
389 
390 
391     @Override
392     @RequestMapping(method = RequestMethod.POST, params = "methodToCall=downloadBOAttachment")
393     public ModelAndView downloadAttachment(DocumentFormBase form, HttpServletResponse response) {
394         PurchasingAccountsPayableDocument document = (PurchasingAccountsPayableDocument) ((OlePurchasingAccountsPayableFormBase) form).getDocument();
395 
396         for (org.kuali.rice.krad.bo.Note note : (java.util.List<org.kuali.rice.krad.bo.Note>) document.getNotes()) {
397             note.refreshReferenceObject("attachment");
398         }
399         return super.downloadAttachment(form, response);
400     }
401 
402 
403     
404 
405 
406 
407 
408 
409 
410 
411 
412 
413 
414 
415     @RequestMapping(method = RequestMethod.POST, params = "methodToCall=calculate")
416     public ModelAndView calculate(@ModelAttribute("KualiForm") UifFormBase uifForm) {
417         return getModelAndView(uifForm);
418 
419     }
420 
421     
422 
423 
424 
425 
426 
427 
428     @RequestMapping(method = RequestMethod.POST, params = "methodToCall=clearAllTaxes")
429     public ModelAndView clearAllTaxes(@ModelAttribute("KualiForm") UifFormBase uifForm, BindingResult result,
430                                       HttpServletRequest request,
431                                       HttpServletResponse response) {
432         return getModelAndView(uifForm);
433 
434     }
435 
436     protected void customCalculate(OlePurchasingAccountsPayableDocument purapDoc) {
437         
438     }
439 
440     
441 
442 
443 
444 
445 
446 
447     @RequestMapping(method = RequestMethod.POST, params = "methodToCall=showAllAccounts")
448     public ModelAndView showAllAccounts(@ModelAttribute("KualiForm") UifFormBase uifForm,
449                                         HttpServletRequest request,
450                                         HttpServletResponse response) {
451         OlePurchasingAccountsPayableFormBase olePurchasingAccountsPayableFormBase = (OlePurchasingAccountsPayableFormBase) uifForm;
452         String accountingLineTab = "AccountingLines";
453         String value = null;
454 
455         Map<String, String> tabStates = olePurchasingAccountsPayableFormBase.getTabStates();
456         Map<String, String> newTabStates = new HashMap<String, String>();
457         for (Map.Entry<String, String> tabEntry : tabStates.entrySet()) {
458             if (tabEntry.getKey().startsWith(accountingLineTab)) {
459                 newTabStates.put(tabEntry.getKey(), "OPEN");
460             } else {
461                 if (tabEntry.getValue() instanceof String) {
462                     value = tabEntry.getValue();
463                 } else {
464                     
465                     
466                     Object result = tabEntry.getValue();
467                     result.getClass();
468                     value = ((String[]) result)[0];
469                 }
470                 newTabStates.put(tabEntry.getKey(), value);
471             }
472         }
473         olePurchasingAccountsPayableFormBase.setTabStates(newTabStates);
474         return getModelAndView(uifForm);
475 
476     }
477 
478     
479 
480 
481 
482 
483 
484 
485 
486     @RequestMapping(method = RequestMethod.POST, params = "methodToCall=hideAllAccounts")
487     public ModelAndView hideAllAccounts(@ModelAttribute("KualiForm") UifFormBase uifForm,
488                                         HttpServletRequest request,
489                                         HttpServletResponse response) {
490         OlePurchasingAccountsPayableFormBase olePurchasingAccountsPayableFormBase = (OlePurchasingAccountsPayableFormBase) uifForm;
491         String accountingLineTab = "AccountingLines";
492         String value = null;
493 
494         Map<String, String> tabStates = olePurchasingAccountsPayableFormBase.getTabStates();
495         Map<String, String> newTabStates = new HashMap<String, String>();
496         for (Map.Entry<String, String> tabEntry : tabStates.entrySet()) {
497             if (tabEntry.getKey().startsWith(accountingLineTab)) {
498                 newTabStates.put(tabEntry.getKey(), "CLOSE");
499             } else {
500                 if (tabEntry.getValue() instanceof String) {
501                     value = tabEntry.getValue();
502                 } else {
503                     
504                     
505                     Object result = tabEntry.getValue();
506                     result.getClass();
507                     value = ((String[]) result)[0];
508                 }
509                 newTabStates.put(tabEntry.getKey(), value);
510             }
511         }
512         olePurchasingAccountsPayableFormBase.setTabStates(newTabStates);
513         return getModelAndView(uifForm);
514 
515     }
516 
517 
518     @Override
519     @RequestMapping(method = RequestMethod.POST, params = "methodToCall=insertNote")
520     public ModelAndView insertNote(DocumentFormBase form) {
521         OlePurchasingAccountsPayableDocument document = (OlePurchasingAccountsPayableDocument) ((OlePurchasingAccountsPayableFormBase) form).getDocument();
522         WorkflowDocument workflowDocument = document.getDocumentHeader().getWorkflowDocument();
523 
524         if (workflowDocument.isInitiated()) {
525             try {
526                 SpringContext.getBean(DocumentService.class).saveDocument(document);
527             } catch (WorkflowException e) {
528                 e.printStackTrace();
529             }
530         }
531 
532         return super.insertNote(form);
533 
534     }
535 
536     @Override
537     protected OLEPurchaseAccountsPayableControllerBaseServiceImpl getControllerService() {
538         return (OLEPurchaseAccountsPayableControllerBaseServiceImpl)  super.getControllerService();
539     }
540 
541     @Override
542     @Autowired
543     @Qualifier("olePurchaseAccountsPayableControllerBaseService")
544     public void setControllerService(ControllerService controllerService) {
545         super.setControllerService(controllerService);
546     }
547 
548 
549 }