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.quicklinks.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.Collections;
025: import java.util.List;
026: import java.util.StringTokenizer;
027:
028: import org.apache.ojb.broker.PersistenceBroker;
029: import org.springmodules.orm.ojb.PersistenceBrokerCallback;
030: import org.springmodules.orm.ojb.PersistenceBrokerTemplate;
031: import org.springmodules.orm.ojb.support.PersistenceBrokerDaoSupport;
032:
033: import edu.iu.uis.eden.EdenConstants;
034: import edu.iu.uis.eden.KEWServiceLocator;
035: import edu.iu.uis.eden.docsearch.DocumentSearchService;
036: import edu.iu.uis.eden.doctype.DocumentType;
037: import edu.iu.uis.eden.doctype.DocumentTypePolicy;
038: import edu.iu.uis.eden.doctype.DocumentTypeService;
039: import edu.iu.uis.eden.exception.WorkflowRuntimeException;
040: import edu.iu.uis.eden.quicklinks.ActionListStats;
041: import edu.iu.uis.eden.quicklinks.InitiatedDocumentType;
042: import edu.iu.uis.eden.quicklinks.WatchedDocument;
043: import edu.iu.uis.eden.user.WorkflowUser;
044: import edu.iu.uis.eden.util.Utilities;
045:
046: public class QuickLinksDAOOjbImpl extends PersistenceBrokerDaoSupport
047: implements QuickLinksDAO {
048:
049: public List getActionListStats(final WorkflowUser workflowUser) {
050: return (List) this .getPersistenceBrokerTemplate().execute(
051: new PersistenceBrokerCallback() {
052: public Object doInPersistenceBroker(
053: PersistenceBroker broker) {
054: PreparedStatement selectActionItems = null;
055: PreparedStatement selectDocTypeLabel = null;
056: ResultSet selectedActionItems = null;
057: ResultSet selectedDocTypeLabel = null;
058: List docTypes = new ArrayList();
059: try {
060: Connection connection = broker
061: .serviceConnectionManager()
062: .getConnection();
063: selectActionItems = connection
064: .prepareStatement("select DOC_TYP_NM, COUNT(*) from EN_ACTN_ITM_T where ACTN_ITM_PRSN_EN_ID = ? "
065: + "and (dlgn_typ is null or dlgn_typ != '"
066: + EdenConstants.DELEGATION_SECONDARY
067: + "') group by DOC_TYP_NM");
068: selectDocTypeLabel = connection
069: .prepareStatement("select DOC_TYP_LBL_TXT from EN_DOC_TYP_T WHERE DOC_TYP_NM = ? and DOC_TYP_CUR_IND = 1");
070: selectActionItems.setString(1, workflowUser
071: .getWorkflowUserId()
072: .getWorkflowId());
073: selectedActionItems = selectActionItems
074: .executeQuery();
075: while (selectedActionItems.next()) {
076: String docTypeName = selectedActionItems
077: .getString(1);
078: int count = selectedActionItems
079: .getInt(2);
080: selectDocTypeLabel.setString(1,
081: docTypeName);
082: selectedDocTypeLabel = selectDocTypeLabel
083: .executeQuery();
084: if (selectedDocTypeLabel.next()) {
085: docTypes.add(new ActionListStats(
086: docTypeName,
087: selectedDocTypeLabel
088: .getString(1),
089: count));
090: }
091: }
092: Collections.sort(docTypes);
093: return docTypes;
094: } catch (Exception e) {
095: throw new WorkflowRuntimeException(
096: "Error getting action list stats for user: "
097: + workflowUser
098: .getAuthenticationUserId()
099: .getAuthenticationId(),
100: e);
101: } finally {
102: if (selectActionItems != null) {
103: try {
104: selectActionItems.close();
105: } catch (SQLException e) {
106: }
107: }
108:
109: if (selectDocTypeLabel != null) {
110: try {
111: selectDocTypeLabel.close();
112: } catch (SQLException e) {
113: }
114: }
115:
116: if (selectedActionItems != null) {
117: try {
118: selectedActionItems.close();
119: } catch (SQLException e) {
120: }
121: }
122:
123: if (selectedDocTypeLabel != null) {
124: try {
125: selectedDocTypeLabel.close();
126: } catch (SQLException e) {
127: }
128: }
129:
130: }
131: }
132: });
133: }
134:
135: public List getInitiatedDocumentTypesList(
136: final WorkflowUser workflowUser) {
137: return (List) this .getPersistenceBrokerTemplate().execute(
138: new PersistenceBrokerCallback() {
139:
140: public Object doInPersistenceBroker(
141: PersistenceBroker broker) {
142: PreparedStatement selectDistinctDocumentTypes = null;
143: ResultSet selectedDistinctDocumentTypes = null;
144: List documentTypesByName = new ArrayList();
145: try {
146: Connection connection = broker
147: .serviceConnectionManager()
148: .getConnection();
149: // select the doc type only if the SUPPORTS_QUICK_INITIATE policy is NULL or true
150: String sql = "select distinct B.DOC_TYP_NM, B.DOC_TYP_LBL_TXT from EN_DOC_HDR_T A, EN_DOC_TYP_T B "
151: + "where A.DOC_INITR_PRSN_EN_ID = ? and A.DOC_TYP_ID = B.DOC_TYP_ID and "
152: + "B.DOC_TYP_ACTV_IND = 1 and B.DOC_TYP_CUR_IND = 1 "
153: + "order by upper(B.DOC_TYP_LBL_TXT)";
154: selectDistinctDocumentTypes = connection
155: .prepareStatement(sql);
156: selectDistinctDocumentTypes.setString(1,
157: workflowUser.getWorkflowUserId()
158: .getWorkflowId());
159: selectedDistinctDocumentTypes = selectDistinctDocumentTypes
160: .executeQuery();
161: String documentNames = Utilities
162: .getApplicationConstant(
163: EdenConstants.QUICK_LINKS_RESTRICT_DOCUMENT_TYPES)
164: .trim();
165: if (documentNames == null) {
166: documentNames = "";
167: }
168: List docTypesToRestrict = new ArrayList();
169: StringTokenizer st = new StringTokenizer(
170: documentNames, ",");
171: while (st.hasMoreTokens()) {
172: docTypesToRestrict.add(st.nextToken());
173: }
174: while (selectedDistinctDocumentTypes.next()) {
175: String docTypeName = selectedDistinctDocumentTypes
176: .getString(1);
177: String docTypeTopParent = "";
178: int firstPeriod = docTypeName
179: .indexOf(".");
180: if (firstPeriod == -1) {
181: docTypeTopParent = docTypeName
182: .substring(0);
183: } else {
184: docTypeTopParent = docTypeName
185: .substring(0, firstPeriod);
186: }
187: if (!docTypesToRestrict
188: .contains(docTypeTopParent)) {
189: // the document types should be cached so this should be pretty quick
190: DocumentType docType = KEWServiceLocator
191: .getDocumentTypeService()
192: .findByName(docTypeName);
193: DocumentTypePolicy quickInitiatePolicy = docType
194: .getSupportsQuickInitiatePolicy();
195: if (quickInitiatePolicy
196: .getPolicyValue()
197: .booleanValue()) {
198: documentTypesByName
199: .add(new InitiatedDocumentType(
200: docTypeName,
201: selectedDistinctDocumentTypes
202: .getString(2)));
203: }
204: }
205: }
206: return documentTypesByName;
207: } catch (Exception e) {
208: throw new WorkflowRuntimeException(
209: "Error getting initiated document types for user: "
210: + workflowUser
211: .getAuthenticationUserId()
212: .getAuthenticationId(),
213: e);
214: } finally {
215: if (selectDistinctDocumentTypes != null) {
216: try {
217: selectDistinctDocumentTypes.close();
218: } catch (SQLException e) {
219: }
220: }
221: if (selectedDistinctDocumentTypes != null) {
222: try {
223: selectedDistinctDocumentTypes
224: .close();
225: } catch (SQLException e) {
226: }
227: }
228:
229: }
230:
231: }
232: });
233: }
234:
235: public List getNamedSearches(WorkflowUser workflowUser) {
236: return getDocumentSearchService()
237: .getNamedSearches(workflowUser);
238: }
239:
240: public List getRecentSearches(WorkflowUser workflowUser) {
241: return getDocumentSearchService().getMostRecentSearches(
242: workflowUser);
243: }
244:
245: public List getWatchedDocuments(final WorkflowUser workflowUser) {
246: return (List) this .getPersistenceBrokerTemplate().execute(
247: new PersistenceBrokerCallback() {
248: public Object doInPersistenceBroker(
249: PersistenceBroker broker) {
250: List watchedDocuments = new ArrayList();
251: PreparedStatement selectWatchedDocuments = null;
252: ResultSet selectedWatchedDocuments = null;
253: try {
254: Connection connection = broker
255: .serviceConnectionManager()
256: .getConnection();
257: selectWatchedDocuments = connection
258: .prepareStatement("select DOC_HDR_ID, DOC_RTE_STAT_CD, DOC_TTL, DOC_CRTE_DT from EN_DOC_HDR_T where DOC_INITR_PRSN_EN_ID = ? and DOC_RTE_STAT_CD in ('"
259: + EdenConstants.ROUTE_HEADER_ENROUTE_CD
260: + "','"
261: + EdenConstants.ROUTE_HEADER_EXCEPTION_CD
262: + "') order by DOC_CRTE_DT desc");
263: selectWatchedDocuments.setString(1,
264: workflowUser.getWorkflowUserId()
265: .getWorkflowId());
266: selectedWatchedDocuments = selectWatchedDocuments
267: .executeQuery();
268: while (selectedWatchedDocuments.next()) {
269: watchedDocuments
270: .add(new WatchedDocument(
271: selectedWatchedDocuments
272: .getString(1),
273: (String) EdenConstants.DOCUMENT_STATUSES
274: .get(selectedWatchedDocuments
275: .getString(2)),
276: selectedWatchedDocuments
277: .getString(3)));
278: }
279: return watchedDocuments;
280: } catch (Exception e) {
281: throw new WorkflowRuntimeException(
282: "Error getting initiated document types for user: "
283: + workflowUser
284: .getAuthenticationUserId()
285: .getAuthenticationId(),
286: e);
287: } finally {
288: if (selectWatchedDocuments != null) {
289: try {
290: selectWatchedDocuments.close();
291: } catch (SQLException e) {
292: }
293: }
294: if (selectedWatchedDocuments != null) {
295: try {
296: selectedWatchedDocuments.close();
297: } catch (SQLException e) {
298: }
299: }
300:
301: }
302: }
303: });
304: }
305:
306: public DocumentTypeService getDocumentTypeService() {
307: return ((DocumentTypeService) KEWServiceLocator
308: .getService(KEWServiceLocator.DOCUMENT_TYPE_SERVICE));
309: }
310:
311: public DocumentSearchService getDocumentSearchService() {
312: return ((DocumentSearchService) KEWServiceLocator
313: .getService(KEWServiceLocator.DOCUMENT_SEARCH_SERVICE));
314: }
315:
316: }
|