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.nuxeo.client.java.NuxeoConnector;
\r
40 import org.collectionspace.services.nuxeo.client.java.RepositoryJavaClientImpl;
\r
41 import org.collectionspace.services.client.IQueryManager;
\r
42 import org.collectionspace.services.common.storage.DatabaseProductType;
\r
43 import org.collectionspace.services.common.storage.JDBCTools;
\r
45 public class QueryManagerNuxeoImpl implements IQueryManager {
\r
47 private static String ECM_FULLTEXT_LIKE =
\r
48 "ecm:fulltext" + SEARCH_TERM_SEPARATOR + IQueryManager.SEARCH_LIKE;
\r
49 private static String SEARCH_LIKE_FORM = null;
\r
51 private final Logger logger = LoggerFactory
\r
52 .getLogger(QueryManagerNuxeoImpl.class);
\r
54 // Consider that letters, letter-markers, numbers, '_' and apostrophe are words
\r
55 private static Pattern nonWordChars = Pattern.compile("[^\\p{L}\\p{M}\\p{N}_']");
\r
56 private static Pattern unescapedDblQuotes = Pattern.compile("(?<!\\\\)\"");
\r
57 private static Pattern unescapedSingleQuote = Pattern.compile("(?<!\\\\)'");
\r
59 private static String getLikeForm() {
\r
60 if(SEARCH_LIKE_FORM == null) {
\r
62 DatabaseProductType type = JDBCTools.getDatabaseProductType();
\r
63 if(type == DatabaseProductType.MYSQL) {
\r
64 SEARCH_LIKE_FORM = IQueryManager.SEARCH_LIKE;
\r
65 } else if(type == DatabaseProductType.POSTGRESQL) {
\r
66 SEARCH_LIKE_FORM = IQueryManager.SEARCH_ILIKE;
\r
68 } catch (Exception e) {
\r
69 SEARCH_LIKE_FORM = IQueryManager.SEARCH_LIKE;
\r
72 return SEARCH_LIKE_FORM;
\r
75 //TODO: This is currently just an example fixed query. This should eventually be
\r
76 // removed or replaced with a more generic method.
\r
78 * @see org.collectionspace.services.common.query.IQueryManager#execQuery(java.lang.String)
\r
80 public void execQuery(String queryString) {
\r
81 NuxeoClient client = null;
\r
83 client = NuxeoConnector.getInstance().getClient();
\r
84 RepositoryInstance repoSession = client.openRepository();
\r
86 DocumentModelList docModelList = repoSession.query("SELECT * FROM Relation WHERE relation:relationtype.documentId1='updated-Subject-1'");
\r
87 // DocumentModelList docModelList = repoSession.query("SELECT * FROM Relation");
\r
88 // DocumentModelList docModelList = repoSession.query("SELECT * FROM CollectionObject WHERE collectionobject:objectNumber='objectNumber-1251305545865'");
\r
89 for (DocumentModel docModel : docModelList) {
\r
90 System.out.println("--------------------------------------------");
\r
91 System.out.println(docModel.getPathAsString());
\r
92 System.out.println(docModel.getName());
\r
93 System.out.println(docModel.getPropertyValue("dc:title"));
\r
94 // System.out.println("documentId1=" + docModel.getProperty("relation", "relationtype/documentId1").toString());
\r
97 } catch (Exception e) {
\r
98 // TODO Auto-generated catch block
\r
99 e.printStackTrace();
\r
104 * @see org.collectionspace.services.common.query.IQueryManager#createWhereClauseFromKeywords(java.lang.String)
\r
106 // TODO handle keywords containing escaped punctuation chars, then we need to qualify the
\r
107 // search by matching on the fulltext.simpletext field.
\r
108 // TODO handle keywords containing unescaped double quotes by matching the phrase
\r
109 // against the fulltext.simpletext field.
\r
110 // Both these require using JDBC, since we cannot get to the fulltext table in NXQL
\r
111 public String createWhereClauseFromKeywords(String keywords) {
\r
112 String result = null;
\r
113 StringBuffer fullTextWhereClause = new StringBuffer(SEARCH_GROUP_OPEN);
\r
114 //StringBuffer phraseWhereClause = new StringBuffer(SEARCH_GROUP_OPEN);
\r
115 boolean phrasesToAdd = false;
\r
116 // Split on unescaped double quotes to handle phrases
\r
117 String[] phrases = unescapedDblQuotes.split(keywords.trim());
\r
118 boolean first = true;
\r
119 for(String phrase : phrases ) {
\r
120 String trimmed = phrase.trim();
\r
121 // Ignore empty strings from match, or goofy input
\r
122 if(trimmed.isEmpty())
\r
124 // Add the phrase to the string to pass in for full text matching.
\r
125 // Note that we can pass in a set of words and it will do the OR for us.
\r
127 fullTextWhereClause.append(ECM_FULLTEXT_LIKE +"'");
\r
130 fullTextWhereClause.append(SEARCH_TERM_SEPARATOR);
\r
132 // ignore the special chars except single quote here - can't hurt
\r
133 // TODO this should become a special function that strips things the
\r
134 // fulltext will ignore, including non-word chars and too-short words,
\r
135 // and escaping single quotes. Can return a boolean for anything stripped,
\r
136 // which triggers the back-up search. We can think about whether stripping
\r
137 // short words not in a quoted phrase should trigger the backup.
\r
138 fullTextWhereClause.append(unescapedSingleQuote.matcher(trimmed).replaceAll("\\\\'"));
\r
139 // If there are non-word chars in the phrase, we need to match the
\r
140 // phrase exactly against the fulltext table for this object
\r
141 //if(nonWordChars.matcher(trimmed).matches()) {
\r
143 if (logger.isTraceEnabled() == true) {
\r
144 logger.trace("Current built whereClause is: " + fullTextWhereClause.toString());
\r
148 throw new RuntimeException("No usable keywords specified in string:["
\r
151 fullTextWhereClause.append("'"+SEARCH_GROUP_CLOSE);
\r
153 result = fullTextWhereClause.toString();
\r
154 if (logger.isDebugEnabled()) {
\r
155 logger.debug("Final built WHERE clause is: " + result);
\r
162 * @see org.collectionspace.services.common.query.IQueryManager#createWhereClauseFromKeywords(java.lang.String)
\r
164 // TODO handle keywords containing escaped punctuation chars, then we need to qualify the
\r
165 // search by matching on the fulltext.simpletext field.
\r
166 // TODO handle keywords containing unescaped double quotes by matching the phrase
\r
167 // against the fulltext.simpletext field.
\r
168 // Both these require using JDBC, since we cannot get to the fulltext table in NXQL
\r
169 public String createWhereClauseForPartialMatch(String field, String partialTerm) {
\r
170 String trimmed = (partialTerm == null)?"":partialTerm.trim();
\r
171 if (trimmed.isEmpty()) {
\r
172 throw new RuntimeException("No partialTerm specified.");
\r
174 if (field==null || field.isEmpty()) {
\r
175 throw new RuntimeException("No match field specified.");
\r
177 String ptClause = field
\r
179 + "'%" + unescapedSingleQuote.matcher(trimmed).replaceAll("\\\\'") + "%'";
\r
187 * @return true if there were any chars filtered, that will require a backup
\r
188 * qualifying search on the actual text.
\r
190 private boolean filterForFullText(String input) {
\r
191 boolean fFilteredChars = false;
\r
193 return fFilteredChars;
\r