| 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 java.sql.Connection; |
| 20 | |
import java.sql.ResultSet; |
| 21 | |
import java.sql.SQLException; |
| 22 | |
import java.sql.Statement; |
| 23 | |
import java.util.ArrayList; |
| 24 | |
import java.util.List; |
| 25 | |
|
| 26 | |
import org.apache.commons.lang.StringUtils; |
| 27 | |
import org.apache.ojb.broker.PersistenceBroker; |
| 28 | |
import org.apache.ojb.broker.accesslayer.LookupException; |
| 29 | |
import org.kuali.rice.kew.docsearch.DocSearchCriteriaDTO; |
| 30 | |
import org.kuali.rice.kew.docsearch.DocSearchDTO; |
| 31 | |
import org.kuali.rice.kew.docsearch.DocumentSearchGenerator; |
| 32 | |
import org.kuali.rice.kew.docsearch.StandardDocumentSearchGenerator; |
| 33 | |
import org.kuali.rice.kew.docsearch.dao.DocumentSearchDAO; |
| 34 | |
import org.kuali.rice.kew.doctype.service.DocumentSecurityService; |
| 35 | |
import org.kuali.rice.kew.service.KEWServiceLocator; |
| 36 | |
import org.kuali.rice.kew.util.KEWConstants; |
| 37 | |
import org.kuali.rice.kew.util.PerformanceLogger; |
| 38 | |
import org.kuali.rice.kew.util.Utilities; |
| 39 | |
import org.kuali.rice.kns.util.KNSConstants; |
| 40 | |
import org.springmodules.orm.ojb.OjbFactoryUtils; |
| 41 | |
import org.springmodules.orm.ojb.support.PersistenceBrokerDaoSupport; |
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 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 | |
|
| 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 | 0 | } |
| 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 = Utilities.getKNSParameterValue(KEWConstants.KEW_NAMESPACE, KNSConstants.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 | |
|
| 167 | |
private int getFetchMoreIterationLimit() { |
| 168 | 0 | int fetchMoreLimit = DEFAULT_FETCH_MORE_ITERATION_LIMIT; |
| 169 | 0 | String fetchMoreLimitValue = Utilities.getKNSParameterValue(KEWConstants.KEW_NAMESPACE, KNSConstants.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 | |
|
| 186 | |
|
| 187 | |
|
| 188 | |
} |