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.fp.document.validation.impl;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.kuali.ole.fp.businessobject.DisbursementVoucherPayeeDetail;
20  import org.kuali.ole.fp.document.DisbursementVoucherDocument;
21  import org.kuali.ole.sys.OLEKeyConstants;
22  import org.kuali.ole.sys.OLEPropertyConstants;
23  import org.kuali.ole.sys.context.SpringContext;
24  import org.kuali.ole.sys.document.AccountingDocument;
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.util.GlobalVariables;
29  import org.kuali.rice.krad.util.MessageMap;
30  import org.kuali.rice.location.api.state.State;
31  import org.kuali.rice.location.api.state.StateService;
32  
33  public class DisbursementVoucherPayeeStateCodeValidation extends GenericValidation {
34      private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(DisbursementVoucherPayeeStateCodeValidation.class);
35  
36      private AccountingDocument accountingDocumentForValidation;
37  
38      /**
39       * @see org.kuali.ole.sys.document.validation.Validation#validate(org.kuali.ole.sys.document.validation.event.AttributedDocumentEvent)
40       */
41      public boolean validate(AttributedDocumentEvent event) {
42          LOG.debug("validate start");
43          boolean isValid = true;
44  
45          DisbursementVoucherDocument document = (DisbursementVoucherDocument) accountingDocumentForValidation;
46          DisbursementVoucherPayeeDetail payeeDetail = document.getDvPayeeDetail();
47  
48          MessageMap errors = GlobalVariables.getMessageMap();
49          errors.addToErrorPath(OLEPropertyConstants.DOCUMENT);
50  
51          DataDictionaryService dataDictionaryService = SpringContext.getBean(DataDictionaryService.class);
52          StateService stateService = SpringContext.getBean(StateService.class);
53  
54          String countryCode = payeeDetail.getDisbVchrPayeeCountryCode();
55          String stateCode = payeeDetail.getDisbVchrPayeeStateCode();
56          if (StringUtils.isNotBlank(stateCode) && StringUtils.isNotBlank(countryCode)) {
57              State state = stateService.getState(countryCode, stateCode);
58              if (state == null) {
59                  String label = dataDictionaryService.getAttributeLabel(DisbursementVoucherPayeeDetail.class, OLEPropertyConstants.DISB_VCHR_PAYEE_STATE_CODE);
60                  String propertyPath = OLEPropertyConstants.DV_PAYEE_DETAIL + "." + OLEPropertyConstants.DISB_VCHR_PAYEE_STATE_CODE;
61                  errors.putError(propertyPath, OLEKeyConstants.ERROR_EXISTENCE, label);
62                  isValid = false;
63              }
64          }
65          
66          countryCode = payeeDetail.getDisbVchrSpecialHandlingCountryCode();
67          stateCode = payeeDetail.getDisbVchrSpecialHandlingStateCode();
68          if (document.isDisbVchrSpecialHandlingCode() && StringUtils.isNotBlank(stateCode) && StringUtils.isNotBlank(countryCode)) {
69              State state = stateService.getState(countryCode, stateCode);
70              if (state == null) {
71                  String label = dataDictionaryService.getAttributeLabel(DisbursementVoucherPayeeDetail.class, OLEPropertyConstants.DISB_VCHR_SPECIAL_HANDLING_STATE_CODE);
72                  String propertyPath = OLEPropertyConstants.DV_PAYEE_DETAIL + "." + OLEPropertyConstants.DISB_VCHR_SPECIAL_HANDLING_STATE_CODE;
73                  errors.putError(propertyPath, OLEKeyConstants.ERROR_EXISTENCE, label);
74                  isValid = false;
75              }
76          }
77  
78          errors.removeFromErrorPath(OLEPropertyConstants.DOCUMENT);
79  
80          return isValid;
81      }
82  
83  
84      /**
85       * Gets the accountingDocumentForValidation attribute.
86       * 
87       * @return Returns the accountingDocumentForValidation.
88       */
89      public AccountingDocument getAccountingDocumentForValidation() {
90          return accountingDocumentForValidation;
91      }
92  
93  
94      /**
95       * Sets the accountingDocumentForValidation attribute value.
96       * 
97       * @param accountingDocumentForValidation The accountingDocumentForValidation to set.
98       */
99      public void setAccountingDocumentForValidation(AccountingDocument accountingDocumentForValidation) {
100         this.accountingDocumentForValidation = accountingDocumentForValidation;
101     }
102 }