]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
a055e9e85ffdb3fda0ab05cbb236d9dbeb1ba87b
[tmp/jakarta-migration.git] /
1 /**     \r
2  * QueryManagerNuxeoImpl.java\r
3  *\r
4  * {Purpose of This Class}\r
5  *\r
6  * {Other Notes Relating to This Class (Optional)}\r
7  *\r
8  * $LastChangedBy: $\r
9  * $LastChangedRevision: $\r
10  * $LastChangedDate: $\r
11  *\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
15  *\r
16  * http://www.collectionspace.org\r
17  * http://wiki.collectionspace.org\r
18  *\r
19  * Copyright © 2009 {Contributing Institution}\r
20  *\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
23  *\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
26  */\r
27 package org.collectionspace.services.common.query.nuxeo;\r
28 \r
29 import org.slf4j.Logger;\r
30 import org.slf4j.LoggerFactory;\r
31 \r
32 import java.util.regex.Pattern;\r
33 \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
38 \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 \r
43 public class QueryManagerNuxeoImpl implements IQueryManager {\r
44         \r
45         private final Logger logger = LoggerFactory\r
46                         .getLogger(RepositoryJavaClientImpl.class);\r
47         \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
52 \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
55         /* (non-Javadoc)\r
56          * @see org.collectionspace.services.common.query.IQueryManager#execQuery(java.lang.String)\r
57          */\r
58         public void execQuery(String queryString) {\r
59                 NuxeoClient client = null;\r
60                 try {\r
61                         client = NuxeoConnector.getInstance().getClient();\r
62                         RepositoryInstance repoSession = client.openRepository();\r
63                         \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
73                         }\r
74                         \r
75                 } catch (Exception e) {\r
76                         // TODO Auto-generated catch block\r
77                         e.printStackTrace();\r
78                 }               \r
79         }\r
80 \r
81         /* (non-Javadoc)\r
82          * @see org.collectionspace.services.common.query.IQueryManager#createWhereClauseFromKeywords(java.lang.String)\r
83          */\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
101                                 continue;\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
104                         if(first) {\r
105                                 fullTextWhereClause.append(ECM_FULLTEXT_LIKE +"'");\r
106                                 first = false;\r
107                         } else {\r
108                                 fullTextWhereClause.append(SEARCH_TERM_SEPARATOR);\r
109                         }\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
120                         //}\r
121                         if (logger.isTraceEnabled() == true) {\r
122                                 logger.trace("Current built whereClause is: " + fullTextWhereClause.toString());\r
123                         }\r
124                 }\r
125                 if(first) {\r
126                         throw new RuntimeException("No usable keywords specified in string:["\r
127                                         +keywords+"]");\r
128                 }\r
129                 fullTextWhereClause.append("'"+SEARCH_GROUP_CLOSE);\r
130                 \r
131                 result = fullTextWhereClause.toString();\r
132             if (logger.isDebugEnabled()) {\r
133                 logger.debug("Final built WHERE clause is: " + result);\r
134             }\r
135             \r
136             return result;\r
137         }\r
138 \r
139         /* (non-Javadoc)\r
140          * @see org.collectionspace.services.common.query.IQueryManager#createWhereClauseFromKeywords(java.lang.String)\r
141          */\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
151                 }\r
152                 if (field==null || field.isEmpty()) {\r
153                         throw new RuntimeException("No match field specified.");\r
154                 }\r
155                 String ptClause = field\r
156                         + IQueryManager.SEARCH_LIKE\r
157                         + "'%" + unescapedSingleQuote.matcher(trimmed).replaceAll("\\\\'") + "%'";\r
158                 return ptClause;\r
159         }\r
160 \r
161 \r
162         \r
163         /**\r
164          * @param input\r
165          * @return true if there were any chars filtered, that will require a backup\r
166          *  qualifying search on the actual text.\r
167          */\r
168         private boolean filterForFullText(String input) {\r
169                 boolean fFilteredChars = false;\r
170                 \r
171                 return fFilteredChars;\r
172         }\r
173 }\r