1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  package org.kuali.ole.pdp.document.validation.impl;
17  
18  import java.util.Collection;
19  import java.util.HashMap;
20  import java.util.Map;
21  
22  import org.apache.commons.lang.StringUtils;
23  import org.apache.log4j.Logger;
24  import org.kuali.ole.pdp.PdpConstants;
25  import org.kuali.ole.pdp.PdpKeyConstants;
26  import org.kuali.ole.pdp.PdpPropertyConstants;
27  import org.kuali.ole.pdp.businessobject.CustomerBank;
28  import org.kuali.ole.pdp.businessobject.CustomerProfile;
29  import org.kuali.ole.sys.businessobject.Bank;
30  import org.kuali.ole.sys.context.SpringContext;
31  import org.kuali.rice.kns.document.MaintenanceDocument;
32  import org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase;
33  import org.kuali.rice.krad.bo.PersistableBusinessObject;
34  import org.kuali.rice.krad.service.BusinessObjectService;
35  import org.kuali.rice.krad.util.GlobalVariables;
36  import org.kuali.rice.krad.util.KRADConstants;
37  import org.kuali.rice.krad.util.MessageMap;
38  import org.kuali.rice.krad.util.ObjectUtils;
39  import org.kuali.rice.location.api.postalcode.PostalCode;
40  import org.kuali.rice.location.api.postalcode.PostalCodeService;
41  
42  public class CustomerProfileRule extends MaintenanceDocumentRuleBase {
43      protected static Logger LOG = org.apache.log4j.Logger.getLogger(CustomerProfileRule.class);
44  
45      
46  
47  
48      @Override
49      public boolean processCustomAddCollectionLineBusinessRules(MaintenanceDocument document, String collectionName, PersistableBusinessObject line) {
50          boolean isValid = true;
51          isValid &= super.processCustomAddCollectionLineBusinessRules(document, collectionName, line);
52          MessageMap errorMap = GlobalVariables.getMessageMap();
53          isValid &= !errorMap.hasErrors();
54  
55          if (isValid) {
56              if (collectionName.equals(PdpPropertyConstants.CustomerProfile.CUSTOMER_PROFILE_BANKS)) {
57                  CustomerBank newCustomerBank = (CustomerBank) line;
58                  if(newCustomerBank.getDisbursementTypeCode()!=null){
59                      
60                  CustomerProfile customerProfile = (CustomerProfile) document.getNewMaintainableObject().getBusinessObject();
61                  for (CustomerBank bank : customerProfile.getCustomerBanks()) {
62                      if (bank.getDisbursementTypeCode().equalsIgnoreCase(newCustomerBank.getDisbursementTypeCode())) {
63                              errorMap.putError(PdpPropertyConstants.DISBURSEMENT_TYPE_CODE, PdpKeyConstants.ERROR_ONE_BANK_PER_DISBURSEMENT_TYPE_CODE);
64                              isValid = false;
65                          }
66                      }
67                  }
68                  if(newCustomerBank.getBank()!=null){
69                      
70                      newCustomerBank.refreshReferenceObject(PdpPropertyConstants.CUSTOMER_BANK);
71                      Bank newBank = newCustomerBank.getBank();
72                      if (newCustomerBank.getDisbursementTypeCode().equalsIgnoreCase(PdpConstants.DisbursementTypeCodes.ACH) && !newBank.isBankAchIndicator()) {
73                          errorMap.putError(PdpPropertyConstants.DISBURSEMENT_TYPE_CODE, PdpKeyConstants.ERROR_PDP_ACH_BANK_NOT_ALLOWED, newBank.getBankCode() + " (" + newBank.getBankName() + ")");
74                          isValid = false;
75                      }
76                      if (newCustomerBank.getDisbursementTypeCode().equalsIgnoreCase(PdpConstants.DisbursementTypeCodes.CHECK) && !newBank.isBankCheckIndicator()) {
77                          errorMap.putError(PdpPropertyConstants.DISBURSEMENT_TYPE_CODE, PdpKeyConstants.ERROR_PDP_CHECK_BANK_NOT_ALLOWED, newBank.getBankCode() + " (" + newBank.getBankName() + ")");
78                          isValid = false;
79                      }
80                  }
81              }
82          }
83          return isValid;
84      }
85  
86      
87  
88  
89  
90  
91  
92  
93  
94  
95      @Override
96      protected boolean processCustomRouteDocumentBusinessRules(MaintenanceDocument document) {
97  
98          boolean isValid = true;
99          isValid &= super.processCustomRouteDocumentBusinessRules(document);
100 
101         CustomerProfile customerProfile = (CustomerProfile) document.getNewMaintainableObject().getBusinessObject();
102 
103         if (customerProfile != null) {
104             customerProfile.refreshNonUpdateableReferences();
105             setStateFromZip(customerProfile);
106         }
107 
108         boolean checkBankPresent = false;
109         boolean ACHBankPresent = false;
110 
111         
112         boolean customerHasACHType = customerProfile.getAchTransactionType() != null ? true : false;
113 
114         
115         if (customerProfile.getCustomerBanks().isEmpty()){
116             putFieldError(KRADConstants.MAINTENANCE_ADD_PREFIX+"customerBanks."+PdpPropertyConstants.DISBURSEMENT_TYPE_CODE, PdpKeyConstants.Format.ErrorMessages.ERROR_FORMAT_BANK_MISSING, customerProfile.getId().toString());
117 
118             isValid = false;
119         }else{
120             for (CustomerBank customerBank : customerProfile.getCustomerBanks()){
121 
122                 
123                 Bank bank = customerBank.getBank();
124                 if (customerBank.getDisbursementTypeCode().equalsIgnoreCase(PdpConstants.DisbursementTypeCodes.ACH) && !bank.isBankAchIndicator()) {
125                     putFieldError(PdpPropertyConstants.DISBURSEMENT_TYPE_CODE, PdpKeyConstants.ERROR_PDP_ACH_BANK_NOT_ALLOWED, bank.getBankCode() + " (" + bank.getBankName() + ")");
126                     isValid = false;
127                 }
128                 if (customerBank.getDisbursementTypeCode().equalsIgnoreCase(PdpConstants.DisbursementTypeCodes.CHECK) && !bank.isBankCheckIndicator()) {
129                     putFieldError(PdpPropertyConstants.DISBURSEMENT_TYPE_CODE, PdpKeyConstants.ERROR_PDP_CHECK_BANK_NOT_ALLOWED, bank.getBankCode() + " (" + bank.getBankName() + ")");
130                     isValid = false;
131                 }
132 
133                 
134                 if(customerBank.getDisbursementTypeCode().equalsIgnoreCase(PdpConstants.DisbursementTypeCodes.CHECK)){
135                     checkBankPresent = true;
136                 }
137                 
138                 if(customerBank.getDisbursementTypeCode().equalsIgnoreCase(PdpConstants.DisbursementTypeCodes.ACH)){
139                         ACHBankPresent = true;
140                  }
141                 if(checkBankPresent && ACHBankPresent){
142                     break;
143                 }
144             }
145 
146             
147             if(!checkBankPresent){
148                 isValid = false;
149                 putFieldError(KRADConstants.MAINTENANCE_ADD_PREFIX+"customerBanks."+PdpPropertyConstants.DISBURSEMENT_TYPE_CODE, PdpKeyConstants.ERROR_PDP_CHECK_BANK_REQUIRED);
150             }
151 
152             
153             if(customerHasACHType && !ACHBankPresent){
154                 isValid = false;
155                 putFieldError(KRADConstants.MAINTENANCE_ADD_PREFIX+"customerBanks."+PdpPropertyConstants.DISBURSEMENT_TYPE_CODE, PdpKeyConstants.ERROR_PDP_ACH_BANK_REQUIRED);
156             }
157 
158         }
159 
160         
161         if (document.isNew()) {
162             isValid &= verifyChartUnitSubUnitIsUnique(customerProfile);
163         }
164 
165         return isValid;
166     }
167 
168     
169     
170 
171 
172 
173 
174     protected void setStateFromZip(CustomerProfile customerProfile) {
175         if (StringUtils.isNotBlank(customerProfile.getZipCode()) && StringUtils.isNotBlank(customerProfile.getCountryCode()) ) {
176             PostalCode zip = SpringContext.getBean(PostalCodeService.class).getPostalCode( customerProfile.getCountryCode(), customerProfile.getZipCode() );
177 
178             
179             if (ObjectUtils.isNotNull(zip)) { 
180                 customerProfile.setCity(zip.getCityName());
181                 customerProfile.setStateCode(zip.getStateCode());
182             }
183         }
184     }
185 
186     
187 
188 
189 
190 
191     protected boolean verifyChartUnitSubUnitIsUnique(CustomerProfile customerProfile) {
192         boolean result = true;
193 
194         if (!StringUtils.isBlank(customerProfile.getChartCode()) && !StringUtils.isBlank(customerProfile.getUnitCode()) && !StringUtils.isBlank(customerProfile.getSubUnitCode())) {
195             final BusinessObjectService businessObjectService = SpringContext.getBean(BusinessObjectService.class);
196             Map<String, Object> searchKeys = new HashMap<String, Object>();
197             searchKeys.put(PdpPropertyConstants.CustomerProfile.CUSTOMER_PROFILE_CHART_CODE, customerProfile.getChartCode());
198             searchKeys.put(PdpPropertyConstants.CustomerProfile.CUSTOMER_PROFILE_UNIT_CODE, customerProfile.getUnitCode());
199             searchKeys.put(PdpPropertyConstants.CustomerProfile.CUSTOMER_PROFILE_SUB_UNIT_CODE, customerProfile.getSubUnitCode());
200 
201             final Collection foundCustomerProfiles = businessObjectService.findMatching(CustomerProfile.class, searchKeys);
202             if (foundCustomerProfiles != null && foundCustomerProfiles.size() > 0) {
203                 result = false;
204                 putFieldError(PdpPropertyConstants.CustomerProfile.CUSTOMER_PROFILE_UNIT_CODE, PdpKeyConstants.ERROR_CUSTOMER_PROFILE_CHART_UNIT_SUB_UNIT_NOT_UNIQUE, new String[] { customerProfile.getChartCode(), customerProfile.getUnitCode(), customerProfile.getSubUnitCode()});
205             }
206         }
207 
208         return result;
209     }
210 
211 }