Coverage Report - org.kuali.rice.kew.quicklinks.dao.impl.QuickLinksDAOOjbImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
QuickLinksDAOOjbImpl
0%
0/8
N/A
4.6
QuickLinksDAOOjbImpl$1
0%
0/39
0%
0/12
4.6
QuickLinksDAOOjbImpl$2
0%
0/42
0%
0/20
4.6
QuickLinksDAOOjbImpl$3
0%
0/21
0%
0/6
4.6
 
 1  
 /*
 2  
  * Copyright 2006-2011 The Kuali Foundation
 3  
  *
 4  
  * Licensed under the Educational Community License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  * http://www.opensource.org/licenses/ecl2.php
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  */
 16  
 
 17  
 package org.kuali.rice.kew.quicklinks.dao.impl;
 18  
 
 19  
 import org.apache.ojb.broker.PersistenceBroker;
 20  
 import org.kuali.rice.core.api.util.KeyValue;
 21  
 import org.kuali.rice.core.framework.services.CoreFrameworkServiceLocator;
 22  
 import org.kuali.rice.kew.api.WorkflowRuntimeException;
 23  
 import org.kuali.rice.kew.api.action.DelegationType;
 24  
 import org.kuali.rice.kew.docsearch.service.DocumentSearchService;
 25  
 import org.kuali.rice.kew.doctype.DocumentTypePolicy;
 26  
 import org.kuali.rice.kew.doctype.bo.DocumentType;
 27  
 import org.kuali.rice.kew.doctype.service.DocumentTypeService;
 28  
 import org.kuali.rice.kew.quicklinks.ActionListStats;
 29  
 import org.kuali.rice.kew.quicklinks.InitiatedDocumentType;
 30  
 import org.kuali.rice.kew.quicklinks.WatchedDocument;
 31  
 import org.kuali.rice.kew.quicklinks.dao.QuickLinksDAO;
 32  
 import org.kuali.rice.kew.service.KEWServiceLocator;
 33  
 import org.kuali.rice.kew.util.KEWConstants;
 34  
 import org.kuali.rice.krad.util.KRADConstants;
 35  
 import org.springmodules.orm.ojb.PersistenceBrokerCallback;
 36  
 import org.springmodules.orm.ojb.support.PersistenceBrokerDaoSupport;
 37  
 
 38  
 import java.sql.Connection;
 39  
 import java.sql.PreparedStatement;
 40  
 import java.sql.ResultSet;
 41  
 import java.sql.SQLException;
 42  
 import java.util.ArrayList;
 43  
 import java.util.Collections;
 44  
 import java.util.List;
 45  
 import java.util.StringTokenizer;
 46  
 
 47  
 
 48  0
 public class QuickLinksDAOOjbImpl extends PersistenceBrokerDaoSupport implements QuickLinksDAO {
 49  
 
 50  
     @Override
 51  
         public List<ActionListStats> getActionListStats(final String principalId) {
 52  0
         return (List<ActionListStats>) this.getPersistenceBrokerTemplate().execute(new PersistenceBrokerCallback() {
 53  
             @Override
 54  
                         public Object doInPersistenceBroker(PersistenceBroker broker) {
 55  0
                 PreparedStatement selectActionItems = null;
 56  0
                 PreparedStatement selectDocTypeLabel = null;
 57  0
                 ResultSet selectedActionItems = null;
 58  0
                 ResultSet selectedDocTypeLabel = null;
 59  0
                 List<ActionListStats> docTypes = new ArrayList<ActionListStats>();
 60  
                 try {
 61  0
                     Connection connection = broker.serviceConnectionManager().getConnection();
 62  
 
 63  0
                     selectActionItems = connection.prepareStatement("select DOC_TYP_NM, COUNT(*) from KREW_ACTN_ITM_T where PRNCPL_ID = ? " +
 64  
                             "and (dlgn_typ is null or dlgn_typ != '" + DelegationType.SECONDARY.getCode() + "') group by DOC_TYP_NM");
 65  0
                     selectDocTypeLabel = connection.prepareStatement("select LBL from KREW_DOC_TYP_T WHERE DOC_TYP_NM = ? and CUR_IND = 1");
 66  0
                     selectActionItems.setString(1, principalId);
 67  0
                     selectedActionItems = selectActionItems.executeQuery();
 68  0
                     while (selectedActionItems.next()) {
 69  0
                         String docTypeName = selectedActionItems.getString(1);
 70  0
                         int count = selectedActionItems.getInt(2);
 71  0
                         selectDocTypeLabel.setString(1, docTypeName);
 72  0
                         selectedDocTypeLabel = selectDocTypeLabel.executeQuery();
 73  0
                         if (selectedDocTypeLabel.next()) {
 74  0
                             docTypes.add(new ActionListStats(docTypeName, selectedDocTypeLabel.getString(1), count));
 75  
                         }
 76  0
                     }
 77  0
                     Collections.sort(docTypes);
 78  0
                     return docTypes;
 79  0
                 } catch (Exception e) {
 80  0
                     throw new WorkflowRuntimeException("Error getting action list stats for user: " + principalId, e);
 81  
                 } finally {
 82  0
                     if (selectActionItems != null) {
 83  
                         try {
 84  0
                             selectActionItems.close();
 85  0
                         } catch (SQLException e) {
 86  0
                         }
 87  
                     }
 88  
 
 89  0
                     if (selectDocTypeLabel != null) {
 90  
                         try {
 91  0
                             selectDocTypeLabel.close();
 92  0
                         } catch (SQLException e) {
 93  0
                         }
 94  
                     }
 95  
 
 96  0
                     if (selectedActionItems != null) {
 97  
                         try {
 98  0
                             selectedActionItems.close();
 99  0
                         } catch (SQLException e) {
 100  0
                         }
 101  
                     }
 102  
 
 103  0
                     if (selectedDocTypeLabel != null) {
 104  
                         try {
 105  0
                             selectedDocTypeLabel.close();
 106  0
                         } catch (SQLException e) {
 107  0
                         }
 108  
                     }
 109  
 
 110  
                 }
 111  
             }
 112  
         });
 113  
     }
 114  
 
 115  
     @Override
 116  
         public List<InitiatedDocumentType> getInitiatedDocumentTypesList(final String principalId) {
 117  0
         return (List<InitiatedDocumentType>)  this.getPersistenceBrokerTemplate().execute(new PersistenceBrokerCallback() {
 118  
 
 119  
             @Override
 120  
                         public Object doInPersistenceBroker(PersistenceBroker broker) {
 121  0
                 PreparedStatement selectDistinctDocumentTypes = null;
 122  0
                 ResultSet selectedDistinctDocumentTypes = null;
 123  0
                 List<InitiatedDocumentType> documentTypesByName = new ArrayList<InitiatedDocumentType>();
 124  
                 try {
 125  0
                     Connection connection = broker.serviceConnectionManager().getConnection();
 126  
 //                  select the doc type only if the SUPPORTS_QUICK_INITIATE policy is NULL or true
 127  0
                     String sql = "select distinct B.DOC_TYP_NM, B.LBL from KREW_DOC_HDR_T A, KREW_DOC_TYP_T B "+
 128  
                             "where A.INITR_PRNCPL_ID = ? and A.DOC_TYP_ID = B.DOC_TYP_ID and " +
 129  
                             "B.ACTV_IND = 1 and B.CUR_IND = 1 " +
 130  
                             "order by upper(B.LBL)";
 131  
 
 132  0
                     selectDistinctDocumentTypes = connection.prepareStatement(sql);
 133  0
                     selectDistinctDocumentTypes.setString(1, principalId);
 134  0
                     selectedDistinctDocumentTypes = selectDistinctDocumentTypes.executeQuery();
 135  
 
 136  0
                     String documentNames = CoreFrameworkServiceLocator.getParameterService().getParameterValueAsString(KEWConstants.KEW_NAMESPACE, KRADConstants.DetailTypes.QUICK_LINK_DETAIL_TYPE, KEWConstants.QUICK_LINKS_RESTRICT_DOCUMENT_TYPES);
 137  0
                     if (documentNames != null) {
 138  
                         // TODO Should this happen???
 139  0
                         documentNames = documentNames.trim();
 140  
                     }
 141  0
                     if (documentNames == null || "none".equals(documentNames)) {
 142  0
                             documentNames = "";
 143  
                     }
 144  
 
 145  0
                     List docTypesToRestrict = new ArrayList();
 146  0
                     StringTokenizer st = new StringTokenizer(documentNames, ",");
 147  0
                     while (st.hasMoreTokens()) {
 148  0
                         docTypesToRestrict.add(st.nextToken());
 149  
                     }
 150  0
                     while (selectedDistinctDocumentTypes.next()) {
 151  0
                         String docTypeName = selectedDistinctDocumentTypes.getString(1);
 152  0
                         String docTypeTopParent = "";
 153  0
                         int firstPeriod = docTypeName.indexOf(".");
 154  0
                         if (firstPeriod == -1) {
 155  0
                             docTypeTopParent = docTypeName.substring(0);
 156  
                         } else {
 157  0
                             docTypeTopParent = docTypeName.substring(0, firstPeriod);
 158  
                         }
 159  0
                         if (!docTypesToRestrict.contains(docTypeTopParent)) {
 160  
                                 // the document types should be cached so this should be pretty quick
 161  0
                                 DocumentType docType = KEWServiceLocator.getDocumentTypeService().findByName(docTypeName);
 162  0
                                 DocumentTypePolicy quickInitiatePolicy = docType.getSupportsQuickInitiatePolicy();
 163  0
                             if (quickInitiatePolicy.getPolicyValue().booleanValue()) {
 164  0
                                     documentTypesByName.add(new InitiatedDocumentType(docTypeName, selectedDistinctDocumentTypes.getString(2)));
 165  
                             }
 166  
                         }
 167  0
                     }
 168  0
                     return documentTypesByName;
 169  0
                 } catch (Exception e) {
 170  0
                     throw new WorkflowRuntimeException("Error getting initiated document types for user: " + principalId, e);
 171  
                 } finally {
 172  0
                     if (selectDistinctDocumentTypes != null) {
 173  
                         try {
 174  0
                             selectDistinctDocumentTypes.close();
 175  0
                         } catch (SQLException e) {
 176  0
                         }
 177  
                     }
 178  0
                     if (selectedDistinctDocumentTypes != null) {
 179  
                         try {
 180  0
                             selectedDistinctDocumentTypes.close();
 181  0
                         } catch (SQLException e) {
 182  0
                         }
 183  
                     }
 184  
 
 185  
                 }
 186  
 
 187  
             }
 188  
         });
 189  
     }
 190  
 
 191  
     @Override
 192  
         public List<KeyValue> getNamedSearches(String principalId) {
 193  0
         return getDocumentSearchService().getNamedSearches(principalId);
 194  
     }
 195  
 
 196  
     @Override
 197  
         public List<KeyValue> getRecentSearches(String principalId) {
 198  0
         return getDocumentSearchService().getMostRecentSearches(principalId);
 199  
     }
 200  
 
 201  
     @Override
 202  
         public List<WatchedDocument> getWatchedDocuments(final String principalId) {
 203  0
         return (List<WatchedDocument>) this.getPersistenceBrokerTemplate().execute(new PersistenceBrokerCallback() {
 204  
             @Override
 205  
                         public Object doInPersistenceBroker(PersistenceBroker broker) {
 206  0
                 List<WatchedDocument> watchedDocuments = new ArrayList<WatchedDocument>();
 207  0
                 PreparedStatement selectWatchedDocuments = null;
 208  0
                 ResultSet selectedWatchedDocuments = null;
 209  
                 try {
 210  0
                     Connection connection = broker.serviceConnectionManager().getConnection();
 211  0
                     selectWatchedDocuments = connection.prepareStatement("select DOC_HDR_ID, DOC_HDR_STAT_CD, TTL, CRTE_DT from KREW_DOC_HDR_T where INITR_PRNCPL_ID = ? and DOC_HDR_STAT_CD in ('"+ KEWConstants.ROUTE_HEADER_ENROUTE_CD +"','"+ KEWConstants.ROUTE_HEADER_EXCEPTION_CD +"') order by CRTE_DT desc");
 212  0
                     selectWatchedDocuments.setString(1, principalId);
 213  0
                     selectedWatchedDocuments = selectWatchedDocuments.executeQuery();
 214  0
                     while (selectedWatchedDocuments.next()) {
 215  0
                         watchedDocuments.add(new WatchedDocument(selectedWatchedDocuments.getString(1), KEWConstants.DOCUMENT_STATUSES.get(selectedWatchedDocuments.getString(2)), selectedWatchedDocuments.getString(3)));
 216  
                     }
 217  0
                     return watchedDocuments;
 218  0
                 } catch (Exception e) {
 219  0
                     throw new WorkflowRuntimeException("Error getting initiated document types for user: " + principalId, e);
 220  
                 } finally {
 221  0
                     if (selectWatchedDocuments != null) {
 222  
                         try {
 223  0
                             selectWatchedDocuments.close();
 224  0
                         } catch (SQLException e) {
 225  0
                         }
 226  
                     }
 227  0
                     if (selectedWatchedDocuments != null) {
 228  
                         try {
 229  0
                             selectedWatchedDocuments.close();
 230  0
                         } catch (SQLException e) {
 231  0
                         }
 232  
                     }
 233  
 
 234  
                 }
 235  
             }
 236  
         });
 237  
     }
 238  
 
 239  
     public DocumentTypeService getDocumentTypeService() {
 240  0
         return ((DocumentTypeService) KEWServiceLocator.getService(KEWServiceLocator.DOCUMENT_TYPE_SERVICE));
 241  
     }
 242  
 
 243  
     public DocumentSearchService getDocumentSearchService() {
 244  0
         return ((DocumentSearchService) KEWServiceLocator.getService(KEWServiceLocator.DOCUMENT_SEARCH_SERVICE));
 245  
     }
 246  
 
 247  
 }