001: /*
002: * Copyright 2007 The Kuali Foundation.
003: *
004: * Licensed under the Educational Community License, Version 1.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.opensource.org/licenses/ecl1.php
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.kuali.module.pdp.rules;
017:
018: import java.util.HashMap;
019: import java.util.List;
020: import java.util.Map;
021:
022: import org.kuali.core.document.MaintenanceDocument;
023: import org.kuali.core.maintenance.rules.MaintenanceDocumentRuleBase;
024: import org.kuali.core.service.BusinessObjectService;
025: import org.kuali.kfs.KFSKeyConstants;
026: import org.kuali.kfs.context.SpringContext;
027: import org.kuali.module.pdp.bo.PayeeAchAccount;
028:
029: /*
030: * import java.util.Iterator; import java.util.List; import org.kuali.core.maintenance.rules.MaintenanceDocumentRuleBase; import
031: * org.kuali.core.service.KeyValuesService; import org.kuali.kfs.bo.Options; import org.kuali.kfs.context.SpringContext; import
032: * org.kuali.module.chart.rules.AccountingPeriodRule;
033: */
034:
035: public class PayeeAchAccountRule extends MaintenanceDocumentRuleBase {
036:
037: protected static org.apache.log4j.Logger LOG = org.apache.log4j.Logger
038: .getLogger(PayeeAchAccount.class);
039:
040: private PayeeAchAccount oldPayeeAchAccount;
041: private PayeeAchAccount newPayeeAchAccount;
042:
043: /**
044: * This method sets the convenience objects like newAccount and oldAccount, so you have short and easy handles to the new and
045: * old objects contained in the maintenance document. It also calls the BusinessObjectBase.refresh(), which will attempt to load
046: * all sub-objects from the DB by their primary keys, if available.
047: *
048: * @param document - the maintenanceDocument being evaluated
049: */
050: public void setupConvenienceObjects() {
051:
052: LOG.info("setupConvenienceObjects called");
053:
054: // setup oldPayeeAchAccount convenience objects, make sure all possible sub-objects are populated
055: oldPayeeAchAccount = (PayeeAchAccount) super .getOldBo();
056:
057: // setup newPayeeAchAccount convenience objects, make sure all possible sub-objects are populated
058: newPayeeAchAccount = (PayeeAchAccount) super .getNewBo();
059: }
060:
061: protected boolean processCustomSaveDocumentBusinessRules(
062: MaintenanceDocument document) {
063:
064: LOG.info("processCustomSaveDocumentBusinessRules called");
065:
066: // call the route rules to report all of the messages, but ignore the result
067: processCustomRouteDocumentBusinessRules(document);
068:
069: // Save always succeeds, even if there are business rule failures
070: return true;
071: }
072:
073: protected boolean processCustomRouteDocumentBusinessRules(
074: MaintenanceDocument document) {
075:
076: boolean validEntry = true;
077: String payeeIdTypeCd, payeeUserId, feinNumber, dvPayeeId, ssn;
078: Integer vendorGnrtdId, vendorAsndId;
079:
080: LOG.info("processCustomRouteDocumentBusinessRules called");
081: setupConvenienceObjects();
082:
083: payeeIdTypeCd = newPayeeAchAccount.getPayeeIdentifierTypeCode();
084:
085: if (payeeIdTypeCd == null)
086: return validEntry;
087:
088: // Create a query to do a lookup on.
089: Map<String, Object> criteria = new HashMap<String, Object>();
090: String identifierField = "";
091:
092: if (payeeIdTypeCd.equals("E")) {
093: identifierField = "personUniversalIdentifier";
094: payeeUserId = newPayeeAchAccount
095: .getPersonUniversalIdentifier();
096: if (payeeUserId == null) {
097: putFieldError("personUniversalIdentifier",
098: KFSKeyConstants.ERROR_REQUIRED,
099: getFieldLabel("personUniversalIdentifier"));
100: validEntry = false;
101: } else
102: criteria.put("personUniversalIdentifier", payeeUserId);
103: } else if (payeeIdTypeCd.equals("V")) {
104: identifierField = "vendorHeaderGeneratedIdentifier";
105: vendorGnrtdId = newPayeeAchAccount
106: .getVendorHeaderGeneratedIdentifier();
107: vendorAsndId = newPayeeAchAccount
108: .getVendorDetailAssignedIdentifier();
109: if ((vendorGnrtdId == null) || (vendorAsndId == null)) {
110: putFieldError(
111: "vendorHeaderGeneratedIdentifier",
112: KFSKeyConstants.ERROR_REQUIRED,
113: getFieldLabel("vendorHeaderGeneratedIdentifier"));
114: validEntry = false;
115: }
116: if (validEntry) {
117: criteria.put("vendorHeaderGeneratedIdentifier",
118: vendorGnrtdId);
119: criteria.put("vendorDetailAssignedIdentifier",
120: vendorAsndId);
121: }
122: } else if (payeeIdTypeCd.equals("F")) {
123: identifierField = "payeeFederalEmployerIdentificationNumber";
124: feinNumber = newPayeeAchAccount
125: .getPayeeFederalEmployerIdentificationNumber();
126: if (feinNumber == null) {
127: putFieldError(
128: "payeeFederalEmployerIdentificationNumber",
129: KFSKeyConstants.ERROR_REQUIRED,
130: getFieldLabel("payeeFederalEmployerIdentificationNumber"));
131: validEntry = false;
132: } else
133: criteria.put(
134: "payeeFederalEmployerIdentificationNumber",
135: feinNumber);
136: } else if (payeeIdTypeCd.equals("S")) {
137: identifierField = "payeeSocialSecurityNumber";
138: ssn = newPayeeAchAccount.getPayeeSocialSecurityNumber();
139: if (ssn == null) {
140: putFieldError("payeeSocialSecurityNumber",
141: KFSKeyConstants.ERROR_REQUIRED,
142: getFieldLabel("payeeSocialSecurityNumber"));
143: validEntry = false;
144: } else
145: criteria.put("payeeSocialSecurityNumber", ssn);
146: } else if (payeeIdTypeCd.equals("P")) {
147: identifierField = "disbVchrPayeeIdNumber";
148: dvPayeeId = newPayeeAchAccount.getDisbVchrPayeeIdNumber();
149: if (dvPayeeId == null) {
150: putFieldError("disbVchrPayeeIdNumber",
151: KFSKeyConstants.ERROR_REQUIRED,
152: getFieldLabel("disbVchrPayeeIdNumber"));
153: validEntry = false;
154: } else
155: criteria.put("disbVchrPayeeIdNumber", dvPayeeId);
156: }
157: if (validEntry)
158: validEntry &= checkForDuplicateRecord(criteria,
159: identifierField);
160:
161: return validEntry;
162: }
163:
164: private boolean checkForDuplicateRecord(
165: Map<String, Object> criteria, String identifierField) {
166:
167: String newPayeeIdTypeCd = newPayeeAchAccount
168: .getPayeeIdentifierTypeCode();
169: String newPsdTransactionCd = newPayeeAchAccount
170: .getPsdTransactionCode();
171: boolean valid = true;
172:
173: // Do not check for a duplicate record if the following conditions are true
174: // 1. editing an existing record (old primary key = new primary key)
175: // 2. new PSD code = old PSD code
176: // 3. new payee type code = old payee type code
177: // 4. depending of the value of payee type code, new correspoding PayeeId = old corresponding PayeeId
178:
179: if (newPayeeAchAccount.getAchAccountGeneratedIdentifier() != null
180: && oldPayeeAchAccount
181: .getAchAccountGeneratedIdentifier() != null
182: && newPayeeAchAccount
183: .getAchAccountGeneratedIdentifier()
184: .equals(
185: oldPayeeAchAccount
186: .getAchAccountGeneratedIdentifier())) {
187: if (newPayeeIdTypeCd.equals(oldPayeeAchAccount
188: .getPayeeIdentifierTypeCode())
189: && newPsdTransactionCd.equals(oldPayeeAchAccount
190: .getPsdTransactionCode())) {
191: if (newPayeeIdTypeCd.equals("E")) {
192: if (newPayeeAchAccount
193: .getPersonUniversalIdentifier()
194: .equals(
195: oldPayeeAchAccount
196: .getPersonUniversalIdentifier()))
197: return valid;
198: } else if (newPayeeIdTypeCd.equals("V")) {
199: if (newPayeeAchAccount
200: .getVendorHeaderGeneratedIdentifier()
201: .equals(
202: oldPayeeAchAccount
203: .getVendorHeaderGeneratedIdentifier())
204: && newPayeeAchAccount
205: .getVendorDetailAssignedIdentifier()
206: .equals(
207: oldPayeeAchAccount
208: .getVendorDetailAssignedIdentifier()))
209: return valid;
210: } else if (newPayeeIdTypeCd.equals("F")) {
211: if (newPayeeAchAccount
212: .getPayeeFederalEmployerIdentificationNumber()
213: .equals(
214: oldPayeeAchAccount
215: .getPayeeFederalEmployerIdentificationNumber()))
216: return valid;
217: } else if (newPayeeIdTypeCd.equals("S")) {
218: if (newPayeeAchAccount
219: .getPayeeSocialSecurityNumber()
220: .equals(
221: oldPayeeAchAccount
222: .getPayeeSocialSecurityNumber()))
223: return valid;
224: } else if (newPayeeIdTypeCd.equals("P")) {
225: if (newPayeeAchAccount
226: .getDisbVchrPayeeIdNumber()
227: .equals(
228: oldPayeeAchAccount
229: .getDisbVchrPayeeIdNumber()))
230: return valid;
231: }
232: }
233: }
234:
235: // check for a duplicate record if creating a new record or editing an old one and above mentioned conditions are not true
236: criteria.put("psdTransactionCode", newPsdTransactionCd);
237: criteria.put("payeeIdentifierTypeCode", newPayeeIdTypeCd);
238:
239: int matches = SpringContext
240: .getBean(BusinessObjectService.class).countMatching(
241: PayeeAchAccount.class, criteria);
242: if (matches > 0) {
243: putFieldError(
244: identifierField,
245: KFSKeyConstants.ERROR_DOCUMENT_PAYEEACHACCOUNTMAINT_DUPLICATE_RECORD);
246: valid = false;
247: }
248: return valid;
249: }
250:
251: }
|