View Javadoc
1   /*
2    * Copyright 2009 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.dataaccess.impl;
17  
18  import java.sql.Date;
19  import java.util.Iterator;
20  
21  import org.apache.commons.lang.StringUtils;
22  import org.apache.ojb.broker.query.Criteria;
23  import org.apache.ojb.broker.query.QueryFactory;
24  import org.apache.ojb.broker.query.ReportQueryByCriteria;
25  import org.kuali.ole.coa.businessobject.AccountDelegate;
26  import org.kuali.ole.coa.dataaccess.AccountDelegateDao;
27  import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb;
28  import org.kuali.rice.krad.maintenance.MaintenanceLock;
29  import org.kuali.rice.krad.util.KRADPropertyConstants;
30  
31  /**
32   * This class is the OJB implementation of the AccountDelegateDao.
33   */
34  public class AccountDelegateDaoOjb extends PlatformAwareDaoBaseOjb implements AccountDelegateDao {
35  
36      /**
37       * @see org.kuali.ole.coa.dataaccess.AccountDelegateDao#getLockingDocumentNumber(java.lang.String, java.lang.String)
38       */
39  
40      public String getLockingDocumentNumber(String lockingRepresentation, String documentNumber) {
41          String lockingDocNumber = "";
42  
43          // build the query criteria
44          Criteria criteria = new Criteria();
45          criteria.addEqualTo("lockingRepresentation", lockingRepresentation);
46  
47          // if a docHeaderId is specified, then it will be excluded from the
48          // locking representation test.
49          if (StringUtils.isNotBlank(documentNumber)) {
50              criteria.addNotEqualTo(KRADPropertyConstants.DOCUMENT_NUMBER, documentNumber);
51          }
52  
53          // attempt to retrieve a document based off this criteria
54          MaintenanceLock maintenanceLock = (MaintenanceLock) getPersistenceBrokerTemplate().getObjectByQuery(QueryFactory.newQuery(MaintenanceLock.class, criteria));
55  
56          // if a document was found, then there's already one out there pending, and
57          // we consider it 'locked' and we return the docnumber.
58          if (maintenanceLock != null) {
59              lockingDocNumber = maintenanceLock.getDocumentNumber();
60          }
61          return lockingDocNumber;
62      }
63  
64      /**
65       * @see org.kuali.ole.coa.dataaccess.AccountDelegateDao#getAccountDelegationsForPerson(java.lang.String)
66       */
67      public Iterator<AccountDelegate> getAccountDelegationsForPerson(String principalId, boolean primary) {
68          Criteria criteria = new Criteria();
69          criteria.addEqualTo("accountDelegateSystemId", principalId);
70          criteria.addEqualTo("active", "Y");
71          criteria.addEqualTo("accountsDelegatePrmrtIndicator", primary);
72  
73          return (Iterator<AccountDelegate>) getPersistenceBrokerTemplate().getIteratorByQuery(QueryFactory.newQuery(AccountDelegate.class, criteria));
74      }
75  
76      /**
77       * @see org.kuali.ole.coa.dataaccess.AccountDelegateDao#isPrincipalInAnyWayShapeOrFormPrimaryAccountDelegate(java.lang.String, java.sql.Date)
78       */
79      public boolean isPrincipalInAnyWayShapeOrFormPrimaryAccountDelegate(String principalId, Date currentSqlDate) {
80          return queryPrincipalIsAccountDelegate(principalId, true, currentSqlDate);
81      }
82  
83      /**
84       * @see org.kuali.ole.coa.dataaccess.AccountDelegateDao#isPrincipalInAnyWayShapeOrFormSecondaryAccountDelegate(java.lang.String, java.sql.Date)
85       */
86      public boolean isPrincipalInAnyWayShapeOrFormSecondaryAccountDelegate(String principalId, Date currentSqlDate) {
87          return queryPrincipalIsAccountDelegate(principalId, false, currentSqlDate);
88      }
89  
90      /**
91       * Determines if any non-closed accounts exist where the principal id is an account delegate
92       * 
93       * @param principalId the principal id to check
94       * @param primary whether to check primary delegations (if true) or secondary delegations (if false)
95       * @param currentSqlDate current sql date
96       * @return true if the principal has an account delegation
97       */
98      protected boolean queryPrincipalIsAccountDelegate(String principalId, boolean primary, Date currentSqlDate) {
99          Criteria criteria = new Criteria();
100         criteria.addEqualTo("accountDelegateSystemId", principalId);
101         criteria.addEqualTo("accountsDelegatePrmrtIndicator", (primary ? "Y" : "N"));
102         criteria.addEqualTo("active", "Y");
103         criteria.addEqualTo("account.active", "Y");
104         criteria.addLessOrEqualThan("accountDelegateStartDate", currentSqlDate);
105 
106         ReportQueryByCriteria reportQuery = QueryFactory.newReportQuery(AccountDelegate.class, criteria);
107         reportQuery.setAttributes(new String[] { "count(*)" });
108 
109         int resultCount = 0;
110         // TODO: getReportQueryIteratorByQuery can be changed to getCount...
111         Iterator iter = getPersistenceBrokerTemplate().getReportQueryIteratorByQuery(reportQuery);
112         while (iter.hasNext()) {
113             final Object[] results = (Object[]) iter.next();
114             resultCount = (results[0] instanceof Number) ? ((Number) results[0]).intValue() : new Integer(results[0].toString()).intValue();
115         }
116         return resultCount > 0;
117     }
118 }