1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.kuali.kfs.module.cg.document.validation.impl;
20
21 import java.util.ArrayList;
22 import java.util.HashMap;
23 import java.util.List;
24 import java.util.Map;
25
26 import org.apache.commons.lang.StringUtils;
27 import org.apache.log4j.Logger;
28 import org.kuali.kfs.integration.ar.AccountsReceivableCustomer;
29 import org.kuali.kfs.module.cg.CGConstants;
30 import org.kuali.kfs.module.cg.CGKeyConstants;
31 import org.kuali.kfs.module.cg.CGPropertyConstants;
32 import org.kuali.kfs.module.cg.businessobject.Agency;
33 import org.kuali.kfs.module.cg.businessobject.AgencyAddress;
34 import org.kuali.kfs.module.cg.businessobject.AgencyType;
35 import org.kuali.kfs.sys.KFSKeyConstants;
36 import org.kuali.kfs.sys.KFSPropertyConstants;
37 import org.kuali.kfs.sys.context.SpringContext;
38 import org.kuali.rice.kns.document.MaintenanceDocument;
39 import org.kuali.rice.kns.service.DataDictionaryService;
40 import org.kuali.rice.krad.bo.PersistableBusinessObject;
41 import org.kuali.rice.krad.service.BusinessObjectService;
42 import org.kuali.rice.krad.util.GlobalVariables;
43 import org.kuali.rice.krad.util.ObjectUtils;
44
45
46
47
48 public class AgencyRule extends CGMaintenanceDocumentRuleBase {
49 protected static Logger LOG = org.apache.log4j.Logger.getLogger(AgencyRule.class);
50
51 protected Agency newAgency;
52 protected Agency oldAgency;
53
54 BusinessObjectService businessObjectService;
55
56
57
58
59 public AgencyRule() {
60 super();
61 businessObjectService = SpringContext.getBean(BusinessObjectService.class);
62 }
63
64
65
66
67 @Override
68 protected boolean processCustomApproveDocumentBusinessRules(MaintenanceDocument document) {
69 LOG.debug("Entering AgencyRule.processCustomApproveDocumentBusinessRules");
70 boolean success = super.processCustomApproveDocumentBusinessRules(document);
71
72 success &= checkAgencyReportsTo(document);
73
74 LOG.info("Leaving AgencyRule.processCustomApproveDocumentBusinessRules");
75 return success;
76 }
77
78
79
80
81 @Override
82 protected boolean processCustomRouteDocumentBusinessRules(MaintenanceDocument document) {
83 LOG.debug("Entering AgencyRule.processCustomRouteDocumentBusinessRules");
84 boolean success = super.processCustomRouteDocumentBusinessRules(document);
85
86 success &= checkAgencyReportsTo(document);
87
88
89 if (contractsGrantsBillingEnhancementActive) {
90
91 success &= checkPrimary(newAgency.getAgencyAddresses(), AgencyAddress.class, KFSPropertyConstants.AGENCY_ADDRESSES, Agency.class);
92 success &= validateAddresses(newAgency);
93
94
95 success &= validateCustomerType(newAgency);
96 }
97
98 LOG.info("Leaving AgencyRule.processCustomRouteDocumentBusinessRules");
99 return success;
100 }
101
102
103
104
105 @Override
106 protected boolean processCustomSaveDocumentBusinessRules(MaintenanceDocument document) {
107 LOG.debug("Entering AgencyRule.processCustomSaveDocumentBusinessRules");
108 boolean success = super.processCustomSaveDocumentBusinessRules(document);
109
110 success &= checkAgencyReportsTo(document);
111 success &= validateAgencyType(document);
112 success &= validateAgencyReportingName(document);
113
114 LOG.info("Leaving AgencyRule.processCustomSaveDocumentBusinessRules");
115 return success;
116 }
117
118
119
120
121
122 protected boolean validateAgencyType(MaintenanceDocument document) {
123 String agencyType = newAgency.getAgencyTypeCode();
124 Map params = new HashMap();
125 params.put("code", agencyType);
126 Object o = businessObjectService.findByPrimaryKey(AgencyType.class, params);
127 if (null == o) {
128 putFieldError("agencyTypeCode", KFSKeyConstants.ERROR_AGENCY_TYPE_NOT_FOUND, agencyType);
129 return false;
130 }
131 return true;
132 }
133
134
135
136
137
138 protected boolean validateAgencyReportingName(MaintenanceDocument document) {
139 String agencyReportingName = newAgency.getReportingName();
140 String agencyExistsValue = newAgency.getCustomerCreationOptionCode();
141 if (CGConstants.AGENCY_CREATE_NEW_CUSTOMER_CODE.equalsIgnoreCase(agencyExistsValue)) {
142 if (agencyReportingName.length() < 3) {
143 putFieldError("reportingName", CGKeyConstants.AgencyConstants.ERROR_AGENCY_NAME_LESS_THAN_THREE_CHARACTERS);
144 return false;
145 }
146 else if (agencyReportingName.substring(0, 3).contains(" ")) {
147 putFieldError("reportingName", CGKeyConstants.AgencyConstants.ERROR_AGENCY_NAME_NO_SPACES_IN_FIRST_THREE_CHARACTERS);
148 return false;
149 }
150 }
151 return true;
152 }
153
154
155
156
157
158 protected boolean checkAgencyReportsTo(MaintenanceDocument document) {
159 boolean success = true;
160
161 if (newAgency.getReportsToAgencyNumber() != null) {
162 if (newAgency.getReportsToAgency() == null) {
163
164 putFieldError("reportsToAgencyNumber", KFSKeyConstants.ERROR_AGENCY_NOT_FOUND, newAgency.getReportsToAgencyNumber());
165 success = false;
166
167 }
168 else if (!newAgency.getReportsToAgency().isActive()) {
169
170 putFieldError("reportsToAgencyNumber", KFSKeyConstants.ERROR_AGENCY_INACTIVE, newAgency.getReportsToAgencyNumber());
171 success = false;
172
173 }
174 else if (newAgency.getAgencyNumber().equals(newAgency.getReportsToAgencyNumber())) {
175
176 putFieldError("reportsToAgencyNumber", KFSKeyConstants.ERROR_AGENCY_REPORTS_TO_SELF, newAgency.getAgencyNumber());
177 success = false;
178
179 }
180 else {
181
182 List agencies = new ArrayList();
183
184 Agency agency = newAgency;
185
186 while (agency.getReportsToAgency() != null && success) {
187 if (!agencies.contains(agency.getAgencyNumber())) {
188 agencies.add(agency.getAgencyNumber());
189 }
190 else {
191
192 putFieldError("reportsToAgencyNumber", KFSKeyConstants.ERROR_AGENCY_CIRCULAR_REPORTING, agency.getAgencyNumber());
193 success = false;
194 }
195
196 agency = agency.getReportsToAgency();
197 }
198 }
199 }
200 return success;
201 }
202
203
204
205
206 @Override
207 public void setupConvenienceObjects() {
208 newAgency = (Agency) super.getNewBo();
209 oldAgency = (Agency) super.getOldBo();
210 }
211
212
213
214
215
216
217
218
219
220 @Override
221 public boolean processAddCollectionLineBusinessRules(MaintenanceDocument document, String collectionName, PersistableBusinessObject line) {
222
223
224 if (contractsGrantsBillingEnhancementActive) {
225 if (line instanceof AgencyAddress) {
226 AgencyAddress newAgencyAddress = (AgencyAddress) line;
227 if (collectionName.equals(KFSPropertyConstants.AGENCY_ADDRESSES)) {
228 newAgency = (Agency) document.getNewMaintainableObject().getBusinessObject();
229 List<AgencyAddress> agencyAddresses = newAgency.getAgencyAddresses();
230 String tmpCode = newAgencyAddress.getCustomerAddressTypeCode();
231
232
233 for (AgencyAddress agencyAddress : agencyAddresses) {
234 String customerAddressTypeCode = agencyAddress.getCustomerAddressTypeCode();
235 if (customerAddressTypeCode != null && customerAddressTypeCode.equals(CGConstants.AGENCY_PRIMARY_ADDRESSES_TYPE_CODE)) {
236 if (ObjectUtils.isNotNull(tmpCode) && !tmpCode.isEmpty() && tmpCode.equals(CGConstants.AGENCY_PRIMARY_ADDRESSES_TYPE_CODE)) {
237 String elementLabel = SpringContext.getBean(DataDictionaryService.class).getCollectionElementLabel(Agency.class.getName(), collectionName, AgencyAddress.class);
238 putFieldError(collectionName, KFSKeyConstants.ERROR_MULTIPLE_PRIMARY, elementLabel);
239 return false;
240 }
241 else {
242 boolean isValid = checkAddressIsValid(newAgencyAddress);
243 if (!isValid) {
244 return isValid;
245 }
246 }
247 }
248 }
249 }
250 }
251
252 }
253
254 return super.processAddCollectionLineBusinessRules(document, collectionName, line);
255 }
256
257
258
259
260
261
262
263 public boolean checkAddressIsValid(AgencyAddress agencyAddress) {
264 boolean isValid = true;
265
266 if (CGConstants.AGENCY_CREATE_NEW_CUSTOMER_CODE.equalsIgnoreCase(newAgency.getCustomerCreationOptionCode())) {
267 if (CGKeyConstants.AgencyConstants.AGENCY_ADDRESS_TYPE_CODE_US.equalsIgnoreCase(agencyAddress.getAgencyCountryCode())) {
268 if (StringUtils.isBlank(agencyAddress.getAgencyZipCode())) {
269 isValid = false;
270 GlobalVariables.getMessageMap().putError(CGPropertyConstants.AgencyFields.AGENCY_ADDRESS_ZIP_CODE, CGKeyConstants.AgencyConstants.ERROR_AGENCY_ADDRESS_ZIP_CODE_REQUIRED_WHEN_COUNTTRY_US);
271 }
272 if (StringUtils.isBlank(agencyAddress.getAgencyStateCode())) {
273 isValid = false;
274 GlobalVariables.getMessageMap().putError(CGPropertyConstants.AgencyFields.AGENCY_ADDRESS_STATE_CODE, CGKeyConstants.AgencyConstants.ERROR_AGENCY_ADDRESS_STATE_CODE_REQUIRED_WHEN_COUNTTRY_US);
275 }
276 }
277 else {
278 if (StringUtils.isBlank(agencyAddress.getAgencyInternationalMailCode())) {
279 isValid = false;
280 GlobalVariables.getMessageMap().putError(CGPropertyConstants.AgencyFields.AGENCY_ADDRESS_INTERNATIONAL_MAIL_CODE, CGKeyConstants.AgencyConstants.ERROR_AGENCY_ADDRESS_INTERNATIONAL_MAIL_CODE_REQUIRED_WHEN_COUNTTRY_NON_US);
281 }
282 if (StringUtils.isBlank(agencyAddress.getAgencyAddressInternationalProvinceName())) {
283 isValid = false;
284 GlobalVariables.getMessageMap().putError(CGPropertyConstants.AgencyFields.AGENCY_ADDRESS_INTERNATIONAL_PROVINCE_NAME, CGKeyConstants.AgencyConstants.ERROR_AGENCY_ADDRESS_INTERNATIONAL_PROVINCE_NAME_REQUIRED_WHEN_COUNTTRY_NON_US);
285 }
286 }
287 }
288 return isValid;
289 }
290
291
292
293
294
295
296
297
298 public boolean checkAddressIsValid(AgencyAddress agencyAddress, int ind) {
299 boolean isValid = true;
300 String propertyName = CGPropertyConstants.AgencyFields.AGENCY_TAB_ADDRESSES + "[" + ind + "].";
301
302 if (CGConstants.AGENCY_CREATE_NEW_CUSTOMER_CODE.equalsIgnoreCase(newAgency.getCustomerCreationOptionCode())) {
303 if (CGKeyConstants.AgencyConstants.AGENCY_ADDRESS_TYPE_CODE_US.equalsIgnoreCase(agencyAddress.getAgencyCountryCode())) {
304 if (StringUtils.isBlank(agencyAddress.getAgencyZipCode())) {
305 isValid = false;
306 putFieldError(propertyName + CGPropertyConstants.AgencyFields.AGENCY_ADDRESS_ZIP_CODE, CGKeyConstants.AgencyConstants.ERROR_AGENCY_ADDRESS_ZIP_CODE_REQUIRED_WHEN_COUNTTRY_US);
307 }
308 if (StringUtils.isBlank(agencyAddress.getAgencyStateCode())) {
309 isValid = false;
310 putFieldError(propertyName + CGPropertyConstants.AgencyFields.AGENCY_ADDRESS_STATE_CODE, CGKeyConstants.AgencyConstants.ERROR_AGENCY_ADDRESS_STATE_CODE_REQUIRED_WHEN_COUNTTRY_US);
311 }
312 }
313 else {
314 if (StringUtils.isBlank(agencyAddress.getAgencyInternationalMailCode())) {
315 isValid = false;
316 putFieldError(propertyName + CGPropertyConstants.AgencyFields.AGENCY_ADDRESS_INTERNATIONAL_MAIL_CODE, CGKeyConstants.AgencyConstants.ERROR_AGENCY_ADDRESS_INTERNATIONAL_MAIL_CODE_REQUIRED_WHEN_COUNTTRY_NON_US);
317 }
318 if (StringUtils.isBlank(agencyAddress.getAgencyAddressInternationalProvinceName())) {
319 isValid = false;
320 putFieldError(propertyName + CGPropertyConstants.AgencyFields.AGENCY_ADDRESS_INTERNATIONAL_PROVINCE_NAME, CGKeyConstants.AgencyConstants.ERROR_AGENCY_ADDRESS_INTERNATIONAL_PROVINCE_NAME_REQUIRED_WHEN_COUNTTRY_NON_US);
321 }
322 }
323 }
324 return isValid;
325 }
326
327
328
329
330
331
332
333
334 public boolean validateAddresses(Agency agency) {
335 boolean isValid = true;
336 int i = 0;
337
338 for (AgencyAddress agencyAddress : agency.getAgencyAddresses()) {
339 isValid &= checkAddressIsValid(agencyAddress, i);
340 i++;
341 }
342
343
344 return isValid;
345 }
346
347
348
349
350
351
352
353 public boolean validateCustomerType(Agency agency) {
354 boolean isValid = true;
355
356
357 if ( StringUtils.equalsIgnoreCase(CGConstants.AGENCY_CREATE_NEW_CUSTOMER_CODE, agency.getCustomerCreationOptionCode()) ){
358
359 if( StringUtils.isEmpty(agency.getCustomerTypeCode()) ){
360 putFieldError(CGPropertyConstants.AgencyFields.AGENCY_CUSTOMER_TYPE_CODE, CGKeyConstants.AgencyConstants.ERROR_AGENCY_CUSTOMER_TYPE_CODE_REQUIRED_WHEN_AGENCY_CUSTOMER_NEW);
361 isValid &= false;
362 }
363 } else if (StringUtils.equalsIgnoreCase(CGConstants.AGENCY_USE_EXISTING_CUSTOMER_CODE, agency.getCustomerCreationOptionCode())) {
364 if (StringUtils.isBlank(agency.getCustomerNumber())) {
365 putFieldError(CGPropertyConstants.AgencyFields.AGENCY_CUSTOMER_NUMBER, CGKeyConstants.AgencyConstants.ERROR_AGECNY_CUSTOMER_NUMBER_REQUIRED_WHEN_AGENCY_CUSTOMER_EXISTING);
366 isValid = false;
367 } else {
368 final AccountsReceivableCustomer customer = agency.getCustomer();
369 if (ObjectUtils.isNull(customer) || !customer.isActive()) {
370 putFieldError(CGPropertyConstants.AgencyFields.AGENCY_CUSTOMER_NUMBER, CGKeyConstants.AgencyConstants.ERROR_AGENCY_ACTUAL_CUSTOMER_REQUIRED_WHEN_AGENCY_CUSTOMER_EXISTING, new String[] { agency.getCustomerNumber() });
371 isValid = false;
372 }
373 }
374 }
375
376 return isValid;
377 }
378 }