001    /*
002     * Copyright 2011 The Kuali Foundation.
003     * 
004     * Licensed under the Educational Community License, Version 2.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/ecl2.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.kfs.coa.document.validation.impl;
017    
018    import org.apache.commons.lang.StringUtils;
019    import org.kuali.kfs.coa.businessobject.Account;
020    import org.kuali.kfs.coa.businessobject.Organization;
021    import org.kuali.kfs.coa.businessobject.OrganizationExtension;
022    import org.kuali.rice.kns.bo.PostalCode;
023    import org.kuali.kfs.sys.context.SpringContext;
024    import org.kuali.rice.kns.service.PostalCodeService;
025    import org.kuali.rice.kns.document.MaintenanceDocument;
026    import org.kuali.rice.kns.service.DateTimeService;
027    import org.kuali.rice.kns.util.ObjectUtils;
028    
029    /**
030     * PreRules checks for the {@link Org} that needs to occur while still in the Struts processing. This includes defaults,
031     * confirmations, etc.
032     */
033    public class OrgPreRules extends MaintenancePreRulesBase {
034        protected Organization newOrg;
035        protected PostalCodeService postalZipCodeService = SpringContext.getBean(PostalCodeService.class);
036    
037        public OrgPreRules() {
038    
039        }
040    
041        /**
042         * This checks to see if a continuation account is necessary and if the HRMS data has changed
043         * 
044         * @see org.kuali.kfs.coa.document.validation.impl.MaintenancePreRulesBase#doCustomPreRules(org.kuali.rice.kns.document.MaintenanceDocument)
045         */
046        @Override
047        protected boolean doCustomPreRules(MaintenanceDocument document) {
048            setupConvenienceObjects(document);
049            checkForContinuationAccounts(); // run this first to avoid side effects
050    
051            LOG.debug("done with continuation account, proceeeding with remaining pre rules");
052    
053            updateHRMSUpdateDate((Organization) document.getOldMaintainableObject().getBusinessObject(), (Organization) document.getNewMaintainableObject().getBusinessObject());
054    
055            return true;
056        }
057    
058        /**
059         * This looks for the org default account number and then sets the values to the continuation account value if it exists
060         */
061        protected void checkForContinuationAccounts() {
062            LOG.debug("entering checkForContinuationAccounts()");
063    
064            if (StringUtils.isNotBlank(newOrg.getOrganizationDefaultAccountNumber())) {
065                Account account = checkForContinuationAccount("Account Number", newOrg.getChartOfAccountsCode(), newOrg.getOrganizationDefaultAccountNumber(), "");
066                if (ObjectUtils.isNotNull(account)) { // override old user inputs
067                    newOrg.setOrganizationDefaultAccountNumber(account.getAccountNumber());
068                    newOrg.setChartOfAccountsCode(account.getChartOfAccountsCode());
069                }
070            }
071        }
072    
073        /**
074         * This method sets the convenience objects like newOrg and copyOrg, so you have short and easy handles to the new and old
075         * objects contained in the maintenance document. It also calls the BusinessObjectBase.refresh(), which will attempt to load all
076         * sub-objects from the DB by their primary keys, if available.
077         * 
078         * @param document
079         */
080        protected void setupConvenienceObjects(MaintenanceDocument document) {
081    
082            // setup newOrg convenience objects, make sure all possible sub-objects are populated
083            newOrg = (Organization) document.getNewMaintainableObject().getBusinessObject();
084        }
085    
086        /**
087         * Check if the HRMS data has changed on this document. If so, update the last update date.
088         * 
089         * @param oldData
090         * @param newData
091         */
092        protected void updateHRMSUpdateDate(Organization oldData, Organization newData) {
093            if (oldData != null) {
094                OrganizationExtension oldExt = oldData.getOrganizationExtension();
095                OrganizationExtension newExt = newData.getOrganizationExtension();
096                if (oldExt != null) {
097                    if (!ObjectUtils.nullSafeEquals(oldExt.getHrmsCompany(), newExt.getHrmsCompany()) || !ObjectUtils.nullSafeEquals(oldExt.getHrmsIuOrganizationAddress2(), newExt.getHrmsIuOrganizationAddress2()) || !ObjectUtils.nullSafeEquals(oldExt.getHrmsIuOrganizationAddress3(), newExt.getHrmsIuOrganizationAddress3()) || !ObjectUtils.nullSafeEquals(oldExt.getHrmsIuCampusCode(), newExt.getHrmsIuCampusCode()) || !ObjectUtils.nullSafeEquals(oldExt.getHrmsIuCampusBuilding(), newExt.getHrmsIuCampusBuilding()) || !ObjectUtils.nullSafeEquals(oldExt.getHrmsIuCampusRoom(), newExt.getHrmsIuCampusRoom()) || oldExt.isHrmsIuPositionAllowedFlag() != newExt.isHrmsIuPositionAllowedFlag() || oldExt.isHrmsIuTenureAllowedFlag() != newExt.isHrmsIuTenureAllowedFlag() || oldExt.isHrmsIuTitleAllowedFlag() != newExt.isHrmsIuTitleAllowedFlag() || oldExt.isHrmsIuOccupationalUnitAllowedFlag() != newExt.isHrmsIuOccupationalUnitAllowedFlag()
098                            || !ObjectUtils.nullSafeEquals(oldExt.getHrmsPersonnelApproverUniversalId(), newExt.getHrmsPersonnelApproverUniversalId()) || !ObjectUtils.nullSafeEquals(oldExt.getFiscalApproverUniversalId(), newExt.getFiscalApproverUniversalId())) {
099                        newExt.setHrmsLastUpdateDate(SpringContext.getBean(DateTimeService.class).getCurrentTimestamp());
100                    }
101                }
102                else {
103                    newExt.setHrmsLastUpdateDate(SpringContext.getBean(DateTimeService.class).getCurrentTimestamp());
104                }
105            }
106            else {
107                newData.getOrganizationExtension().setHrmsLastUpdateDate(SpringContext.getBean(DateTimeService.class).getCurrentTimestamp());
108            }
109        }
110    
111        /**
112         * This takes the org zip code and fills in state, city and country code based off of it
113         * 
114         * @param maintenanceDocument
115         */
116        protected void setLocationFromZip(MaintenanceDocument maintenanceDocument) {
117    
118            // organizationStateCode , organizationCityName are populated by looking up
119            // the zip code and getting the state and city from that
120            if (!StringUtils.isBlank(newOrg.getOrganizationZipCode()) && !StringUtils.isBlank(newOrg.getOrganizationCountryCode())) {
121                PostalCode zip = postalZipCodeService.getByPrimaryId(newOrg.getOrganizationCountryCode(), newOrg.getOrganizationZipCode());
122    
123                // If user enters a valid zip code, override city name and state code entered by user
124                if (ObjectUtils.isNotNull(zip)) { // override old user inputs
125                    newOrg.setOrganizationCityName(zip.getPostalCityName());
126                    newOrg.setOrganizationStateCode(zip.getPostalStateCode());
127                }
128            }
129        }
130    
131    
132    }