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.cam.document.dataaccess.impl;
20  
21  import java.util.ArrayList;
22  import java.util.Collection;
23  import java.util.Iterator;
24  import java.util.List;
25  
26  import org.apache.ojb.broker.query.Criteria;
27  import org.apache.ojb.broker.query.QueryByCriteria;
28  import org.apache.ojb.broker.query.QueryFactory;
29  import org.apache.ojb.broker.query.ReportQueryByCriteria;
30  import org.kuali.kfs.module.cam.CamsPropertyConstants;
31  import org.kuali.kfs.module.cam.businessobject.Asset;
32  import org.kuali.kfs.module.cam.businessobject.AssetPayment;
33  import org.kuali.kfs.module.cam.document.dataaccess.AssetDepreciationUtilDao;
34  import org.kuali.kfs.sys.businessobject.FinancialSystemDocumentHeader;
35  import org.kuali.kfs.sys.businessobject.GeneralLedgerPendingEntry;
36  import org.kuali.kfs.sys.util.TransactionalServiceUtils;
37  import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb;
38  import org.kuali.rice.krad.service.BusinessObjectService;
39  import org.kuali.rice.krad.util.KRADPropertyConstants;
40  import org.springframework.transaction.annotation.Transactional;
41  
42  @Transactional
43  public class AssetDepreciationUtilDaoOjb extends PlatformAwareDaoBaseOjb implements AssetDepreciationUtilDao {
44  
45      private BusinessObjectService businessObjectService;    
46  
47      public String getMaxDocumentNumber() {
48          Criteria criteria = new Criteria();
49          ReportQueryByCriteria query = QueryFactory.newReportQuery(FinancialSystemDocumentHeader.class, new Criteria());
50  
51          query.setAttributes(new String[] { "max(" + KRADPropertyConstants.DOCUMENT_NUMBER + ")" });
52          Iterator iterator = getPersistenceBrokerTemplate().getReportQueryIteratorByQuery(query);
53          String maxDocumentNumber = "";
54          if (iterator.hasNext()) {
55              Object[] data = (Object[]) TransactionalServiceUtils.retrieveFirstAndExhaustIterator(iterator);
56              if (data[0] != null) {
57                  maxDocumentNumber= (String) data[0];
58              }
59          }
60          return maxDocumentNumber;    
61      }        
62  
63  
64      //    public List<GeneralLedgerPendingEntry> getGeneralLedgerPendingEntries(String documentNumber) {
65      //        Map<String, String> fieldValues = new HashMap<String, String>();
66      //        fieldValues.put(KRADPropertyConstants.DOCUMENT_NUMBER, documentNumber);
67      //        return (List<GeneralLedgerPendingEntry>) businessObjectService.findMatching(GeneralLedgerPendingEntry.class, fieldValues);
68      //    }   
69  
70  
71      public Collection<AssetPayment> getAssetPayments(List<Asset> assets) {
72          List<Long> capitalAssetNumbers = new ArrayList<Long>();
73  
74          for(Asset asset : assets) {
75              capitalAssetNumbers.add(asset.getCapitalAssetNumber());
76          }
77  
78          Criteria criteria = new Criteria();
79          criteria.addIn(CamsPropertyConstants.AssetPayment.CAPITAL_ASSET_NUMBER, capitalAssetNumbers);
80  
81          QueryByCriteria q = QueryFactory.newQuery(AssetPayment.class, criteria);
82          q.addOrderByAscending(CamsPropertyConstants.AssetPayment.CAPITAL_ASSET_NUMBER);
83          q.addOrderByAscending(CamsPropertyConstants.AssetPayment.PAYMENT_SEQ_NUMBER);
84          Collection<AssetPayment> assetPayments = getPersistenceBrokerTemplate().getCollectionByQuery(q);
85          return assetPayments;
86      }
87  
88      /**
89       * 
90       * @see org.kuali.kfs.module.cam.document.dataaccess.AssetDepreciationUtilDao#deleteAssetPayment()
91       */
92      public void deleteAssetPayment(List<Asset> assets) {
93          List<Long> capitalAssetNumbers = new ArrayList<Long>();
94          for(Asset asset : assets) {
95              capitalAssetNumbers.add(asset.getCapitalAssetNumber());
96          }        
97  
98          Criteria criteria = new Criteria();
99          QueryByCriteria q = QueryFactory.newQuery(AssetPayment.class, criteria);
100         getPersistenceBrokerTemplate().deleteByQuery(q);
101     }
102 
103     /**
104      * 
105      * @see org.kuali.kfs.module.cam.document.dataaccess.AssetDepreciationUtilDao#deleteAssets(java.util.List)
106      */
107     public void deleteAssets(List<Asset> assets) {
108         List<Long> capitalAssetNumbers = new ArrayList<Long>();
109         for(Asset asset : assets) {
110             capitalAssetNumbers.add(asset.getCapitalAssetNumber());
111         }        
112         Criteria criteria = new Criteria();
113         criteria.addIn(CamsPropertyConstants.AssetPayment.CAPITAL_ASSET_NUMBER, capitalAssetNumbers);
114         QueryByCriteria q = QueryFactory.newQuery(Asset.class, criteria);
115         getPersistenceBrokerTemplate().deleteByQuery(q);
116     }
117 
118     /**
119      * 
120      * @see org.kuali.kfs.module.cam.document.dataaccess.AssetDepreciationUtilDao#deleteGLPEs()
121      */
122     public void deleteGLPEs() {
123         QueryByCriteria q = QueryFactory.newQuery(GeneralLedgerPendingEntry.class, new Criteria());
124         getPersistenceBrokerTemplate().deleteByQuery(q);
125     }
126 }