001: /*
002: * Copyright 2005-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.dao.ojb;
017:
018: import java.util.ArrayList;
019: import java.util.Collection;
020: import java.util.Iterator;
021: import java.util.List;
022:
023: import org.apache.ojb.broker.query.Criteria;
024: import org.apache.ojb.broker.query.QueryFactory;
025: import org.kuali.core.bo.user.UniversalUser;
026: import org.kuali.core.dao.ojb.PlatformAwareDaoBaseOjb;
027: import org.kuali.core.service.DateTimeService;
028: import org.kuali.core.util.KualiDecimal;
029: import org.kuali.core.util.ObjectUtils;
030: import org.kuali.kfs.KFSConstants;
031: import org.kuali.kfs.bo.AccountResponsibility;
032: import org.kuali.kfs.context.SpringContext;
033: import org.kuali.module.chart.bo.Account;
034: import org.kuali.module.chart.bo.Delegate;
035: import org.kuali.module.chart.dao.AccountDao;
036:
037: /**
038: * This class is the OJB implementation of the AccountDao interface.
039: */
040: public class AccountDaoOjb extends PlatformAwareDaoBaseOjb implements
041: AccountDao {
042: private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger
043: .getLogger(AccountDaoOjb.class);
044:
045: private DateTimeService dateTimeService;
046:
047: /**
048: * Retrieves account business object by primary key
049: *
050: * @param chartOfAccountsCode - the FIN_COA_CD of the Chart Code that is part of the composite key of Account
051: * @param accountNumber - the ACCOUNT_NBR part of the composite key of Accont
052: * @return Account
053: * @see AccountDao
054: */
055: public Account getByPrimaryId(String chartOfAccountsCode,
056: String accountNumber) {
057: LOG.debug("getByPrimaryId() started");
058:
059: Criteria criteria = new Criteria();
060: criteria.addEqualTo("chartOfAccountsCode", chartOfAccountsCode);
061: criteria.addEqualTo("accountNumber", accountNumber);
062:
063: return (Account) getPersistenceBrokerTemplate()
064: .getObjectByQuery(
065: QueryFactory.newQuery(Account.class, criteria));
066: }
067:
068: /**
069: * fetch the accounts that the user is either the fiscal officer or a delegate of the fiscal officer
070: *
071: * @param kualiUser
072: * @return a list of Accounts that the user has responsibility for
073: */
074: public List getAccountsThatUserIsResponsibleFor(
075: UniversalUser universalUser) {
076: LOG.debug("getAccountsThatUserIsResponsibleFor() started");
077:
078: List accountResponsibilities = new ArrayList();
079: accountResponsibilities
080: .addAll(getFiscalOfficerResponsibilities(universalUser));
081: accountResponsibilities
082: .addAll(getDelegatedResponsibilities(universalUser));
083: return accountResponsibilities;
084: }
085:
086: /**
087: * This method determines if the given user has any responsibilities on the given account
088: *
089: * @param universalUser the user to check responsibilities for
090: * @param account the account to check responsibilities on
091: * @return true if user is somehow responsible for account, false if otherwise
092: */
093: public boolean determineUserResponsibilityOnAccount(
094: UniversalUser universalUser, Account account) {
095: boolean result = hasFiscalOfficerResponsibility(universalUser,
096: account);
097: if (!result) {
098: result = hasDelegatedResponsibility(universalUser, account);
099: }
100: return result;
101: }
102:
103: /**
104: * Resolves the Primary Delegate for the given delegate example. If the primary delegate exists for a specific Document Type
105: * Code and for a Document Type Code of "ALL", the delegate for the specific document type code is returned;
106: *
107: * @see org.kuali.module.chart.dao.AccountDao#getPrimaryDelegationByExample(org.kuali.module.chart.bo.Delegate,
108: * java.lang.String)
109: */
110: public Delegate getPrimaryDelegationByExample(
111: Delegate delegateExample, String totalDollarAmount) {
112: Collection collection = getPersistenceBrokerTemplate()
113: .getCollectionByQuery(
114: QueryFactory.newQuery(Delegate.class,
115: getDelegateByExampleCriteria(
116: delegateExample,
117: totalDollarAmount, "Y")));
118: if (collection.isEmpty()) {
119: return null;
120: }
121: for (Iterator iterator = collection.iterator(); iterator
122: .hasNext();) {
123: Delegate delegate = (Delegate) iterator.next();
124: if (!"ALL".equals(delegate.getFinancialDocumentTypeCode())) {
125: return delegate;
126: }
127: }
128: return (Delegate) collection.iterator().next();
129: }
130:
131: /**
132: * @see org.kuali.module.chart.dao.AccountDao#getSecondaryDelegationsByExample(org.kuali.module.chart.bo.Delegate,
133: * java.lang.String)
134: */
135: public List getSecondaryDelegationsByExample(
136: Delegate delegateExample, String totalDollarAmount) {
137: return new ArrayList(getPersistenceBrokerTemplate()
138: .getCollectionByQuery(
139: QueryFactory.newQuery(Delegate.class,
140: getDelegateByExampleCriteria(
141: delegateExample,
142: totalDollarAmount, "N"))));
143: }
144:
145: /**
146: * This method creates a {@link Criteria} based on {@link Delegate}, dollar amount and whether or not it is the primary
147: * delegate
148: *
149: * @param delegateExample
150: * @param totalDollarAmount
151: * @param accountsDelegatePrmrtIndicator
152: * @return example {@link Delegate} {@link Criteria}
153: */
154: private Criteria getDelegateByExampleCriteria(
155: Delegate delegateExample, String totalDollarAmount,
156: String accountsDelegatePrmrtIndicator) {
157: Criteria criteria = new Criteria();
158: criteria.addEqualTo(
159: KFSConstants.CHART_OF_ACCOUNTS_CODE_PROPERTY_NAME,
160: delegateExample.getChartOfAccountsCode());
161: criteria.addEqualTo(KFSConstants.ACCOUNT_NUMBER_PROPERTY_NAME,
162: delegateExample.getAccountNumber());
163: Criteria docTypeMatchCriteria = new Criteria();
164: docTypeMatchCriteria.addEqualTo(
165: KFSConstants.FINANCIAL_DOCUMENT_TYPE_CODE,
166: delegateExample.getFinancialDocumentTypeCode());
167: Criteria docTypeAllCriteria = new Criteria();
168: docTypeAllCriteria.addEqualTo(
169: KFSConstants.FINANCIAL_DOCUMENT_TYPE_CODE, "ALL");
170: Criteria docTypeOrCriteria = new Criteria();
171: docTypeOrCriteria.addOrCriteria(docTypeMatchCriteria);
172: docTypeOrCriteria.addOrCriteria(docTypeAllCriteria);
173: criteria.addAndCriteria(docTypeOrCriteria);
174: criteria.addEqualTo("accountDelegateActiveIndicator", "Y");
175: criteria.addLessOrEqualThan("accountDelegateStartDate",
176: SpringContext.getBean(DateTimeService.class)
177: .getCurrentTimestamp());
178: criteria.addEqualTo("accountsDelegatePrmrtIndicator",
179: accountsDelegatePrmrtIndicator);
180: if (totalDollarAmount != null) {
181: Criteria totalDollarAmountInRangeCriteria = new Criteria();
182: totalDollarAmountInRangeCriteria.addLessOrEqualThan(
183: "finDocApprovalFromThisAmt", totalDollarAmount);
184: totalDollarAmountInRangeCriteria.addGreaterOrEqualThan(
185: "finDocApprovalToThisAmount", totalDollarAmount);
186: Criteria totalDollarAmountZeroCriteria = new Criteria();
187: totalDollarAmountZeroCriteria.addEqualTo(
188: "finDocApprovalToThisAmount", "0");
189: totalDollarAmountZeroCriteria.addLessOrEqualThan(
190: "finDocApprovalFromThisAmt", totalDollarAmount);
191: Criteria totalDollarAmountOrCriteria = new Criteria();
192: totalDollarAmountOrCriteria
193: .addOrCriteria(totalDollarAmountInRangeCriteria);
194: totalDollarAmountOrCriteria
195: .addOrCriteria(totalDollarAmountZeroCriteria);
196: criteria.addAndCriteria(totalDollarAmountOrCriteria);
197: }
198: return criteria;
199: }
200:
201: /**
202: * method to get the fo responsibilities for the account
203: *
204: * @param universalUser - fiscal officer to check for
205: * @return list of {@link AccountResponsibility} for this fiscal officer
206: */
207: private List getFiscalOfficerResponsibilities(
208: UniversalUser universalUser) {
209: List fiscalOfficerResponsibilities = new ArrayList();
210: Criteria criteria = new Criteria();
211: criteria.addEqualTo("accountFiscalOfficerSystemIdentifier",
212: universalUser.getPersonUniversalIdentifier());
213: Collection accounts = getPersistenceBrokerTemplate()
214: .getCollectionByQuery(
215: QueryFactory.newQuery(Account.class, criteria));
216: for (Iterator iter = accounts.iterator(); iter.hasNext();) {
217: Account account = (Account) iter.next();
218: AccountResponsibility accountResponsibility = new AccountResponsibility(
219: AccountResponsibility.FISCAL_OFFICER_RESPONSIBILITY,
220: new KualiDecimal("0"), new KualiDecimal("0"), "",
221: account);
222: fiscalOfficerResponsibilities.add(accountResponsibility);
223: }
224: return fiscalOfficerResponsibilities;
225: }
226:
227: /**
228: * This method determines if a given user has fiscal officer responsiblity on a given account.
229: *
230: * @param universalUser the user to check responsibilities for
231: * @param account the account to check responsibilities on
232: * @return true if user does have fiscal officer responsibility on account, false if otherwise
233: */
234: private boolean hasFiscalOfficerResponsibility(
235: UniversalUser universalUser, Account account) {
236: boolean hasFiscalOfficerResponsibility = false;
237: Criteria criteria = new Criteria();
238: criteria.addEqualTo("accountFiscalOfficerSystemIdentifier",
239: universalUser.getPersonUniversalIdentifier());
240: criteria.addEqualTo("chartOfAccountsCode", account
241: .getChartOfAccountsCode());
242: criteria
243: .addEqualTo("accountNumber", account.getAccountNumber());
244: Collection accounts = getPersistenceBrokerTemplate()
245: .getCollectionByQuery(
246: QueryFactory.newQuery(Account.class, criteria));
247: if (accounts != null && accounts.size() > 0) {
248: Account retrievedAccount = (Account) accounts.iterator()
249: .next();
250: if (ObjectUtils.isNotNull(retrievedAccount)) {
251: hasFiscalOfficerResponsibility = true;
252: }
253: }
254: return hasFiscalOfficerResponsibility;
255: }
256:
257: /**
258: * method to get the fo delegated responsibilities for the account
259: *
260: * @param universalUser - user to check against
261: * @return a list of {@link AccountResponsibility} objects for a delegate
262: */
263: private List getDelegatedResponsibilities(
264: UniversalUser universalUser) {
265: List delegatedResponsibilities = new ArrayList();
266: Criteria criteria = new Criteria();
267: criteria.addEqualTo("accountDelegateSystemId", universalUser
268: .getPersonUniversalIdentifier());
269: Collection accountDelegates = getPersistenceBrokerTemplate()
270: .getCollectionByQuery(
271: QueryFactory.newQuery(Delegate.class, criteria));
272: for (Iterator iter = accountDelegates.iterator(); iter
273: .hasNext();) {
274: Delegate accountDelegate = (Delegate) iter.next();
275: if (accountDelegate.isAccountDelegateActiveIndicator()) {
276: // the start_date should never be null in the real world, but
277: // there is some test data that
278: // contains null startDates, therefore this check.
279: if (ObjectUtils.isNotNull(accountDelegate
280: .getAccountDelegateStartDate())) {
281: if (!accountDelegate.getAccountDelegateStartDate()
282: .after(dateTimeService.getCurrentDate())) {
283: Account account = getByPrimaryId(
284: accountDelegate
285: .getChartOfAccountsCode(),
286: accountDelegate.getAccount()
287: .getAccountNumber());
288: AccountResponsibility accountResponsibility = new AccountResponsibility(
289: AccountResponsibility.DELEGATED_RESPONSIBILITY,
290: accountDelegate
291: .getFinDocApprovalFromThisAmt(),
292: accountDelegate
293: .getFinDocApprovalToThisAmount(),
294: accountDelegate
295: .getFinancialDocumentTypeCode(),
296: account);
297: delegatedResponsibilities
298: .add(accountResponsibility);
299: }
300: }
301: }
302: }
303: return delegatedResponsibilities;
304: }
305:
306: /**
307: * This method determines if a user has delegated responsibilities on a given account.
308: *
309: * @param universalUser the user to check responsibilities for
310: * @param account the account to check responsibilities on
311: * @return true if user has delegated responsibilities
312: */
313: private boolean hasDelegatedResponsibility(
314: UniversalUser universalUser, Account account) {
315: boolean hasResponsibility = false;
316: Criteria criteria = new Criteria();
317: criteria.addEqualTo("accountDelegateSystemId", universalUser
318: .getPersonUniversalIdentifier());
319: criteria.addEqualTo("chartOfAccountsCode", account
320: .getChartOfAccountsCode());
321: criteria
322: .addEqualTo("accountNumber", account.getAccountNumber());
323: Collection accountDelegates = getPersistenceBrokerTemplate()
324: .getCollectionByQuery(
325: QueryFactory.newQuery(Delegate.class, criteria));
326: for (Iterator iter = accountDelegates.iterator(); iter
327: .hasNext()
328: && !hasResponsibility;) {
329: Delegate accountDelegate = (Delegate) iter.next();
330: if (accountDelegate.isAccountDelegateActiveIndicator()) {
331: // the start_date should never be null in the real world, but
332: // there is some test data that
333: // contains null startDates, therefore this check.
334: if (ObjectUtils.isNotNull(accountDelegate
335: .getAccountDelegateStartDate())) {
336: if (!accountDelegate.getAccountDelegateStartDate()
337: .after(dateTimeService.getCurrentDate())) {
338: hasResponsibility = true;
339: }
340: }
341: }
342: }
343: return hasResponsibility;
344: }
345:
346: /**
347: * @see org.kuali.module.chart.dao.AccountDao#getAllAccounts()
348: * @return an iterator for all accounts
349: */
350: public Iterator getAllAccounts() {
351: LOG.debug("getAllAccounts() started");
352:
353: Criteria criteria = new Criteria();
354: return getPersistenceBrokerTemplate().getIteratorByQuery(
355: QueryFactory.newQuery(Account.class, criteria));
356: }
357:
358: public void setDateTimeService(DateTimeService dateTimeService) {
359: this.dateTimeService = dateTimeService;
360: }
361: }
|