Coverage Report - org.kuali.rice.kew.docsearch.dao.impl.DocumentSearchDAOOjbImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
DocumentSearchDAOOjbImpl
0%
0/89
0%
0/22
5.2
 
 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.docsearch.dao.impl;
 18  
 
 19  
 import org.apache.commons.lang.StringUtils;
 20  
 import org.apache.ojb.broker.PersistenceBroker;
 21  
 import org.apache.ojb.broker.accesslayer.LookupException;
 22  
 import org.kuali.rice.core.framework.services.CoreFrameworkServiceLocator;
 23  
 import org.kuali.rice.kew.docsearch.DocSearchCriteriaDTO;
 24  
 import org.kuali.rice.kew.docsearch.DocSearchDTO;
 25  
 import org.kuali.rice.kew.docsearch.DocumentSearchGenerator;
 26  
 import org.kuali.rice.kew.docsearch.StandardDocumentSearchGenerator;
 27  
 import org.kuali.rice.kew.docsearch.dao.DocumentSearchDAO;
 28  
 import org.kuali.rice.kew.doctype.service.DocumentSecurityService;
 29  
 import org.kuali.rice.kew.service.KEWServiceLocator;
 30  
 import org.kuali.rice.kew.util.KEWConstants;
 31  
 import org.kuali.rice.kew.util.PerformanceLogger;
 32  
 import org.kuali.rice.krad.util.KRADConstants;
 33  
 import org.springmodules.orm.ojb.OjbFactoryUtils;
 34  
 import org.springmodules.orm.ojb.support.PersistenceBrokerDaoSupport;
 35  
 
 36  
 import java.sql.Connection;
 37  
 import java.sql.ResultSet;
 38  
 import java.sql.SQLException;
 39  
 import java.sql.Statement;
 40  
 import java.util.ArrayList;
 41  
 import java.util.List;
 42  
 
 43  
 /**
 44  
  *
 45  
  * Ojb implementation of the DocumentSearchDAO
 46  
  *
 47  
  * @deprecated Replaced by {@link DocumentSearchDAOJdbcImpl}
 48  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 49  
  *
 50  
  */
 51  0
 public class DocumentSearchDAOOjbImpl extends PersistenceBrokerDaoSupport implements DocumentSearchDAO {
 52  
 
 53  0
     public static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(DocumentSearchDAOOjbImpl.class);
 54  
 
 55  
     private static final int DEFAULT_FETCH_MORE_ITERATION_LIMIT = 10;
 56  
 
 57  
     public List<DocSearchDTO> getListBoundedByCritera(DocumentSearchGenerator documentSearchGenerator, DocSearchCriteriaDTO criteria, String principalId) {
 58  0
         return getList(documentSearchGenerator, criteria, criteria.getThreshold(), principalId);
 59  
     }
 60  
 
 61  
     public List<DocSearchDTO> getList(DocumentSearchGenerator documentSearchGenerator, DocSearchCriteriaDTO criteria, String principalId) {
 62  0
         return getList(documentSearchGenerator, criteria, Integer.valueOf(getSearchResultCap(documentSearchGenerator)), principalId);
 63  
     }
 64  
 
 65  
     private List<DocSearchDTO> getList(DocumentSearchGenerator documentSearchGenerator, DocSearchCriteriaDTO criteria, Integer searchResultCap, String principalId) {
 66  0
         LOG.debug("start getList");
 67  0
         DocumentSecurityService documentSecurityService = KEWServiceLocator.getDocumentSecurityService();
 68  0
         List docList = new ArrayList();
 69  0
         PersistenceBroker broker = null;
 70  0
         Connection conn = null;
 71  0
         Statement statement = null;
 72  0
         Statement searchAttributeStatement = null;
 73  0
         ResultSet rs = null;
 74  
         try {
 75  0
             broker = getPersistenceBroker(false);
 76  0
             conn = broker.serviceConnectionManager().getConnection();
 77  0
             statement = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
 78  0
             criteria.setThreshold(searchResultCap);
 79  0
             if (searchResultCap != null) {
 80  0
                 int fetchLimit = getFetchMoreIterationLimit() * searchResultCap.intValue();
 81  0
                 criteria.setFetchLimit(Integer.valueOf(fetchLimit));
 82  0
                 statement.setFetchSize(searchResultCap.intValue() + 1);
 83  0
                 statement.setMaxRows(fetchLimit + 1);
 84  0
             } else {
 85  0
                 criteria.setFetchLimit(null);
 86  
             }
 87  0
             PerformanceLogger perfLog = new PerformanceLogger();
 88  0
             String sql = documentSearchGenerator.generateSearchSql(criteria);
 89  0
             perfLog.log("Time to generate search sql from documentSearchGenerator class: " + documentSearchGenerator.getClass().getName(), true);
 90  0
             LOG.info("Executing document search with statement max rows: " + statement.getMaxRows());
 91  0
             LOG.info("Executing document search with statement fetch size: " + statement.getFetchSize());
 92  0
             perfLog = new PerformanceLogger();
 93  0
             rs = statement.executeQuery(sql);
 94  0
             perfLog.log("Time to execute doc search database query.", true);
 95  
             // TODO delyea - look at refactoring
 96  0
             searchAttributeStatement = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
 97  
 
 98  0
             if(documentSearchGenerator.isProcessResultSet()){
 99  0
                     docList = documentSearchGenerator.processResultSet(searchAttributeStatement, rs, criteria, principalId);
 100  
                 }else{
 101  0
                         docList = new StandardDocumentSearchGenerator().processResultSet(searchAttributeStatement, rs, criteria, principalId);
 102  
                 }
 103  
 
 104  0
         } catch (SQLException sqle) {
 105  0
             String errorMsg = "SQLException: " + sqle.getMessage();
 106  0
             LOG.error("getList() " + errorMsg, sqle);
 107  0
             throw new RuntimeException(errorMsg, sqle);
 108  0
         } catch (LookupException le) {
 109  0
             String errorMsg = "LookupException: " + le.getMessage();
 110  0
             LOG.error("getList() " + errorMsg, le);
 111  0
             throw new RuntimeException(errorMsg, le);
 112  
         } finally {
 113  0
             if (rs != null) {
 114  
                 try {
 115  0
                     rs.close();
 116  0
                 } catch (SQLException e) {
 117  0
                     LOG.warn("Could not close result set.");
 118  0
                 }
 119  
             }
 120  0
             if (statement != null) {
 121  
                 try {
 122  0
                     statement.close();
 123  0
                 } catch (SQLException e) {
 124  0
                     LOG.warn("Could not close statement.");
 125  0
                 }
 126  
             }
 127  0
             if (searchAttributeStatement != null) {
 128  
                 try {
 129  0
                     searchAttributeStatement.close();
 130  0
                 } catch (SQLException e) {
 131  0
                     LOG.warn("Could not close search attribute statement.");
 132  0
                 }
 133  
             }
 134  0
             if (broker != null) {
 135  
                 try {
 136  0
                     OjbFactoryUtils.releasePersistenceBroker(broker, this.getPersistenceBrokerTemplate().getPbKey());
 137  0
                 } catch (Exception e) {
 138  0
                     LOG.error("Failed closing connection: " + e.getMessage(), e);
 139  0
                 }
 140  
             }
 141  
         }
 142  
 
 143  0
         LOG.info("end getlist");
 144  0
         return docList;
 145  
     }
 146  
 
 147  
     private int getSearchResultCap(DocumentSearchGenerator docSearchGenerator) {
 148  0
         int resultCap = docSearchGenerator.getDocumentSearchResultSetLimit();
 149  0
         String resultCapValue = CoreFrameworkServiceLocator.getParameterService().getParameterValueAsString(KEWConstants.KEW_NAMESPACE, KRADConstants.DetailTypes.DOCUMENT_SEARCH_DETAIL_TYPE, KEWConstants.DOC_SEARCH_RESULT_CAP);
 150  0
         if (!StringUtils.isBlank(resultCapValue)) {
 151  
             try {
 152  0
                 Integer maxResultCap = Integer.parseInt(resultCapValue);
 153  0
                 if (resultCap > maxResultCap) {
 154  0
                     LOG.warn("Document Search Generator (" + docSearchGenerator.getClass().getName() + ") gives result set cap of " + resultCap + " which is greater than parameter " + KEWConstants.DOC_SEARCH_RESULT_CAP + " value of " + maxResultCap);
 155  0
                     resultCap = maxResultCap;
 156  0
                 } else if (maxResultCap <= 0) {
 157  0
                     LOG.warn(KEWConstants.DOC_SEARCH_RESULT_CAP + " was less than or equal to zero.  Please use a positive integer.");
 158  
                 }
 159  0
             } catch (NumberFormatException e) {
 160  0
                 LOG.warn(KEWConstants.DOC_SEARCH_RESULT_CAP + " is not a valid number.  Value was " + resultCapValue);
 161  0
             }
 162  
         }
 163  0
         return resultCap;
 164  
     }
 165  
 
 166  
     // TODO delyea: use searchable attribute count here?
 167  
     private int getFetchMoreIterationLimit() {
 168  0
         int fetchMoreLimit = DEFAULT_FETCH_MORE_ITERATION_LIMIT;
 169  0
         String fetchMoreLimitValue = CoreFrameworkServiceLocator.getParameterService().getParameterValueAsString(KEWConstants.KEW_NAMESPACE, KRADConstants.DetailTypes.DOCUMENT_SEARCH_DETAIL_TYPE, KEWConstants.DOC_SEARCH_FETCH_MORE_ITERATION_LIMIT);
 170  0
         if (!StringUtils.isBlank(fetchMoreLimitValue)) {
 171  
             try {
 172  0
                 fetchMoreLimit = Integer.parseInt(fetchMoreLimitValue);
 173  0
                 if (fetchMoreLimit < 0) {
 174  0
                     LOG.warn(KEWConstants.DOC_SEARCH_FETCH_MORE_ITERATION_LIMIT + " was less than zero.  Please use a value greater than or equal to zero.");
 175  0
                     fetchMoreLimit = DEFAULT_FETCH_MORE_ITERATION_LIMIT;
 176  
                 }
 177  0
             } catch (NumberFormatException e) {
 178  0
                 LOG.warn(KEWConstants.DOC_SEARCH_FETCH_MORE_ITERATION_LIMIT + " is not a valid number.  Value was " + fetchMoreLimitValue);
 179  0
             }
 180  
         }
 181  0
         return fetchMoreLimit;
 182  
     }
 183  
 
 184  
     //
 185  
     //    protected DatabasePlatform getPlatform() {
 186  
     //            return (DatabasePlatform)GlobalResourceLoader.getService(KEWServiceLocator.DB_PLATFORM);
 187  
     //    }
 188  
 }