View Javadoc

1   /*
2    * Copyright 2007 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.module.purap.document.dataaccess.impl;
17  
18  import org.apache.ojb.broker.query.Criteria;
19  import org.apache.ojb.broker.query.QueryByCriteria;
20  import org.apache.ojb.broker.query.QueryFactory;
21  import org.apache.ojb.broker.query.ReportQueryByCriteria;
22  import org.kuali.ole.module.purap.PurapPropertyConstants;
23  import org.kuali.ole.module.purap.businessobject.AutoClosePurchaseOrderView;
24  import org.kuali.ole.module.purap.businessobject.PurchaseOrderItem;
25  import org.kuali.ole.module.purap.document.PurchaseOrderDocument;
26  import org.kuali.ole.module.purap.document.dataaccess.PurchaseOrderDao;
27  import org.kuali.ole.sys.OLEPropertyConstants;
28  import org.kuali.rice.core.api.util.type.KualiDecimal;
29  import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb;
30  import org.springframework.transaction.annotation.Transactional;
31  
32  import java.util.ArrayList;
33  import java.util.Collection;
34  import java.util.List;
35  
36  /**
37   * OJB implementation of PurchaseOrderDao.
38   */
39  @Transactional
40  public class PurchaseOrderDaoOjb extends PlatformAwareDaoBaseOjb implements PurchaseOrderDao {
41      private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(PurchaseOrderDaoOjb.class);
42  
43      public Integer getPurchaseOrderIdForCurrentPurchaseOrderByRelatedDocId(Integer accountsPayablePurchasingDocumentLinkIdentifier) {
44          Criteria criteria = new Criteria();
45          criteria.addEqualTo("accountsPayablePurchasingDocumentLinkIdentifier", accountsPayablePurchasingDocumentLinkIdentifier);
46          criteria.addEqualTo(PurapPropertyConstants.PURCHASE_ORDER_CURRENT_INDICATOR, "Y");
47  
48          Collection<PurchaseOrderDocument> poList = getPersistenceBrokerTemplate().getCollectionByQuery(new QueryByCriteria(PurchaseOrderDocument.class, criteria));
49          for (PurchaseOrderDocument purchaseOrderDocument : poList) {
50              //should be only one
51              return purchaseOrderDocument.getPurapDocumentIdentifier();
52          }
53  
54          return null;
55      }
56  
57      public PurchaseOrderDocument getCurrentPurchaseOrder(Integer id) {
58          Criteria criteria = new Criteria();
59          criteria.addEqualTo(PurapPropertyConstants.PURAP_DOC_ID, id);
60          criteria.addEqualTo(PurapPropertyConstants.PURCHASE_ORDER_CURRENT_INDICATOR, "Y");
61  
62          return (PurchaseOrderDocument) getPersistenceBrokerTemplate().getObjectByQuery(new QueryByCriteria(PurchaseOrderDocument.class, criteria));
63      }
64  
65  
66      /**
67       * @see org.kuali.ole.module.purap.document.dataaccess.PurchaseOrderDao#getDocumentNumberForPurchaseOrderId(java.lang.Integer)
68       */
69      public String getDocumentNumberForPurchaseOrderId(Integer id) {
70          Criteria criteria = new Criteria();
71          criteria.addEqualTo(PurapPropertyConstants.PURAP_DOC_ID, id);
72  
73          return getDocumentNumberUsingPurchaseOrderCriteria(criteria);
74      }
75  
76      /**
77       * @see org.kuali.ole.module.purap.document.dataaccess.PurchaseOrderDao#getDocumentNumberForCurrentPurchaseOrder(java.lang.Integer)
78       */
79      public String getDocumentNumberForCurrentPurchaseOrder(Integer id) {
80          Criteria criteria = new Criteria();
81          criteria.addEqualTo(PurapPropertyConstants.PURAP_DOC_ID, id);
82          criteria.addEqualTo(PurapPropertyConstants.PURCHASE_ORDER_CURRENT_INDICATOR, "Y");
83  
84          return getDocumentNumberUsingPurchaseOrderCriteria(criteria);
85      }
86  
87      /**
88       * @see org.kuali.ole.module.purap.document.dataaccess.PurchaseOrderDao#getOldestPurchaseOrderDocumentNumber(java.lang.Integer)
89       */
90      public String getOldestPurchaseOrderDocumentNumber(Integer id) {
91          Criteria criteria = new Criteria();
92          criteria.addEqualTo(PurapPropertyConstants.PURAP_DOC_ID, id);
93          ReportQueryByCriteria rqbc = QueryFactory.newReportQuery(PurchaseOrderDocument.class, criteria);
94          rqbc.setAttributes(new String[]{OLEPropertyConstants.DOCUMENT_NUMBER});
95          //the documents need to be sorted in descending order because we want the 
96          //the oldest document number to get the oldest purchase order
97          //because the notes remoteobjectid is set to the object id of the oldest
98          //purchase order document.
99          //OLEMI-8394
100         rqbc.addOrderByDescending(OLEPropertyConstants.DOCUMENT_NUMBER);
101 
102         String oldestDocumentNumber = null;
103         java.util.Iterator iter = getPersistenceBrokerTemplate().getReportQueryIteratorByQuery(rqbc);
104         while (iter.hasNext()) {
105             final Object[] results = (Object[]) iter.next();
106             oldestDocumentNumber = (String) results[0];
107         }
108 
109         return oldestDocumentNumber;
110     }
111 
112     /**
113      * Retrieves the document number of the purchase order returned by the passed in criteria.
114      *
115      * @param criteria - list of criteria to use in the retrieve
116      * @return Document number string if a valid purchase order is found, null if no purchase order is found
117      */
118     protected String getDocumentNumberUsingPurchaseOrderCriteria(Criteria criteria) {
119         List<String> returnList = getDocumentNumbersUsingPurchaseOrderCriteria(criteria);
120 
121         if (returnList.isEmpty()) {
122             return null;
123         }
124 
125         if (returnList.size() > 1) {
126             // the list should have held only a single doc id of data but it holds 2 or more
127             String errorMsg = "Expected single document number for given criteria but multiple (at least 2) were returned";
128             LOG.error(errorMsg);
129             throw new RuntimeException();
130 
131         } else {
132             // at this part of the code, we know there's no more elements in iterator
133             return returnList.get(0);
134         }
135     }
136 
137     /**
138      * Retrieves a list of document numbers of the purchase order returned by the passed in criteria.
139      *
140      * @param criteria - list of criteria to use in the retrieve
141      * @return Iterator of document numbers
142      */
143     protected List<String> getDocumentNumbersUsingPurchaseOrderCriteria(Criteria criteria) {
144         ReportQueryByCriteria rqbc = new ReportQueryByCriteria(PurchaseOrderDocument.class, criteria);
145         List<String> returnList = new ArrayList<String>();
146 
147         rqbc.addOrderByAscending(OLEPropertyConstants.DOCUMENT_NUMBER);
148 
149         List<PurchaseOrderDocument> poDocs = (List<PurchaseOrderDocument>) getPersistenceBrokerTemplate().getCollectionByQuery(rqbc);
150 
151         for (PurchaseOrderDocument poDoc : poDocs) {
152             returnList.add(poDoc.getDocumentNumber());
153         }
154 
155         return returnList;
156     }
157 
158     /**
159      * @see org.kuali.ole.module.purap.document.dataaccess.PurchaseOrderDao#itemExistsOnPurchaseOrder(java.lang.Integer, java.lang.String)
160      */
161     public boolean itemExistsOnPurchaseOrder(Integer poItemLineNumber, String docNumber) {
162         boolean existsInPo = false;
163 
164         Criteria criteria = new Criteria();
165         criteria.addEqualTo("documentNumber", docNumber);
166         criteria.addEqualTo("itemLineNumber", poItemLineNumber);
167 
168         ReportQueryByCriteria rqbc = new ReportQueryByCriteria(PurchaseOrderItem.class, criteria);
169         //  rqbc.setAttributes(new String[] { OLEPropertyConstants.DOCUMENT_NUMBER });
170         rqbc.addOrderByAscending(OLEPropertyConstants.DOCUMENT_NUMBER);
171 
172         List<PurchaseOrderItem> poItems = (List<PurchaseOrderItem>) getPersistenceBrokerTemplate().getCollectionByQuery(rqbc);
173         if (!poItems.isEmpty()) {
174             existsInPo = true;
175         }
176 
177         return existsInPo;
178     }
179 
180     /**
181      * @see org.kuali.ole.module.purap.document.dataaccess.PurchaseOrderDao#getAllOpenPurchaseOrders(java.util.List)
182      */
183     public List<AutoClosePurchaseOrderView> getAllOpenPurchaseOrders(List<String> excludedVendorChoiceCodes) {
184         LOG.debug("getAllOpenPurchaseOrders() started");
185         Criteria criteria = new Criteria();
186         criteria.addIsNull(PurapPropertyConstants.RECURRING_PAYMENT_TYPE_CODE);
187         criteria.addEqualTo(PurapPropertyConstants.TOTAL_ENCUMBRANCE, new KualiDecimal(0));
188         criteria.addEqualTo(PurapPropertyConstants.PURCHASE_ORDER_CURRENT_INDICATOR, true);
189         for (String excludeCode : excludedVendorChoiceCodes) {
190             criteria.addNotEqualTo(PurapPropertyConstants.VENDOR_CHOICE_CODE, excludeCode);
191         }
192         QueryByCriteria qbc = new QueryByCriteria(AutoClosePurchaseOrderView.class, criteria);
193         if (LOG.isDebugEnabled()) {
194             LOG.debug("getAllOpenPurchaseOrders() Query criteria is " + criteria.toString());
195         }
196         List<AutoClosePurchaseOrderView> l = (List<AutoClosePurchaseOrderView>) getPersistenceBrokerTemplate().getCollectionByQuery(qbc);
197         LOG.debug("getAllOpenPurchaseOrders() ended.");
198         return l;
199     }
200 
201     /**
202      * @see org.kuali.ole.module.purap.document.dataaccess.PurchaseOrderDao#getAutoCloseRecurringPurchaseOrders(java.util.List)
203      */
204     public List<AutoClosePurchaseOrderView> getAutoCloseRecurringPurchaseOrders(List<String> excludedVendorChoiceCodes) {
205         LOG.debug("getAutoCloseRecurringPurchaseOrders() started.");
206         Criteria criteria = new Criteria();
207         criteria.addNotNull(PurapPropertyConstants.RECURRING_PAYMENT_TYPE_CODE);
208         //PURCHASE_ORDER_STATUS_CODE does not exist in tables anymore but it is on workflowdocument.
209         //the checking for open status is done in PurchaseOrderServiceImpl class - autoCloseRecurringOrders method.
210         for (String excludeCode : excludedVendorChoiceCodes) {
211             criteria.addNotEqualTo(PurapPropertyConstants.VENDOR_CHOICE_CODE, excludeCode);
212         }
213         QueryByCriteria qbc = new QueryByCriteria(AutoClosePurchaseOrderView.class, criteria);
214         if (LOG.isDebugEnabled()) {
215             LOG.debug("getAutoCloseRecurringPurchaseOrders() Query criteria is " + criteria.toString());
216         }
217         List<AutoClosePurchaseOrderView> l = (List<AutoClosePurchaseOrderView>) getPersistenceBrokerTemplate().getCollectionByQuery(qbc);
218         //we need to include in this list only those whose workflowdocument appDocStatus = APPDOC_OPEN
219 
220         LOG.debug("getAutoCloseRecurringPurchaseOrders() ended.");
221 
222         return l;
223     }
224 
225     public List<PurchaseOrderDocument> getPendingPurchaseOrdersForFaxing() {
226         LOG.debug("Getting pending purchase orders for faxing");
227         Criteria criteria = new Criteria();
228         QueryByCriteria qbc = new QueryByCriteria(PurchaseOrderDocument.class, criteria);
229         List<PurchaseOrderDocument> l = (List<PurchaseOrderDocument>) getPersistenceBrokerTemplate().getCollectionByQuery(qbc);
230 
231         return l;
232     }
233 }