View Javadoc
1   /*
2    * The Kuali Financial System, a comprehensive financial management system for higher education.
3    * 
4    * Copyright 2005-2014 The Kuali Foundation
5    * 
6    * This program is free software: you can redistribute it and/or modify
7    * it under the terms of the GNU Affero General Public License as
8    * published by the Free Software Foundation, either version 3 of the
9    * License, or (at your option) any later version.
10   * 
11   * This program is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU Affero General Public License for more details.
15   * 
16   * You should have received a copy of the GNU Affero General Public License
17   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18   */
19  package org.kuali.kfs.module.bc.document.dataaccess.impl;
20  
21  import java.util.ArrayList;
22  import java.util.List;
23  
24  import org.apache.commons.lang.StringUtils;
25  import org.apache.ojb.broker.query.Criteria;
26  import org.apache.ojb.broker.query.QueryFactory;
27  import org.apache.ojb.broker.query.ReportQueryByCriteria;
28  import org.kuali.kfs.module.bc.BCConstants;
29  import org.kuali.kfs.module.bc.BCPropertyConstants;
30  import org.kuali.kfs.module.bc.businessobject.BudgetConstructionFundingLock;
31  import org.kuali.kfs.module.bc.businessobject.BudgetConstructionHeader;
32  import org.kuali.kfs.module.bc.businessobject.BudgetConstructionPosition;
33  import org.kuali.kfs.module.bc.businessobject.PendingBudgetConstructionAppointmentFunding;
34  import org.kuali.kfs.module.bc.document.dataaccess.BudgetConstructionDao;
35  import org.kuali.kfs.module.bc.document.dataaccess.BudgetConstructionLockDao;
36  import org.kuali.kfs.sys.KFSPropertyConstants;
37  import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb;
38  
39  /**
40   * @see org.kuali.kfs.module.bc.document.dataaccess.BudgetConstructionLockDao
41   */
42  public class BudgetConstructionLockDaoOjb extends PlatformAwareDaoBaseOjb implements BudgetConstructionLockDao {
43      private BudgetConstructionDao budgetConstructionDao;
44  
45      /**
46       * @see org.kuali.kfs.module.bc.document.dataaccess.BudgetConstructionLockDao#getAllAccountLocks(java.lang.String)
47       */
48      public List<BudgetConstructionHeader> getAllAccountLocks(String lockUnivId) {
49          Criteria criteria = new Criteria();
50  
51          if (StringUtils.isNotBlank(lockUnivId)) {
52              criteria.addEqualTo(BCPropertyConstants.BUDGET_LOCK_USER_IDENTIFIER, lockUnivId);
53          }
54          else {
55              criteria.addNotNull(BCPropertyConstants.BUDGET_LOCK_USER_IDENTIFIER);
56          }
57          
58          ReportQueryByCriteria query = QueryFactory.newReportQuery(BudgetConstructionHeader.class, criteria);
59          query.addOrderByAscending(KFSPropertyConstants.UNIVERSITY_FISCAL_YEAR);
60          query.addOrderByAscending(KFSPropertyConstants.CHART_OF_ACCOUNTS_CODE);
61          query.addOrderByAscending(KFSPropertyConstants.ACCOUNT_NUMBER);
62          query.addOrderByAscending(KFSPropertyConstants.SUB_ACCOUNT_NUMBER);
63          
64          return (List<BudgetConstructionHeader>) getPersistenceBrokerTemplate().getCollectionByQuery(query);
65      }
66  
67      /**
68       * @see org.kuali.kfs.module.bc.document.dataaccess.BudgetConstructionLockDao#getAllPositionFundingLocks(java.lang.String)
69       */
70      public List<PendingBudgetConstructionAppointmentFunding> getAllPositionFundingLocks(String lockUnivId) {
71          Criteria criteria = new Criteria();
72  
73          if (StringUtils.isNotBlank(lockUnivId)) {
74              criteria.addEqualTo(BCPropertyConstants.APPOINTMENT_FUNDING_LOCK_USER_ID, lockUnivId);
75          }
76          else {
77              criteria.addNotNull(BCPropertyConstants.APPOINTMENT_FUNDING_LOCK_USER_ID);
78          }
79  
80          ReportQueryByCriteria query = QueryFactory.newReportQuery(BudgetConstructionFundingLock.class, criteria);
81          query.addOrderByAscending(KFSPropertyConstants.UNIVERSITY_FISCAL_YEAR);
82          query.addOrderByAscending(KFSPropertyConstants.CHART_OF_ACCOUNTS_CODE);
83          query.addOrderByAscending(KFSPropertyConstants.ACCOUNT_NUMBER);
84          query.addOrderByAscending(KFSPropertyConstants.SUB_ACCOUNT_NUMBER);
85  
86          List<BudgetConstructionFundingLock> allFundingLocks = (List<BudgetConstructionFundingLock>) getPersistenceBrokerTemplate().getCollectionByQuery(query);
87  
88          List<PendingBudgetConstructionAppointmentFunding> positionFundingLocks = new ArrayList<PendingBudgetConstructionAppointmentFunding>();
89          for (BudgetConstructionFundingLock fundingLock : allFundingLocks) {
90              Criteria criteria2 = new Criteria();
91              criteria2.addEqualTo(KFSPropertyConstants.UNIVERSITY_FISCAL_YEAR, fundingLock.getUniversityFiscalYear());
92              criteria2.addEqualTo(KFSPropertyConstants.CHART_OF_ACCOUNTS_CODE, fundingLock.getChartOfAccountsCode());
93              criteria2.addEqualTo(KFSPropertyConstants.ACCOUNT_NUMBER, fundingLock.getAccountNumber());
94              criteria2.addEqualTo(KFSPropertyConstants.SUB_ACCOUNT_NUMBER, fundingLock.getSubAccountNumber());
95              criteria2.addEqualTo(BCPropertyConstants.BUDGET_CONSTRUCTION_POSITION + "." + BCPropertyConstants.POSITION_LOCK_USER_IDENTIFIER, fundingLock.getAppointmentFundingLockUserId());
96  
97              positionFundingLocks.addAll((List<PendingBudgetConstructionAppointmentFunding>) getPersistenceBrokerTemplate().getCollectionByQuery(QueryFactory.newQuery(PendingBudgetConstructionAppointmentFunding.class, criteria2)));
98          }
99  
100         return positionFundingLocks;
101     }
102 
103     /**
104      * @see org.kuali.kfs.module.bc.document.dataaccess.BudgetConstructionLockDao#getOrphanedPositionLocks(java.lang.String)
105      */
106     public List<BudgetConstructionPosition> getOrphanedPositionLocks(String lockUnivId) {
107         Criteria criteria = new Criteria();
108 
109         if (StringUtils.isNotBlank(lockUnivId)) {
110             criteria.addEqualTo(BCPropertyConstants.POSITION_LOCK_USER_IDENTIFIER, lockUnivId);
111         }
112         else {
113             criteria.addNotNull(BCPropertyConstants.POSITION_LOCK_USER_IDENTIFIER);
114         }
115         
116         ReportQueryByCriteria query = QueryFactory.newReportQuery(BudgetConstructionPosition.class, criteria);
117         query.addOrderByAscending(KFSPropertyConstants.UNIVERSITY_FISCAL_YEAR);
118         query.addOrderByAscending(BCPropertyConstants.POSITION_NUMBER);
119         
120         List<BudgetConstructionPosition> allPositionLocks = (List<BudgetConstructionPosition>) getPersistenceBrokerTemplate().getCollectionByQuery(query);
121 
122         List<BudgetConstructionPosition> orphanedPositionLocks = new ArrayList<BudgetConstructionPosition>();
123         for (BudgetConstructionPosition position : allPositionLocks) {
124             Criteria criteria2 = new Criteria();
125             criteria2.addEqualTo(KFSPropertyConstants.UNIVERSITY_FISCAL_YEAR, position.getUniversityFiscalYear());
126             criteria2.addEqualTo(BCPropertyConstants.POSITION_NUMBER, position.getPositionNumber());
127             
128             Criteria subCrit = new Criteria();
129             subCrit.addEqualToField(KFSPropertyConstants.UNIVERSITY_FISCAL_YEAR, Criteria.PARENT_QUERY_PREFIX + KFSPropertyConstants.UNIVERSITY_FISCAL_YEAR);
130             subCrit.addEqualToField(KFSPropertyConstants.CHART_OF_ACCOUNTS_CODE, Criteria.PARENT_QUERY_PREFIX + KFSPropertyConstants.CHART_OF_ACCOUNTS_CODE);
131             subCrit.addEqualToField(KFSPropertyConstants.ACCOUNT_NUMBER, Criteria.PARENT_QUERY_PREFIX + KFSPropertyConstants.ACCOUNT_NUMBER);
132             subCrit.addEqualToField(KFSPropertyConstants.SUB_ACCOUNT_NUMBER, Criteria.PARENT_QUERY_PREFIX + KFSPropertyConstants.SUB_ACCOUNT_NUMBER);
133             subCrit.addEqualTo(BCPropertyConstants.APPOINTMENT_FUNDING_LOCK_USER_ID, position.getPositionLockUserIdentifier());
134             
135             ReportQueryByCriteria subQuery = QueryFactory.newReportQuery(BudgetConstructionFundingLock.class, subCrit);   
136             subQuery.setAttributes(new String[] { "1" });
137             criteria2.addExists(subQuery);
138             
139             List<PendingBudgetConstructionAppointmentFunding> appointmentFundingLocks = (List<PendingBudgetConstructionAppointmentFunding>) getPersistenceBrokerTemplate().getCollectionByQuery(QueryFactory.newQuery(PendingBudgetConstructionAppointmentFunding.class, criteria2));
140             if (appointmentFundingLocks == null || appointmentFundingLocks.isEmpty()) {
141                 orphanedPositionLocks.add(position);
142             }
143         }
144 
145         return orphanedPositionLocks;
146     }
147 
148     /**
149      * @see org.kuali.kfs.module.bc.document.dataaccess.BudgetConstructionLockDao#getAllTransactionLocks(java.lang.String)
150      */
151     public List<BudgetConstructionHeader> getAllTransactionLocks(String lockUnivId) {
152         Criteria criteria = new Criteria();
153 
154         if (StringUtils.isNotBlank(lockUnivId)) {
155             criteria.addEqualTo(BCPropertyConstants.BUDGET_TRANSACTION_LOCK_USER_IDENTIFIER, lockUnivId);
156         }
157         else {
158             criteria.addNotNull(BCPropertyConstants.BUDGET_TRANSACTION_LOCK_USER_IDENTIFIER);
159         }
160         
161         ReportQueryByCriteria query = QueryFactory.newReportQuery(BudgetConstructionHeader.class, criteria);
162         query.addOrderByAscending(KFSPropertyConstants.UNIVERSITY_FISCAL_YEAR);
163         query.addOrderByAscending(KFSPropertyConstants.CHART_OF_ACCOUNTS_CODE);
164         query.addOrderByAscending(KFSPropertyConstants.ACCOUNT_NUMBER);
165         query.addOrderByAscending(KFSPropertyConstants.SUB_ACCOUNT_NUMBER);
166 
167         return (List<BudgetConstructionHeader>) getPersistenceBrokerTemplate().getCollectionByQuery(query);
168     }
169 
170     /**
171      * @see org.kuali.kfs.module.bc.document.dataaccess.BudgetConstructionLockDao#getOrphanedFundingLocks(java.lang.String)
172      */
173     public List<BudgetConstructionFundingLock> getOrphanedFundingLocks(String lockUnivId) {
174         Criteria criteria = new Criteria();
175 
176         if (StringUtils.isNotBlank(lockUnivId)) {
177             criteria.addEqualTo(BCPropertyConstants.APPOINTMENT_FUNDING_LOCK_USER_ID, lockUnivId);
178         }
179         else {
180             criteria.addNotNull(BCPropertyConstants.APPOINTMENT_FUNDING_LOCK_USER_ID);
181         }
182         
183         ReportQueryByCriteria query = QueryFactory.newReportQuery(BudgetConstructionFundingLock.class, criteria);
184         query.addOrderByAscending(KFSPropertyConstants.UNIVERSITY_FISCAL_YEAR);
185         query.addOrderByAscending(KFSPropertyConstants.CHART_OF_ACCOUNTS_CODE);
186         query.addOrderByAscending(KFSPropertyConstants.ACCOUNT_NUMBER);
187         query.addOrderByAscending(KFSPropertyConstants.SUB_ACCOUNT_NUMBER);
188 
189         List<BudgetConstructionFundingLock> allFundingLocks = (List<BudgetConstructionFundingLock>) getPersistenceBrokerTemplate().getCollectionByQuery(query);
190 
191         List<BudgetConstructionFundingLock> orphanedFundingLocks = new ArrayList<BudgetConstructionFundingLock>();
192         for (BudgetConstructionFundingLock fundingLock : allFundingLocks) {
193             String positionNumber = budgetConstructionDao.getPositionAssociatedWithFundingLock(fundingLock);
194             if (BCConstants.POSITION_NUMBER_NOT_FOUND.equals(positionNumber)) {
195                 orphanedFundingLocks.add(fundingLock);
196             }
197         }
198 
199         return orphanedFundingLocks;
200     }
201 
202     /**
203      * Sets the budgetConstructionDao attribute value.
204      * 
205      * @param budgetConstructionDao The budgetConstructionDao to set.
206      */
207     public void setBudgetConstructionDao(BudgetConstructionDao budgetConstructionDao) {
208         this.budgetConstructionDao = budgetConstructionDao;
209     }
210 
211 }