001/*
002 * Copyright 2008 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 */
016package org.kuali.ole.pdp.businessobject.lookup;
017
018import java.util.ArrayList;
019import java.util.List;
020import java.util.Map;
021import java.util.Properties;
022
023import org.apache.commons.lang.StringUtils;
024import org.kuali.ole.pdp.PdpConstants;
025import org.kuali.ole.pdp.PdpKeyConstants;
026import org.kuali.ole.pdp.PdpParameterConstants;
027import org.kuali.ole.pdp.PdpPropertyConstants;
028import org.kuali.ole.pdp.businessobject.Batch;
029import org.kuali.ole.pdp.businessobject.PaymentDetail;
030import org.kuali.ole.pdp.service.BatchMaintenanceService;
031import org.kuali.ole.pdp.service.PdpAuthorizationService;
032import org.kuali.ole.sys.OLEConstants;
033import org.kuali.rice.core.api.config.property.ConfigurationService;
034import org.kuali.rice.core.web.format.BooleanFormatter;
035import org.kuali.rice.kim.api.identity.Person;
036import org.kuali.rice.kns.lookup.HtmlData;
037import org.kuali.rice.kns.lookup.HtmlData.AnchorHtmlData;
038import org.kuali.rice.kns.lookup.KualiLookupableHelperServiceImpl;
039import org.kuali.rice.kns.util.KNSGlobalVariables;
040import org.kuali.rice.krad.bo.BusinessObject;
041import org.kuali.rice.krad.dao.LookupDao;
042import org.kuali.rice.krad.exception.ValidationException;
043import org.kuali.rice.krad.util.GlobalVariables;
044import org.kuali.rice.krad.util.KRADConstants;
045import org.kuali.rice.krad.util.UrlFactory;
046
047/**
048 * This class allows custom handling of Batches within the lookup framework.
049 */
050public class BatchLookupableHelperService extends KualiLookupableHelperServiceImpl {
051    private BatchMaintenanceService batchMaintenanceService;
052    private ConfigurationService configurationService;
053    private LookupDao lookupDao;
054    private PdpAuthorizationService pdpAuthorizationService;
055
056    /**
057     * @see org.kuali.rice.kns.lookup.KualiLookupableHelperServiceImpl#getSearchResults(java.util.Map)
058     */
059    @Override
060    public List<? extends BusinessObject> getSearchResults(Map<String, String> fieldValues) {
061        Map parameters = super.getParameters();
062        String errorList;
063
064        if (parameters.containsKey(PdpParameterConstants.ACTION_SUCCESSFUL_PARAM)) {
065            String[] actionSuccessRequestParm = (String[]) parameters.get(PdpParameterConstants.ACTION_SUCCESSFUL_PARAM);
066            Boolean actionSuccess = (Boolean) (new BooleanFormatter()).convertFromPresentationFormat(actionSuccessRequestParm[0]);
067
068            if (actionSuccess != null) {
069
070                if (!actionSuccess) {
071
072                    // if the action performed on batch was not successful we get the error message list and add them to
073                    // GlobalVariables errorMap
074                    if (parameters.containsKey(PdpParameterConstants.ERROR_KEY_LIST_PARAM)) {
075                        String[] errorListParam = (String[]) parameters.get(PdpParameterConstants.ERROR_KEY_LIST_PARAM);
076                        errorList = errorListParam[0];
077                        if (StringUtils.isNotEmpty(errorList)) {
078                            String[] errorMsgs = StringUtils.split(errorList, PdpParameterConstants.ERROR_KEY_LIST_SEPARATOR);
079                            for (String error : errorMsgs) {
080                                if (StringUtils.isNotEmpty(error)) {
081                                    GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, error);
082                                }
083                            }
084                        }
085                    }
086                }
087                else {
088                    if (parameters.containsKey(PdpParameterConstants.MESSAGE_PARAM)) {
089                        String[] messageRequestParm = (String[]) parameters.get(PdpParameterConstants.MESSAGE_PARAM);
090                        String message = messageRequestParm[0];
091                        KNSGlobalVariables.getMessageList().add(message);
092                    }
093                }
094            }
095        }
096
097        List results = super.getSearchResults(fieldValues);
098        return results;
099    }
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}