001/*
002 * Copyright 2009 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.Collections;
020import java.util.List;
021import java.util.Map;
022
023import org.apache.commons.lang.StringUtils;
024import org.kuali.ole.fp.businessobject.DisbursementPayee;
025import org.kuali.ole.fp.businessobject.lookup.AbstractPayeeLookupableHelperServiceImpl;
026import org.kuali.ole.pdp.PdpConstants;
027import org.kuali.ole.pdp.PdpKeyConstants;
028import org.kuali.ole.pdp.businessobject.ACHPayee;
029import org.kuali.ole.sys.OLEKeyConstants;
030import org.kuali.ole.sys.OLEPropertyConstants;
031import org.kuali.ole.vnd.businessobject.VendorDetail;
032import org.kuali.rice.kim.api.identity.Person;
033import org.kuali.rice.kim.impl.KIMPropertyConstants;
034import org.kuali.rice.krad.bo.BusinessObject;
035import org.kuali.rice.krad.exception.ValidationException;
036import org.kuali.rice.krad.lookup.CollectionIncomplete;
037import org.kuali.rice.krad.util.BeanPropertyComparator;
038import org.kuali.rice.krad.util.GlobalVariables;
039
040/**
041 * Payee lookupable for PDP Payee ACH. Builds off of DV Payee lookup by taking off payment reason code, and adding adding entity id
042 * to search and return url
043 */
044public class ACHPayeeLookupableHelperServiceImpl extends AbstractPayeeLookupableHelperServiceImpl {
045
046    /**
047     * @see org.kuali.ole.fp.businessobject.lookup.DisbursementPayeeLookupableHelperServiceImpl#getSearchResults(java.util.Map)
048     *
049     * KRAD Conversion: Lookupable performs customization of the search results and performs a sort
050     * by retrieving the default sort columns using data dictionary service..
051     *
052     * Uses data dictionary.
053     */
054    @Override
055    public List<? extends BusinessObject> getSearchResults(Map<String, String> fieldValues) {
056        List<DisbursementPayee> searchResults = new ArrayList<DisbursementPayee>();
057
058        String payeeTypeCode = fieldValues.get(OLEPropertyConstants.PAYEE_TYPE_CODE);
059        if (StringUtils.isBlank(payeeTypeCode)) {
060            GlobalVariables.getMessageMap().putInfo(OLEPropertyConstants.PAYEE_TYPE_CODE, PdpKeyConstants.MESSAGE_PDP_ACH_PAYEE_LOOKUP_NO_PAYEE_TYPE);
061        }
062
063        if (StringUtils.isNotBlank(fieldValues.get(OLEPropertyConstants.VENDOR_NUMBER)) || StringUtils.isNotBlank(fieldValues.get(OLEPropertyConstants.VENDOR_NAME)) || (StringUtils.isNotBlank(payeeTypeCode) && PdpConstants.PayeeIdTypeCodes.VENDOR_ID.equals(payeeTypeCode))) {
064            searchResults.addAll(this.getVendorsAsPayees(fieldValues));
065        }
066        else if (StringUtils.isNotBlank(fieldValues.get(KIMPropertyConstants.Person.EMPLOYEE_ID)) || StringUtils.isNotBlank(fieldValues.get(KIMPropertyConstants.Person.ENTITY_ID)) || (StringUtils.isNotBlank(payeeTypeCode) && (PdpConstants.PayeeIdTypeCodes.EMPLOYEE.equals(payeeTypeCode) || PdpConstants.PayeeIdTypeCodes.ENTITY.equals(payeeTypeCode)))) {
067            searchResults.addAll(this.getPersonAsPayees(fieldValues));
068        }
069        else {
070            searchResults.addAll(this.getVendorsAsPayees(fieldValues));
071            searchResults.addAll(this.getPersonAsPayees(fieldValues));
072        }
073
074        CollectionIncomplete results = new CollectionIncomplete(searchResults, Long.valueOf(searchResults.size()));
075
076        // sort list if default sort column given
077        List<String> defaultSortColumns = getDefaultSortColumns();
078        if (defaultSortColumns.size() > 0) {
079            Collections.sort(results, new BeanPropertyComparator(getDefaultSortColumns(), true));
080        }
081
082        return results;
083    }
084
085
086    /**
087     * Override to set entity id as the payee id and set the pdp payee type
088     *
089     * @see org.kuali.ole.fp.businessobject.lookup.DisbursementPayeeLookupableHelperServiceImpl#getPayeeFromPerson(org.kuali.rice.kim.api.identity.Person,
090     *      java.util.Map)
091     */
092    @Override
093    protected DisbursementPayee getPayeeFromPerson(Person personDetail, Map<String, String> fieldValues) {
094        DisbursementPayee payee = super.getPayeeFromPerson(personDetail, fieldValues);
095
096        String payeeTypeCode = fieldValues.get(OLEPropertyConstants.PAYEE_TYPE_CODE);
097
098        ACHPayee achPayee = new ACHPayee();
099
100        if (PdpConstants.PayeeIdTypeCodes.ENTITY.equals(payeeTypeCode)) {
101            achPayee.setPayeeIdNumber(personDetail.getEntityId());
102            achPayee.setPayeeTypeCode(PdpConstants.PayeeIdTypeCodes.ENTITY);
103        }
104        else {
105            achPayee.setPayeeIdNumber(personDetail.getEmployeeId());
106            achPayee.setPayeeTypeCode(PdpConstants.PayeeIdTypeCodes.EMPLOYEE);
107        }
108
109        achPayee.setPayeeName(payee.getPayeeName());
110        achPayee.setPrincipalId(payee.getPrincipalId());
111        achPayee.setTaxNumber(payee.getTaxNumber());
112        achPayee.setAddress(payee.getAddress());
113        achPayee.setActive(payee.isActive());
114
115        return achPayee;
116    }
117
118
119    /**
120     * @see org.kuali.ole.fp.businessobject.lookup.DisbursementPayeeLookupableHelperServiceImpl#getPayeeFromVendor(org.kuali.ole.vnd.businessobject.VendorDetail,
121     *      java.util.Map)
122     */
123    @Override
124    protected DisbursementPayee getPayeeFromVendor(VendorDetail vendorDetail, Map<String, String> fieldValues) {
125        DisbursementPayee payee = super.getPayeeFromVendor(vendorDetail, fieldValues);
126
127        ACHPayee achPayee = new ACHPayee();
128
129        achPayee.setPayeeIdNumber(payee.getPayeeIdNumber());
130        achPayee.setPayeeTypeCode(PdpConstants.PayeeIdTypeCodes.VENDOR_ID);
131        achPayee.setPayeeName(payee.getPayeeName());
132        achPayee.setPrincipalId(payee.getPrincipalId());
133        achPayee.setTaxNumber(payee.getTaxNumber());
134        achPayee.setAddress(payee.getAddress());
135        achPayee.setActive(payee.isActive());
136
137        return achPayee;
138    }
139
140
141    /**
142     * @see org.kuali.ole.fp.businessobject.lookup.DisbursementPayeeLookupableHelperServiceImpl#validateSearchParameters(java.util.Map)
143     */
144    @Override
145    public void validateSearchParameters(Map fieldValues) {
146        super.validateSearchParameters(fieldValues);
147
148        String vendorName = (String) fieldValues.get(OLEPropertyConstants.VENDOR_NAME);
149        String vendorNumber = (String) fieldValues.get(OLEPropertyConstants.VENDOR_NUMBER);
150        String employeeId = (String) fieldValues.get(KIMPropertyConstants.Person.EMPLOYEE_ID);
151        String entityId = (String) fieldValues.get(KIMPropertyConstants.Person.ENTITY_ID);
152        String payeeTypeCode = (String) fieldValues.get(OLEPropertyConstants.PAYEE_TYPE_CODE);
153
154        // only can use the vendor name and vendor number fields or the employee id field, but not both.
155        boolean isVendorInfoEntered = StringUtils.isNotBlank(vendorName) || StringUtils.isNotBlank(vendorNumber);
156        if (StringUtils.isNotBlank(entityId) && isVendorInfoEntered) {
157            String messageKey = OLEKeyConstants.ERROR_DV_VENDOR_EMPLOYEE_CONFUSION;
158
159            String vendorNameLabel = this.getAttributeLabel(OLEPropertyConstants.VENDOR_NAME);
160            String vendorNumberLabel = this.getAttributeLabel(OLEPropertyConstants.VENDOR_NUMBER);
161            String entityIdLabel = this.getAttributeLabel(KIMPropertyConstants.Person.ENTITY_ID);
162
163            GlobalVariables.getMessageMap().putError(KIMPropertyConstants.Person.ENTITY_ID, messageKey, entityIdLabel, vendorNameLabel, vendorNumberLabel);
164        }
165
166        boolean isEmployeeInfoEntered = StringUtils.isNotBlank(employeeId) || StringUtils.isNotBlank(entityId);
167        boolean payeeTypeEntered = StringUtils.isNotBlank(payeeTypeCode);
168
169        if (payeeTypeEntered && PdpConstants.PayeeIdTypeCodes.VENDOR_ID.equals(payeeTypeCode) && isEmployeeInfoEntered) {
170            String messageKey = PdpKeyConstants.ERROR_PAYEE_LOOKUP_VENDOR_EMPLOYEE_CONFUSION;
171
172            String employeeIdLabel = this.getAttributeLabel(KIMPropertyConstants.Person.EMPLOYEE_ID);
173            String entityIdLabel = this.getAttributeLabel(KIMPropertyConstants.Person.ENTITY_ID);
174            String payeeTypeLabel = this.getAttributeLabel(OLEPropertyConstants.PAYEE_TYPE_CODE);
175
176            GlobalVariables.getMessageMap().putError(OLEPropertyConstants.PAYEE_TYPE_CODE, messageKey, payeeTypeLabel, payeeTypeCode, employeeIdLabel, entityIdLabel);
177        }
178        else if (payeeTypeEntered && (PdpConstants.PayeeIdTypeCodes.EMPLOYEE.equals(payeeTypeCode) || PdpConstants.PayeeIdTypeCodes.ENTITY.equals(payeeTypeCode)) && isVendorInfoEntered) {
179            String messageKey = PdpKeyConstants.ERROR_PAYEE_LOOKUP_VENDOR_EMPLOYEE_CONFUSION;
180
181            String vendorNameLabel = this.getAttributeLabel(OLEPropertyConstants.VENDOR_NAME);
182            String vendorNumberLabel = this.getAttributeLabel(OLEPropertyConstants.VENDOR_NUMBER);
183            String payeeTypeLabel = this.getAttributeLabel(OLEPropertyConstants.PAYEE_TYPE_CODE);
184
185            GlobalVariables.getMessageMap().putError(OLEPropertyConstants.PAYEE_TYPE_CODE, messageKey, payeeTypeLabel, payeeTypeCode, vendorNameLabel, vendorNumberLabel);
186        }
187
188        if (GlobalVariables.getMessageMap().hasErrors()) {
189            throw new ValidationException("errors in search criteria");
190        }
191    }
192
193}