1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
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
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
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
80
81 @Override
82 public boolean duplicateExists(Integer vendorNumberHeaderId, Integer vendorNumberDetailId, String creditMemoNumber) {
83 LOG.debug("duplicateExists() started");
84
85
86 Criteria criteria = new Criteria();
87 criteria.addEqualTo("vendorHeaderGeneratedIdentifier", vendorNumberHeaderId);
88 criteria.addEqualTo("vendorDetailAssignedIdentifier", vendorNumberDetailId);
89 criteria.addEqualTo("creditMemoNumber", creditMemoNumber);
90
91
92
93
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
104
105 @Override
106 public boolean duplicateExists(Integer vendorNumberHeaderId, Integer vendorNumberDetailId, Date date, KualiDecimal amount) {
107 LOG.debug("duplicateExists() started");
108
109
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
117
118
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
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
139
140
141
142
143 protected String getDocumentNumberOfCreditMemoByCriteria(Criteria criteria) {
144 LOG.debug("getDocumentNumberOfCreditMemoByCriteria() started");
145
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
162
163
164
165
166
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
196 QueryByCriteria qbc = new QueryByCriteria(PaymentRequestDocument.class, criteria);
197
198 returnList = getDocumentNumbersOfCreditMemoByCriteria(criteria, false);
199 return returnList;
200 }
201 }