View Javadoc
1   /*
2    * Copyright 2008-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.businessobject.PurApAccountingLine;
22  import org.kuali.ole.sys.OLEKeyConstants;
23  import org.kuali.ole.sys.document.validation.GenericValidation;
24  import org.kuali.ole.sys.document.validation.event.AddAccountingLineEvent;
25  import org.kuali.ole.sys.document.validation.event.AttributedDocumentEvent;
26  import org.kuali.rice.kns.service.DataDictionaryService;
27  import org.kuali.rice.krad.bo.BusinessObject;
28  import org.kuali.rice.krad.util.GlobalVariables;
29  import org.kuali.rice.krad.util.ObjectUtils;
30  
31  public class VendorCreditMemoAccountPercentBetween0And100Validation extends GenericValidation {
32  
33      private DataDictionaryService dataDictionaryService;
34  
35      public boolean validate(AttributedDocumentEvent event) {
36          boolean isValid = true;
37          PurApAccountingLine account = (PurApAccountingLine) ((AddAccountingLineEvent) event).getAccountingLine();
38  
39          if (validateRequiredField(account, PurapPropertyConstants.ACCOUNT_LINE_PERCENT)) {
40              double pct = account.getAccountLinePercent().doubleValue();
41              if (pct <= 0 || pct > 100) {
42                  GlobalVariables.getMessageMap().putError(PurapPropertyConstants.ACCOUNT_LINE_PERCENT, PurapKeyConstants.ERROR_CREDIT_MEMO_LINE_PERCENT);
43                  isValid = false;
44              }
45          } else {
46              isValid = false;
47          }
48  
49          return isValid;
50      }
51  
52      /**
53       * Helper method to perform required field checks add error messages if the validation fails. Adds an error required to
54       * GlobalVariables.errorMap using the given fieldName as the error key and retrieving the error label from the data dictionary
55       * for the error required message param.
56       *
57       * @param businessObject - Business object to check for value
58       * @param fieldName      - Name of the property in the business object
59       */
60      protected boolean validateRequiredField(BusinessObject businessObject, String fieldName) {
61          boolean valid = true;
62  
63          Object fieldValue = ObjectUtils.getPropertyValue(businessObject, fieldName);
64          if (fieldValue == null || (fieldValue instanceof String && StringUtils.isBlank(fieldName))) {
65              String label = dataDictionaryService.getAttributeErrorLabel(businessObject.getClass(), fieldName);
66              GlobalVariables.getMessageMap().putError(fieldName, OLEKeyConstants.ERROR_REQUIRED, label);
67              valid = false;
68          }
69  
70          return valid;
71      }
72  
73      public DataDictionaryService getDataDictionaryService() {
74          return dataDictionaryService;
75      }
76  
77      public void setDataDictionaryService(DataDictionaryService dataDictionaryService) {
78          this.dataDictionaryService = dataDictionaryService;
79      }
80  
81  }