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 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
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
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
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
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
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
97
98
99
100
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
115
116
117
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
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
134 return returnList.get(0);
135 }
136 }
137
138
139
140
141
142
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
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
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
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
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
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
237
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
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 }