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  
19  import org.apache.ojb.broker.query.Criteria;
20  import org.apache.ojb.broker.query.QueryByCriteria;
21  import org.apache.ojb.broker.query.ReportQueryByCriteria;
22  import org.kuali.ole.module.purap.PurapPropertyConstants;
23  import org.kuali.ole.module.purap.document.PaymentRequestDocument;
24  import org.kuali.ole.module.purap.document.VendorCreditMemoDocument;
25  import org.kuali.ole.module.purap.document.dataaccess.CreditMemoDao;
26  import org.kuali.ole.module.purap.util.VendorGroupingHelper;
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  
31  import java.sql.Date;
32  import java.util.ArrayList;
33  import java.util.Collection;
34  import java.util.List;
35  
36  /**
37   * OJB Implementation of CreditMemoDao. Provides persistence layer methods for the credit memo document.
38   */
39  public class CreditMemoDaoOjb extends PlatformAwareDaoBaseOjb implements CreditMemoDao {
40      private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(CreditMemoDaoOjb.class);
41  
42      /**
43       * @see org.kuali.ole.module.purap.document.dataaccess.CreditMemoDao#getCreditMemosToExtract(java.lang.String)
44       */
45      @Override
46      public List<VendorCreditMemoDocument> getCreditMemosToExtract(String chartCode) {
47          LOG.debug("getCreditMemosToExtract() started");
48  
49          Criteria criteria = new Criteria();
50          criteria.addEqualTo("processingCampusCode", chartCode);
51          criteria.addIsNull("extractedTimestamp");
52          criteria.addEqualTo("holdIndicator", Boolean.FALSE);
53  
54          return (List<VendorCreditMemoDocument>) getPersistenceBrokerTemplate().getCollectionByQuery(new QueryByCriteria(VendorCreditMemoDocument.class, criteria));
55      }
56  
57  
58      /**
59       * @see org.kuali.ole.module.purap.document.dataaccess.CreditMemoDao#getCreditMemosToExtractByVendor(java.lang.String, java.lang.Integer, java.lang.Integer)
60       */
61      @Override
62      public Collection<VendorCreditMemoDocument> getCreditMemosToExtractByVendor(String chartCode, VendorGroupingHelper vendor) {
63          LOG.debug("getCreditMemosToExtractByVendor() started");
64  
65          Criteria criteria = new Criteria();
66          criteria.addEqualTo("processingCampusCode", chartCode);
67          criteria.addIsNull("extractedTimestamp");
68          criteria.addEqualTo("holdIndicator", Boolean.FALSE);
69          criteria.addEqualTo("vendorHeaderGeneratedIdentifier", vendor.getVendorHeaderGeneratedIdentifier());
70          criteria.addEqualTo("vendorDetailAssignedIdentifier", vendor.getVendorDetailAssignedIdentifier());
71          criteria.addEqualTo("vendorCountryCode", vendor.getVendorCountry());
72          criteria.addLike("vendorPostalCode", vendor.getVendorPostalCode() + "%");
73  
74          return getPersistenceBrokerTemplate().getCollectionByQuery(new QueryByCriteria(VendorCreditMemoDocument.class, criteria));
75      }
76  
77  
78      /**
79       * @see edu.iu.uis.pur.cm.dao.CreditMemoDao#duplicateExists(java.lang.String, java.lang.String)
80       */
81      @Override
82      public boolean duplicateExists(Integer vendorNumberHeaderId, Integer vendorNumberDetailId, String creditMemoNumber) {
83          LOG.debug("duplicateExists() started");
84  
85          // criteria: vendorNumberHeader AND vendorNumberDetail AND creditMemoNumber
86          Criteria criteria = new Criteria();
87          criteria.addEqualTo("vendorHeaderGeneratedIdentifier", vendorNumberHeaderId);
88          criteria.addEqualTo("vendorDetailAssignedIdentifier", vendorNumberDetailId);
89          criteria.addEqualTo("creditMemoNumber", creditMemoNumber);
90  
91          // use the criteria to do a Count against the DB, and return the resulting
92          // number. Any positive non-zero result means that a potential duplicate
93          // exists and we return true, otherwise, return false.
94          int cmCount = getPersistenceBrokerTemplate().getCount(new QueryByCriteria(VendorCreditMemoDocument.class, criteria));
95          if (cmCount > 0) {
96              return true;
97          } else {
98              return false;
99          }
100     }
101 
102     /**
103      * @see edu.iu.uis.pur.cm.dao.CreditMemoDao#duplicateExists(java.lang.String, java.lang.String)
104      */
105     @Override
106     public boolean duplicateExists(Integer vendorNumberHeaderId, Integer vendorNumberDetailId, Date date, KualiDecimal amount) {
107         LOG.debug("duplicateExists() started");
108 
109         // criteria: vendorNumberHeader AND vendorNumberDetail AND date AND amount
110         Criteria criteria = new Criteria();
111         criteria.addEqualTo("vendorHeaderGeneratedIdentifier", vendorNumberHeaderId);
112         criteria.addEqualTo("vendorDetailAssignedIdentifier", vendorNumberDetailId);
113         criteria.addEqualTo("creditMemoDate", date);
114         criteria.addEqualTo("creditMemoAmount", amount);
115 
116         // use the criteria to do a Count against the DB, and return the resulting
117         // number. Any positive non-zero result means that a potential duplicate
118         // exists and we return true, otherwise, return false.
119         int cmCount = getPersistenceBrokerTemplate().getCount(new QueryByCriteria(VendorCreditMemoDocument.class, criteria));
120         if (cmCount > 0) {
121             return true;
122         } else {
123             return false;
124         }
125     }
126 
127     /**
128      * @see org.kuali.ole.module.purap.document.dataaccess.CreditMemoDao#getDocumentNumberByCreditMemoId(java.lang.Integer)
129      */
130     @Override
131     public String getDocumentNumberByCreditMemoId(Integer id) {
132         Criteria criteria = new Criteria();
133         criteria.addEqualTo(PurapPropertyConstants.PURAP_DOC_ID, id);
134         return getDocumentNumberOfCreditMemoByCriteria(criteria);
135     }
136 
137     /**
138      * Retrieves a document number for a credit memo by user defined criteria.
139      *
140      * @param criteria - holds field and value pairs defined by the calling method
141      * @return - document number
142      */
143     protected String getDocumentNumberOfCreditMemoByCriteria(Criteria criteria) {
144         LOG.debug("getDocumentNumberOfCreditMemoByCriteria() started");
145         //   Iterator<Object[]> iter = getDocumentNumbersOfCreditMemoByCriteria(criteria, false);
146         List<String> returnList = getDocumentNumbersOfCreditMemoByCriteria(criteria, false);
147         if (returnList.isEmpty()) {
148             return null;
149         }
150 
151         if (returnList.size() > 1) {
152             String errorMsg = "Expected single document number for given criteria but multiple (at least 2) were returned";
153             LOG.error(errorMsg);
154             throw new RuntimeException();
155         } else {
156             return returnList.get(0);
157         }
158     }
159 
160     /**
161      * Retrieves a document number for a credit memo by user defined criteria and sorts the values ascending if orderByAscending
162      * parameter is true, descending otherwise.
163      *
164      * @param criteria         - list of criteria to use in the retrieve
165      * @param orderByAscending - boolean indicating results should be sorted ascending, descending otherwise
166      * @return - Iterator of document numbers
167      */
168     protected List<String> getDocumentNumbersOfCreditMemoByCriteria(Criteria criteria, boolean orderByAscending) {
169         LOG.debug("getDocumentNumberOfCreditMemoByCriteria() started");
170         ReportQueryByCriteria rqbc = new ReportQueryByCriteria(VendorCreditMemoDocument.class, criteria);
171         if (orderByAscending) {
172             rqbc.addOrderByAscending(OLEPropertyConstants.DOCUMENT_NUMBER);
173         } else {
174             rqbc.addOrderByDescending(OLEPropertyConstants.DOCUMENT_NUMBER);
175         }
176 
177         List<VendorCreditMemoDocument> vcmds = (List<VendorCreditMemoDocument>) getPersistenceBrokerTemplate().getCollectionByQuery(rqbc);
178 
179         List<String> returnList = new ArrayList<String>();
180 
181         for (VendorCreditMemoDocument vcmd : vcmds) {
182             returnList.add(vcmd.getDocumentNumber());
183         }
184         return returnList;
185     }
186 
187     @Override
188     public List<String> getActiveCreditMemoDocumentNumbersForPurchaseOrder(Integer purchaseOrderId) {
189         LOG.debug("getActiveCreditmemoDocumentNumbersForPurchaseOrder() started");
190 
191         List<String> returnList = new ArrayList<String>();
192         Criteria criteria = new Criteria();
193 
194         criteria.addEqualTo(PurapPropertyConstants.PURCHASE_ORDER_IDENTIFIER, purchaseOrderId);
195         //criteria.addIn(PurapPropertyConstants.STATUS_CODE, Arrays.asList(CreditMemoStatuses.STATUSES_POTENTIALLY_ACTIVE));
196         QueryByCriteria qbc = new QueryByCriteria(PaymentRequestDocument.class, criteria);
197 
198         returnList = getDocumentNumbersOfCreditMemoByCriteria(criteria, false);
199         return returnList;
200     }
201 }