001: /*
002: * Copyright 2006-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.chart.rules;
017:
018: import java.util.HashMap;
019:
020: import org.apache.commons.lang.StringUtils;
021: import org.kuali.core.document.MaintenanceDocument;
022: import org.kuali.core.service.BusinessObjectService;
023: import org.kuali.core.service.DateTimeService;
024: import org.kuali.core.util.ObjectUtils;
025: import org.kuali.kfs.bo.PostalZipCode;
026: import org.kuali.kfs.context.SpringContext;
027: import org.kuali.module.chart.bo.Account;
028: import org.kuali.module.chart.bo.Org;
029: import org.kuali.module.chart.bo.OrganizationExtension;
030:
031: /**
032: * PreRules checks for the {@link Org} that needs to occur while still in the Struts processing. This includes defaults, confirmations,
033: * etc.
034: */
035: public class OrgPreRules extends MaintenancePreRulesBase {
036: private Org newOrg;
037: private Org copyOrg;
038:
039: public OrgPreRules() {
040:
041: }
042:
043: /**
044: * This checks to see if a continuation account is necessary and if the HRMS data has changed
045: *
046: * @see org.kuali.module.chart.rules.MaintenancePreRulesBase#doCustomPreRules(org.kuali.core.document.MaintenanceDocument)
047: */
048: @Override
049: protected boolean doCustomPreRules(MaintenanceDocument document) {
050: setupConvenienceObjects(document);
051: checkForContinuationAccounts(); // run this first to avoid side effects
052:
053: LOG
054: .debug("done with continuation account, proceeeding with remaining pre rules");
055:
056: updateHRMSUpdateDate((Org) document.getOldMaintainableObject()
057: .getBusinessObject(), (Org) document
058: .getNewMaintainableObject().getBusinessObject());
059:
060: return true;
061: }
062:
063: /**
064: *
065: * This looks for the org default account number and then sets the values to the continuation account value if it exists
066: */
067: private void checkForContinuationAccounts() {
068: LOG.debug("entering checkForContinuationAccounts()");
069:
070: if (StringUtils.isNotBlank(newOrg
071: .getOrganizationDefaultAccountNumber())) {
072: Account account = checkForContinuationAccount(
073: "Account Number", newOrg.getChartOfAccountsCode(),
074: newOrg.getOrganizationDefaultAccountNumber(), "");
075: if (ObjectUtils.isNotNull(account)) { // override old user inputs
076: newOrg.setOrganizationDefaultAccountNumber(account
077: .getAccountNumber());
078: newOrg.setChartOfAccountsCode(account
079: .getChartOfAccountsCode());
080: }
081: }
082: }
083:
084: /**
085: *
086: * This method sets the convenience objects like newOrg and copyOrg, so you have short and easy handles to the new and
087: * old objects contained in the maintenance document. It also calls the BusinessObjectBase.refresh(), which will attempt to load
088: * all sub-objects from the DB by their primary keys, if available.
089: * @param document
090: */
091: private void setupConvenienceObjects(MaintenanceDocument document) {
092:
093: // setup newOrg convenience objects, make sure all possible sub-objects are populated
094: newOrg = (Org) document.getNewMaintainableObject()
095: .getBusinessObject();
096: copyOrg = (Org) ObjectUtils.deepCopy(newOrg);
097: copyOrg.refresh();
098: }
099:
100: /**
101: * Check if the HRMS data has changed on this document. If so, update the last update date.
102: *
103: * @param oldData
104: * @param newData
105: */
106: private void updateHRMSUpdateDate(Org oldData, Org newData) {
107: if (oldData != null) {
108: OrganizationExtension oldExt = oldData
109: .getOrganizationExtension();
110: OrganizationExtension newExt = newData
111: .getOrganizationExtension();
112: if (oldExt != null) {
113: if (!ObjectUtils.nullSafeEquals(
114: oldExt.getHrmsCompany(), newExt
115: .getHrmsCompany())
116: || !ObjectUtils.nullSafeEquals(oldExt
117: .getHrmsIuOrganizationAddress2(),
118: newExt.getHrmsIuOrganizationAddress2())
119: || !ObjectUtils.nullSafeEquals(oldExt
120: .getHrmsIuOrganizationAddress3(),
121: newExt.getHrmsIuOrganizationAddress3())
122: || !ObjectUtils.nullSafeEquals(oldExt
123: .getHrmsIuCampusCode(), newExt
124: .getHrmsIuCampusCode())
125: || !ObjectUtils.nullSafeEquals(oldExt
126: .getHrmsIuCampusBuilding(), newExt
127: .getHrmsIuCampusBuilding())
128: || !ObjectUtils.nullSafeEquals(oldExt
129: .getHrmsIuCampusRoom(), newExt
130: .getHrmsIuCampusRoom())
131: || oldExt.isHrmsIuPositionAllowedFlag() != newExt
132: .isHrmsIuPositionAllowedFlag()
133: || oldExt.isHrmsIuTenureAllowedFlag() != newExt
134: .isHrmsIuTenureAllowedFlag()
135: || oldExt.isHrmsIuTitleAllowedFlag() != newExt
136: .isHrmsIuTitleAllowedFlag()
137: || oldExt.isHrmsIuOccupationalUnitAllowedFlag() != newExt
138: .isHrmsIuOccupationalUnitAllowedFlag()
139: || !ObjectUtils
140: .nullSafeEquals(
141: oldExt
142: .getHrmsPersonnelApproverUniversalId(),
143: newExt
144: .getHrmsPersonnelApproverUniversalId())
145: || !ObjectUtils.nullSafeEquals(oldExt
146: .getFiscalApproverUniversalId(), newExt
147: .getFiscalApproverUniversalId())) {
148: newExt.setHrmsLastUpdateDate(SpringContext.getBean(
149: DateTimeService.class)
150: .getCurrentTimestamp());
151: }
152: } else {
153: newExt.setHrmsLastUpdateDate(SpringContext.getBean(
154: DateTimeService.class).getCurrentTimestamp());
155: }
156: } else {
157: newData.getOrganizationExtension().setHrmsLastUpdateDate(
158: SpringContext.getBean(DateTimeService.class)
159: .getCurrentTimestamp());
160: }
161: }
162:
163: /**
164: *
165: * This takes the org zip code and fills in state, city and country code based off of it
166: * @param maintenanceDocument
167: */
168: private void setLocationFromZip(
169: MaintenanceDocument maintenanceDocument) {
170:
171: // organizationStateCode , organizationCityName are populated by looking up
172: // the zip code and getting the state and city from that
173: if (!StringUtils.isBlank(copyOrg.getOrganizationZipCode())) {
174:
175: HashMap primaryKeys = new HashMap();
176: primaryKeys.put("postalZipCode", copyOrg
177: .getOrganizationZipCode());
178: PostalZipCode zip = (PostalZipCode) SpringContext.getBean(
179: BusinessObjectService.class).findByPrimaryKey(
180: PostalZipCode.class, primaryKeys);
181:
182: // If user enters a valid zip code, override city name and state code entered by user
183: if (ObjectUtils.isNotNull(zip)) { // override old user inputs
184: newOrg.setOrganizationCityName(zip.getPostalCityName());
185: newOrg.setOrganizationStateCode(zip
186: .getPostalStateCode());
187: newOrg.setOrganizationCountryCode("US");// no way to look up
188: }
189: }
190: }
191:
192: }
|