001    /*
002     * Copyright 2011 The Kuali Foundation.
003     * 
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     * 
008     * http://www.opensource.org/licenses/ecl2.php
009     * 
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.kuali.kfs.pdp.businessobject.lookup;
017    
018    import java.util.ArrayList;
019    import java.util.List;
020    import java.util.Map;
021    import java.util.Properties;
022    
023    import org.apache.commons.lang.StringUtils;
024    import org.kuali.kfs.pdp.PdpConstants;
025    import org.kuali.kfs.pdp.PdpKeyConstants;
026    import org.kuali.kfs.pdp.PdpParameterConstants;
027    import org.kuali.kfs.pdp.PdpPropertyConstants;
028    import org.kuali.kfs.pdp.businessobject.Batch;
029    import org.kuali.kfs.pdp.businessobject.PaymentDetail;
030    import org.kuali.kfs.pdp.service.BatchMaintenanceService;
031    import org.kuali.kfs.pdp.service.PdpAuthorizationService;
032    import org.kuali.kfs.sys.KFSConstants;
033    import org.kuali.rice.kim.bo.Person;
034    import org.kuali.rice.kns.bo.BusinessObject;
035    import org.kuali.rice.kns.dao.LookupDao;
036    import org.kuali.rice.kns.exception.ValidationException;
037    import org.kuali.rice.kns.lookup.HtmlData;
038    import org.kuali.rice.kns.lookup.KualiLookupableHelperServiceImpl;
039    import org.kuali.rice.kns.lookup.HtmlData.AnchorHtmlData;
040    import org.kuali.rice.kns.service.KualiConfigurationService;
041    import org.kuali.rice.kns.util.GlobalVariables;
042    import org.kuali.rice.kns.util.KNSConstants;
043    import org.kuali.rice.kns.util.UrlFactory;
044    import org.kuali.rice.kns.web.format.BooleanFormatter;
045    
046    /**
047     * This class allows custom handling of Batches within the lookup framework.
048     */
049    public class BatchLookupableHelperService extends KualiLookupableHelperServiceImpl {
050        private BatchMaintenanceService batchMaintenanceService;
051        private KualiConfigurationService configurationService;
052        private LookupDao lookupDao;
053        private PdpAuthorizationService pdpAuthorizationService;
054    
055        /**
056         * @see org.kuali.rice.kns.lookup.KualiLookupableHelperServiceImpl#getSearchResults(java.util.Map)
057         */
058        @Override
059        public List<? extends BusinessObject> getSearchResults(Map<String, String> fieldValues) {
060            Map parameters = super.getParameters();
061            String errorList;
062    
063            if (parameters.containsKey(PdpParameterConstants.ACTION_SUCCESSFUL_PARAM)) {
064                String[] actionSuccessRequestParm = (String[]) parameters.get(PdpParameterConstants.ACTION_SUCCESSFUL_PARAM);
065                Boolean actionSuccess = (Boolean) (new BooleanFormatter()).convertFromPresentationFormat(actionSuccessRequestParm[0]);
066    
067                if (actionSuccess != null) {
068    
069                    if (!actionSuccess) {
070    
071                        // if the action performed on batch was not successful we get the error message list and add them to
072                        // GlobalVariables errorMap
073                        if (parameters.containsKey(PdpParameterConstants.ERROR_KEY_LIST_PARAM)) {
074                            String[] errorListParam = (String[]) parameters.get(PdpParameterConstants.ERROR_KEY_LIST_PARAM);
075                            errorList = errorListParam[0];
076                            if (StringUtils.isNotEmpty(errorList)) {
077                                String[] errorMsgs = StringUtils.split(errorList, PdpParameterConstants.ERROR_KEY_LIST_SEPARATOR);
078                                for (String error : errorMsgs) {
079                                    if (StringUtils.isNotEmpty(error)) {
080                                        GlobalVariables.getMessageMap().putError(KNSConstants.GLOBAL_ERRORS, error);
081                                    }
082                                }
083                            }
084                        }
085                    }
086                    else {
087                        if (parameters.containsKey(PdpParameterConstants.MESSAGE_PARAM)) {
088                            String[] messageRequestParm = (String[]) parameters.get(PdpParameterConstants.MESSAGE_PARAM);
089                            String message = messageRequestParm[0];
090                            GlobalVariables.getMessageList().add(message);
091                        }
092                    }
093                }
094            }
095    
096            List results = super.getSearchResults(fieldValues);
097            return results;
098        }
099    
100        /**
101         * @see org.kuali.rice.kns.lookup.AbstractLookupableHelperServiceImpl#getInquiryUrl(org.kuali.rice.kns.bo.BusinessObject,
102         *      java.lang.String)
103         */
104        @Override
105        public HtmlData getInquiryUrl(BusinessObject bo, String propertyName) {
106            AnchorHtmlData inquiryUrl = (AnchorHtmlData) super.getInquiryUrl(bo, propertyName);
107            Batch batch = (Batch) bo;
108            if (propertyName.equalsIgnoreCase(PdpPropertyConstants.BatchConstants.BATCH_ID)) {
109                Properties params = new Properties();
110                params.put(KFSConstants.DISPATCH_REQUEST_PARAMETER, KFSConstants.SEARCH_METHOD);
111                params.put(KFSConstants.BUSINESS_OBJECT_CLASS_ATTRIBUTE, PaymentDetail.class.getName());
112                params.put(KNSConstants.DOC_FORM_KEY, "88888888");
113                params.put(KFSConstants.HIDE_LOOKUP_RETURN_LINK, "true");
114                params.put(KFSConstants.BACK_LOCATION, configurationService.getPropertyString(KNSConstants.APPLICATION_URL_KEY) + "/" + KFSConstants.MAPPING_PORTAL + ".do");
115                params.put(PdpPropertyConstants.PaymentDetail.PAYMENT_GROUP_BATCH_ID, UrlFactory.encode(String.valueOf(batch.getId())));
116                String url = UrlFactory.parameterizeUrl(KNSConstants.LOOKUP_ACTION, params);
117                inquiryUrl.setHref(url);
118            }
119            return inquiryUrl;
120        }
121    
122        /**
123         * @see org.kuali.rice.kns.lookup.AbstractLookupableHelperServiceImpl#getCustomActionUrls(org.kuali.rice.kns.bo.BusinessObject,
124         *      java.util.List)
125         */
126        @Override
127        public List<HtmlData> getCustomActionUrls(BusinessObject businessObject, List pkNames) {
128    
129            if (businessObject instanceof Batch) {
130                Person person = GlobalVariables.getUserSession().getPerson();
131                Batch batch = (Batch) businessObject;
132                Integer batchId = batch.getId().intValue();
133                List<HtmlData> anchorHtmlDataList = new ArrayList<HtmlData>();
134                String linkText = KFSConstants.EMPTY_STRING;
135                String url = KFSConstants.EMPTY_STRING;
136                String basePath = configurationService.getPropertyString(KFSConstants.APPLICATION_URL_KEY) + "/" + PdpConstants.Actions.BATCH_SEARCH_DETAIL_ACTION;
137    
138                if ( pdpAuthorizationService.hasCancelPaymentPermission(person.getPrincipalId()) && batchMaintenanceService.doBatchPaymentsHaveOpenOrHeldStatus(batchId)) {
139    
140                    Properties params = new Properties();
141                    params.put(KFSConstants.DISPATCH_REQUEST_PARAMETER, PdpConstants.ActionMethods.CONFIRM_CANCEL_ACTION);
142                    params.put(PdpParameterConstants.BatchConstants.BATCH_ID_PARAM, UrlFactory.encode(String.valueOf(batchId)));
143                    url = UrlFactory.parameterizeUrl(basePath, params);
144    
145                    linkText = configurationService.getPropertyString(PdpKeyConstants.BatchConstants.LinkText.CANCEL_BATCH);
146    
147                    AnchorHtmlData anchorHtmlData = new AnchorHtmlData(url, PdpConstants.ActionMethods.CONFIRM_CANCEL_ACTION, linkText);
148                    anchorHtmlDataList.add(anchorHtmlData);
149                }
150                
151                if ( pdpAuthorizationService.hasHoldPaymentPermission(person.getPrincipalId())) {
152    
153                    if (batchMaintenanceService.doBatchPaymentsHaveHeldStatus(batchId)) {
154    
155                        Properties params = new Properties();
156                        params.put(KFSConstants.DISPATCH_REQUEST_PARAMETER, PdpConstants.ActionMethods.CONFIRM_REMOVE_HOLD_ACTION);
157                        params.put(PdpParameterConstants.BatchConstants.BATCH_ID_PARAM, UrlFactory.encode(String.valueOf(batchId)));
158                        url = UrlFactory.parameterizeUrl(basePath, params);
159    
160                        linkText = configurationService.getPropertyString(PdpKeyConstants.BatchConstants.LinkText.REMOVE_BATCH_HOLD);
161    
162                        AnchorHtmlData anchorHtmlData = new AnchorHtmlData(url, PdpConstants.ActionMethods.CONFIRM_REMOVE_HOLD_ACTION, linkText);
163                        anchorHtmlDataList.add(anchorHtmlData);
164                    }
165                    else if (batchMaintenanceService.doBatchPaymentsHaveOpenStatus(batchId)) {
166    
167                        Properties params = new Properties();
168                        params.put(KFSConstants.DISPATCH_REQUEST_PARAMETER, PdpConstants.ActionMethods.CONFIRM_HOLD_ACTION);
169                        params.put(PdpParameterConstants.BatchConstants.BATCH_ID_PARAM, UrlFactory.encode(String.valueOf(batchId)));
170                        url = UrlFactory.parameterizeUrl(basePath, params);
171    
172                        linkText = configurationService.getPropertyString(PdpKeyConstants.BatchConstants.LinkText.HOLD_BATCH);
173                        AnchorHtmlData anchorHtmlData = new AnchorHtmlData(url, PdpConstants.ActionMethods.CONFIRM_HOLD_ACTION, linkText);
174                        anchorHtmlDataList.add(anchorHtmlData);
175                    }
176                }
177    
178                if (anchorHtmlDataList.isEmpty()) {
179                    AnchorHtmlData anchorHtmlData = new AnchorHtmlData(url, "&nbsp;", "&nbsp;");
180                    anchorHtmlDataList.add(anchorHtmlData);
181                }
182    
183                return anchorHtmlDataList;
184            }
185            return super.getEmptyActionUrls();
186        }
187    
188        /**
189         * @see org.kuali.rice.kns.lookup.AbstractLookupableHelperServiceImpl#validateSearchParameters(java.util.Map)
190         */
191        @Override
192        public void validateSearchParameters(Map fieldValues) {
193            // call super method to check validation against DD
194            super.validateSearchParameters(fieldValues);
195    
196            // get field values
197            String batchIdValue = (String) fieldValues.get(PdpPropertyConstants.BatchConstants.BATCH_ID);
198            String paymentCountValue = (String) fieldValues.get(PdpPropertyConstants.BatchConstants.PAYMENT_COUNT);
199            String paymentTotalAmountValue = (String) fieldValues.get(PdpPropertyConstants.BatchConstants.PAYMENT_TOTAL_AMOUNT);
200            String fileCreationTimeValueLower = (String) fieldValues.get(KNSConstants.LOOKUP_RANGE_LOWER_BOUND_PROPERTY_PREFIX + PdpPropertyConstants.BatchConstants.FILE_CREATION_TIME);
201            String fileCreationTimeValueUpper = (String) fieldValues.get(KNSConstants.LOOKUP_DEFAULT_RANGE_SEARCH_UPPER_BOUND_LABEL + PdpPropertyConstants.BatchConstants.FILE_CREATION_TIME);
202            String chartCodeValue = (String) fieldValues.get(PdpPropertyConstants.BatchConstants.CHART_CODE);
203            String orgCodeValue = (String) fieldValues.get(PdpPropertyConstants.BatchConstants.ORG_CODE);
204            String subUnitCodeValue = (String) fieldValues.get(PdpPropertyConstants.BatchConstants.SUB_UNIT_CODE);
205    
206            // check if there is any search criteria entered
207            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)) {
208                GlobalVariables.getMessageMap().putError(KFSConstants.DOCUMENT_HEADER_ERRORS, PdpKeyConstants.BatchConstants.ErrorMessages.ERROR_BATCH_CRITERIA_NONE_ENTERED);
209            }
210            else if (StringUtils.isBlank(batchIdValue) && StringUtils.isBlank(paymentCountValue) && StringUtils.isBlank(paymentTotalAmountValue)) {
211                // If batchId, paymentCount, and paymentTotalAmount are empty then at least creation date is required
212                if (StringUtils.isBlank(fileCreationTimeValueLower) && StringUtils.isBlank(fileCreationTimeValueUpper) ) {
213                    GlobalVariables.getMessageMap().putError(PdpPropertyConstants.BatchConstants.FILE_CREATION_TIME, PdpKeyConstants.BatchConstants.ErrorMessages.ERROR_BATCH_CRITERIA_NO_DATE);
214                }
215                else if (StringUtils.isBlank(fileCreationTimeValueLower) || StringUtils.isBlank(fileCreationTimeValueUpper)) {
216                    // If we have one (but not both) dates the user must enter either the chartCode, orgCode, or subUnitCode
217                    if (StringUtils.isBlank(chartCodeValue) && StringUtils.isBlank(orgCodeValue) && StringUtils.isBlank(subUnitCodeValue)) {
218                        GlobalVariables.getMessageMap().putError(KNSConstants.GLOBAL_ERRORS, PdpKeyConstants.BatchConstants.ErrorMessages.ERROR_BATCH_CRITERIA_SOURCE_MISSING);
219                    }
220                }
221            }
222    
223            if (!GlobalVariables.getMessageMap().isEmpty()) {
224                throw new ValidationException("errors in search criteria");
225            }
226        }
227    
228        /**
229         * This method gets the kualiConfigurationService.
230         * 
231         * @return the configurationService
232         */
233        public KualiConfigurationService getConfigurationService() {
234            return configurationService;
235        }
236    
237        /**
238         * This method sets the configurationService.
239         * 
240         * @param configurationService KualiConfigurationService
241         */
242        public void setConfigurationService(KualiConfigurationService configurationService) {
243            this.configurationService = configurationService;
244        }
245    
246        /**
247         * This method gets the batchMaintenanceService.
248         * 
249         * @return the batchMaintenanceService
250         */
251        public BatchMaintenanceService getBatchMaintenanceService() {
252            return batchMaintenanceService;
253        }
254    
255        /**
256         * This method sets the batchMaintenanceService.
257         * 
258         * @param batchMaintenanceService BatchMaintenanceService
259         */
260        public void setBatchMaintenanceService(BatchMaintenanceService batchMaintenanceService) {
261            this.batchMaintenanceService = batchMaintenanceService;
262        }
263    
264        /**
265         * This method gets the lookupDao.
266         * 
267         * @return the lookupDao
268         */
269        public LookupDao getLookupDao() {
270            return lookupDao;
271        }
272    
273        /**
274         * This method sets lookupDao.
275         * 
276         * @param lookupDao LookupDao
277         */
278        public void setLookupDao(LookupDao lookupDao) {
279            this.lookupDao = lookupDao;
280        }
281    
282        /**
283         * This method sets the pdpAuthorizationService.
284         * @param pdpAuthorizationService The pdpAuthorizationService to be set.
285         */
286        public void setPdpAuthorizationService(PdpAuthorizationService pdpAuthorizationService) {
287            this.pdpAuthorizationService = pdpAuthorizationService;
288        }
289    
290    }