View Javadoc
1   /*
2    * The Kuali Financial System, a comprehensive financial management system for higher education.
3    * 
4    * Copyright 2005-2014 The Kuali Foundation
5    * 
6    * This program is free software: you can redistribute it and/or modify
7    * it under the terms of the GNU Affero General Public License as
8    * published by the Free Software Foundation, either version 3 of the
9    * License, or (at your option) any later version.
10   * 
11   * This program is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU Affero General Public License for more details.
15   * 
16   * You should have received a copy of the GNU Affero General Public License
17   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18   */
19  package org.kuali.kfs.module.tem.document.validation.impl;
20  
21  import org.apache.commons.lang.StringUtils;
22  import org.kuali.kfs.module.tem.TemPropertyConstants;
23  import org.kuali.kfs.module.tem.businessobject.TravelPayment;
24  import org.kuali.kfs.sys.KFSKeyConstants;
25  import org.kuali.kfs.sys.KFSPropertyConstants;
26  import org.kuali.kfs.sys.document.validation.GenericValidation;
27  import org.kuali.kfs.sys.document.validation.event.AttributedDocumentEvent;
28  import org.kuali.rice.krad.service.DataDictionaryService;
29  import org.kuali.rice.krad.util.GlobalVariables;
30  import org.kuali.rice.krad.util.MessageMap;
31  import org.kuali.rice.location.api.state.State;
32  import org.kuali.rice.location.api.state.StateService;
33  
34  /**
35   * Validates that the special handling state and special handling country align
36   */
37  public class TravelPaymentSpecialHandlingStateCodeValidation extends GenericValidation {
38      protected TravelPayment travelPaymentForValidation;
39      protected StateService stateService;
40      protected DataDictionaryService dataDictionaryService;
41  
42      /**
43       * Validates that the special handling state and special handling country align
44       * @see org.kuali.kfs.sys.document.validation.Validation#validate(org.kuali.kfs.sys.document.validation.event.AttributedDocumentEvent)
45       */
46      @Override
47      public boolean validate(AttributedDocumentEvent event) {
48          boolean isValid = true;
49  
50          MessageMap errors = GlobalVariables.getMessageMap();
51          errors.addToErrorPath(KFSPropertyConstants.DOCUMENT+"."+TemPropertyConstants.TRAVEL_PAYMENT);
52  
53          final String countryCode = getTravelPaymentForValidation().getSpecialHandlingCountryCode();
54          final String stateCode = getTravelPaymentForValidation().getSpecialHandlingStateCode();
55          if (getTravelPaymentForValidation().isSpecialHandlingCode() && StringUtils.isNotBlank(stateCode) && StringUtils.isNotBlank(countryCode)) {
56              final State state = getStateService().getState(countryCode, stateCode);
57              if (state == null) {
58                  final String label = getDataDictionaryService().getAttributeLabel(TravelPayment.class, TemPropertyConstants.TravelPaymentProperties.SPECIAL_HANDLING_STATE_CODE);
59                  final String propertyPath = "." + TemPropertyConstants.TravelPaymentProperties.SPECIAL_HANDLING_STATE_CODE;
60                  errors.putError(propertyPath, KFSKeyConstants.ERROR_EXISTENCE, label);
61                  isValid = false;
62              }
63          }
64  
65          errors.removeFromErrorPath(KFSPropertyConstants.DOCUMENT+"."+TemPropertyConstants.TRAVEL_PAYMENT);
66  
67          return isValid;
68      }
69  
70      /**
71       * @return the travel payment which should be validated
72       */
73      public TravelPayment getTravelPaymentForValidation() {
74          return travelPaymentForValidation;
75      }
76  
77      /**
78       * Sets the travel payment which should be validated
79       * @param travelPaymentForValidation the travel payment which should be validated
80       */
81      public void setTravelPaymentForValidation(TravelPayment travelPaymentForValidation) {
82          this.travelPaymentForValidation = travelPaymentForValidation;
83      }
84  
85      /**
86       * @return the implementation of the StateService to use
87       */
88      public StateService getStateService() {
89          return stateService;
90      }
91  
92      /**
93       * Sets the implementation of the StateService to use
94       * @param stateService the implementation of the StateService to use
95       */
96      public void setStateService(StateService stateService) {
97          this.stateService = stateService;
98      }
99  
100     /**
101      * @return the implementation of the DataDictionaryService to use
102      */
103     public DataDictionaryService getDataDictionaryService() {
104         return dataDictionaryService;
105     }
106 
107     /**
108      * Sets the implementation of the DataDictionaryService to use
109      * @param dataDictionaryService the implementation of the DataDictionaryService to use
110      */
111     public void setDataDictionaryService(DataDictionaryService dataDictionaryService) {
112         this.dataDictionaryService = dataDictionaryService;
113     }
114 
115 }