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.sys.service.impl;
17  
18  import java.util.Calendar;
19  import java.util.HashMap;
20  
21  import org.kuali.ole.coa.businessobject.Account;
22  import org.kuali.ole.coa.businessobject.ObjectCode;
23  import org.kuali.ole.coa.businessobject.OffsetDefinition;
24  import org.kuali.ole.coa.service.AccountService;
25  import org.kuali.ole.coa.service.ObjectCodeService;
26  import org.kuali.ole.fp.businessobject.OffsetAccount;
27  import org.kuali.ole.gl.businessobject.FlexibleAccountUpdateable;
28  import org.kuali.ole.sys.OLEConstants;
29  import org.kuali.ole.sys.exception.InvalidFlexibleOffsetException;
30  import org.kuali.ole.sys.service.FlexibleOffsetAccountService;
31  import org.kuali.rice.core.api.datetime.DateTimeService;
32  import org.kuali.rice.coreservice.framework.parameter.ParameterService;
33  import org.kuali.rice.krad.service.BusinessObjectService;
34  
35  /**
36   * This is the default implementation of the FlexibleOffsetAccountService interface.
37   */
38  public class FlexibleOffsetAccountServiceImpl implements FlexibleOffsetAccountService {
39      private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(FlexibleOffsetAccountServiceImpl.class);
40  
41      private BusinessObjectService businessObjectService;
42      private AccountService accountService;
43      private ObjectCodeService objectCodeService;
44      private DateTimeService dateTimeService;
45      private ParameterService parameterService;
46  
47      /**
48       * This method uses the parameters provided to retrieve an OffsetAccount instance if the flexible offset account flag is
49       * enabled.
50       * 
51       * @param chartOfAccountsCode The chart code used to retrieve the flexible offset account.
52       * @param accountNumber The account number of the flexible offset account being retrieved.
53       * @param financialOffsetObjectCode The offset object code used to retrieve the offset account.
54       * @return A flexible offset account based on the parameters provided, or null if offsets are not enabled.
55       * 
56       * @see FlexibleOffsetAccountService#getByPrimaryIdIfEnabled
57       */
58      public OffsetAccount getByPrimaryIdIfEnabled(String chartOfAccountsCode, String accountNumber, String financialOffsetObjectCode) {
59          LOG.debug("getByPrimaryIdIfEnabled() started");
60  
61          if (!getEnabled()) {
62              return null;
63          }
64          HashMap<String,Object> keys = new HashMap();
65          keys.put("chartOfAccountsCode", chartOfAccountsCode);
66          keys.put("accountNumber", accountNumber);
67          keys.put("financialOffsetObjectCode", financialOffsetObjectCode);
68          return (OffsetAccount) businessObjectService.findByPrimaryKey(OffsetAccount.class, keys);
69      }
70  
71      /**
72       * This method queries the parameter table to retrieve the value of the flexible offset flag and returns the resulting value.
73       * 
74       * @return True if flexible offsets are enabled, false otherwise. 
75       * 
76       * @see FlexibleOffsetAccountService#getEnabled
77       */
78      public boolean getEnabled() {
79          LOG.debug("getEnabled() started");
80          return parameterService.getParameterValueAsBoolean(OffsetDefinition.class, OLEConstants.SystemGroupParameterNames.FLEXIBLE_OFFSET_ENABLED_FLAG);
81      }
82  
83      /**
84       * This method modifies the origin entry provided with values from the associated flexible offset account, which is 
85       * retrieved from the database using values provided by the origin entry.
86       * 
87       * @param originEntry The origin entry to be updated with offset account details.
88       * @return False if the flexible offset flag is false, if there is no corresponding flexbile offset account, true otherwise.
89       * 
90       * @see org.kuali.ole.sys.service.FlexibleOffsetAccountService#updateOffset(org.kuali.ole.gl.businessobject.OriginEntryFull)
91       */
92      public boolean updateOffset(FlexibleAccountUpdateable transaction) {
93          LOG.debug("setBusinessObjectService() started");
94  
95          if (!getEnabled()) {
96              return false;
97          }
98          String keyOfErrorMessage = "";
99  
100         Integer fiscalYear = transaction.getUniversityFiscalYear();
101         String chartOfAccountsCode = transaction.getChartOfAccountsCode();
102         String accountNumber = transaction.getAccountNumber();
103 
104         String balanceTypeCode = transaction.getFinancialBalanceTypeCode();
105         String documentTypeCode = transaction.getFinancialDocumentTypeCode();
106 
107         // do nothing if there is no the offset account with the given chart of accounts code,
108         // account number and offset object code in the offset table.
109         OffsetAccount flexibleOffsetAccount = getByPrimaryIdIfEnabled(chartOfAccountsCode, accountNumber, transaction.getFinancialObjectCode());
110         if (flexibleOffsetAccount == null) {
111             return false;
112         }
113 
114         String offsetAccountNumber = flexibleOffsetAccount.getFinancialOffsetAccountNumber();
115         String offsetChartOfAccountsCode = flexibleOffsetAccount.getFinancialOffsetChartOfAccountCode();
116 
117         Account offsetAccount = accountService.getByPrimaryId(offsetChartOfAccountsCode, offsetAccountNumber);
118         if (offsetAccount == null) {
119             throw new InvalidFlexibleOffsetException("Invalid Flexible Offset Account " + offsetChartOfAccountsCode + "-" + offsetAccountNumber);
120         }
121 
122         // Can't be closed and can't be expired
123         if (!offsetAccount.isActive()) {
124             throw new InvalidFlexibleOffsetException("Closed Flexible Offset Account " + offsetChartOfAccountsCode + "-" + offsetAccountNumber);
125         }
126         if ((offsetAccount.getAccountExpirationDate() != null) && isExpired(offsetAccount, dateTimeService.getCurrentCalendar())) {
127             throw new InvalidFlexibleOffsetException("Expired Flexible Offset Account " + offsetChartOfAccountsCode + "-" + offsetAccountNumber);
128         }
129 
130         // If the chart changes, make sure the object code is still valid
131         if (!chartOfAccountsCode.equals(offsetChartOfAccountsCode)) {
132             ObjectCode objectCode = objectCodeService.getByPrimaryId(fiscalYear, offsetChartOfAccountsCode, transaction.getFinancialObjectCode());
133             if (objectCode == null) {
134                 throw new InvalidFlexibleOffsetException("Invalid Object Code for flexible offset " + fiscalYear + "-" + offsetChartOfAccountsCode + "-" + transaction.getFinancialObjectCode());
135             }
136         }
137 
138         // replace the chart and account of the given transaction with those of the offset account obtained above
139         transaction.setAccount(offsetAccount);
140         transaction.setAccountNumber(offsetAccountNumber);
141         transaction.setChartOfAccountsCode(offsetChartOfAccountsCode);
142 
143         // blank out the sub account and sub object since the account has been replaced
144         transaction.setSubAccountNumber(OLEConstants.getDashSubAccountNumber());
145         transaction.setFinancialSubObjectCode(OLEConstants.getDashFinancialSubObjectCode());
146         return true;
147     }
148 
149     /**
150      * This method determines if an account has expired.  An account has expired if the expiration year of the account is 
151      * less than the run date year or if the date of expiration occurred before the run date provided.
152      * 
153      * @param account The account to be examined.
154      * @param runCalendar The date the expiration date is tested against.
155      * @return True if the account has expired, false otherwise.
156      */
157     protected boolean isExpired(Account account, Calendar runCalendar) {
158 
159         Calendar expirationDate = Calendar.getInstance();
160         expirationDate.setTimeInMillis(account.getAccountExpirationDate().getTime());
161 
162         int expirationYear = expirationDate.get(Calendar.YEAR);
163         int runYear = runCalendar.get(Calendar.YEAR);
164         int expirationDay = expirationDate.get(Calendar.DAY_OF_YEAR);
165         int runDay = runCalendar.get(Calendar.DAY_OF_YEAR);
166 
167         return (expirationYear < runYear) || (expirationYear == runYear && expirationDay < runDay);
168     }
169 
170     /**
171      * Sets the local dateTimeService attribute.
172      * @param dateTimeService The DateTimeService instance to be set.
173      */
174     public void setDateTimeService(DateTimeService dateTimeService) {
175         this.dateTimeService = dateTimeService;
176     }
177 
178     /**
179      * Sets the local accountService attribute.
180      * @param accountService The AccountService instance to be set.
181      */
182     public void setAccountService(AccountService accountService) {
183         this.accountService = accountService;
184     }
185 
186     /**
187      * Sets the local objectCodeService attribute.
188      * @param objectCodeService The ObjectCodeService instance to be set.
189      */
190     public void setObjectCodeService(ObjectCodeService objectCodeService) {
191         this.objectCodeService = objectCodeService;
192     }
193 
194     /**
195      * Sets the local businessObjectService attribute.
196      * @param businessObjectService The BusinessObjectService instance to be set.
197      */
198     public void setBusinessObjectService(BusinessObjectService businessObjectService) {
199         this.businessObjectService = businessObjectService;
200     }
201 
202     /**
203      * Sets the local parameterService attribute.
204      * @param parameterService The ParameterService instance to be set.
205      */
206     public void setParameterService(ParameterService parameterService) {
207         this.parameterService = parameterService;
208     }
209 }