]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
9d268b0f3d3acba484c91800110a0eb5ab471af4
[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 import org.collectionspace.services.common.storage.DatabaseProductType;\r
43 import org.collectionspace.services.common.storage.JDBCTools;\r
44 \r
45 public class QueryManagerNuxeoImpl implements IQueryManager {\r
46         \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
50 \r
51         private final Logger logger = LoggerFactory\r
52                         .getLogger(QueryManagerNuxeoImpl.class);\r
53         \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
58         \r
59         private static String getLikeForm() {\r
60                 if(SEARCH_LIKE_FORM == null) {\r
61                         try {\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
67                                 }\r
68                         } catch (Exception e) {\r
69                                 SEARCH_LIKE_FORM = IQueryManager.SEARCH_LIKE;\r
70                         }\r
71                 }\r
72                 return SEARCH_LIKE_FORM;\r
73         }\r
74 \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
77         /* (non-Javadoc)\r
78          * @see org.collectionspace.services.common.query.IQueryManager#execQuery(java.lang.String)\r
79          */\r
80         public void execQuery(String queryString) {\r
81                 NuxeoClient client = null;\r
82                 try {\r
83                         client = NuxeoConnector.getInstance().getClient();\r
84                         RepositoryInstance repoSession = client.openRepository();\r
85                         \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
95                         }\r
96                         \r
97                 } catch (Exception e) {\r
98                         // TODO Auto-generated catch block\r
99                         e.printStackTrace();\r
100                 }               \r
101         }\r
102 \r
103         /* (non-Javadoc)\r
104          * @see org.collectionspace.services.common.query.IQueryManager#createWhereClauseFromKeywords(java.lang.String)\r
105          */\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
123                                 continue;\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
126                         if(first) {\r
127                                 fullTextWhereClause.append(ECM_FULLTEXT_LIKE +"'");\r
128                                 first = false;\r
129                         } else {\r
130                                 fullTextWhereClause.append(SEARCH_TERM_SEPARATOR);\r
131                         }\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
142                         //}\r
143                         if (logger.isTraceEnabled() == true) {\r
144                                 logger.trace("Current built whereClause is: " + fullTextWhereClause.toString());\r
145                         }\r
146                 }\r
147                 if(first) {\r
148                         throw new RuntimeException("No usable keywords specified in string:["\r
149                                         +keywords+"]");\r
150                 }\r
151                 fullTextWhereClause.append("'"+SEARCH_GROUP_CLOSE);\r
152                 \r
153                 result = fullTextWhereClause.toString();\r
154             if (logger.isDebugEnabled()) {\r
155                 logger.debug("Final built WHERE clause is: " + result);\r
156             }\r
157             \r
158             return result;\r
159         }\r
160 \r
161         /* (non-Javadoc)\r
162          * @see org.collectionspace.services.common.query.IQueryManager#createWhereClauseFromKeywords(java.lang.String)\r
163          */\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
173                 }\r
174                 if (field==null || field.isEmpty()) {\r
175                         throw new RuntimeException("No match field specified.");\r
176                 }\r
177                 String ptClause = field\r
178                         + getLikeForm()\r
179                         + "'%" + unescapedSingleQuote.matcher(trimmed).replaceAll("\\\\'") + "%'";\r
180                 return ptClause;\r
181         }\r
182 \r
183 \r
184         \r
185         /**\r
186          * @param input\r
187          * @return true if there were any chars filtered, that will require a backup\r
188          *  qualifying search on the actual text.\r
189          */\r
190         private boolean filterForFullText(String input) {\r
191                 boolean fFilteredChars = false;\r
192                 \r
193                 return fFilteredChars;\r
194         }\r
195 }\r