2 * QueryManagerNuxeoImpl.java
\r
4 * {Purpose of This Class}
\r
6 * {Other Notes Relating to This Class (Optional)}
\r
9 * $LastChangedRevision: $
\r
10 * $LastChangedDate: $
\r
12 * This document is a part of the source code and related artifacts
\r
13 * for CollectionSpace, an open source collections management system
\r
14 * for museums and related institutions:
\r
16 * http://www.collectionspace.org
\r
17 * http://wiki.collectionspace.org
\r
19 * Copyright © 2009 {Contributing Institution}
\r
21 * Licensed under the Educational Community License (ECL), Version 2.0.
\r
22 * You may not use this file except in compliance with this License.
\r
24 * You may obtain a copy of the ECL 2.0 License at
\r
25 * https://source.collectionspace.org/collection-space/LICENSE.txt
\r
27 package org.collectionspace.services.common.query.nuxeo;
\r
29 import org.slf4j.Logger;
\r
30 import org.slf4j.LoggerFactory;
\r
32 import java.util.regex.Pattern;
\r
34 import org.nuxeo.ecm.core.api.DocumentModel;
\r
35 import org.nuxeo.ecm.core.api.DocumentModelList;
\r
36 import org.nuxeo.ecm.core.api.repository.RepositoryInstance;
\r
37 //import org.nuxeo.ecm.core.client.NuxeoClient;
\r
39 import org.collectionspace.services.jaxb.InvocableJAXBSchema;
\r
40 //import org.collectionspace.services.nuxeo.client.java.NuxeoConnector;
\r
41 //import org.collectionspace.services.nuxeo.client.java.NxConnect;
\r
42 import org.collectionspace.services.nuxeo.client.java.NuxeoClientEmbedded;
\r
43 import org.collectionspace.services.nuxeo.client.java.NuxeoConnectorEmbedded;
\r
45 import org.collectionspace.services.client.IQueryManager;
\r
46 import org.collectionspace.services.common.invocable.Invocable;
\r
47 import org.collectionspace.services.common.invocable.InvocableUtils;
\r
48 import org.collectionspace.services.common.storage.DatabaseProductType;
\r
49 import org.collectionspace.services.common.storage.JDBCTools;
\r
51 public class QueryManagerNuxeoImpl implements IQueryManager {
\r
53 private static String ECM_FULLTEXT_LIKE = "ecm:fulltext"
\r
54 + SEARCH_TERM_SEPARATOR + IQueryManager.SEARCH_LIKE;
\r
55 private static String SEARCH_LIKE_FORM = null;
\r
57 private final Logger logger = LoggerFactory
\r
58 .getLogger(QueryManagerNuxeoImpl.class);
\r
60 // Consider that letters, letter-markers, numbers, '_' and apostrophe are
\r
62 private static Pattern nonWordChars = Pattern
\r
63 .compile("[^\\p{L}\\p{M}\\p{N}_']");
\r
64 private static Pattern unescapedDblQuotes = Pattern.compile("(?<!\\\\)\"");
\r
65 private static Pattern unescapedSingleQuote = Pattern.compile("(?<!\\\\)'");
\r
66 private static Pattern kwdSearchProblemChars = Pattern
\r
67 .compile("[\\:\\(\\)]");
\r
68 private static Pattern kwdSearchHyphen = Pattern.compile(" - ");
\r
70 private static String getLikeForm() {
\r
71 if (SEARCH_LIKE_FORM == null) {
\r
73 DatabaseProductType type = JDBCTools.getDatabaseProductType();
\r
74 if (type == DatabaseProductType.MYSQL) {
\r
75 SEARCH_LIKE_FORM = IQueryManager.SEARCH_LIKE;
\r
76 } else if (type == DatabaseProductType.POSTGRESQL) {
\r
77 SEARCH_LIKE_FORM = IQueryManager.SEARCH_ILIKE;
\r
79 } catch (Exception e) {
\r
80 SEARCH_LIKE_FORM = IQueryManager.SEARCH_LIKE;
\r
83 return SEARCH_LIKE_FORM;
\r
86 // TODO: This is currently just an example fixed query. This should
\r
88 // removed or replaced with a more generic method.
\r
93 * org.collectionspace.services.common.query.IQueryManager#execQuery(java
\r
98 public void execQuery(String queryString) {
\r
99 NuxeoClientEmbedded client = null;
\r
101 client = NuxeoConnectorEmbedded.getInstance().getClient();
\r
102 RepositoryInstance repoSession = client.openRepository();
\r
104 DocumentModelList docModelList = repoSession
\r
105 .query("SELECT * FROM Relation WHERE relations_common:subjectCsid='updated-Subject-1'");
\r
106 // DocumentModelList docModelList =
\r
107 // repoSession.query("SELECT * FROM Relation");
\r
108 // DocumentModelList docModelList =
\r
109 // repoSession.query("SELECT * FROM CollectionObject WHERE collectionobject:objectNumber='objectNumber-1251305545865'");
\r
110 for (DocumentModel docModel : docModelList) {
\r
112 .println("--------------------------------------------");
\r
113 System.out.println(docModel.getPathAsString());
\r
114 System.out.println(docModel.getName());
\r
115 System.out.println(docModel.getPropertyValue("dc:title"));
\r
116 // System.out.println("subjectCsid=" +
\r
117 // docModel.getProperty("relations_common",
\r
118 // "subjectCsid").toString());
\r
121 } catch (Exception e) {
\r
122 // TODO Auto-generated catch block
\r
123 e.printStackTrace();
\r
128 public String createWhereClauseFromAdvancedSearch(String advancedSearch) {
\r
129 String result = null;
\r
131 // Process search term. FIXME: REM - Do we need to perform and string filtering here?
\r
133 if (advancedSearch != null && !advancedSearch.isEmpty()) {
\r
134 StringBuffer advancedSearchWhereClause = new StringBuffer(
\r
136 result = advancedSearchWhereClause.toString();
\r
145 * @see org.collectionspace.services.common.query.IQueryManager#
\r
146 * createWhereClauseFromKeywords(java.lang.String)
\r
148 // TODO handle keywords containing escaped punctuation chars, then we need
\r
150 // search by matching on the fulltext.simpletext field.
\r
151 // TODO handle keywords containing unescaped double quotes by matching the
\r
153 // against the fulltext.simpletext field.
\r
154 // Both these require using JDBC, since we cannot get to the fulltext table
\r
157 public String createWhereClauseFromKeywords(String keywords) {
\r
158 String result = null;
\r
159 StringBuffer fullTextWhereClause = new StringBuffer(SEARCH_GROUP_OPEN);
\r
160 // StringBuffer phraseWhereClause = new StringBuffer(SEARCH_GROUP_OPEN);
\r
161 boolean phrasesToAdd = false;
\r
162 // Split on unescaped double quotes to handle phrases
\r
163 String[] phrases = unescapedDblQuotes.split(keywords.trim());
\r
164 boolean first = true;
\r
165 for (String phrase : phrases) {
\r
166 String trimmed = phrase.trim();
\r
167 // Ignore empty strings from match, or goofy input
\r
168 if (trimmed.isEmpty())
\r
170 // Add the phrase to the string to pass in for full text matching.
\r
171 // Note that we can pass in a set of words and it will do the OR for
\r
174 fullTextWhereClause.append(ECM_FULLTEXT_LIKE + "'");
\r
177 fullTextWhereClause.append(SEARCH_TERM_SEPARATOR);
\r
179 // ignore the special chars except single quote here - can't hurt
\r
180 // TODO this should become a special function that strips things the
\r
181 // fulltext will ignore, including non-word chars and too-short
\r
183 // and escaping single quotes. Can return a boolean for anything
\r
185 // which triggers the back-up search. We can think about whether
\r
187 // short words not in a quoted phrase should trigger the backup.
\r
188 trimmed = unescapedSingleQuote.matcher(trimmed).replaceAll("\\\\'");
\r
189 // If there are non-word chars in the phrase, we need to match the
\r
190 // phrase exactly against the fulltext table for this object
\r
191 // if(nonWordChars.matcher(trimmed).matches()) {
\r
193 // Replace problem chars with spaces. Patches CSPACE-4147,
\r
195 trimmed = kwdSearchProblemChars.matcher(trimmed).replaceAll(" ");
\r
196 trimmed = kwdSearchHyphen.matcher(trimmed).replaceAll(" ");
\r
198 fullTextWhereClause.append(trimmed);
\r
199 if (logger.isTraceEnabled() == true) {
\r
200 logger.trace("Current built whereClause is: "
\r
201 + fullTextWhereClause.toString());
\r
205 throw new RuntimeException(
\r
206 "No usable keywords specified in string:[" + keywords + "]");
\r
208 fullTextWhereClause.append("'" + SEARCH_GROUP_CLOSE);
\r
210 result = fullTextWhereClause.toString();
\r
211 if (logger.isDebugEnabled()) {
\r
212 logger.debug("Final built WHERE clause is: " + result);
\r
221 * @see org.collectionspace.services.common.query.IQueryManager#
\r
222 * createWhereClauseFromKeywords(java.lang.String)
\r
224 // TODO handle keywords containing escaped punctuation chars, then we need
\r
226 // search by matching on the fulltext.simpletext field.
\r
227 // TODO handle keywords containing unescaped double quotes by matching the
\r
229 // against the fulltext.simpletext field.
\r
230 // Both these require using JDBC, since we cannot get to the fulltext table
\r
233 public String createWhereClauseForPartialMatch(String field,
\r
234 String partialTerm) {
\r
235 String trimmed = (partialTerm == null) ? "" : partialTerm.trim();
\r
236 if (trimmed.isEmpty()) {
\r
237 throw new RuntimeException("No partialTerm specified.");
\r
239 if (field == null || field.isEmpty()) {
\r
240 throw new RuntimeException("No match field specified.");
\r
242 String ptClause = field + getLikeForm() + "'%"
\r
243 + unescapedSingleQuote.matcher(trimmed).replaceAll("\\\\'")
\r
249 * Creates a filtering where clause from docType, for invocables.
\r
254 * @return the string
\r
257 public String createWhereClauseForInvocableByDocType(String schema,
\r
259 String trimmed = (docType == null) ? "" : docType.trim();
\r
260 if (trimmed.isEmpty()) {
\r
261 throw new RuntimeException("No docType specified.");
\r
263 if (schema == null || schema.isEmpty()) {
\r
264 throw new RuntimeException("No match schema specified.");
\r
266 String wClause = schema + ":" + InvocableJAXBSchema.FOR_DOC_TYPES
\r
267 + " = '" + trimmed + "'";
\r
272 * Creates a filtering where clause from invocation mode, for invocables.
\r
277 * @return the string
\r
280 public String createWhereClauseForInvocableByMode(String schema, String mode) {
\r
281 String trimmed = (mode == null) ? "" : mode.trim();
\r
282 if (trimmed.isEmpty()) {
\r
283 throw new RuntimeException("No docType specified.");
\r
285 if (schema == null || schema.isEmpty()) {
\r
286 throw new RuntimeException("No match schema specified.");
\r
288 String wClause = InvocableUtils.getPropertyNameForInvocationMode(
\r
289 schema, trimmed) + " != 0";
\r
295 * @return true if there were any chars filtered, that will require a backup
\r
296 * qualifying search on the actual text.
\r
298 private boolean filterForFullText(String input) {
\r
299 boolean fFilteredChars = false;
\r
301 return fFilteredChars;
\r
305 * Creates a query to filter a qualified (string) field according to a list of string values.
\r
306 * @param qualifiedField The schema-qualified field to filter on
\r
307 * @param filterTerms the list of one or more strings to filter on
\r
308 * @param fExclude If true, will require qualifiedField NOT match the filters strings.
\r
309 * If false, will require qualifiedField does match one of the filters strings.
\r
310 * @return queryString
\r
313 public String createWhereClauseToFilterFromStringList(String qualifiedField, String[] filterTerms, boolean fExclude) {
\r
314 // Start with the qualified termStatus field
\r
315 StringBuilder filterClause = new StringBuilder(qualifiedField);
\r
316 if(filterTerms.length == 1) {
\r
317 filterClause.append(fExclude?" <> '":" = '");
\r
318 filterClause.append(filterTerms[0]);
\r
319 filterClause.append('\'');
\r
321 filterClause.append(fExclude?" NOT IN (":" IN (");
\r
322 for(int i=0; i<filterTerms.length; i++) {
\r
324 filterClause.append(',');
\r
326 filterClause.append('\'');
\r
327 filterClause.append(filterTerms[i]);
\r
328 filterClause.append('\'');
\r
330 filterClause.append(')');
\r
332 return filterClause.toString();
\r