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
43 public class QueryManagerNuxeoImpl implements IQueryManager {
\r
45 private final Logger logger = LoggerFactory
\r
46 .getLogger(QueryManagerNuxeoImpl.class);
\r
48 // Consider that letters, letter-markers, numbers, '_' and apostrophe are words
\r
49 private static Pattern nonWordChars = Pattern.compile("[^\\p{L}\\p{M}\\p{N}_']");
\r
50 private static Pattern unescapedDblQuotes = Pattern.compile("(?<!\\\\)\"");
\r
51 private static Pattern unescapedSingleQuote = Pattern.compile("(?<!\\\\)'");
\r
53 //TODO: This is currently just an example fixed query. This should eventually be
\r
54 // removed or replaced with a more generic method.
\r
56 * @see org.collectionspace.services.common.query.IQueryManager#execQuery(java.lang.String)
\r
58 public void execQuery(String queryString) {
\r
59 NuxeoClient client = null;
\r
61 client = NuxeoConnector.getInstance().getClient();
\r
62 RepositoryInstance repoSession = client.openRepository();
\r
64 DocumentModelList docModelList = repoSession.query("SELECT * FROM Relation WHERE relation:relationtype.documentId1='updated-Subject-1'");
\r
65 // DocumentModelList docModelList = repoSession.query("SELECT * FROM Relation");
\r
66 // DocumentModelList docModelList = repoSession.query("SELECT * FROM CollectionObject WHERE collectionobject:objectNumber='objectNumber-1251305545865'");
\r
67 for (DocumentModel docModel : docModelList) {
\r
68 System.out.println("--------------------------------------------");
\r
69 System.out.println(docModel.getPathAsString());
\r
70 System.out.println(docModel.getName());
\r
71 System.out.println(docModel.getPropertyValue("dc:title"));
\r
72 // System.out.println("documentId1=" + docModel.getProperty("relation", "relationtype/documentId1").toString());
\r
75 } catch (Exception e) {
\r
76 // TODO Auto-generated catch block
\r
77 e.printStackTrace();
\r
82 * @see org.collectionspace.services.common.query.IQueryManager#createWhereClauseFromKeywords(java.lang.String)
\r
84 // TODO handle keywords containing escaped punctuation chars, then we need to qualify the
\r
85 // search by matching on the fulltext.simpletext field.
\r
86 // TODO handle keywords containing unescaped double quotes by matching the phrase
\r
87 // against the fulltext.simpletext field.
\r
88 // Both these require using JDBC, since we cannot get to the fulltext table in NXQL
\r
89 public String createWhereClauseFromKeywords(String keywords) {
\r
90 String result = null;
\r
91 StringBuffer fullTextWhereClause = new StringBuffer(SEARCH_GROUP_OPEN);
\r
92 //StringBuffer phraseWhereClause = new StringBuffer(SEARCH_GROUP_OPEN);
\r
93 boolean phrasesToAdd = false;
\r
94 // Split on unescaped double quotes to handle phrases
\r
95 String[] phrases = unescapedDblQuotes.split(keywords.trim());
\r
96 boolean first = true;
\r
97 for(String phrase : phrases ) {
\r
98 String trimmed = phrase.trim();
\r
99 // Ignore empty strings from match, or goofy input
\r
100 if(trimmed.isEmpty())
\r
102 // Add the phrase to the string to pass in for full text matching.
\r
103 // Note that we can pass in a set of words and it will do the OR for us.
\r
105 fullTextWhereClause.append(ECM_FULLTEXT_LIKE +"'");
\r
108 fullTextWhereClause.append(SEARCH_TERM_SEPARATOR);
\r
110 // ignore the special chars except single quote here - can't hurt
\r
111 // TODO this should become a special function that strips things the
\r
112 // fulltext will ignore, including non-word chars and too-short words,
\r
113 // and escaping single quotes. Can return a boolean for anything stripped,
\r
114 // which triggers the back-up search. We can think about whether stripping
\r
115 // short words not in a quoted phrase should trigger the backup.
\r
116 fullTextWhereClause.append(unescapedSingleQuote.matcher(trimmed).replaceAll("\\\\'"));
\r
117 // If there are non-word chars in the phrase, we need to match the
\r
118 // phrase exactly against the fulltext table for this object
\r
119 //if(nonWordChars.matcher(trimmed).matches()) {
\r
121 if (logger.isTraceEnabled() == true) {
\r
122 logger.trace("Current built whereClause is: " + fullTextWhereClause.toString());
\r
126 throw new RuntimeException("No usable keywords specified in string:["
\r
129 fullTextWhereClause.append("'"+SEARCH_GROUP_CLOSE);
\r
131 result = fullTextWhereClause.toString();
\r
132 if (logger.isDebugEnabled()) {
\r
133 logger.debug("Final built WHERE clause is: " + result);
\r
140 * @see org.collectionspace.services.common.query.IQueryManager#createWhereClauseFromKeywords(java.lang.String)
\r
142 // TODO handle keywords containing escaped punctuation chars, then we need to qualify the
\r
143 // search by matching on the fulltext.simpletext field.
\r
144 // TODO handle keywords containing unescaped double quotes by matching the phrase
\r
145 // against the fulltext.simpletext field.
\r
146 // Both these require using JDBC, since we cannot get to the fulltext table in NXQL
\r
147 public String createWhereClauseForPartialMatch(String field, String partialTerm) {
\r
148 String trimmed = (partialTerm == null)?"":partialTerm.trim();
\r
149 if (trimmed.isEmpty()) {
\r
150 throw new RuntimeException("No partialTerm specified.");
\r
152 if (field==null || field.isEmpty()) {
\r
153 throw new RuntimeException("No match field specified.");
\r
155 String ptClause = field
\r
156 + IQueryManager.SEARCH_LIKE
\r
157 + "'%" + unescapedSingleQuote.matcher(trimmed).replaceAll("\\\\'") + "%'";
\r
165 * @return true if there were any chars filtered, that will require a backup
\r
166 * qualifying search on the actual text.
\r
168 private boolean filterForFullText(String input) {
\r
169 boolean fFilteredChars = false;
\r
171 return fFilteredChars;
\r