View Javadoc
1   /*
2    * Copyright 2008 The Kuali Foundation
3    * 
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    * http://www.opensource.org/licenses/ecl2.php
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * This class allows custom handling of Batches within the lookup framework.
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       * @see org.kuali.rice.kns.lookup.KualiLookupableHelperServiceImpl#getSearchResults(java.util.Map)
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                      // if the action performed on batch was not successful we get the error message list and add them to
73                      // GlobalVariables errorMap
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      * @see org.kuali.rice.kns.lookup.AbstractLookupableHelperServiceImpl#getInquiryUrl(org.kuali.rice.krad.bo.BusinessObject,
103      *      java.lang.String)
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      * @see org.kuali.rice.kns.lookup.AbstractLookupableHelperServiceImpl#getCustomActionUrls(org.kuali.rice.krad.bo.BusinessObject,
125      *      java.util.List)
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, "&nbsp;", "&nbsp;");
181                 anchorHtmlDataList.add(anchorHtmlData);
182             }
183 
184             return anchorHtmlDataList;
185         }
186         return super.getEmptyActionUrls();
187     }
188 
189     /**
190      * @see org.kuali.rice.kns.lookup.AbstractLookupableHelperServiceImpl#validateSearchParameters(java.util.Map)
191      */
192     @Override
193     public void validateSearchParameters(Map fieldValues) {
194         // call super method to check validation against DD
195         super.validateSearchParameters(fieldValues);
196 
197         // get field values
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         // check if there is any search criteria entered
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             // If batchId, paymentCount, and paymentTotalAmount are empty then at least creation date is required
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                 // If we have one (but not both) dates the user must enter either the chartCode, orgCode, or subUnitCode
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      * This method gets the kualiConfigurationService.
231      * 
232      * @return the configurationService
233      */
234     public ConfigurationService getConfigurationService() {
235         return configurationService;
236     }
237 
238     /**
239      * This method sets the configurationService.
240      * 
241      * @param configurationService ConfigurationService
242      */
243     public void setConfigurationService(ConfigurationService configurationService) {
244         this.configurationService = configurationService;
245     }
246 
247     /**
248      * This method gets the batchMaintenanceService.
249      * 
250      * @return the batchMaintenanceService
251      */
252     public BatchMaintenanceService getBatchMaintenanceService() {
253         return batchMaintenanceService;
254     }
255 
256     /**
257      * This method sets the batchMaintenanceService.
258      * 
259      * @param batchMaintenanceService BatchMaintenanceService
260      */
261     public void setBatchMaintenanceService(BatchMaintenanceService batchMaintenanceService) {
262         this.batchMaintenanceService = batchMaintenanceService;
263     }
264 
265     /**
266      * This method gets the lookupDao.
267      * 
268      * @return the lookupDao
269      */
270     public LookupDao getLookupDao() {
271         return lookupDao;
272     }
273 
274     /**
275      * This method sets lookupDao.
276      * 
277      * @param lookupDao LookupDao
278      */
279     public void setLookupDao(LookupDao lookupDao) {
280         this.lookupDao = lookupDao;
281     }
282 
283     /**
284      * This method sets the pdpAuthorizationService.
285      * @param pdpAuthorizationService The pdpAuthorizationService to be set.
286      */
287     public void setPdpAuthorizationService(PdpAuthorizationService pdpAuthorizationService) {
288         this.pdpAuthorizationService = pdpAuthorizationService;
289     }
290 
291 }