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