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