| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 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.kns.util.KNSConstants; |
| 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 | |
|
| 44 | |
|
| 45 | |
|
| 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 | |
|
| 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, KNSConstants.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 | |
|
| 158 | |
private int getFetchMoreIterationLimit() { |
| 159 | 0 | int fetchMoreLimit = DEFAULT_FETCH_MORE_ITERATION_LIMIT; |
| 160 | 0 | String fetchMoreLimitValue = CoreFrameworkServiceLocator.getParameterService().getParameterValueAsString(KEWConstants.KEW_NAMESPACE, KNSConstants.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 | |
|
| 177 | |
|
| 178 | |
|
| 179 | |
} |