001: /*
002: * Copyright 2007 The Kuali Foundation.
003: *
004: * Licensed under the Educational Community License, Version 1.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.opensource.org/licenses/ecl1.php
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.kuali.module.pdp.dao.ojb;
017:
018: import java.math.BigDecimal;
019: import java.util.ArrayList;
020: import java.util.Iterator;
021: import java.util.List;
022:
023: import org.apache.ojb.broker.query.Criteria;
024: import org.apache.ojb.broker.query.QueryByCriteria;
025: import org.apache.ojb.broker.query.QueryFactory;
026: import org.apache.ojb.broker.query.ReportQueryByCriteria;
027: import org.kuali.core.dao.ojb.PlatformAwareDaoBaseOjb;
028: import org.kuali.core.exceptions.UserNotFoundException;
029: import org.kuali.core.service.UniversalUserService;
030: import org.kuali.module.pdp.bo.Batch;
031: import org.kuali.module.pdp.bo.PaymentGroup;
032: import org.kuali.module.pdp.bo.PaymentGroupHistory;
033: import org.kuali.module.pdp.bo.PaymentProcess;
034: import org.kuali.module.pdp.bo.UserRequired;
035: import org.kuali.module.pdp.dao.PaymentGroupDao;
036:
037: public class PaymentGroupDaoOjb extends PlatformAwareDaoBaseOjb
038: implements PaymentGroupDao {
039: private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger
040: .getLogger(PaymentGroupDaoOjb.class);
041:
042: private UniversalUserService userService;
043:
044: public PaymentGroupDaoOjb() {
045: super ();
046: }
047:
048: /**
049: * @see org.kuali.module.pdp.dao.PaymentGroupDao#getDisbursementNumbersByDisbursementType(java.lang.Integer, java.lang.String)
050: */
051: public List<Integer> getDisbursementNumbersByDisbursementType(
052: Integer pid, String disbursementType) {
053: LOG.debug("getDisbursementNumbersByDisbursementType() started");
054:
055: List<Integer> results = new ArrayList<Integer>();
056:
057: Criteria criteria = new Criteria();
058: criteria.addEqualTo("processId", pid);
059: criteria.addEqualTo("disbursementTypeCode", disbursementType);
060:
061: String[] fields = new String[] { "disbursementNbr" };
062:
063: ReportQueryByCriteria rq = QueryFactory.newReportQuery(
064: PaymentGroup.class, fields, criteria, true);
065: rq.addOrderBy("disbursementNbr", true);
066:
067: Iterator i = getPersistenceBrokerTemplate()
068: .getReportQueryIteratorByQuery(rq);
069: while (i.hasNext()) {
070: Object[] data = (Object[]) i.next();
071: BigDecimal d = (BigDecimal) data[0];
072: results.add(new Integer(d.intValue()));
073: }
074: return results;
075: }
076:
077: /**
078: * @see org.kuali.module.pdp.dao.PaymentGroupDao#getByDisbursementTypeStatusCode(java.lang.String, java.lang.String)
079: */
080: public Iterator getByDisbursementTypeStatusCode(
081: String disbursementType, String paymentStatusCode) {
082: LOG.debug("getByDisbursementTypeStatusCode() started");
083:
084: Criteria criteria = new Criteria();
085: criteria.addEqualTo("disbursementTypeCode", disbursementType);
086: criteria.addEqualTo("paymentStatusCode", paymentStatusCode);
087:
088: QueryByCriteria qbc = new QueryByCriteria(PaymentGroup.class,
089: criteria);
090: qbc.addOrderBy("disbursementNbr", true);
091:
092: return getPersistenceBrokerTemplate().getIteratorByQuery(qbc);
093: }
094:
095: /**
096: * @see org.kuali.module.pdp.dao.PaymentGroupDao#getByProcessId(java.lang.Integer)
097: */
098: public Iterator getByProcessId(Integer pid) {
099: LOG.debug("getByProcessId() started");
100:
101: Criteria criteria = new Criteria();
102: criteria.addEqualTo("processId", pid);
103:
104: QueryByCriteria qbc = new QueryByCriteria(PaymentGroup.class,
105: criteria);
106: qbc.addOrderBy("sortValue", true);
107: qbc.addOrderBy("payeeName", true);
108: qbc.addOrderBy("line1Address", true);
109:
110: return getPersistenceBrokerTemplate().getIteratorByQuery(qbc);
111: }
112:
113: /**
114: * @see org.kuali.module.pdp.dao.PaymentGroupDao#getByProcess(org.kuali.module.pdp.bo.PaymentProcess)
115: */
116: public Iterator getByProcess(PaymentProcess p) {
117: LOG.debug("getByProcess() started");
118:
119: return getByProcessId(p.getId());
120: }
121:
122: public PaymentGroup get(Integer id) {
123: LOG.debug("get(id) started");
124: List data = new ArrayList();
125:
126: Criteria criteria = new Criteria();
127: criteria.addEqualTo("id", id);
128:
129: PaymentGroup cp = (PaymentGroup) getPersistenceBrokerTemplate()
130: .getObjectByQuery(
131: new QueryByCriteria(PaymentGroup.class,
132: criteria));
133:
134: if (cp.getBatch() != null) {
135: updateBatchUser(cp.getBatch());
136: }
137: if (cp.getProcess() != null) {
138: updateProcessUser(cp.getProcess());
139: }
140: if (cp.getPaymentGroupHistory() != null) {
141: updatePaymentGroupHistoryList(cp.getPaymentGroupHistory());
142: }
143: return cp;
144: }
145:
146: public void save(PaymentGroup pg) {
147: LOG.debug("save() started");
148:
149: getPersistenceBrokerTemplate().store(pg);
150: }
151:
152: public List getByDisbursementNumber(Integer disbursementNbr) {
153: LOG.debug("getByDisbursementNumber() enter method");
154: Criteria criteria = new Criteria();
155: criteria.addEqualTo("disbursementNbr", disbursementNbr);
156: return (List) getPersistenceBrokerTemplate()
157: .getCollectionByQuery(
158: new QueryByCriteria(PaymentGroup.class,
159: criteria));
160: }
161:
162: public List getByBatchId(Integer batchId) {
163: LOG.debug("getByBatchId() enter method");
164: Criteria criteria = new Criteria();
165: criteria.addEqualTo("batchId", batchId);
166: return (List) getPersistenceBrokerTemplate()
167: .getCollectionByQuery(
168: new QueryByCriteria(PaymentGroup.class,
169: criteria));
170: }
171:
172: private void updatePaymentGroupHistoryList(List l) {
173: for (Iterator iter = l.iterator(); iter.hasNext();) {
174: PaymentGroupHistory element = (PaymentGroupHistory) iter
175: .next();
176: updateChangeUser(element);
177: }
178: }
179:
180: private void updateChangeUser(PaymentGroupHistory b) {
181: UserRequired ur = (UserRequired) b;
182: try {
183: ur.updateUser(userService);
184: } catch (UserNotFoundException e) {
185: b.setChangeUser(null);
186: }
187: }
188:
189: private void updateBatchUser(Batch b) {
190: UserRequired ur = (UserRequired) b;
191: try {
192: ur.updateUser(userService);
193: } catch (UserNotFoundException e) {
194: b.setSubmiterUser(null);
195: }
196: }
197:
198: private void updateProcessUser(PaymentProcess b) {
199: UserRequired ur = (UserRequired) b;
200: try {
201: ur.updateUser(userService);
202: } catch (UserNotFoundException e) {
203: b.setProcessUser(null);
204: }
205: }
206:
207: // Inject
208: public void setUniversalUserService(UniversalUserService us) {
209: userService = us;
210: }
211: }
|