1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
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  
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  
38  
39  
40  
41  
42  
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  
56  
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  
71  
72      protected boolean isCardHolderDefaultTurnedOn() {
73          return getParameterService().getParameterValueAsBoolean(ProcurementCardCreateDocumentsStep.class, ProcurementCardCreateDocumentsStep.USE_CARD_HOLDER_DEFAULT_PARAMETER_NAME);
74      }
75  
76      
77  
78  
79      protected boolean isAccountDefaultTurnedOn() {
80          return getParameterService().getParameterValueAsBoolean(ProcurementCardCreateDocumentsStep.class, ProcurementCardCreateDocumentsStep.USE_ACCOUNTING_DEFAULT_PARAMETER_NAME);
81      }
82  
83      
84  
85  
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 
136 
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 
159 
160 
161 
162 
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 
171 
172     protected synchronized ParameterService getParameterService() {
173         if (parameterService == null) {
174             parameterService = SpringContext.getBean(ParameterService.class);
175         }
176         return parameterService;
177     }
178 
179 }