001: /*
002: * Copyright 2005-2007 The Kuali Foundation.
003: *
004: *
005: * Licensed under the Educational Community License, Version 1.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.opensource.org/licenses/ecl1.php
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package edu.iu.uis.eden.routeheader.dao;
018:
019: import java.sql.Connection;
020: import java.sql.PreparedStatement;
021: import java.sql.ResultSet;
022: import java.sql.SQLException;
023: import java.util.ArrayList;
024: import java.util.Collection;
025: import java.util.Iterator;
026: import java.util.Set;
027:
028: import org.apache.ojb.broker.PersistenceBroker;
029: import org.apache.ojb.broker.accesslayer.LookupException;
030: import org.apache.ojb.broker.query.Criteria;
031: import org.apache.ojb.broker.query.QueryByCriteria;
032: import org.kuali.rice.resourceloader.GlobalResourceLoader;
033: import org.springframework.dao.CannotAcquireLockException;
034: import org.springmodules.orm.ojb.OjbFactoryUtils;
035: import org.springmodules.orm.ojb.PersistenceBrokerCallback;
036: import org.springmodules.orm.ojb.support.PersistenceBrokerDaoSupport;
037:
038: import edu.iu.uis.eden.EdenConstants;
039: import edu.iu.uis.eden.KEWServiceLocator;
040: import edu.iu.uis.eden.actionitem.ActionItem;
041: import edu.iu.uis.eden.actionlist.ActionListService;
042: import edu.iu.uis.eden.database.platform.Platform;
043: import edu.iu.uis.eden.docsearch.SearchableAttributeValue;
044: import edu.iu.uis.eden.exception.EdenUserNotFoundException;
045: import edu.iu.uis.eden.exception.LockingException;
046: import edu.iu.uis.eden.exception.WorkflowRuntimeException;
047: import edu.iu.uis.eden.routeheader.DocumentRouteHeaderValue;
048: import edu.iu.uis.eden.routeheader.DocumentRouteHeaderValueContent;
049:
050: public class DocumentRouteHeaderDAOOjbImpl extends
051: PersistenceBrokerDaoSupport implements DocumentRouteHeaderDAO {
052:
053: private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger
054: .getLogger(DocumentRouteHeaderDAOOjbImpl.class);
055:
056: public void saveRouteHeader(DocumentRouteHeaderValue routeHeader) {
057: this .getPersistenceBrokerTemplate().store(routeHeader);
058: routeHeader.getDocumentContent().setRouteHeaderId(
059: routeHeader.getRouteHeaderId());
060: this .getPersistenceBrokerTemplate().store(
061: routeHeader.getDocumentContent());
062: }
063:
064: public DocumentRouteHeaderValueContent getContent(Long routeHeaderId) {
065: Criteria crit = new Criteria();
066: crit.addEqualTo("routeHeaderId", routeHeaderId);
067: return (DocumentRouteHeaderValueContent) this
068: .getPersistenceBrokerTemplate().getObjectByQuery(
069: new QueryByCriteria(
070: DocumentRouteHeaderValueContent.class,
071: crit));
072: }
073:
074: public void clearRouteHeaderSearchValues(
075: DocumentRouteHeaderValue routeHeader) {
076: Criteria crit = new Criteria();
077: crit
078: .addEqualTo("routeHeaderId", routeHeader
079: .getRouteHeaderId());
080: this .getPersistenceBrokerTemplate().deleteByQuery(
081: new QueryByCriteria(SearchableAttributeValue.class,
082: crit));
083: }
084:
085: public void lockRouteHeader(final Long routeHeaderId,
086: final boolean wait) {
087:
088: /*
089: * String sql = (wait ? LOCK_SQL_WAIT : LOCK_SQL_NOWAIT); try { getJdbcTemplate().update(sql, new Object[] { routeHeaderId }); } catch (CannotAcquireLockException e) { throw new LockingException("Could not aquire lock on document, routeHeaderId=" + routeHeaderId, e); }
090: */
091:
092: this .getPersistenceBrokerTemplate().execute(
093: new PersistenceBrokerCallback() {
094: public Object doInPersistenceBroker(
095: PersistenceBroker broker) {
096: PreparedStatement statement = null;
097: try {
098: Connection connection = broker
099: .serviceConnectionManager()
100: .getConnection();
101: String sql = getPlatform()
102: .getLockRouteHeaderQuerySQL(
103: routeHeaderId, wait);
104: statement = connection
105: .prepareStatement(sql);
106: statement.setLong(1, routeHeaderId
107: .longValue());
108: statement.execute();
109: return null;
110: } catch (SQLException e) {
111: throw new LockingException(
112: "Could not aquire lock on document, routeHeaderId="
113: + routeHeaderId, e);
114: } catch (LookupException e) {
115: throw new LockingException(
116: "Could not aquire lock on document, routeHeaderId="
117: + routeHeaderId, e);
118: } catch (CannotAcquireLockException e) {
119: throw new LockingException(
120: "Could not aquire lock on document, routeHeaderId="
121: + routeHeaderId, e);
122: } finally {
123: if (statement != null) {
124: try {
125: statement.close();
126: } catch (SQLException e) {
127: }
128: }
129: }
130: }
131: });
132:
133: }
134:
135: public DocumentRouteHeaderValue findRouteHeader(Long routeHeaderId) {
136: return findRouteHeader(routeHeaderId, false);
137: }
138:
139: public DocumentRouteHeaderValue findRouteHeader(Long routeHeaderId,
140: boolean clearCache) {
141: Criteria crit = new Criteria();
142: crit.addEqualTo("routeHeaderId", routeHeaderId);
143: if (clearCache) {
144: this .getPersistenceBrokerTemplate().clearCache();
145: }
146: return (DocumentRouteHeaderValue) this
147: .getPersistenceBrokerTemplate().getObjectByQuery(
148: new QueryByCriteria(
149: DocumentRouteHeaderValue.class, crit));
150: }
151:
152: public void deleteRouteHeader(DocumentRouteHeaderValue routeHeader) {
153: // need to clear action list cache for users who have this item in their action list
154: ActionListService actionListSrv = KEWServiceLocator
155: .getActionListService();
156: Collection actionItems = actionListSrv
157: .findByRouteHeaderId(routeHeader.getRouteHeaderId());
158: for (Iterator iter = actionItems.iterator(); iter.hasNext();) {
159: ActionItem actionItem = (ActionItem) iter.next();
160: try {
161: KEWServiceLocator.getUserOptionsService()
162: .saveRefreshUserOption(actionItem.getUser());
163: } catch (EdenUserNotFoundException e) {
164: LOG.error("error saving refreshUserOption", e);
165: }
166: }
167: this .getPersistenceBrokerTemplate().delete(routeHeader);
168: }
169:
170: public Long getNextRouteHeaderId() {
171: return (Long) this .getPersistenceBrokerTemplate().execute(
172: new PersistenceBrokerCallback() {
173: public Object doInPersistenceBroker(
174: PersistenceBroker broker) {
175: return getPlatform().getNextValSQL(
176: "SEQ_DOCUMENT_ROUTE_HEADER", broker);
177: }
178: });
179: }
180:
181: protected Platform getPlatform() {
182: return (Platform) GlobalResourceLoader
183: .getService(KEWServiceLocator.DB_PLATFORM);
184: }
185:
186: public Collection findPendingByResponsibilityIds(
187: Set responsibilityIds) {
188: Collection routeHeaderIds = new ArrayList();
189: if (responsibilityIds.isEmpty()) {
190: return routeHeaderIds;
191: }
192: PersistenceBroker broker = null;
193: Connection conn = null;
194: ResultSet rs = null;
195: try {
196: broker = getPersistenceBroker(false);
197: conn = broker.serviceConnectionManager().getConnection();
198: String respIds = "(";
199: int index = 0;
200: for (Iterator iterator = responsibilityIds.iterator(); iterator
201: .hasNext(); index++) {
202: Long responsibilityId = (Long) iterator.next();
203: respIds += responsibilityId
204: + (index == responsibilityIds.size() - 1 ? ""
205: : ",");
206: }
207: respIds += ")";
208: String query = "SELECT DISTINCT(doc_hdr_id) FROM EN_ACTN_RQST_T "
209: + "WHERE (ACTN_RQST_STAT_CD='"
210: + EdenConstants.ACTION_REQUEST_INITIALIZED
211: + "' OR ACTN_RQST_STAT_CD='"
212: + EdenConstants.ACTION_REQUEST_ACTIVATED
213: + "') AND ACTN_RQST_RESP_ID IN " + respIds;
214: LOG.debug("Query to find pending documents for requeue: "
215: + query);
216: rs = conn.createStatement().executeQuery(query);
217: while (rs.next()) {
218: routeHeaderIds.add(new Long(rs.getLong(1)));
219: }
220: rs.close();
221: } catch (SQLException sqle) {
222: LOG.error("SQLException: " + sqle.getMessage(), sqle);
223: throw new WorkflowRuntimeException(sqle);
224: } catch (LookupException le) {
225: LOG.error("LookupException: " + le.getMessage(), le);
226: throw new WorkflowRuntimeException(le);
227: } finally {
228: try {
229: if (broker != null) {
230: OjbFactoryUtils.releasePersistenceBroker(broker,
231: this .getPersistenceBrokerTemplate()
232: .getPbKey());
233: }
234: } catch (Exception e) {
235: LOG.error("Failed closing connection: "
236: + e.getMessage(), e);
237: }
238: }
239: return routeHeaderIds;
240: }
241:
242: public String getMessageEntityByDocumentId(Long documentId) {
243: if (documentId == null) {
244: throw new IllegalArgumentException(
245: "Encountered a null document ID.");
246: }
247: String messageEntity = null;
248: PersistenceBroker broker = null;
249: Connection conn = null;
250: PreparedStatement statement = null;
251: ResultSet rs = null;
252: try {
253: broker = this .getPersistenceBroker(false);
254: conn = broker.serviceConnectionManager().getConnection();
255: String query = "SELECT DT.MESSAGE_ENTITY_NM FROM EN_DOC_TYP_T DT, EN_DOC_HDR_T DH "
256: + "WHERE DH.DOC_TYP_ID=DT.DOC_TYP_ID AND "
257: + "DH.DOC_HDR_ID=?";
258: statement = conn.prepareStatement(query);
259: statement.setLong(1, documentId);
260: rs = statement.executeQuery();
261: if (rs.next()) {
262: messageEntity = rs.getString(1);
263: if (rs.wasNull()) {
264: messageEntity = null;
265: }
266: }
267: rs.close();
268: } catch (SQLException sqle) {
269: LOG.error("SQLException: " + sqle.getMessage(), sqle);
270: throw new WorkflowRuntimeException(sqle);
271: } catch (LookupException le) {
272: LOG.error("LookupException: " + le.getMessage(), le);
273: throw new WorkflowRuntimeException(le);
274: } finally {
275: try {
276: if (broker != null) {
277: OjbFactoryUtils.releasePersistenceBroker(broker,
278: this .getPersistenceBrokerTemplate()
279: .getPbKey());
280: }
281: } catch (Exception e) {
282: LOG.error("Failed closing connection: "
283: + e.getMessage(), e);
284: }
285: }
286: return messageEntity;
287: }
288: }
|