View Javadoc
1   /*
2    * Copyright 2006 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.ole.coa.document.validation.impl;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.kuali.ole.coa.businessobject.Account;
20  import org.kuali.ole.coa.businessobject.Organization;
21  import org.kuali.ole.coa.businessobject.OrganizationExtension;
22  import org.kuali.ole.sys.context.SpringContext;
23  import org.kuali.rice.core.api.datetime.DateTimeService;
24  import org.kuali.rice.kns.document.MaintenanceDocument;
25  import org.kuali.rice.krad.util.ObjectUtils;
26  import org.kuali.rice.location.api.postalcode.PostalCode;
27  import org.kuali.rice.location.api.postalcode.PostalCodeService;
28  
29  /**
30   * PreRules checks for the {@link Org} that needs to occur while still in the Struts processing. This includes defaults,
31   * confirmations, etc.
32   */
33  public class OrgPreRules extends MaintenancePreRulesBase {
34      protected Organization newOrg;
35      protected PostalCodeService postalZipCodeService = SpringContext.getBean(PostalCodeService.class);
36  
37      /**
38       * This checks to see if a continuation account is necessary and if the HRMS data has changed
39       *
40       * @see org.kuali.ole.coa.document.validation.impl.MaintenancePreRulesBase#doCustomPreRules(org.kuali.rice.kns.document.MaintenanceDocument)
41       */
42      @Override
43      protected boolean doCustomPreRules(MaintenanceDocument document) {
44          setupConvenienceObjects(document);
45          checkForContinuationAccounts(); // run this first to avoid side effects
46  
47          LOG.debug("done with continuation account, proceeeding with remaining pre rules");
48  
49          //Code changes for JIRA OLE2344. No need to update HRMS updated date as HRMS tab is removed
50  
51         // updateHRMSUpdateDate((Organization) document.getOldMaintainableObject().getBusinessObject(), (Organization) document.getNewMaintainableObject().getBusinessObject());
52  
53          return true;
54      }
55  
56      /**
57       * This looks for the org default account number and then sets the values to the continuation account value if it exists
58       */
59      protected void checkForContinuationAccounts() {
60          LOG.debug("entering checkForContinuationAccounts()");
61  
62          if (StringUtils.isNotBlank(newOrg.getOrganizationDefaultAccountNumber())) {
63              Account account = checkForContinuationAccount("Account Number", newOrg.getChartOfAccountsCode(), newOrg.getOrganizationDefaultAccountNumber(), "");
64              if (ObjectUtils.isNotNull(account)) { // override old user inputs
65                  newOrg.setOrganizationDefaultAccountNumber(account.getAccountNumber());
66                  newOrg.setChartOfAccountsCode(account.getChartOfAccountsCode());
67              }
68          }
69      }
70  
71      /**
72       * This method sets the convenience objects like newOrg and copyOrg, so you have short and easy handles to the new and old
73       * objects contained in the maintenance document. It also calls the BusinessObjectBase.refresh(), which will attempt to load all
74       * sub-objects from the DB by their primary keys, if available.
75       *
76       * @param document
77       */
78      protected void setupConvenienceObjects(MaintenanceDocument document) {
79  
80          // setup newOrg convenience objects, make sure all possible sub-objects are populated
81          newOrg = (Organization) document.getNewMaintainableObject().getBusinessObject();
82      }
83  
84      /**
85       * Check if the HRMS data has changed on this document. If so, update the last update date.
86       *
87       * @param oldData
88       * @param newData
89       */
90      protected void updateHRMSUpdateDate(Organization oldData, Organization newData) {
91          if (oldData != null) {
92              OrganizationExtension oldExt = oldData.getOrganizationExtension();
93              OrganizationExtension newExt = newData.getOrganizationExtension();
94              if (oldExt != null) {
95                  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()
96                          || !StringUtils.equals(oldExt.getHrmsPersonnelApproverUniversalId(), newExt.getHrmsPersonnelApproverUniversalId()) || !StringUtils.equals(oldExt.getFiscalApproverUniversalId(), newExt.getFiscalApproverUniversalId())) {
97                      newExt.setHrmsLastUpdateDate(SpringContext.getBean(DateTimeService.class).getCurrentTimestamp());
98                  }
99              }
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 }