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.util.ArrayList;
33 import java.util.Collection;
34 import java.util.List;
35
36
37
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
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
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
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
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
96
97
98
99
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
114
115
116
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
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
133 return returnList.get(0);
134 }
135 }
136
137
138
139
140
141
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
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
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
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
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
209
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
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 }