From: Aron Roberts Date: Wed, 27 Mar 2013 23:29:40 +0000 (-0700) Subject: CSPACE-5943: Initial work at getting values for SQL queries from individual document... X-Git-Url: https://git.aero2k.de/?a=commitdiff_plain;h=5884c813d33d75d8cbc04912e8f7872d42951f6e;p=tmp%2Fjakarta-migration.git CSPACE-5943: Initial work at getting values for SQL queries from individual document handlers. Still needs implementation in four other authorities, in addition to location and person. This is an interim solution with lots of code smells still to be worked out. --- diff --git a/services/authority/service/src/main/java/org/collectionspace/services/common/vocabulary/nuxeo/AuthorityItemDocumentModelHandler.java b/services/authority/service/src/main/java/org/collectionspace/services/common/vocabulary/nuxeo/AuthorityItemDocumentModelHandler.java index 007dded89..849466f5d 100644 --- a/services/authority/service/src/main/java/org/collectionspace/services/common/vocabulary/nuxeo/AuthorityItemDocumentModelHandler.java +++ b/services/authority/service/src/main/java/org/collectionspace/services/common/vocabulary/nuxeo/AuthorityItemDocumentModelHandler.java @@ -91,6 +91,7 @@ public abstract class AuthorityItemDocumentModelHandler protected String authorityRefNameBase = null; // Used to determine when the displayName changes as part of the update. protected String oldDisplayNameOnUpdate = null; + private String TERM_GROUP_TABLE_NAME_SUFFIX = "termgroup"; public AuthorityItemDocumentModelHandler(String authorityItemCommonSchemaName) { this.authorityItemCommonSchemaName = authorityItemCommonSchemaName; @@ -669,7 +670,7 @@ public abstract class AuthorityItemDocumentModelHandler return authorityItemCommonSchemaName; } - @Override + // @Override public boolean isJDBCQuery() { boolean result = false; @@ -685,4 +686,15 @@ public abstract class AuthorityItemDocumentModelHandler return result; } + protected String getTermGroupTableName() { + return TERM_GROUP_TABLE_NAME_SUFFIX; + } + + @Override + public Map getJDBCQueryParams() { + Map params = super.getJDBCQueryParams(); + params.put(RepositoryJavaClientImpl.JDBC_TABLE_NAME_PARAM, getTermGroupTableName()); + return params; + } + } diff --git a/services/common/src/main/java/org/collectionspace/services/common/document/AbstractDocumentHandlerImpl.java b/services/common/src/main/java/org/collectionspace/services/common/document/AbstractDocumentHandlerImpl.java index c77961655..f96702cbe 100644 --- a/services/common/src/main/java/org/collectionspace/services/common/document/AbstractDocumentHandlerImpl.java +++ b/services/common/src/main/java/org/collectionspace/services/common/document/AbstractDocumentHandlerImpl.java @@ -445,4 +445,9 @@ public abstract class AbstractDocumentHandlerImpl return false; } + @Override + public Map getJDBCQueryParams() { + return new HashMap<>(); + } + } diff --git a/services/common/src/main/java/org/collectionspace/services/common/document/DocumentHandler.java b/services/common/src/main/java/org/collectionspace/services/common/document/DocumentHandler.java index d6da4bea9..85bacec1e 100644 --- a/services/common/src/main/java/org/collectionspace/services/common/document/DocumentHandler.java +++ b/services/common/src/main/java/org/collectionspace/services/common/document/DocumentHandler.java @@ -346,4 +346,12 @@ public interface DocumentHandler { * Returns TRUE if a JDBC/SQL query should be used (instead of an NXQL query) */ public boolean isJDBCQuery(); + + /** + * Returns parameter values, relevant to this document handler, that can be used in JDBC/SQL queries + * + * @return a set of zero or more parameter values relevant to this handler + */ + public Map getJDBCQueryParams(); + } diff --git a/services/common/src/main/java/org/collectionspace/services/nuxeo/client/java/RepositoryJavaClientImpl.java b/services/common/src/main/java/org/collectionspace/services/nuxeo/client/java/RepositoryJavaClientImpl.java index 5bba1a706..eda28777f 100644 --- a/services/common/src/main/java/org/collectionspace/services/nuxeo/client/java/RepositoryJavaClientImpl.java +++ b/services/common/src/main/java/org/collectionspace/services/nuxeo/client/java/RepositoryJavaClientImpl.java @@ -101,7 +101,7 @@ public class RepositoryJavaClientImpl implements RepositoryClient queryParams = ctx.getQueryParams(); final String partialTerm = queryParams.getFirst(IQueryManager.SEARCH_TYPE_PARTIALTERM); - // FIXME: Replace this placeholder with an appropriate per-authority value - // obtained from the relevant document handler - final String termGroupTableName = "loctermgroup"; - - // AuthorityItemDocModelHandler authHandler = (AuthorityItemDocModelHandler) handler; - - // FIXME: Replace this placeholder query with an actual query from CSPACE-5945 - - // IMPORTANT FIXME: Guard against SQL injection attacks, since partialTerm - // is obtained from user-supplied query parameters - // See, for example: http://stackoverflow.com/a/7127189 + // FIXME: Replace this placeholder query with an actual query resulting + // from CSPACE-5945 work String sql = "SELECT DISTINCT hierarchy.id as id " + " FROM hierarchy " + " LEFT JOIN hierarchy h1 " + " ON h1.parentid = hierarchy.id " - + " LEFT JOIN " + termGroupTableName + " tg " + + " LEFT JOIN " + handler.getJDBCQueryParams().get(JDBC_TABLE_NAME_PARAM) + " tg " + " ON tg.id = h1.id " + " WHERE tg.termdisplayname ILIKE ?"; - PreparedStatementBuilder partialTermMatchStatementBuilder = new PreparedStatementBuilder(sql){ + PreparedStatementBuilder jdbcFilterBuilder = new PreparedStatementBuilder(sql){ @Override protected void preparePrepared(PreparedStatement preparedStatement) throws SQLException @@ -948,8 +939,8 @@ public class RepositoryJavaClientImpl implements RepositoryClient docIds = new ArrayList(); - try (CachedRowSet crs = JDBCTools.executePreparedQuery(partialTermMatchStatementBuilder, + List docIds = new ArrayList<>(); + try (CachedRowSet crs = JDBCTools.executePreparedQuery(jdbcFilterBuilder, dataSourceName, repositoryName, sql)) { // If the response to the query is null or contains zero rows, diff --git a/services/location/service/src/main/java/org/collectionspace/services/location/nuxeo/LocationDocumentModelHandler.java b/services/location/service/src/main/java/org/collectionspace/services/location/nuxeo/LocationDocumentModelHandler.java index d9f9eb356..a5bac79fb 100644 --- a/services/location/service/src/main/java/org/collectionspace/services/location/nuxeo/LocationDocumentModelHandler.java +++ b/services/location/service/src/main/java/org/collectionspace/services/location/nuxeo/LocationDocumentModelHandler.java @@ -117,5 +117,10 @@ public class LocationDocumentModelHandler public String getQProperty(String prop) { return LocationConstants.NUXEO_SCHEMA_NAME + ":" + prop; } + + @Override + protected String getTermGroupTableName() { + return "loc" + super.getTermGroupTableName(); + } } diff --git a/services/person/service/src/main/java/org/collectionspace/services/person/nuxeo/PersonDocumentModelHandler.java b/services/person/service/src/main/java/org/collectionspace/services/person/nuxeo/PersonDocumentModelHandler.java index 8b703b125..5219690c7 100644 --- a/services/person/service/src/main/java/org/collectionspace/services/person/nuxeo/PersonDocumentModelHandler.java +++ b/services/person/service/src/main/java/org/collectionspace/services/person/nuxeo/PersonDocumentModelHandler.java @@ -175,5 +175,10 @@ public class PersonDocumentModelHandler public String getQProperty(String prop) { return PersonConstants.NUXEO_SCHEMA_NAME + ":" + prop; } + + @Override + protected String getTermGroupTableName() { + return "person" + super.getTermGroupTableName(); + } }