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.fp.document.service.impl;
17  
18  import java.util.ArrayList;
19  import java.util.Collection;
20  import java.util.HashMap;
21  import java.util.List;
22  import java.util.Map;
23  
24  import org.apache.commons.lang.StringUtils;
25  import org.kuali.ole.fp.businessobject.DisbursementPayee;
26  import org.kuali.ole.fp.businessobject.PaymentReasonCode;
27  import org.kuali.ole.fp.document.DisbursementVoucherConstants;
28  import org.kuali.ole.fp.document.DisbursementVoucherDocument;
29  import org.kuali.ole.fp.document.service.DisbursementVoucherPayeeService;
30  import org.kuali.ole.fp.document.service.DisbursementVoucherPaymentReasonService;
31  import org.kuali.ole.sys.OLEConstants;
32  import org.kuali.ole.sys.OLEKeyConstants;
33  import org.kuali.ole.sys.OLEPropertyConstants;
34  import org.kuali.ole.sys.context.SpringContext;
35  import org.kuali.rice.core.api.parameter.ParameterEvaluatorService;
36  import org.kuali.rice.coreservice.framework.parameter.ParameterService;
37  import org.kuali.rice.kns.util.MessageList;
38  import org.kuali.rice.krad.service.BusinessObjectService;
39  
40  /**
41   * implementing the service methods defined in DisbursementVoucherPaymentReasonService
42   * 
43   * @see DisbursementVoucherPaymentReasonService
44   */
45  public class DisbursementVoucherPaymentReasonServiceImpl implements DisbursementVoucherPaymentReasonService {
46      private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(DisbursementVoucherPaymentReasonServiceImpl.class);
47  
48      protected ParameterService parameterService;
49      protected BusinessObjectService businessObjectService;
50      protected DisbursementVoucherPayeeService disbursementVoucherPayeeService;
51  
52      /**
53       * @see org.kuali.ole.fp.document.service.DisbursementVoucherPaymentReasonService#isPayeeQualifiedForPayment(org.kuali.ole.fp.businessobject.DisbursementPayee,
54       *      java.lang.String)
55       */
56      public boolean isPayeeQualifiedForPayment(DisbursementPayee payee, String paymentReasonCode) {
57          Collection<String> payeeTypeCodes = this.getPayeeTypesByPaymentReason(paymentReasonCode);
58          return this.isPayeeQualifiedForPayment(payee, paymentReasonCode, payeeTypeCodes);
59      }
60  
61      /**
62       * @see org.kuali.ole.fp.document.service.DisbursementVoucherPaymentReasonService#isPayeeQualifiedForPayment(org.kuali.ole.fp.businessobject.DisbursementPayee,
63       *      java.lang.String, java.util.List)
64       */
65      public boolean isPayeeQualifiedForPayment(DisbursementPayee payee, String paymentReasonCode, Collection<String> payeeTypeCodes) {
66          if (payeeTypeCodes == null || payeeTypeCodes.isEmpty()) {
67              return false;
68          }
69  
70          String payeeTypeCode = payee.getPayeeTypeCode();
71          if (!payeeTypeCodes.contains(payeeTypeCode)) {
72              return false;
73          }
74          
75          if (disbursementVoucherPayeeService.isVendor(payee)) {
76              Collection<String> vendorOwnershipTypeCodes = this.getVendorOwnershipTypesByPaymentReason(paymentReasonCode);
77              
78              if (vendorOwnershipTypeCodes != null && !vendorOwnershipTypeCodes.isEmpty()) {                
79                  String vendorOwnershipTypeCodeOfPayee = disbursementVoucherPayeeService.getVendorOwnershipTypeCode(payee);
80                  return vendorOwnershipTypeCodes.contains(vendorOwnershipTypeCodeOfPayee);
81              } 
82          }
83        //Commented for the jira issue OLE-3415
84          /*if (this.isPrepaidTravelPaymentReason(paymentReasonCode)) {
85              boolean isActiveVendorEmployee = payee.isActive();
86              isActiveVendorEmployee &= disbursementVoucherPayeeService.isVendor(payee);
87              isActiveVendorEmployee &= disbursementVoucherPayeeService.isEmployee(payee);
88  
89              // Active vendor employees cannot be paid for prepaid travel
90              return !isActiveVendorEmployee;
91          }*/
92  
93          return true;
94      }
95  
96      /**
97       * @see org.kuali.ole.fp.document.service.DisbursementVoucherPaymentReasonService#isMovingPaymentReason(java.lang.String)
98       */
99      public boolean isMovingPaymentReason(String paymentReasonCode) {
100         String typeParameterName = DisbursementVoucherConstants.MOVING_PAYMENT_REASONS_PARM_NM;
101         return this.isPaymentReasonOfType(typeParameterName, paymentReasonCode);
102     }
103 
104     /**
105      * @see org.kuali.ole.fp.document.service.DisbursementVoucherPaymentReasonService#isPrepaidTravelPaymentReason(java.lang.String)
106      */
107     public boolean isPrepaidTravelPaymentReason(String paymentReasonCode) {
108         String typeParameterName = DisbursementVoucherConstants.PREPAID_TRAVEL_PAYMENT_REASONS_PARM_NM;
109         return this.isPaymentReasonOfType(typeParameterName, paymentReasonCode);
110     }
111     
112     /**
113      * @see org.kuali.ole.fp.document.service.DisbursementVoucherPaymentReasonService#isNonEmployeeTravelPaymentReason(java.lang.String)
114      */
115     public boolean isNonEmployeeTravelPaymentReason(String paymentReasonCode) {
116         String typeParameterName = DisbursementVoucherConstants.NONEMPLOYEE_TRAVEL_PAY_REASONS_PARM_NM;
117         return this.isPaymentReasonOfType(typeParameterName, paymentReasonCode);
118     }
119 
120     /**
121      * @see org.kuali.ole.fp.document.service.DisbursementVoucherPaymentReasonService#isResearchPaymentReason(java.lang.String)
122      */
123     public boolean isResearchPaymentReason(String paymentReasonCode) {
124         String typeParameterName = DisbursementVoucherConstants.RESEARCH_PAYMENT_REASONS_PARM_NM;
125         return this.isPaymentReasonOfType(typeParameterName, paymentReasonCode);
126     }
127 
128     /**
129      * @see org.kuali.ole.fp.document.service.DisbursementVoucherPaymentReasonService#isRevolvingFundPaymentReason(java.lang.String)
130      */
131     public boolean isRevolvingFundPaymentReason(String paymentReasonCode) {
132         String typeParameterName = DisbursementVoucherConstants.REVOLVING_FUND_PAYMENT_REASONS_PARM_NM;
133         return this.isPaymentReasonOfType(typeParameterName, paymentReasonCode);
134     }
135     
136     /**
137      * @see org.kuali.ole.fp.document.service.DisbursementVoucherPaymentReasonService#isDecedentCompensationPaymentReason(java.lang.String)
138      */
139     public boolean isDecedentCompensationPaymentReason(String paymentReasonCode) {
140         String typeParameterName = DisbursementVoucherConstants.DECEDENT_COMPENSATION_PAYMENT_REASONS_PARM_NM;
141         return this.isPaymentReasonOfType(typeParameterName, paymentReasonCode);
142     }
143 
144     /**
145      * @see org.kuali.ole.fp.document.service.DisbursementVoucherPaymentReasonService#isPaymentReasonOfType(java.lang.String,
146      *      java.lang.String)
147      */
148     public boolean isPaymentReasonOfType(String typeParameterName, String paymentReasonCode) {
149         return /*REFACTORME*/SpringContext.getBean(ParameterEvaluatorService.class).getParameterEvaluator(DisbursementVoucherDocument.class, typeParameterName, paymentReasonCode).evaluationSucceeds();
150     }
151 
152     /**
153      * @see org.kuali.ole.fp.document.service.DisbursementVoucherPaymentReasonService#getReserchNonVendorPayLimit()
154      */
155     public String getReserchNonVendorPayLimit() {
156         return parameterService.getParameterValueAsString(DisbursementVoucherDocument.class, DisbursementVoucherConstants.RESEARCH_NON_VENDOR_PAY_LIMIT_AMOUNT_PARM_NM);
157     }
158 
159     /**
160      * @see org.kuali.ole.fp.document.service.DisbursementVoucherPaymentReasonService#getPayeeTypesByPaymentReason(java.lang.String)
161      */
162     public Collection<String> getPayeeTypesByPaymentReason(String paymentReasonCode) {
163         return parameterService.getSubParameterValuesAsString(DisbursementVoucherDocument.class, DisbursementVoucherConstants.VALID_PAYEE_TYPES_BY_PAYMENT_REASON_PARM, paymentReasonCode);
164     }
165     
166     /**
167      * @see org.kuali.ole.fp.document.service.DisbursementVoucherPaymentReasonService#getVendorOwnershipTypesByPaymentReason(java.lang.String)
168      */
169     public Collection<String> getVendorOwnershipTypesByPaymentReason(String paymentReasonCode) {
170         return parameterService.getSubParameterValuesAsString(DisbursementVoucherDocument.class, DisbursementVoucherConstants.VALID_VENDOR_OWNERSHIP_TYPES_BY_PAYMENT_REASON, paymentReasonCode);
171     }
172 
173     /**
174      * @see org.kuali.ole.fp.document.service.DisbursementVoucherPaymentReasonService#getPaymentReasonByPrimaryId(java.lang.String)
175      */
176     public PaymentReasonCode getPaymentReasonByPrimaryId(String paymentReasonCode) {
177         Map<String, Object> primaryKeys = new HashMap<String, Object>();
178         primaryKeys.put(OLEPropertyConstants.CODE, paymentReasonCode);
179 
180         return (PaymentReasonCode) businessObjectService.findByPrimaryKey(PaymentReasonCode.class, primaryKeys);
181     }
182     
183     /**
184      * @see org.kuali.ole.fp.document.service.DisbursementVoucherPaymentReasonService#isTaxReviewRequired(java.lang.String)
185      */
186     public boolean isTaxReviewRequired(String paymentReasonCode) {
187         String parameterName = DisbursementVoucherConstants.PAYMENT_REASONS_REQUIRING_TAX_REVIEW_PARM_NM;
188         List<String> values = new ArrayList<String>( parameterService.getParameterValuesAsString(DisbursementVoucherDocument.class, parameterName) );
189         
190         return values != null && values.contains(paymentReasonCode);
191     }
192 
193     /**
194      * @see org.kuali.ole.fp.document.service.DisbursementVoucherPaymentReasonService#postPaymentReasonCodeUsage(java.lang.String,
195      *      org.kuali.rice.kns.util.MessageList)
196      */
197     public void postPaymentReasonCodeUsage(String paymentReasonCode, MessageList messageList) {
198         Collection<String> payeeTypeCodes = this.getPayeeTypesByPaymentReason(paymentReasonCode);
199         if (payeeTypeCodes == null || payeeTypeCodes.isEmpty()) {
200             return;
201         }
202 
203         String descriptivePayeeTypes = this.getDescriptivePayeeTypesAsString(payeeTypeCodes);
204         String descriptivePaymentReason = this.getPaymentReasonByPrimaryId(paymentReasonCode).getCodeAndDescription();
205         if (payeeTypeCodes.size() > 1) {
206             String messageKey = OLEKeyConstants.WARNING_DV_PAYMENT_REASON_VALID_FOR_MULTIPLE_PAYEE_TYPES;
207             messageList.add(messageKey, descriptivePaymentReason, descriptivePayeeTypes);
208         }
209         else if (payeeTypeCodes.size() == 1) {
210             String messageKey = OLEKeyConstants.WARNING_DV_PAYMENT_REASON_VALID_FOR_SINGEL_PAYEE_TYPE;
211             messageList.add(messageKey, descriptivePaymentReason, descriptivePayeeTypes);
212         }
213       //Commented for the jira issue OLE-3415
214         /*if (this.isResearchPaymentReason(paymentReasonCode)) {
215             String payLimit = this.getReserchNonVendorPayLimit();
216             String messageKey = OLEKeyConstants.WARNING_DV_REASERCH_PAYMENT_REASON;
217 
218             List<String> vendorTypeCodes = new ArrayList<String>();
219             vendorTypeCodes.addAll(payeeTypeCodes);
220             vendorTypeCodes.remove(DisbursementVoucherConstants.DV_PAYEE_TYPE_EMPLOYEE);
221             String vendorTypes = this.getDescriptivePayeeTypesAsString(vendorTypeCodes);
222 
223             messageList.add(messageKey, descriptivePaymentReason, descriptivePayeeTypes, vendorTypes, payLimit);
224         }
225 
226         if (this.isMovingPaymentReason(paymentReasonCode)) {
227             List<String> individualOwnerShipTypeCodes = new ArrayList<String>( parameterService.getParameterValuesAsString(DisbursementVoucherDocument.class, DisbursementVoucherConstants.INDIVIDUAL_OWNERSHIP_TYPES_PARM_NM) );
228             String ownerShipTypeAsString = this.convertListToString(individualOwnerShipTypeCodes);
229 
230             String messageKey = OLEKeyConstants.WARNING_DV_MOVING_PAYMENT_REASON;
231             messageList.add(messageKey, ownerShipTypeAsString);
232         }
233 
234         if (this.isPrepaidTravelPaymentReason(paymentReasonCode)) {
235             String messageKey = OLEKeyConstants.WARNING_DV_PREPAID_TRAVEL_PAYMENT_REASON;
236             messageList.add(messageKey, descriptivePaymentReason, descriptivePayeeTypes);
237         }*/
238     }
239 
240     // get and concatenate the descriptive payee types of the given codes
241     protected String getDescriptivePayeeTypesAsString(Collection<String> payeeTypeCodes) {
242         List<String> payeeTypeDescriptions = new ArrayList<String>();
243 
244         for (String payeeTypeCode : payeeTypeCodes) {
245             String description = SpringContext.getBean(DisbursementVoucherPayeeService.class).getPayeeTypeDescription(payeeTypeCode);
246             payeeTypeDescriptions.add(description);
247         }
248 
249         return this.convertListToString(payeeTypeDescriptions);
250     }
251 
252     protected String convertListToString(List<String> list) {
253         if (list == null || list.isEmpty()) {
254             return StringUtils.EMPTY;
255         }
256 
257         String oneSpace = " ";
258         StringBuilder listAsString = new StringBuilder();
259         for (int index = 0; index < list.size(); index++) {
260             String emlement = list.get(index);
261 
262             if (index == 0) {
263                 listAsString.append(emlement);
264             }
265             else if (index < list.size() - 1) {
266                 listAsString.append(OLEConstants.COMMA).append(oneSpace).append(emlement);
267             }
268             else if (index == list.size() - 1) {
269                 listAsString.append(oneSpace).append(OLEConstants.AND).append(oneSpace).append(emlement);
270             }
271         }
272 
273         return listAsString.toString();
274     }
275 
276     /**
277      * Sets the parameterService attribute value.
278      * 
279      * @param parameterService The parameterService to set.
280      */
281     public void setParameterService(ParameterService parameterService) {
282         this.parameterService = parameterService;
283     }
284 
285     /**
286      * Sets the businessObjectService attribute value.
287      * 
288      * @param businessObjectService The businessObjectService to set.
289      */
290     public void setBusinessObjectService(BusinessObjectService businessObjectService) {
291         this.businessObjectService = businessObjectService;
292     }
293 
294     /**
295      * Sets the disbursementVoucherPayeeService attribute value.
296      * 
297      * @param disbursementVoucherPayeeService The disbursementVoucherPayeeService to set.
298      */
299     public void setDisbursementVoucherPayeeService(DisbursementVoucherPayeeService disbursementVoucherPayeeService) {
300         this.disbursementVoucherPayeeService = disbursementVoucherPayeeService;
301     }
302 }