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.vendor.rules;
017:
018: import java.util.ArrayList;
019: import java.util.List;
020:
021: import org.apache.commons.lang.StringUtils;
022: import org.kuali.core.document.Document;
023: import org.kuali.core.document.MaintenanceDocument;
024: import org.kuali.core.service.BusinessObjectService;
025: import org.kuali.core.service.DateTimeService;
026: import org.kuali.core.util.GlobalVariables;
027: import org.kuali.core.util.ObjectUtils;
028: import org.kuali.kfs.KFSConstants;
029: import org.kuali.kfs.context.SpringContext;
030: import org.kuali.module.chart.rules.MaintenancePreRulesBase;
031: import org.kuali.module.vendor.VendorConstants;
032: import org.kuali.module.vendor.bo.VendorDetail;
033: import org.kuali.module.vendor.bo.VendorHeader;
034: import org.kuali.module.vendor.bo.VendorTaxChange;
035: import org.kuali.module.vendor.bo.VendorType;
036:
037: /**
038: * Business Prerules applicable to VendorDetail documents. These PreRules checks for the VendorDetail that needs to occur while
039: * still in the Struts processing. This includes setting the vendorName field using the values from vendorLastName and
040: * vendorFirstName, and could be used for many other purposes.
041: */
042: public class VendorPreRules extends MaintenancePreRulesBase {
043:
044: protected static org.apache.log4j.Logger LOG = org.apache.log4j.Logger
045: .getLogger(VendorPreRules.class);
046:
047: private VendorDetail newVendorDetail;
048: private VendorDetail copyVendorDetail;
049: private String universalUserId;
050:
051: public VendorPreRules() {
052: }
053:
054: /**
055: * Returns the Universal User Id of the current logged-in user
056: *
057: * @return String the UniversalUserId
058: */
059:
060: public String getUniversalUserId() {
061: if (ObjectUtils.isNull(universalUserId)) {
062: this .universalUserId = GlobalVariables.getUserSession()
063: .getUniversalUser().getPersonUniversalIdentifier();
064: }
065: return this .universalUserId;
066: }
067:
068: /**
069: * Sets up a convenience object and few other vendor attributes
070: *
071: * @see org.kuali.module.chart.rules.MaintenancePreRulesBase#doCustomPreRules(org.kuali.core.document.MaintenanceDocument)
072: */
073: @Override
074: protected boolean doCustomPreRules(MaintenanceDocument document) {
075: setupConvenienceObjects(document);
076: setVendorNamesAndIndicator(document);
077: setVendorRestriction(document);
078: setVendorTaxChange(document);
079: displayReview(document);
080: return true;
081: }
082:
083: /**
084: * Sets the convenience objects like newVendorDetail and oldVendorDetail, so you have short and easy handles to the new and old
085: * objects contained in the maintenance document. It also calls the BusinessObjectBase.refresh(), which will attempt to load all
086: * sub-objects from the DB by their primary keys, if available.
087: *
088: * @param document - the maintenanceDocument being evaluated
089: */
090: private void setupConvenienceObjects(MaintenanceDocument document) {
091: // setup newAccount convenience objects, make sure all possible sub-objects are populated
092: newVendorDetail = (VendorDetail) document
093: .getNewMaintainableObject().getBusinessObject();
094: copyVendorDetail = (VendorDetail) ObjectUtils
095: .deepCopy(newVendorDetail);
096: copyVendorDetail.refresh();
097: }
098:
099: /**
100: * Sets the vendorFirstLastNameIndicator to true if the first name and last name fields were filled in but the vendorName field
101: * is blank and it sets the vendorFirstLastNameIndicator to false if the vendorName field is filled in and the first name and
102: * last name fields were both blank.
103: *
104: * @param document - the maintenanceDocument being evaluated
105: */
106: private void setVendorNamesAndIndicator(MaintenanceDocument document) {
107: if (StringUtils.isBlank(copyVendorDetail.getVendorName())
108: && !StringUtils.isBlank(copyVendorDetail
109: .getVendorFirstName())
110: && !StringUtils.isBlank(copyVendorDetail
111: .getVendorLastName())) {
112:
113: newVendorDetail.setVendorFirstLastNameIndicator(true);
114: newVendorDetail
115: .setVendorFirstName(removeDelimiter(newVendorDetail
116: .getVendorFirstName()));
117: newVendorDetail
118: .setVendorLastName(removeDelimiter(newVendorDetail
119: .getVendorLastName()));
120:
121: } else if (!StringUtils.isBlank(copyVendorDetail
122: .getVendorName())
123: && StringUtils.isBlank(copyVendorDetail
124: .getVendorFirstName())
125: && StringUtils.isBlank(copyVendorDetail
126: .getVendorLastName())) {
127: newVendorDetail.setVendorFirstLastNameIndicator(false);
128: }
129: }
130:
131: /**
132: * Sets the vendorRestrictedDate and vendorRestrictedPersonIdentifier if the vendor restriction has changed from No to Yes.
133: *
134: * @param document - the maintenanceDocument being evaluated
135: */
136: private void setVendorRestriction(MaintenanceDocument document) {
137: VendorDetail oldVendorDetail = (VendorDetail) document
138: .getOldMaintainableObject().getBusinessObject();
139: Boolean oldVendorRestrictedIndicator = null;
140: if (ObjectUtils.isNotNull(oldVendorDetail)) {
141: oldVendorRestrictedIndicator = oldVendorDetail
142: .getVendorRestrictedIndicator();
143: }
144: // If the Vendor Restricted Indicator will change, change the date and person id appropriately.
145: if ((ObjectUtils.isNull(oldVendorRestrictedIndicator) || (!oldVendorRestrictedIndicator))
146: && ObjectUtils.isNotNull(newVendorDetail
147: .getVendorRestrictedIndicator())
148: && newVendorDetail.getVendorRestrictedIndicator()) {
149: // Indicator changed from (null or false) to true.
150: newVendorDetail
151: .setVendorRestrictedDate(SpringContext.getBean(
152: DateTimeService.class).getCurrentSqlDate());
153: newVendorDetail
154: .setVendorRestrictedPersonIdentifier(getUniversalUserId());
155: } else if (ObjectUtils.isNotNull(oldVendorRestrictedIndicator)
156: && oldVendorRestrictedIndicator
157: && ObjectUtils.isNotNull(newVendorDetail
158: .getVendorRestrictedIndicator())
159: && (!newVendorDetail.getVendorRestrictedIndicator())) {
160: // Indicator changed from true to false.
161: newVendorDetail.setVendorRestrictedDate(null);
162: newVendorDetail.setVendorRestrictedPersonIdentifier(null);
163: }
164:
165: }
166:
167: /**
168: * Creates the VendorTaxChange if there are any changes related to either the tax number or the tax type code, set the tax
169: * change to the list of changes and set the list to the new vendor detail
170: *
171: * @param document - the maintenanceDocument being evaluated
172: */
173: private void setVendorTaxChange(MaintenanceDocument document) {
174: VendorDetail oldVendorDetail = (VendorDetail) document
175: .getOldMaintainableObject().getBusinessObject();
176: VendorHeader newVendorHeader = newVendorDetail
177: .getVendorHeader();
178: // If this is a pre-existing parent vendor, and if the Tax Number or the Tax Type Code will change,
179: // log the change in the Tax Change table.
180: if (newVendorDetail.isVendorParentIndicator()) {
181: VendorHeader oldVendorHeader = oldVendorDetail
182: .getVendorHeader();
183:
184: if (ObjectUtils.isNotNull(oldVendorHeader)) { // Does not apply if this is a new parent vendor.
185: String oldVendorTaxNumber = oldVendorHeader
186: .getVendorTaxNumber();
187: String oldVendorTaxTypeCode = oldVendorHeader
188: .getVendorTaxTypeCode();
189:
190: String vendorTaxNumber = newVendorHeader
191: .getVendorTaxNumber();
192: String vendorTaxTypeCode = newVendorHeader
193: .getVendorTaxTypeCode();
194:
195: if ((!StringUtils.equals(vendorTaxNumber,
196: oldVendorTaxNumber))
197: || (!StringUtils.equals(vendorTaxTypeCode,
198: oldVendorTaxTypeCode))) {
199: VendorTaxChange taxChange = new VendorTaxChange(
200: newVendorDetail
201: .getVendorHeaderGeneratedIdentifier(),
202: SpringContext
203: .getBean(DateTimeService.class)
204: .getCurrentSqlDate(),
205: oldVendorTaxNumber, oldVendorTaxTypeCode,
206: getUniversalUserId());
207: List<VendorTaxChange> changes = newVendorHeader
208: .getVendorTaxChanges();
209: if (ObjectUtils.isNull(changes)) {
210: changes = new ArrayList();
211: }
212: changes.add(taxChange);
213: newVendorHeader.setVendorTaxChanges(changes);
214: }
215: }
216: }
217: }
218:
219: /**
220: * This is a helper method to remove all the delimiters from the vendor name
221: *
222: * @param str the original vendorName
223: * @return result String the vendorName after the delimiters have been removed
224: */
225: private String removeDelimiter(String str) {
226: String result = str.replaceAll(VendorConstants.NAME_DELIM,
227: KFSConstants.BLANK_SPACE);
228: return result;
229: }
230:
231: /**
232: * Displays a review if indicated by the vendor type and the associated text from that type
233: *
234: * @param document - vendordetail document
235: */
236: public void displayReview(Document document) {
237: VendorDetail vendorDetail = (VendorDetail) document
238: .getDocumentBusinessObject();
239:
240: VendorType vendorType = vendorDetail.getVendorHeader()
241: .getVendorType();
242:
243: if (vendorType == null) {
244: vendorType = new VendorType();
245: vendorType.setVendorTypeCode(vendorDetail.getVendorHeader()
246: .getVendorTypeCode());
247: vendorType = (VendorType) SpringContext.getBean(
248: BusinessObjectService.class).retrieve(vendorType);
249: }
250: if (vendorType != null
251: && vendorType.isVendorShowReviewIndicator()) {
252: String questionText = vendorType.getVendorReviewText();
253: if (vendorDetail.getVendorName() != null) {
254: questionText = questionText.replace("{0}", vendorDetail
255: .getVendorName());
256: } else {
257: questionText = questionText.replace("{0}",
258: "(not entered)");
259: }
260: questionText = questionText.replace("{1}", document
261: .getDocumentNumber());
262: Boolean proceed = super.askOrAnalyzeYesNoQuestion(
263: VendorConstants.ACKNOWLEDGE_NEW_VENDOR_INFO,
264: questionText);
265:
266: if (!proceed) {
267: abortRulesCheck();
268: }
269: }
270: }
271:
272: }
|