View Javadoc
1   package org.kuali.ole.select.controller;
2   
3   import org.apache.commons.lang.StringUtils;
4   import org.apache.commons.lang.time.DateUtils;
5   import org.kuali.ole.OLEKeyConstants;
6   import org.kuali.ole.batch.service.OLEClaimNoticeService;
7   import org.kuali.ole.select.bo.OLEClaimingAddress;
8   import org.kuali.ole.select.bo.OLEClaimingByTitle;
9   import org.kuali.ole.select.bo.OLEClaimingByVendor;
10  import org.kuali.ole.select.bo.OLEPOClaimHistory;
11  import org.kuali.ole.select.businessobject.OlePurchaseOrderItem;
12  import org.kuali.ole.select.businessobject.OleRequisitionItem;
13  import org.kuali.ole.select.document.OlePurchaseOrderDocument;
14  import org.kuali.ole.select.form.OLEAddTitlesToInvoiceForm;
15  import org.kuali.ole.select.form.OLEClaimingForm;
16  import org.kuali.ole.select.lookup.DocData;
17  import org.kuali.ole.vnd.businessobject.VendorDetail;
18  import org.kuali.rice.core.api.util.type.KualiInteger;
19  import org.kuali.rice.krad.service.KRADServiceLocator;
20  import org.kuali.rice.krad.util.GlobalVariables;
21  import org.kuali.rice.krad.web.controller.UifControllerBase;
22  import org.kuali.rice.krad.web.form.UifFormBase;
23  import org.springframework.stereotype.Controller;
24  import org.springframework.validation.BindingResult;
25  import org.springframework.web.bind.annotation.ModelAttribute;
26  import org.springframework.web.bind.annotation.RequestMapping;
27  import org.springframework.web.servlet.ModelAndView;
28  
29  import javax.servlet.http.HttpServletRequest;
30  import javax.servlet.http.HttpServletResponse;
31  import java.io.IOException;
32  import java.sql.*;
33  import java.sql.Date;
34  import java.util.*;
35  
36  /**
37   * Created with IntelliJ IDEA.
38   * User: vivekb
39   * Date: 1/18/14
40   * Time: 7:11 PM
41   * To change oleClaimingByTitle template use File | Settings | File Templates.
42   */
43  @Controller
44  @RequestMapping(value = "/oleClaimingController")
45  public class OLEClaimingController extends UifControllerBase {
46  
47      /**
48       * oleClaimingByTitle method converts UifFormBase to OLEClaimingForm
49       *
50       * @param form
51       * @param result
52       * @param request
53       * @param response
54       * @return ModelAndView
55       */
56      @Override
57      @RequestMapping(params = "methodToCall=start")
58      public ModelAndView start(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
59                                HttpServletRequest request, HttpServletResponse response) {
60          OLEClaimingForm oleClaimingForm = (OLEClaimingForm)form;
61          String poItemList = request.getParameter("poItemList");
62          String poItemListSelection = request.getParameter("poItemListSelection");
63          String titleListSelection = request.getParameter("titleListSelection");
64          String vendorListSelection = request.getParameter("vendorListSelection");
65          String[] poListArray = poItemList!=null ? poItemList.split("~") : new String[0];
66          String[] poListSelectionArray = poItemListSelection!=null ? poItemListSelection.split("~") : new String[0];
67          String[] titleListSelectionArray = titleListSelection!=null ? titleListSelection.split("~") : new String[0];
68          String[] vendorListSelectionArray = vendorListSelection!=null ? vendorListSelection.split("~") : new String[0];
69          HashMap<String,List<OLEClaimingByTitle>> hashMap = new HashMap<>();
70          List<OLEClaimingByTitle> claimTitles = null;
71          for (int i = 1; i < poListArray.length; i++) {
72              if (poListArray[i].equals("true")) {
73                  if (hashMap.containsKey( vendorListSelectionArray[i])) {
74                      claimTitles = hashMap.get(vendorListSelectionArray[i]);
75                      OLEClaimingByTitle oleClaimingByTitle = new OLEClaimingByTitle();
76                      oleClaimingByTitle.setTitle(titleListSelectionArray[i]);
77                      oleClaimingByTitle.setPoItemId(poListSelectionArray[i]);
78                      claimTitles.add(oleClaimingByTitle);
79                      hashMap.put(vendorListSelectionArray[i],claimTitles);
80                  }
81                  else {
82                      claimTitles = new ArrayList<>();
83                      OLEClaimingByTitle oleClaimingByTitle = new OLEClaimingByTitle();
84                      oleClaimingByTitle.setTitle(titleListSelectionArray[i]);
85                      oleClaimingByTitle.setPoItemId(poListSelectionArray[i]);
86                      claimTitles.add(oleClaimingByTitle);
87                      hashMap.put(vendorListSelectionArray[i],claimTitles);
88                  }
89              }
90          }
91          Iterator<Map.Entry<String,List<OLEClaimingByTitle>>> entries = hashMap.entrySet().iterator();
92          List<OLEClaimingByVendor> oleClaimingByVendors = new ArrayList<>();
93          while (entries.hasNext()) {
94              Map.Entry<String,List<OLEClaimingByTitle>> entry = entries.next();
95              List<OLEClaimingByTitle> oleClaimingByTitles = entry.getValue();
96              OLEClaimingByVendor oleClaimingByVendor = new OLEClaimingByVendor();
97              oleClaimingByVendor.setVendorName(entry.getKey());
98              oleClaimingByVendor.setOleClaimingByTitles(oleClaimingByTitles);
99              oleClaimingByVendors.add(oleClaimingByVendor);
100         }
101         oleClaimingForm.setOleClaimingByVendors(oleClaimingByVendors);
102         return super.start(oleClaimingForm, result, request, response);
103     }
104     @Override
105     protected UifFormBase createInitialForm(HttpServletRequest request) {
106         return new OLEClaimingForm();
107     }
108 
109     @RequestMapping(params = "methodToCall=claim")
110     public ModelAndView claim(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result, HttpServletRequest request, HttpServletResponse response) throws IOException {
111         OLEClaimingForm oleClaimingForm = (OLEClaimingForm) form;
112         OLEClaimNoticeService oleClaimNoticeService = new OLEClaimNoticeService();
113         List<OLEClaimingByVendor>  oleClaimingByVendors = oleClaimingForm.getOleClaimingByVendors();
114         for(OLEClaimingByVendor oleClaimingByVendor:  oleClaimingByVendors){
115             for(OLEClaimingByTitle oleClaimingByTitle : oleClaimingByVendor.getOleClaimingByTitles()){
116                 Map map = new HashMap();
117                 map.put("itemIdentifier", oleClaimingByTitle.getPoItemId());
118                 OlePurchaseOrderItem olePurchaseOrderItem = KRADServiceLocator.getBusinessObjectService().findByPrimaryKey(OlePurchaseOrderItem.class,map);
119                 if(olePurchaseOrderItem.getClaimCount()!=null){
120                     Integer count = olePurchaseOrderItem.getClaimCount().intValue();
121                     count++;
122                     olePurchaseOrderItem.setClaimCount(new KualiInteger(count));
123                 }else {
124                     olePurchaseOrderItem.setClaimCount(new KualiInteger(1));
125                 }
126                 // TODO olePurchaseOrderItem.setClaimDate();
127                 String emailAddress="";
128                 String claimInterval="";
129                 OlePurchaseOrderDocument olePurchaseOrderDocument = olePurchaseOrderItem.getPurapDocument();
130                 VendorDetail vendorDetail =olePurchaseOrderDocument.getVendorDetail();
131                 java.util.Date actionDate=null;
132                 if( vendorDetail.getVendorContacts()!=null &&  vendorDetail.getVendorContacts().size()>0){
133                     emailAddress = vendorDetail.getVendorContacts().get(0).getVendorContactEmailAddress();
134                     claimInterval = vendorDetail.getClaimInterval();
135                 }
136                 if((emailAddress==null || emailAddress.isEmpty() ) && vendorDetail.getVendorAddresses()!=null &&  vendorDetail.getVendorAddresses().size()>0){
137                     emailAddress = vendorDetail.getVendorAddresses().get(0).getVendorAddressEmailAddress();
138                 }
139                 if (StringUtils.isNotBlank(claimInterval)) {
140                     boolean actIntvlFlag = isNumber(claimInterval);
141                     if (actIntvlFlag) {
142                         Integer actIntvl = Integer.parseInt(claimInterval);
143                         actionDate = DateUtils.addDays(new java.util.Date(), actIntvl);
144                         olePurchaseOrderItem.setClaimDate(new Date(actionDate.getTime()));
145                     }
146                 }  else{
147                     olePurchaseOrderItem.setClaimDate(null);
148                 }
149                 oleClaimNoticeService.updateOleClaimingByTitle(oleClaimingByTitle, olePurchaseOrderItem, olePurchaseOrderDocument);
150                 OLEClaimingAddress toAddress = oleClaimNoticeService.updateClaimingToAddress(olePurchaseOrderDocument);
151                 OLEClaimingAddress fromAddress = oleClaimNoticeService.updateClaimingFromAddress(olePurchaseOrderDocument);
152                 oleClaimingByVendor.setToAddress(toAddress);
153                 oleClaimingByVendor.setFromAddress(fromAddress);
154                 oleClaimingByVendor.setVendorTransmissionFormatDetail(oleClaimingByTitle.getVendorTransmissionFormatDetail());
155                 oleClaimingByVendor.setEmailAddress(emailAddress);
156                 OLEPOClaimHistory claimHistory = new OLEPOClaimHistory();
157                 claimHistory.setClaimCount(olePurchaseOrderItem.getClaimCount().intValue());
158                 claimHistory.setClaimDate(new Date(System.currentTimeMillis()));
159                 claimHistory.setReqItemId(olePurchaseOrderItem.getCopyList().get(0).getReqItemId());
160                 claimHistory.setOperator(GlobalVariables.getUserSession().getPrincipalName());
161                 olePurchaseOrderItem.getClaimHistories().add(claimHistory);
162                 KRADServiceLocator.getBusinessObjectService().save(olePurchaseOrderItem);
163             }
164             try{
165                 oleClaimNoticeService.generatePdf(oleClaimingByVendor);
166             }catch (Exception e){
167                 e.printStackTrace();
168             }
169         }
170         //GlobalVariables.getMessageMap().putInfo(OLEKeyConstants.CLAIM_SUCCESS_MSG,OLEKeyConstants.CLAIM_SUCCESS_MSG);
171         oleClaimingForm.setSuccessMsg("Claimed Successfully");
172         return getUIFModelAndView(oleClaimingForm);
173     }
174 
175     @RequestMapping(params = "methodToCall=cancelProcess")
176     public ModelAndView cancelProcess(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result, HttpServletRequest request, HttpServletResponse response) {
177         OLEClaimingForm oleClaimingForm = (OLEClaimingForm) form;
178         oleClaimingForm.setCancelBox(true);
179         return getUIFModelAndView(oleClaimingForm);
180     }
181 
182     private static boolean isNumber(String actionInterval) {
183         String actStr = actionInterval;
184         for (int i = 0; i < actStr.length(); i++) {
185             if (!Character.isDigit(actStr.charAt(i)))
186                 return false;
187         }
188         return true;
189     }
190 
191 }