1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  package org.kuali.ole.pdp.businessobject.lookup;
17  
18  import java.util.ArrayList;
19  import java.util.List;
20  import java.util.Map;
21  import java.util.Properties;
22  
23  import org.apache.commons.lang.StringUtils;
24  import org.kuali.ole.pdp.PdpConstants;
25  import org.kuali.ole.pdp.PdpKeyConstants;
26  import org.kuali.ole.pdp.PdpParameterConstants;
27  import org.kuali.ole.pdp.PdpPropertyConstants;
28  import org.kuali.ole.pdp.businessobject.Batch;
29  import org.kuali.ole.pdp.businessobject.PaymentDetail;
30  import org.kuali.ole.pdp.service.BatchMaintenanceService;
31  import org.kuali.ole.pdp.service.PdpAuthorizationService;
32  import org.kuali.ole.sys.OLEConstants;
33  import org.kuali.rice.core.api.config.property.ConfigurationService;
34  import org.kuali.rice.core.web.format.BooleanFormatter;
35  import org.kuali.rice.kim.api.identity.Person;
36  import org.kuali.rice.kns.lookup.HtmlData;
37  import org.kuali.rice.kns.lookup.HtmlData.AnchorHtmlData;
38  import org.kuali.rice.kns.lookup.KualiLookupableHelperServiceImpl;
39  import org.kuali.rice.kns.util.KNSGlobalVariables;
40  import org.kuali.rice.krad.bo.BusinessObject;
41  import org.kuali.rice.krad.dao.LookupDao;
42  import org.kuali.rice.krad.exception.ValidationException;
43  import org.kuali.rice.krad.util.GlobalVariables;
44  import org.kuali.rice.krad.util.KRADConstants;
45  import org.kuali.rice.krad.util.UrlFactory;
46  
47  
48  
49  
50  public class BatchLookupableHelperService extends KualiLookupableHelperServiceImpl {
51      private BatchMaintenanceService batchMaintenanceService;
52      private ConfigurationService configurationService;
53      private LookupDao lookupDao;
54      private PdpAuthorizationService pdpAuthorizationService;
55  
56      
57  
58  
59      @Override
60      public List<? extends BusinessObject> getSearchResults(Map<String, String> fieldValues) {
61          Map parameters = super.getParameters();
62          String errorList;
63  
64          if (parameters.containsKey(PdpParameterConstants.ACTION_SUCCESSFUL_PARAM)) {
65              String[] actionSuccessRequestParm = (String[]) parameters.get(PdpParameterConstants.ACTION_SUCCESSFUL_PARAM);
66              Boolean actionSuccess = (Boolean) (new BooleanFormatter()).convertFromPresentationFormat(actionSuccessRequestParm[0]);
67  
68              if (actionSuccess != null) {
69  
70                  if (!actionSuccess) {
71  
72                      
73                      
74                      if (parameters.containsKey(PdpParameterConstants.ERROR_KEY_LIST_PARAM)) {
75                          String[] errorListParam = (String[]) parameters.get(PdpParameterConstants.ERROR_KEY_LIST_PARAM);
76                          errorList = errorListParam[0];
77                          if (StringUtils.isNotEmpty(errorList)) {
78                              String[] errorMsgs = StringUtils.split(errorList, PdpParameterConstants.ERROR_KEY_LIST_SEPARATOR);
79                              for (String error : errorMsgs) {
80                                  if (StringUtils.isNotEmpty(error)) {
81                                      GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, error);
82                                  }
83                              }
84                          }
85                      }
86                  }
87                  else {
88                      if (parameters.containsKey(PdpParameterConstants.MESSAGE_PARAM)) {
89                          String[] messageRequestParm = (String[]) parameters.get(PdpParameterConstants.MESSAGE_PARAM);
90                          String message = messageRequestParm[0];
91                          KNSGlobalVariables.getMessageList().add(message);
92                      }
93                  }
94              }
95          }
96  
97          List results = super.getSearchResults(fieldValues);
98          return results;
99      }
100 
101     
102 
103 
104 
105     @Override
106     public HtmlData getInquiryUrl(BusinessObject bo, String propertyName) {
107         AnchorHtmlData inquiryUrl = (AnchorHtmlData) super.getInquiryUrl(bo, propertyName);
108         Batch batch = (Batch) bo;
109         if (propertyName.equalsIgnoreCase(PdpPropertyConstants.BatchConstants.BATCH_ID)) {
110             Properties params = new Properties();
111             params.put(OLEConstants.DISPATCH_REQUEST_PARAMETER, OLEConstants.SEARCH_METHOD);
112             params.put(OLEConstants.BUSINESS_OBJECT_CLASS_ATTRIBUTE, PaymentDetail.class.getName());
113             params.put(KRADConstants.DOC_FORM_KEY, "88888888");
114             params.put(OLEConstants.HIDE_LOOKUP_RETURN_LINK, "true");
115             params.put(OLEConstants.BACK_LOCATION, configurationService.getPropertyValueAsString(KRADConstants.APPLICATION_URL_KEY) + "/" + OLEConstants.MAPPING_PORTAL + ".do");
116             params.put(PdpPropertyConstants.PaymentDetail.PAYMENT_GROUP_BATCH_ID, UrlFactory.encode(String.valueOf(batch.getId())));
117             String url = UrlFactory.parameterizeUrl(KRADConstants.LOOKUP_ACTION, params);
118             inquiryUrl.setHref(url);
119         }
120         return inquiryUrl;
121     }
122 
123     
124 
125 
126 
127     @Override
128     public List<HtmlData> getCustomActionUrls(BusinessObject businessObject, List pkNames) {
129 
130         if (businessObject instanceof Batch) {
131             Person person = GlobalVariables.getUserSession().getPerson();
132             Batch batch = (Batch) businessObject;
133             Integer batchId = batch.getId().intValue();
134             List<HtmlData> anchorHtmlDataList = new ArrayList<HtmlData>();
135             String linkText = OLEConstants.EMPTY_STRING;
136             String url = OLEConstants.EMPTY_STRING;
137             String basePath = configurationService.getPropertyValueAsString(OLEConstants.APPLICATION_URL_KEY) + "/" + PdpConstants.Actions.BATCH_SEARCH_DETAIL_ACTION;
138 
139             if ( pdpAuthorizationService.hasCancelPaymentPermission(person.getPrincipalId()) && batchMaintenanceService.doBatchPaymentsHaveOpenOrHeldStatus(batchId)) {
140 
141                 Properties params = new Properties();
142                 params.put(OLEConstants.DISPATCH_REQUEST_PARAMETER, PdpConstants.ActionMethods.CONFIRM_CANCEL_ACTION);
143                 params.put(PdpParameterConstants.BatchConstants.BATCH_ID_PARAM, UrlFactory.encode(String.valueOf(batchId)));
144                 url = UrlFactory.parameterizeUrl(basePath, params);
145 
146                 linkText = configurationService.getPropertyValueAsString(PdpKeyConstants.BatchConstants.LinkText.CANCEL_BATCH);
147 
148                 AnchorHtmlData anchorHtmlData = new AnchorHtmlData(url, PdpConstants.ActionMethods.CONFIRM_CANCEL_ACTION, linkText);
149                 anchorHtmlDataList.add(anchorHtmlData);
150             }
151             
152             if ( pdpAuthorizationService.hasHoldPaymentPermission(person.getPrincipalId())) {
153 
154                 if (batchMaintenanceService.doBatchPaymentsHaveHeldStatus(batchId)) {
155 
156                     Properties params = new Properties();
157                     params.put(OLEConstants.DISPATCH_REQUEST_PARAMETER, PdpConstants.ActionMethods.CONFIRM_REMOVE_HOLD_ACTION);
158                     params.put(PdpParameterConstants.BatchConstants.BATCH_ID_PARAM, UrlFactory.encode(String.valueOf(batchId)));
159                     url = UrlFactory.parameterizeUrl(basePath, params);
160 
161                     linkText = configurationService.getPropertyValueAsString(PdpKeyConstants.BatchConstants.LinkText.REMOVE_BATCH_HOLD);
162 
163                     AnchorHtmlData anchorHtmlData = new AnchorHtmlData(url, PdpConstants.ActionMethods.CONFIRM_REMOVE_HOLD_ACTION, linkText);
164                     anchorHtmlDataList.add(anchorHtmlData);
165                 }
166                 else if (batchMaintenanceService.doBatchPaymentsHaveOpenStatus(batchId)) {
167 
168                     Properties params = new Properties();
169                     params.put(OLEConstants.DISPATCH_REQUEST_PARAMETER, PdpConstants.ActionMethods.CONFIRM_HOLD_ACTION);
170                     params.put(PdpParameterConstants.BatchConstants.BATCH_ID_PARAM, UrlFactory.encode(String.valueOf(batchId)));
171                     url = UrlFactory.parameterizeUrl(basePath, params);
172 
173                     linkText = configurationService.getPropertyValueAsString(PdpKeyConstants.BatchConstants.LinkText.HOLD_BATCH);
174                     AnchorHtmlData anchorHtmlData = new AnchorHtmlData(url, PdpConstants.ActionMethods.CONFIRM_HOLD_ACTION, linkText);
175                     anchorHtmlDataList.add(anchorHtmlData);
176                 }
177             }
178 
179             if (anchorHtmlDataList.isEmpty()) {
180                 AnchorHtmlData anchorHtmlData = new AnchorHtmlData(url, " ", " ");
181                 anchorHtmlDataList.add(anchorHtmlData);
182             }
183 
184             return anchorHtmlDataList;
185         }
186         return super.getEmptyActionUrls();
187     }
188 
189     
190 
191 
192     @Override
193     public void validateSearchParameters(Map fieldValues) {
194         
195         super.validateSearchParameters(fieldValues);
196 
197         
198         String batchIdValue = (String) fieldValues.get(PdpPropertyConstants.BatchConstants.BATCH_ID);
199         String paymentCountValue = (String) fieldValues.get(PdpPropertyConstants.BatchConstants.PAYMENT_COUNT);
200         String paymentTotalAmountValue = (String) fieldValues.get(PdpPropertyConstants.BatchConstants.PAYMENT_TOTAL_AMOUNT);
201         String fileCreationTimeValueLower = (String) fieldValues.get(KRADConstants.LOOKUP_RANGE_LOWER_BOUND_PROPERTY_PREFIX + PdpPropertyConstants.BatchConstants.FILE_CREATION_TIME);
202         String fileCreationTimeValueUpper = (String) fieldValues.get(KRADConstants.LOOKUP_DEFAULT_RANGE_SEARCH_UPPER_BOUND_LABEL + PdpPropertyConstants.BatchConstants.FILE_CREATION_TIME);
203         String chartCodeValue = (String) fieldValues.get(PdpPropertyConstants.BatchConstants.CHART_CODE);
204         String orgCodeValue = (String) fieldValues.get(PdpPropertyConstants.BatchConstants.ORG_CODE);
205         String subUnitCodeValue = (String) fieldValues.get(PdpPropertyConstants.BatchConstants.SUB_UNIT_CODE);
206 
207         
208         if (StringUtils.isBlank(batchIdValue) && StringUtils.isBlank(chartCodeValue) && StringUtils.isBlank(orgCodeValue) && StringUtils.isBlank(subUnitCodeValue) && StringUtils.isBlank(paymentCountValue) && StringUtils.isBlank(paymentTotalAmountValue) && StringUtils.isBlank(fileCreationTimeValueLower) && StringUtils.isBlank(fileCreationTimeValueUpper)) {
209             GlobalVariables.getMessageMap().putError(OLEConstants.DOCUMENT_HEADER_ERRORS, PdpKeyConstants.BatchConstants.ErrorMessages.ERROR_BATCH_CRITERIA_NONE_ENTERED);
210         }
211         else if (StringUtils.isBlank(batchIdValue) && StringUtils.isBlank(paymentCountValue) && StringUtils.isBlank(paymentTotalAmountValue)) {
212             
213             if (StringUtils.isBlank(fileCreationTimeValueLower) && StringUtils.isBlank(fileCreationTimeValueUpper) ) {
214                 GlobalVariables.getMessageMap().putError(PdpPropertyConstants.BatchConstants.FILE_CREATION_TIME, PdpKeyConstants.BatchConstants.ErrorMessages.ERROR_BATCH_CRITERIA_NO_DATE);
215             }
216             else if (StringUtils.isBlank(fileCreationTimeValueLower) || StringUtils.isBlank(fileCreationTimeValueUpper)) {
217                 
218                 if (StringUtils.isBlank(chartCodeValue) && StringUtils.isBlank(orgCodeValue) && StringUtils.isBlank(subUnitCodeValue)) {
219                     GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, PdpKeyConstants.BatchConstants.ErrorMessages.ERROR_BATCH_CRITERIA_SOURCE_MISSING);
220                 }
221             }
222         }
223 
224         if (GlobalVariables.getMessageMap().hasErrors()) {
225             throw new ValidationException("errors in search criteria");
226         }
227     }
228 
229     
230 
231 
232 
233 
234     public ConfigurationService getConfigurationService() {
235         return configurationService;
236     }
237 
238     
239 
240 
241 
242 
243     public void setConfigurationService(ConfigurationService configurationService) {
244         this.configurationService = configurationService;
245     }
246 
247     
248 
249 
250 
251 
252     public BatchMaintenanceService getBatchMaintenanceService() {
253         return batchMaintenanceService;
254     }
255 
256     
257 
258 
259 
260 
261     public void setBatchMaintenanceService(BatchMaintenanceService batchMaintenanceService) {
262         this.batchMaintenanceService = batchMaintenanceService;
263     }
264 
265     
266 
267 
268 
269 
270     public LookupDao getLookupDao() {
271         return lookupDao;
272     }
273 
274     
275 
276 
277 
278 
279     public void setLookupDao(LookupDao lookupDao) {
280         this.lookupDao = lookupDao;
281     }
282 
283     
284 
285 
286 
287     public void setPdpAuthorizationService(PdpAuthorizationService pdpAuthorizationService) {
288         this.pdpAuthorizationService = pdpAuthorizationService;
289     }
290 
291 }