1 package org.kuali.ole.select.controller;
2
3 import org.apache.commons.lang3.StringUtils;
4 import org.apache.log4j.Logger;
5 import org.kuali.ole.OLEPropertyConstants;
6 import org.kuali.ole.select.bo.OLEDonor;
7 import org.kuali.ole.select.bo.OLELinkPurapDonor;
8 import org.kuali.ole.select.businessobject.OlePurchaseOrderItem;
9 import org.kuali.ole.select.document.OlePurchaseOrderDocument;
10 import org.kuali.ole.select.form.OLEEncumberedReportForm;
11 import org.kuali.ole.service.impl.OleLicenseRequestServiceImpl;
12 import org.kuali.ole.sys.OLEConstants;
13 import org.kuali.rice.core.api.config.property.ConfigContext;
14 import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
15 import org.kuali.rice.core.api.util.type.KualiDecimal;
16 import org.kuali.rice.krad.service.BusinessObjectService;
17 import org.kuali.rice.krad.service.KRADServiceLocator;
18 import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
19 import org.kuali.rice.krad.service.LookupService;
20 import org.kuali.rice.krad.uif.UifConstants;
21 import org.kuali.rice.krad.uif.UifParameters;
22 import org.kuali.rice.krad.util.GlobalVariables;
23 import org.kuali.rice.krad.util.KRADConstants;
24 import org.kuali.rice.krad.web.controller.UifControllerBase;
25 import org.kuali.rice.krad.web.form.UifFormBase;
26 import org.springframework.stereotype.Controller;
27 import org.springframework.validation.BindingResult;
28 import org.springframework.web.bind.annotation.ModelAttribute;
29 import org.springframework.web.bind.annotation.RequestMapping;
30 import org.springframework.web.servlet.ModelAndView;
31
32 import javax.servlet.http.HttpServletRequest;
33 import javax.servlet.http.HttpServletResponse;
34 import java.text.SimpleDateFormat;
35 import java.util.*;
36
37
38
39
40
41
42
43
44 @Controller
45 @RequestMapping(value = "/searchDonorEncumberedReportController")
46 public class OLEEncumberedReportController extends UifControllerBase {
47
48 private BusinessObjectService boService = KRADServiceLocator.getBusinessObjectService();
49 private static final Logger LOG = Logger.getLogger(OLEEResourceSearchController.class);
50 private static final SimpleDateFormat dateFormat = new SimpleDateFormat(org.kuali.ole.OLEConstants.OLEEResourceRecord.CREATED_DATE_FORMAT);
51
52 private LookupService getLookupService() {
53 return KRADServiceLocatorWeb.getLookupService();
54 }
55
56 @Override
57 protected UifFormBase createInitialForm(HttpServletRequest httpServletRequest) {
58 return new OLEEncumberedReportForm();
59 }
60
61 @RequestMapping(params = "methodToCall=search")
62 public ModelAndView search(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
63 HttpServletRequest request, HttpServletResponse response) throws Exception {
64 OLEEncumberedReportForm oleEncumberedReportForm = (OLEEncumberedReportForm) form;
65 List<OLEDonor> oleDonorList = new ArrayList<>();
66 List<OLELinkPurapDonor> oleLinkPurapDonors = new ArrayList<>();
67 if (StringUtils.isEmpty(oleEncumberedReportForm.getDonorCode())) {
68 oleLinkPurapDonors = (List<OLELinkPurapDonor>) KRADServiceLocator.getBusinessObjectService().findAll(OLELinkPurapDonor.class);
69 } else {
70 Map map = new HashMap();
71 map.put(OLEConstants.DONOR_CODE, oleEncumberedReportForm.getDonorCode());
72 List<OLEDonor> donorList = (List<OLEDonor>) KRADServiceLocator.getBusinessObjectService().findMatching(OLEDonor.class, map);
73 if (donorList != null && donorList.size() > 0) {
74 oleLinkPurapDonors = (List<OLELinkPurapDonor>) KRADServiceLocator.getBusinessObjectService().findMatching(OLELinkPurapDonor.class, map);
75 } else {
76 oleEncumberedReportForm.setEncumberedReportDocumentList(null);
77 GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, org.kuali.ole.OLEConstants.DONOR_NOT_FOUND);
78 return getUIFModelAndView(oleEncumberedReportForm);
79 }
80 }
81 Map<String, List<Integer>> donorMap = new HashMap<>();
82 for (OLELinkPurapDonor oleLinkPurapDonor : oleLinkPurapDonors) {
83 Map poItemIdList = new HashMap();
84 poItemIdList.put("itemIdentifier", oleLinkPurapDonor.getPoItemId());
85 OlePurchaseOrderItem olePurchaseOrderItem = KRADServiceLocator.getBusinessObjectService().findByPrimaryKey(OlePurchaseOrderItem.class, poItemIdList);
86 if (oleLinkPurapDonor.getDonorCode() != null && olePurchaseOrderItem != null && olePurchaseOrderItem.getItemIdentifier() != null) {
87 if (!(oleEncumberedReportForm.getFromDate() == null && oleEncumberedReportForm.getToDate() == null)) {
88 Map map = new HashMap();
89 map.put(org.kuali.ole.OLEConstants.DOC_NUM, olePurchaseOrderItem.getDocumentNumber());
90 OlePurchaseOrderDocument olePurchaseOrderDocument = boService.findByPrimaryKey(OlePurchaseOrderDocument.class, map);
91 Date purchaseOrderDate = olePurchaseOrderDocument.getPurchaseOrderCreateTimestamp();
92 try {
93 String begin = null;
94 if (oleEncumberedReportForm.getFromDate() != null) {
95 begin = dateFormat.format(oleEncumberedReportForm.getFromDate());
96 }
97 String end = null;
98 if (oleEncumberedReportForm.getToDate() != null) {
99 end = dateFormat.format(oleEncumberedReportForm.getToDate());
100 }
101 boolean isValid = false;
102 OleLicenseRequestServiceImpl oleLicenseRequestService = GlobalResourceLoader.getService(org.kuali.ole.OLEConstants.OleLicenseRequest.LICENSE_REQUEST_SERVICE);
103 isValid = oleLicenseRequestService.validateDate(purchaseOrderDate, begin, end);
104 if (isValid) {
105 if (donorMap.containsKey(oleLinkPurapDonor.getDonorCode())) {
106 donorMap.get(oleLinkPurapDonor.getDonorCode()).add(olePurchaseOrderItem.getItemIdentifier());
107 } else {
108 List<Integer> poItemIds = new ArrayList<>();
109 poItemIds.add(olePurchaseOrderItem.getItemIdentifier());
110 donorMap.put(oleLinkPurapDonor.getDonorCode(), poItemIds);
111 }
112 }
113 } catch (Exception e) {
114 LOG.error("Exception while calling the licenseRequest service" + e.getMessage());
115 throw new RuntimeException(e);
116 }
117 } else {
118 if (donorMap.containsKey(oleLinkPurapDonor.getDonorCode())) {
119 donorMap.get(oleLinkPurapDonor.getDonorCode()).add(olePurchaseOrderItem.getItemIdentifier());
120 } else {
121 List<Integer> poItemIds = new ArrayList<>();
122 poItemIds.add(olePurchaseOrderItem.getItemIdentifier());
123 donorMap.put(oleLinkPurapDonor.getDonorCode(), poItemIds);
124 }
125 }
126 }
127 }
128 for (Object donorKey : donorMap.keySet()) {
129 OLEDonor oleDonor = new OLEDonor();
130 oleDonor.setDonorCode((String) donorKey);
131 Map map = new HashMap();
132 map.put(OLEConstants.DONOR_CODE, oleDonor.getDonorCode());
133 OLEDonor donor = KRADServiceLocator.getBusinessObjectService().findByPrimaryKey(OLEDonor.class, map);
134 if (donor != null) {
135 oleDonor.setDonorAmount(donor.getDonorAmount() != null ? donor.getDonorAmount() : KualiDecimal.ZERO);
136 oleDonor.setDonorNote(donor.getDonorNote());
137 oleDonor.setDonorId(donor.getDonorId());
138 }
139 if (oleDonor.getDonorAmount() != null) {
140 KualiDecimal encAmt = KualiDecimal.ZERO;
141 KualiDecimal expAmt = KualiDecimal.ZERO;
142 List<Integer> poLineItemIds = (ArrayList) donorMap.get((String) donorKey);
143 for (Integer poLineItemId : poLineItemIds) {
144 map.clear();
145 map.put(org.kuali.ole.OLEConstants.BIB_ITEM_ID, poLineItemId);
146 OlePurchaseOrderItem olePurchaseOrderItem = boService.findByPrimaryKey(OlePurchaseOrderItem.class, map);
147 if (olePurchaseOrderItem.getItemOutstandingEncumberedAmount() != null) {
148 encAmt = encAmt.add(olePurchaseOrderItem.getItemOutstandingEncumberedAmount().divide(new KualiDecimal(olePurchaseOrderItem.getOleDonors().size())));
149 }
150 if (olePurchaseOrderItem.getItemInvoicedTotalAmount() != null) {
151 expAmt = expAmt.add(olePurchaseOrderItem.getItemInvoicedTotalAmount().divide(new KualiDecimal(olePurchaseOrderItem.getOleDonors().size())));
152 }
153 }
154 oleDonor.setEncumberedAmount(encAmt);
155 oleDonor.setExpensedAmount(expAmt);
156 }
157 oleDonorList.add(oleDonor);
158 }
159 if (oleDonorList.size() == 0) {
160 oleEncumberedReportForm.setEncumberedReportDocumentList(null);
161 GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, org.kuali.ole.OLEConstants.NO_RECORD_FOUND);
162 } else {
163 oleEncumberedReportForm.setEncumberedReportDocumentList(oleDonorList);
164 }
165 return getUIFModelAndView(oleEncumberedReportForm);
166 }
167
168 @RequestMapping(params = "methodToCall=clearSearch")
169 public ModelAndView clearSearch(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
170 HttpServletRequest request, HttpServletResponse response) {
171 OLEEncumberedReportForm oleEncumberedReportForm = (OLEEncumberedReportForm) form;
172 oleEncumberedReportForm.setDonorCode(null);
173 oleEncumberedReportForm.setFromDate(null);
174 oleEncumberedReportForm.setToDate(null);
175 oleEncumberedReportForm.setEncumberedReportDocumentList(null);
176 return getUIFModelAndView(oleEncumberedReportForm);
177 }
178
179 @RequestMapping(params = "methodToCall=cancel")
180 public ModelAndView cancel(@ModelAttribute("KualiForm") UifFormBase form, BindingResult result,
181 HttpServletRequest request, HttpServletResponse response) {
182 String baseUrl = ConfigContext.getCurrentContextConfig().getProperty(OLEPropertyConstants.OLE_URL_BASE);
183 String url = baseUrl + "/portal.do";
184 Properties props = new Properties();
185 props.put(UifParameters.METHOD_TO_CALL, UifConstants.MethodToCallNames.REFRESH);
186 if (org.apache.commons.lang.StringUtils.isNotBlank(form.getReturnFormKey())) {
187 props.put(UifParameters.FORM_KEY, form.getReturnFormKey());
188 }
189 return performRedirect(form, url, props);
190 }
191
192 }