View Javadoc
1   /*
2    * Copyright 2011 The Kuali Foundation.
3    *
4    * Licensed under the Educational Community License, Version 1.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/ecl1.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.batch.ProcurementCardCreateDocumentsStep;
20  import org.kuali.ole.fp.businessobject.ProcurementCardDefault;
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.rice.coreservice.framework.parameter.ParameterService;
25  import org.kuali.rice.kns.document.MaintenanceDocument;
26  import org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase;
27  
28  /**
29   * This class represents business rules for the procurement cardholder maintenance document
30   */
31  
32  public class ProcurementCardDefaultRule extends MaintenanceDocumentRuleBase {
33      protected static final String WARNING_CARDHOLDER_LAST_ACTIVE_MEMBER = "warning.document.procurementcardholderdetail.cardholder.last.active";
34      protected static ParameterService parameterService;
35  
36      /**
37       * Returns true procurement card defaults maintenance document is routed successfully
38       *
39       * @param document submitted procurement card defaults maintenance document
40       * @return true if chart/account/organization is valid
41       *
42       * @see org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase#processCustomRouteDocumentBusinessRules(org.kuali.rice.kns.document.MaintenanceDocument)
43       */
44      @Override
45      protected boolean processCustomRouteDocumentBusinessRules(MaintenanceDocument document) {
46          boolean continueRouting = super.processCustomRouteDocumentBusinessRules(document);
47          final ProcurementCardDefault newProcurementCardDefault = (ProcurementCardDefault)getNewBo();
48  
49          continueRouting &= validateCardHolderDefault(newProcurementCardDefault);
50          continueRouting &= validateAccountingDefault(newProcurementCardDefault);
51  
52          return continueRouting;
53      }
54      /**
55       * Always returns true; provides user feedback on procurement card defaults maintenance document
56       * @see org.kuali.rice.kns.rules.DocumentRuleBase#processCustomSaveDocumentBusinessRules(org.kuali.rice.kns.document.Document)
57       */
58      @Override
59      protected boolean processCustomSaveDocumentBusinessRules(MaintenanceDocument document) {
60          super.processCustomSaveDocumentBusinessRules(document);
61  
62          final ProcurementCardDefault newProcurementCardDefault = (ProcurementCardDefault)getNewBo();
63          validateCardHolderDefault(newProcurementCardDefault);
64          validateAccountingDefault(newProcurementCardDefault);
65  
66          return true;
67      }
68  
69      /**
70       * @return true if use of card holder defaults is turned on via parameter, false if it is turned off
71       */
72      protected boolean isCardHolderDefaultTurnedOn() {
73          return getParameterService().getParameterValueAsBoolean(ProcurementCardCreateDocumentsStep.class, ProcurementCardCreateDocumentsStep.USE_CARD_HOLDER_DEFAULT_PARAMETER_NAME);
74      }
75  
76      /**
77       * @return true if use of accounting defaults is turned on via parameter, false if it is turned off
78       */
79      protected boolean isAccountDefaultTurnedOn() {
80          return getParameterService().getParameterValueAsBoolean(ProcurementCardCreateDocumentsStep.class, ProcurementCardCreateDocumentsStep.USE_ACCOUNTING_DEFAULT_PARAMETER_NAME);
81      }
82  
83      /**
84       * Validates the card holder information
85       * @return true if the card holder is valid, false otherwise
86       */
87      protected boolean validateCardHolderDefault(ProcurementCardDefault newProcurementCardDefault) {
88          boolean valid = true;
89          if (isCardHolderDefaultTurnedOn()) {
90              if (StringUtils.isBlank(newProcurementCardDefault.getCardHolderLine1Address())) {
91                  putFieldErrorWithLabel(OLEPropertyConstants.PROCUREMENT_CARD_HOLDER_LINE1_ADDRESS, OLEKeyConstants.ERROR_REQUIRED);
92                  valid = false;
93              }
94              if (StringUtils.isBlank(newProcurementCardDefault.getCardHolderCityName())) {
95                  putFieldErrorWithLabel(OLEPropertyConstants.PROCUREMENT_CARD_HOLDER_CITY_NAME, OLEKeyConstants.ERROR_REQUIRED);
96                  valid = false;
97              }
98              if (StringUtils.isBlank(newProcurementCardDefault.getCardHolderStateCode())) {
99                  putFieldErrorWithLabel(OLEPropertyConstants.PROCUREMENT_CARD_HOLDER_STATE, OLEKeyConstants.ERROR_REQUIRED);
100                 valid = false;
101             }
102             if (StringUtils.isBlank(newProcurementCardDefault.getCardHolderZipCode())) {
103                 putFieldErrorWithLabel(OLEPropertyConstants.PROCUREMENT_CARD_HOLDER_ZIP_CODE, OLEKeyConstants.ERROR_REQUIRED);
104                 valid = false;
105             }
106             if (StringUtils.isBlank(newProcurementCardDefault.getCardHolderWorkPhoneNumber())) {
107                 putFieldErrorWithLabel(OLEPropertyConstants.PROCUREMENT_CARD_HOLDER_WORK_PHONE_NUMBER, OLEKeyConstants.ERROR_REQUIRED);
108                 valid = false;
109             }
110             if (newProcurementCardDefault.getCardLimit() == null) {
111                 putFieldErrorWithLabel(OLEPropertyConstants.PROCUREMENT_CARD_LIMIT, OLEKeyConstants.ERROR_REQUIRED);
112                 valid = false;
113             }
114             if (newProcurementCardDefault.getCardCycleAmountLimit() == null) {
115                 putFieldErrorWithLabel(OLEPropertyConstants.PROCUREMENT_CARD_CYCLE_AMOUNT_LIMIT, OLEKeyConstants.ERROR_REQUIRED);
116                 valid = false;
117             }
118             if (newProcurementCardDefault.getCardCycleVolumeLimit() == null) {
119                 putFieldErrorWithLabel(OLEPropertyConstants.PROCUREMENT_CARD_CYCLE_VOLUME_LIMIT, OLEKeyConstants.ERROR_REQUIRED);
120                 valid = false;
121             }
122             if (StringUtils.isBlank(newProcurementCardDefault.getCardStatusCode())) {
123                 putFieldErrorWithLabel(OLEPropertyConstants.PROCUREMENT_CARD_STATUS_CODE, OLEKeyConstants.ERROR_REQUIRED);
124                 valid = false;
125             }
126             if (StringUtils.isBlank(newProcurementCardDefault.getCardNoteText())) {
127                 putFieldErrorWithLabel(OLEPropertyConstants.PROCUREMENT_CARD_NOTE_TEXT, OLEKeyConstants.ERROR_REQUIRED);
128                 valid = false;
129             }
130         }
131         return valid;
132     }
133 
134     /**
135      * Validates the accounting default information
136      * @return true if the accounting default information is valid, false otherwise
137      */
138     protected boolean validateAccountingDefault(ProcurementCardDefault newProcurementCardDefault) {
139         boolean valid = true;
140         if (isAccountDefaultTurnedOn() || isCardHolderDefaultTurnedOn()) {
141             if (StringUtils.isBlank(newProcurementCardDefault.getChartOfAccountsCode())) {
142                 putFieldErrorWithLabel(OLEPropertyConstants.CHART_OF_ACCOUNTS_CODE, OLEKeyConstants.ERROR_REQUIRED);
143                 valid = false;
144             }
145             if (StringUtils.isBlank(newProcurementCardDefault.getAccountNumber())) {
146                 putFieldErrorWithLabel(OLEPropertyConstants.ACCOUNT_NUMBER, OLEKeyConstants.ERROR_REQUIRED);
147                 valid = false;
148             }
149             if (StringUtils.isBlank(newProcurementCardDefault.getFinancialObjectCode())) {
150                 putFieldErrorWithLabel(OLEPropertyConstants.FINANCIAL_OBJECT_CODE, OLEKeyConstants.ERROR_REQUIRED);
151                 valid = false;
152             }
153         }
154         return valid;
155     }
156 
157     /**
158      * Adds a property-specific error to the global errors list, with the DD short label as the single argument.
159      *
160      * @param propertyName - Property name of the element that is associated with the error. Used to mark the field as errored in
161      *        the UI.
162      * @param errorConstant - Error Constant that can be mapped to a resource for the actual text message.
163      */
164     protected void putFieldErrorWithLabel(String propertyName, String errorConstant) {
165         final String label = getDataDictionaryService().getAttributeLabel(getNewBo().getClass(), propertyName);
166         putFieldError(propertyName, errorConstant, label);
167     }
168 
169     /**
170      * @return the default implementation of the ParameterService
171      */
172     protected synchronized ParameterService getParameterService() {
173         if (parameterService == null) {
174             parameterService = SpringContext.getBean(ParameterService.class);
175         }
176         return parameterService;
177     }
178 
179 }