View Javadoc

1   /*
2    * Copyright 2009 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.module.purap.document.validation.impl;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.kuali.ole.module.purap.PurapKeyConstants;
20  import org.kuali.ole.module.purap.PurapPropertyConstants;
21  import org.kuali.ole.module.purap.document.VendorCreditMemoDocument;
22  import org.kuali.ole.module.purap.document.service.PaymentRequestService;
23  import org.kuali.ole.select.document.OleVendorCreditMemoDocument;
24  import org.kuali.ole.sys.OLEKeyConstants;
25  import org.kuali.ole.sys.document.validation.GenericValidation;
26  import org.kuali.ole.sys.document.validation.event.AttributedDocumentEvent;
27  import org.kuali.rice.kns.service.DataDictionaryService;
28  import org.kuali.rice.krad.bo.BusinessObject;
29  import org.kuali.rice.krad.util.GlobalVariables;
30  import org.kuali.rice.krad.util.ObjectUtils;
31  
32  public class VendorCreditMemoInitTabRequiredFieldsValidation extends GenericValidation {
33  
34      private DataDictionaryService dataDictionaryService;
35      private PaymentRequestService paymentRequestService;
36  
37      /**
38       * Validates the necessary fields on the init tab were given and credit memo date is valid. (NOTE: formats for cm date and
39       * number already performed by pojo conversion)
40       */
41      public boolean validate(AttributedDocumentEvent event) {
42          OleVendorCreditMemoDocument cmDocument = (OleVendorCreditMemoDocument) event.getDocument();
43  
44          boolean valid = true;
45          if(cmDocument.getInvoiceIdentifier() == null || cmDocument.getInvoiceIdentifier().compareTo(new Integer(0)) == 0) {
46              valid = validateRequiredField(cmDocument, PurapPropertyConstants.CREDIT_MEMO_NUMBER);
47          }
48  
49  
50          valid = valid && validateRequiredField(cmDocument, PurapPropertyConstants.CREDIT_MEMO_AMOUNT);
51          boolean creditMemoDateExist = validateRequiredField(cmDocument, PurapPropertyConstants.CREDIT_MEMO_DATE);
52  
53          if (creditMemoDateExist) {
54              if (paymentRequestService.isInvoiceDateAfterToday(cmDocument.getCreditMemoDate())) {
55                  String label = dataDictionaryService.getAttributeErrorLabel(VendorCreditMemoDocument.class, PurapPropertyConstants.CREDIT_MEMO_DATE);
56                  GlobalVariables.getMessageMap().putError(PurapPropertyConstants.CREDIT_MEMO_DATE, PurapKeyConstants.ERROR_INVALID_INVOICE_DATE, label);
57                  valid = false;
58              }
59          }
60  
61          return valid;
62  
63      }
64  
65      /**
66       * Helper method to perform required field checks add error messages if the validation fails. Adds an error required to
67       * GlobalVariables.errorMap using the given fieldName as the error key and retrieving the error label from the data dictionary
68       * for the error required message param.
69       *
70       * @param businessObject - Business object to check for value
71       * @param fieldName      - Name of the property in the business object
72       */
73      protected boolean validateRequiredField(BusinessObject businessObject, String fieldName) {
74          boolean valid = true;
75  
76          Object fieldValue = ObjectUtils.getPropertyValue(businessObject, fieldName);
77          if (fieldValue == null || (fieldValue instanceof String && StringUtils.isBlank(fieldName))) {
78              String label = dataDictionaryService.getAttributeErrorLabel(businessObject.getClass(), fieldName);
79              GlobalVariables.getMessageMap().putError(fieldName, OLEKeyConstants.ERROR_REQUIRED, label);
80              valid = false;
81          }
82  
83          return valid;
84      }
85  
86      public DataDictionaryService getDataDictionaryService() {
87          return dataDictionaryService;
88      }
89  
90      public void setDataDictionaryService(DataDictionaryService dataDictionaryService) {
91          this.dataDictionaryService = dataDictionaryService;
92      }
93  
94      public PaymentRequestService getPaymentRequestService() {
95          return paymentRequestService;
96      }
97  
98      public void setPaymentRequestService(PaymentRequestService paymentRequestService) {
99          this.paymentRequestService = paymentRequestService;
100     }
101 
102 }