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