]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
cc9aa87a9d56f8743402e3aaabb5e4cd7a70a390
[tmp/jakarta-migration.git] /
1 /**\r
2  * This document is a part of the source code and related artifacts for\r
3  * CollectionSpace, an open source collections management system for museums and\r
4  * related institutions:\r
5  *\r
6  * http://www.collectionspace.org http://wiki.collectionspace.org\r
7  *\r
8  * Copyright 2009 University of California at Berkeley\r
9  *\r
10  * Licensed under the Educational Community License (ECL), Version 2.0. You may\r
11  * not use this file except in compliance with this License.\r
12  *\r
13  * You may obtain a copy of the ECL 2.0 License at\r
14  *\r
15  * https://source.collectionspace.org/collection-space/LICENSE.txt\r
16  *\r
17  * Unless required by applicable law or agreed to in writing, software\r
18  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT\r
19  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the\r
20  * License for the specific language governing permissions and limitations under\r
21  * the License.\r
22  */\r
23 package org.collectionspace.services.common.vocabulary;\r
24 \r
25 import java.util.ArrayList;\r
26 import java.util.HashMap;\r
27 import java.util.Iterator;\r
28 import java.util.List;\r
29 import java.util.Map;\r
30 \r
31 import org.nuxeo.ecm.core.api.ClientException;\r
32 import org.nuxeo.ecm.core.api.DocumentModel;\r
33 import org.nuxeo.ecm.core.api.DocumentModelList;\r
34 import org.nuxeo.ecm.core.api.model.Property;\r
35 import org.nuxeo.ecm.core.api.model.PropertyException;\r
36 import org.nuxeo.ecm.core.api.model.PropertyNotFoundException;\r
37 import org.nuxeo.ecm.core.api.model.impl.primitives.StringProperty;\r
38 import org.nuxeo.ecm.core.api.repository.RepositoryInstance;\r
39 import org.slf4j.Logger;\r
40 import org.slf4j.LoggerFactory;\r
41 \r
42 import org.collectionspace.services.client.PoxPayloadIn;\r
43 import org.collectionspace.services.client.PoxPayloadOut;\r
44 import org.collectionspace.services.common.ServiceMain;\r
45 import org.collectionspace.services.common.StoredValuesUriTemplate;\r
46 import org.collectionspace.services.common.UriTemplateFactory;\r
47 import org.collectionspace.services.common.UriTemplateRegistry;\r
48 import org.collectionspace.services.common.UriTemplateRegistryKey;\r
49 import org.collectionspace.services.common.context.ServiceContext;\r
50 import org.collectionspace.services.common.context.AbstractServiceContextImpl;\r
51 import org.collectionspace.services.common.api.RefNameUtils;\r
52 import org.collectionspace.services.common.api.Tools;\r
53 import org.collectionspace.services.common.api.RefNameUtils.AuthorityTermInfo;\r
54 import org.collectionspace.services.common.authorityref.AuthorityRefDocList;\r
55 import org.collectionspace.services.common.config.TenantBindingConfigReaderImpl;\r
56 import org.collectionspace.services.common.context.ServiceBindingUtils;\r
57 import org.collectionspace.services.common.document.DocumentException;\r
58 import org.collectionspace.services.common.document.DocumentFilter;\r
59 import org.collectionspace.services.common.document.DocumentNotFoundException;\r
60 import org.collectionspace.services.common.document.DocumentUtils;\r
61 import org.collectionspace.services.common.document.DocumentWrapper;\r
62 import org.collectionspace.services.common.query.QueryManager;\r
63 import org.collectionspace.services.common.repository.RepositoryClient;\r
64 import org.collectionspace.services.nuxeo.client.java.DocHandlerBase;\r
65 import org.collectionspace.services.nuxeo.client.java.RepositoryJavaClientImpl;\r
66 import org.collectionspace.services.common.security.SecurityUtils;\r
67 import org.collectionspace.services.config.service.ServiceBindingType;\r
68 import org.collectionspace.services.jaxb.AbstractCommonList;\r
69 import org.collectionspace.services.nuxeo.util.NuxeoUtils;\r
70 import org.jboss.resteasy.spi.ResteasyProviderFactory;\r
71 \r
72 \r
73 /**\r
74  * RefNameServiceUtils is a collection of services utilities related to refName\r
75  * usage.\r
76  *\r
77  * $LastChangedRevision: $ $LastChangedDate: $\r
78  */\r
79 public class RefNameServiceUtils {\r
80 \r
81     public static class AuthRefConfigInfo {\r
82 \r
83         public String getQualifiedDisplayName() {\r
84             return (Tools.isBlank(schema))\r
85                     ? displayName : DocumentUtils.appendSchemaName(schema, displayName);\r
86         }\r
87 \r
88         public String getDisplayName() {\r
89             return displayName;\r
90         }\r
91 \r
92         public void setDisplayName(String displayName) {\r
93             this.displayName = displayName;\r
94         }\r
95         String displayName;\r
96         String schema;\r
97 \r
98         public String getSchema() {\r
99             return schema;\r
100         }\r
101 \r
102         public void setSchema(String schema) {\r
103             this.schema = schema;\r
104         }\r
105 \r
106         public String getFullPath() {\r
107             return fullPath;\r
108         }\r
109 \r
110         public void setFullPath(String fullPath) {\r
111             this.fullPath = fullPath;\r
112         }\r
113         String fullPath;\r
114         protected String[] pathEls;\r
115 \r
116         public AuthRefConfigInfo(AuthRefConfigInfo arci) {\r
117             this.displayName = arci.displayName;\r
118             this.schema = arci.schema;\r
119             this.fullPath = arci.fullPath;\r
120             this.pathEls = arci.pathEls;\r
121             // Skip the pathElse check, since we are creatign from another (presumably valid) arci.\r
122         }\r
123 \r
124         public AuthRefConfigInfo(String displayName, String schema, String fullPath, String[] pathEls) {\r
125             this.displayName = displayName;\r
126             this.schema = schema;\r
127             this.fullPath = fullPath;\r
128             this.pathEls = pathEls;\r
129             checkPathEls();\r
130         }\r
131 \r
132         // Split a config value string like "intakes_common:collector", or\r
133         // "collectionobjects_common:contentPeoples|contentPeople"\r
134         // "collectionobjects_common:assocEventGroupList/*/assocEventPlace"\r
135         // If has a pipe ('|') second part is a displayLabel, and first is path\r
136         // Otherwise, entry is a path, and can use the last pathElement as displayName\r
137         // Should be schema qualified.\r
138         public AuthRefConfigInfo(String configString) {\r
139             String[] pair = configString.split("\\|", 2);\r
140             String[] pathEls;\r
141             String displayName, fullPath;\r
142             if (pair.length == 1) {\r
143                 // no label specifier, so we'll defer getting label\r
144                 fullPath = pair[0];\r
145                 pathEls = pair[0].split("/");\r
146                 displayName = pathEls[pathEls.length - 1];\r
147             } else {\r
148                 fullPath = pair[0];\r
149                 pathEls = pair[0].split("/");\r
150                 displayName = pair[1];\r
151             }\r
152             String[] schemaSplit = pathEls[0].split(":", 2);\r
153             String schema;\r
154             if (schemaSplit.length == 1) {    // schema not specified\r
155                 schema = null;\r
156             } else {\r
157                 schema = schemaSplit[0];\r
158                 if (pair.length == 1 && pathEls.length == 1) {    // simplest case of field in top level schema, no labelll\r
159                     displayName = schemaSplit[1];    // Have to fix up displayName to have no schema\r
160                 }\r
161             }\r
162             this.displayName = displayName;\r
163             this.schema = schema;\r
164             this.fullPath = fullPath;\r
165             this.pathEls = pathEls;\r
166             checkPathEls();\r
167         }\r
168 \r
169         protected void checkPathEls() {\r
170             int len = pathEls.length;\r
171             if (len < 1) {\r
172                 throw new InternalError("Bad values in authRef info - caller screwed up:" + fullPath);\r
173             }\r
174             // Handle case of them putting a leading slash on the path\r
175             if (len > 1 && pathEls[0].endsWith(":")) {\r
176                 len--;\r
177                 String[] newArray = new String[len];\r
178                 newArray[0] = pathEls[0] + pathEls[1];\r
179                 if (len >= 2) {\r
180                     System.arraycopy(pathEls, 2, newArray, 1, len - 1);\r
181                 }\r
182                 pathEls = newArray;\r
183             }\r
184         }\r
185     }\r
186 \r
187     public static class AuthRefInfo extends AuthRefConfigInfo {\r
188 \r
189         public Property getProperty() {\r
190             return property;\r
191         }\r
192 \r
193         public void setProperty(Property property) {\r
194             this.property = property;\r
195         }\r
196         Property property;\r
197 \r
198         public AuthRefInfo(String displayName, String schema, String fullPath, String[] pathEls, Property prop) {\r
199             super(displayName, schema, fullPath, pathEls);\r
200             this.property = prop;\r
201         }\r
202 \r
203         public AuthRefInfo(AuthRefConfigInfo arci, Property prop) {\r
204             super(arci);\r
205             this.property = prop;\r
206         }\r
207     }\r
208     private static final Logger logger = LoggerFactory.getLogger(RefNameServiceUtils.class);\r
209     private static ArrayList<String> refNameServiceTypes = null;\r
210 \r
211     public static List<AuthRefConfigInfo> getConfiguredAuthorityRefs(ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx) {\r
212         List<String> authRefFields =\r
213                 ((AbstractServiceContextImpl) ctx).getAllPartsPropertyValues(\r
214                 ServiceBindingUtils.AUTH_REF_PROP, ServiceBindingUtils.QUALIFIED_PROP_NAMES);\r
215         ArrayList<AuthRefConfigInfo> authRefsInfo = new ArrayList<AuthRefConfigInfo>(authRefFields.size());\r
216         for (String spec : authRefFields) {\r
217             AuthRefConfigInfo arci = new AuthRefConfigInfo(spec);\r
218             authRefsInfo.add(arci);\r
219         }\r
220         return authRefsInfo;\r
221     }\r
222 \r
223     public static AuthorityRefDocList getAuthorityRefDocs(\r
224             RepositoryInstance repoSession,\r
225             ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx,\r
226             UriTemplateRegistry uriTemplateRegistry,\r
227             RepositoryClient<PoxPayloadIn, PoxPayloadOut> repoClient,\r
228             List<String> serviceTypes,\r
229             String refName,\r
230             String refPropName,\r
231             DocumentFilter filter, boolean computeTotal)\r
232             throws DocumentException, DocumentNotFoundException {\r
233         AuthorityRefDocList wrapperList = new AuthorityRefDocList();\r
234         AbstractCommonList commonList = (AbstractCommonList) wrapperList;\r
235         int pageNum = filter.getStartPage();\r
236         int pageSize = filter.getPageSize();\r
237         \r
238         List<AuthorityRefDocList.AuthorityRefDocItem> list =\r
239                 wrapperList.getAuthorityRefDocItem();\r
240 \r
241         Map<String, ServiceBindingType> queriedServiceBindings = new HashMap<String, ServiceBindingType>();\r
242         Map<String, List<AuthRefConfigInfo>> authRefFieldsByService = new HashMap<String, List<AuthRefConfigInfo>>();\r
243 \r
244         RepositoryJavaClientImpl nuxeoRepoClient = (RepositoryJavaClientImpl) repoClient;\r
245         try {\r
246             // Ignore any provided page size and number query parameters in\r
247             // the following call, as they pertain to the list of authority\r
248             // references to be returned, not to the list of documents to be\r
249             // scanned for those references.\r
250             DocumentModelList docList = findAuthorityRefDocs(ctx, repoClient, repoSession,\r
251                     serviceTypes, refName, refPropName, queriedServiceBindings, authRefFieldsByService,\r
252                     filter.getWhereClause(), null, 0 /* pageSize */, 0 /* pageNum */, computeTotal);\r
253 \r
254             if (docList == null) { // found no authRef fields - nothing to process\r
255                 return wrapperList;\r
256             }\r
257 \r
258             // set the fieldsReturned list. Even though this is a fixed schema, app layer treats\r
259             // this like other abstract common lists\r
260             /*\r
261              * <xs:element name="docType" type="xs:string" minOccurs="1" />\r
262              * <xs:element name="docId" type="xs:string" minOccurs="1" />\r
263              * <xs:element name="docNumber" type="xs:string" minOccurs="0" />\r
264              * <xs:element name="docName" type="xs:string" minOccurs="0" />\r
265              * <xs:element name="sourceField" type="xs:string" minOccurs="1" />\r
266              * <xs:element name="uri" type="xs:anyURI" minOccurs="1" />\r
267              * <xs:element name="updatedAt" type="xs:string" minOccurs="1" />\r
268              * <xs:element name="workflowState" type="xs:string" minOccurs="1"\r
269              * />\r
270              */\r
271             String fieldList = "docType|docId|docNumber|docName|sourceField|uri|updatedAt|workflowState";\r
272             commonList.setFieldsReturned(fieldList);\r
273 \r
274             // As a side-effect, the method called below modifies the value of\r
275             // the 'list' variable, which holds the list of references to\r
276             // an authority item.\r
277             //\r
278             // There can be more than one reference to a particular authority\r
279             // item within any individual document scanned, so the number of\r
280             // authority references may potentially exceed the total number\r
281             // of documents scanned.\r
282             int nRefsFound = processRefObjsDocList(docList, ctx.getTenantId(), refName, queriedServiceBindings, authRefFieldsByService, // the actual list size needs to be updated to the size of "list"\r
283                     list, null);\r
284 \r
285             commonList.setPageSize(pageSize);\r
286             \r
287             // Values returned in the pagination block above the list items\r
288             // need to reflect the number of references to authority items\r
289             // returned, rather than the number of documents originally scanned\r
290             // to find such references.\r
291             commonList.setPageNum(pageNum);\r
292             commonList.setTotalItems(list.size());\r
293 \r
294             // Slice the list to return only the specified page of items\r
295             // in the list results.\r
296             //\r
297             // FIXME: There may well be a pattern-based way to do this\r
298             // in our framework, and if we can eliminate much of the\r
299             // non-DRY code below, that would be desirable.\r
300             \r
301             int startIndex = 0;\r
302             int endIndex = 0;\r
303             \r
304             // Return all results if pageSize is 0.\r
305             if (pageSize == 0) {\r
306                 startIndex = 0;\r
307                 endIndex = list.size();\r
308             } else {\r
309                startIndex = pageNum * pageSize;\r
310             }\r
311             \r
312             // Return an empty list when the start of the requested page is\r
313             // beyond the last item in the list.\r
314             if (startIndex > list.size()) {\r
315                 wrapperList.getAuthorityRefDocItem().clear();\r
316                 commonList.setItemsInPage(wrapperList.getAuthorityRefDocItem().size());\r
317                 return wrapperList;\r
318             }\r
319 \r
320             // Otherwise, return a list of items from the start of the specified\r
321             // page through the last item on that page, or otherwise through the\r
322             // last item in the entire list, if that occurs earlier than the end\r
323             // of the specified page.\r
324             if (endIndex == 0) {\r
325                 int pageEndIndex = ((startIndex + pageSize));\r
326                 endIndex = (pageEndIndex > list.size()) ? list.size() : pageEndIndex;\r
327             }\r
328             \r
329             // Slice the list to return only the specified page of results.\r
330             // Note: the second argument to List.subList(), endIndex, is\r
331             // exclusive of the item at its index position, reflecting the\r
332             // zero-index nature of the list.\r
333             List<AuthorityRefDocList.AuthorityRefDocItem> currentPageList =\r
334                     new ArrayList<AuthorityRefDocList.AuthorityRefDocItem>(list.subList(startIndex, endIndex));\r
335             wrapperList.getAuthorityRefDocItem().clear();\r
336             wrapperList.getAuthorityRefDocItem().addAll(currentPageList);\r
337             commonList.setItemsInPage(currentPageList.size());\r
338             \r
339             if (logger.isDebugEnabled() && (nRefsFound < docList.size())) {\r
340                 logger.debug("Internal curiosity: got fewer matches of refs than # docs matched..."); // We found a ref to ourself and have excluded it.\r
341             }\r
342         } catch (Exception e) {\r
343             logger.error("Could not retrieve a list of documents referring to the specified authority item", e);\r
344             wrapperList = null;\r
345         }\r
346 \r
347         return wrapperList;\r
348     }\r
349 \r
350     private static ArrayList<String> getRefNameServiceTypes() {\r
351         if (refNameServiceTypes == null) {\r
352             refNameServiceTypes = new ArrayList<String>();\r
353             refNameServiceTypes.add(ServiceBindingUtils.SERVICE_TYPE_AUTHORITY);\r
354             refNameServiceTypes.add(ServiceBindingUtils.SERVICE_TYPE_OBJECT);\r
355             refNameServiceTypes.add(ServiceBindingUtils.SERVICE_TYPE_PROCEDURE);\r
356         }\r
357         return refNameServiceTypes;\r
358     }\r
359     // Seems like a good value - no real data to set this well.\r
360     // Note: can set this value lower during debugging; e.g. to 3 - ADR 2012-07-10\r
361     private static final int N_OBJS_TO_UPDATE_PER_LOOP = 100;\r
362 \r
363     public static int updateAuthorityRefDocs(\r
364             ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx,\r
365             RepositoryClient<PoxPayloadIn, PoxPayloadOut> repoClient,\r
366             RepositoryInstance repoSession,\r
367             String oldRefName,\r
368             String newRefName,\r
369             String refPropName) throws Exception {\r
370         Map<String, ServiceBindingType> queriedServiceBindings = new HashMap<String, ServiceBindingType>();\r
371         Map<String, List<AuthRefConfigInfo>> authRefFieldsByService = new HashMap<String, List<AuthRefConfigInfo>>();\r
372 \r
373         int docsScanned = 0;\r
374         int nRefsFound = 0;\r
375         int currentPage = 0;\r
376         int docsInCurrentPage = 0;\r
377         final String WHERE_CLAUSE_ADDITIONS_VALUE = null;\r
378         final String ORDER_BY_VALUE = "collectionspace_core:createdAt";\r
379 \r
380         if (!(repoClient instanceof RepositoryJavaClientImpl)) {\r
381             throw new InternalError("updateAuthorityRefDocs() called with unknown repoClient type!");\r
382         }\r
383         try { // REM - How can we deal with transaction and timeout issues here.\r
384             final int pageSize = N_OBJS_TO_UPDATE_PER_LOOP;\r
385             DocumentModelList docList;\r
386             boolean morePages = true;\r
387             while (morePages) {\r
388 \r
389                 docList = findAuthorityRefDocs(ctx, repoClient, repoSession,\r
390                         getRefNameServiceTypes(), oldRefName, refPropName,\r
391                         queriedServiceBindings, authRefFieldsByService, WHERE_CLAUSE_ADDITIONS_VALUE, ORDER_BY_VALUE, pageSize, currentPage, false);\r
392 \r
393                 if (docList == null) {\r
394                     logger.debug("updateAuthorityRefDocs: no documents could be found that referenced the old refName");\r
395                     break;\r
396                 }\r
397                 docsInCurrentPage = docList.size();\r
398                 logger.debug("updateAuthorityRefDocs: current page=" + currentPage + " documents included in page=" + docsInCurrentPage);\r
399                 if (docsInCurrentPage == 0) {\r
400                     logger.debug("updateAuthorityRefDocs: no more documents requiring refName updates could be found");\r
401                     break;\r
402                 }\r
403                 if (docsInCurrentPage < pageSize) {\r
404                     logger.debug("updateAuthorityRefDocs: assuming no more documents requiring refName updates will be found, as docsInCurrentPage < pageSize");\r
405                     morePages = false;\r
406                 }\r
407 \r
408                 int nRefsFoundThisPage = processRefObjsDocList(docList, ctx.getTenantId(), oldRefName, queriedServiceBindings, authRefFieldsByService,\r
409                         null, newRefName);\r
410                 if (nRefsFoundThisPage > 0) {\r
411                     ((RepositoryJavaClientImpl) repoClient).saveDocListWithoutHandlerProcessing(ctx, repoSession, docList, true);\r
412                     nRefsFound += nRefsFoundThisPage;\r
413                 }\r
414 \r
415                 // FIXME: Per REM, set a limit of num objects - something like\r
416                 // 1000K objects - and also add a log Warning after some threshold\r
417                 docsScanned += docsInCurrentPage;\r
418                 if (morePages) {\r
419                     currentPage++;\r
420                 }\r
421 \r
422             }\r
423         } catch (Exception e) {\r
424             logger.error("Internal error updating the AuthorityRefDocs: " + e.getLocalizedMessage());\r
425             logger.debug(Tools.errorToString(e, true));\r
426             throw e;\r
427         }\r
428         logger.debug("updateAuthorityRefDocs replaced a total of " + nRefsFound + " authority references, within as many as " + docsScanned + " scanned document(s)");\r
429         return nRefsFound;\r
430     }\r
431 \r
432     private static DocumentModelList findAuthorityRefDocs(\r
433             ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx,\r
434             RepositoryClient<PoxPayloadIn, PoxPayloadOut> repoClient,\r
435             RepositoryInstance repoSession, List<String> serviceTypes,\r
436             String refName,\r
437             String refPropName,\r
438             Map<String, ServiceBindingType> queriedServiceBindings,\r
439             Map<String, List<AuthRefConfigInfo>> authRefFieldsByService,\r
440             String whereClauseAdditions,\r
441             String orderByClause,\r
442             int pageSize,\r
443             int pageNum,\r
444             boolean computeTotal) throws DocumentException, DocumentNotFoundException {\r
445 \r
446         // Get the service bindings for this tenant\r
447         TenantBindingConfigReaderImpl tReader =\r
448                 ServiceMain.getInstance().getTenantBindingConfigReader();\r
449         // We need to get all the procedures, authorities, and objects.\r
450         List<ServiceBindingType> servicebindings = tReader.getServiceBindingsByType(ctx.getTenantId(), serviceTypes);\r
451         if (servicebindings == null || servicebindings.isEmpty()) {\r
452             logger.error("RefNameServiceUtils.getAuthorityRefDocs: No services bindings found, cannot proceed!");\r
453             return null;\r
454         }\r
455         // Filter the list for current user rights\r
456         servicebindings = SecurityUtils.getReadableServiceBindingsForCurrentUser(servicebindings);\r
457 \r
458         ArrayList<String> docTypes = new ArrayList<String>();\r
459 \r
460         String query = computeWhereClauseForAuthorityRefDocs(refName, refPropName, docTypes, servicebindings,\r
461                 queriedServiceBindings, authRefFieldsByService);\r
462         if (query == null) { // found no authRef fields - nothing to query\r
463             return null;\r
464         }\r
465         // Additional qualifications, like workflow state\r
466         if (Tools.notBlank(whereClauseAdditions)) {\r
467             query += " AND " + whereClauseAdditions;\r
468         }\r
469         // Now we have to issue the search\r
470         RepositoryJavaClientImpl nuxeoRepoClient = (RepositoryJavaClientImpl) repoClient;\r
471         DocumentWrapper<DocumentModelList> docListWrapper = nuxeoRepoClient.findDocs(ctx, repoSession,\r
472                 docTypes, query, orderByClause, pageSize, pageNum, computeTotal);\r
473         // Now we gather the info for each document into the list and return\r
474         DocumentModelList docList = docListWrapper.getWrappedObject();\r
475         return docList;\r
476     }\r
477     private static final boolean READY_FOR_COMPLEX_QUERY = true;\r
478 \r
479     private static String computeWhereClauseForAuthorityRefDocs(\r
480             String refName,\r
481             String refPropName,\r
482             ArrayList<String> docTypes,\r
483             List<ServiceBindingType> servicebindings,\r
484             Map<String, ServiceBindingType> queriedServiceBindings,\r
485             Map<String, List<AuthRefConfigInfo>> authRefFieldsByService) {\r
486 \r
487         boolean fFirst = true;\r
488         List<String> authRefFieldPaths;\r
489         for (ServiceBindingType sb : servicebindings) {\r
490             // Gets the property names for each part, qualified with the part label (which\r
491             // is also the table name, the way that the repository works).\r
492             authRefFieldPaths =\r
493                     ServiceBindingUtils.getAllPartsPropertyValues(sb,\r
494                     refPropName, ServiceBindingUtils.QUALIFIED_PROP_NAMES);\r
495             if (authRefFieldPaths.isEmpty()) {\r
496                 continue;\r
497             }\r
498             ArrayList<AuthRefConfigInfo> authRefsInfo = new ArrayList<AuthRefConfigInfo>();\r
499             for (String spec : authRefFieldPaths) {\r
500                 AuthRefConfigInfo arci = new AuthRefConfigInfo(spec);\r
501                 authRefsInfo.add(arci);\r
502             }\r
503 \r
504             String docType = sb.getObject().getName();\r
505             queriedServiceBindings.put(docType, sb);\r
506             authRefFieldsByService.put(docType, authRefsInfo);\r
507             docTypes.add(docType);\r
508             fFirst = false;\r
509         }\r
510         if (fFirst) { // found no authRef fields - nothing to query\r
511             return null;\r
512         }\r
513         // We used to build a complete matches query, but that was too complex.\r
514         // Just build a keyword query based upon some key pieces - the urn syntax elements and the shortID\r
515         // Note that this will also match the Item itself, but that will get filtered out when\r
516         // we compute actual matches.\r
517         AuthorityTermInfo authTermInfo = RefNameUtils.parseAuthorityTermInfo(refName);\r
518 \r
519         String keywords = RefNameUtils.URN_PREFIX\r
520                 + " AND " + (authTermInfo.inAuthority.name != null\r
521                 ? authTermInfo.inAuthority.name : authTermInfo.inAuthority.csid)\r
522                 + " AND " + (authTermInfo.name != null\r
523                 ? authTermInfo.name : authTermInfo.csid);\r
524 \r
525         String whereClauseStr = QueryManager.createWhereClauseFromKeywords(keywords);\r
526 \r
527         if (logger.isTraceEnabled()) {\r
528             logger.trace("The 'where' clause to find refObjs is: ", whereClauseStr);\r
529         }\r
530 \r
531         return whereClauseStr;\r
532     }\r
533 \r
534     /*\r
535      * Runs through the list of found docs, processing them. If list is\r
536      * non-null, then processing means gather the info for items. If list is\r
537      * null, and newRefName is non-null, then processing means replacing and\r
538      * updating. If processing/updating, this must be called in teh context of\r
539      * an open session, and caller must release Session after calling this.\r
540      *\r
541      */\r
542     private static int processRefObjsDocList(\r
543             DocumentModelList docList,\r
544             String tenantId,\r
545             String refName,\r
546             Map<String, ServiceBindingType> queriedServiceBindings,\r
547             Map<String, List<AuthRefConfigInfo>> authRefFieldsByService,\r
548             List<AuthorityRefDocList.AuthorityRefDocItem> list,\r
549             String newAuthorityRefName) {\r
550         Iterator<DocumentModel> iter = docList.iterator();\r
551         int nRefsFoundTotal = 0;\r
552         while (iter.hasNext()) {\r
553             DocumentModel docModel = iter.next();\r
554             AuthorityRefDocList.AuthorityRefDocItem ilistItem;\r
555 \r
556             String docType = docModel.getDocumentType().getName();\r
557             docType = ServiceBindingUtils.getUnqualifiedTenantDocType(docType);\r
558             ServiceBindingType sb = queriedServiceBindings.get(docType);\r
559             if (sb == null) {\r
560                 throw new RuntimeException(\r
561                         "getAuthorityRefDocs: No Service Binding for docType: " + docType);\r
562             }\r
563 \r
564             if (list == null) { // no list - should be update refName case.\r
565                 if (newAuthorityRefName == null) {\r
566                     throw new InternalError("processRefObjsDocList() called with neither an itemList nor a new RefName!");\r
567                 }\r
568                 ilistItem = null;\r
569             } else {    // Have a list - refObjs case\r
570                 if (newAuthorityRefName != null) {\r
571                     throw new InternalError("processRefObjsDocList() called with both an itemList and a new RefName!");\r
572                 }\r
573                 ilistItem = new AuthorityRefDocList.AuthorityRefDocItem();\r
574                 String csid = NuxeoUtils.getCsid(docModel);//NuxeoUtils.extractId(docModel.getPathAsString());\r
575                 ilistItem.setDocId(csid);\r
576                 UriTemplateRegistry uriTemplateRegistry = ServiceMain.getInstance().getUriTemplateRegistry();\r
577                 StoredValuesUriTemplate storedValuesResourceTemplate = uriTemplateRegistry.get(new UriTemplateRegistryKey(docType, tenantId));\r
578                 Map<String, String> additionalValues = new HashMap<String, String>();\r
579                 if (storedValuesResourceTemplate.getUriTemplateType() == UriTemplateFactory.ITEM) {\r
580                     try {\r
581                         String inAuthorityCsid = (String) docModel.getPropertyValue("inAuthority"); // AuthorityItemJAXBSchema.IN_AUTHORITY\r
582                         additionalValues.put(UriTemplateFactory.IDENTIFIER_VAR, inAuthorityCsid);\r
583                         additionalValues.put(UriTemplateFactory.ITEM_IDENTIFIER_VAR, csid);\r
584                     } catch (Exception e) {\r
585                         logger.warn("Could not extract inAuthority property from authority item record: " + e.getMessage());\r
586                     }\r
587                 } else {\r
588                     additionalValues.put(UriTemplateFactory.IDENTIFIER_VAR, csid);\r
589                 }\r
590                 String uriStr = storedValuesResourceTemplate.buildUri(additionalValues);\r
591                 ilistItem.setUri(uriStr);\r
592                 try {\r
593                     ilistItem.setWorkflowState(docModel.getCurrentLifeCycleState());\r
594                     ilistItem.setUpdatedAt(DocHandlerBase.getUpdatedAtAsString(docModel));\r
595                 } catch (Exception e) {\r
596                     logger.error("Error getting core values for doc [" + csid + "]: " + e.getLocalizedMessage());\r
597                 }\r
598                 ilistItem.setDocType(docType);\r
599                 ilistItem.setDocNumber(\r
600                         ServiceBindingUtils.getMappedFieldInDoc(sb, ServiceBindingUtils.OBJ_NUMBER_PROP, docModel));\r
601                 ilistItem.setDocName(\r
602                         ServiceBindingUtils.getMappedFieldInDoc(sb, ServiceBindingUtils.OBJ_NAME_PROP, docModel));\r
603             }\r
604             // Now, we have to loop over the authRefFieldsByService to figure\r
605             // out which field(s) matched this.\r
606             List<AuthRefConfigInfo> matchingAuthRefFields = authRefFieldsByService.get(docType);\r
607             if (matchingAuthRefFields == null || matchingAuthRefFields.isEmpty()) {\r
608                 throw new RuntimeException(\r
609                         "getAuthorityRefDocs: internal logic error: can't fetch authRefFields for DocType.");\r
610             }\r
611             //String authRefAncestorField = "";\r
612             //String authRefDescendantField = "";\r
613             //String sourceField = "";\r
614             int nRefsFoundInDoc = 0;\r
615 \r
616             ArrayList<RefNameServiceUtils.AuthRefInfo> foundProps = new ArrayList<RefNameServiceUtils.AuthRefInfo>();\r
617             try {\r
618                 findAuthRefPropertiesInDoc(docModel, matchingAuthRefFields, refName, foundProps);\r
619                 for (RefNameServiceUtils.AuthRefInfo ari : foundProps) {\r
620                     if (ilistItem != null) {\r
621                         if (nRefsFoundInDoc == 0) {    // First one?\r
622                             ilistItem.setSourceField(ari.getQualifiedDisplayName());\r
623                         } else {    // duplicates from one object\r
624                             ilistItem = cloneAuthRefDocItem(ilistItem, ari.getQualifiedDisplayName());\r
625                         }\r
626                         list.add(ilistItem);\r
627                     } else {    // update refName case\r
628                         Property propToUpdate = ari.getProperty();\r
629                         propToUpdate.setValue(newAuthorityRefName);\r
630                     }\r
631                     nRefsFoundInDoc++;\r
632                 }\r
633             } catch (ClientException ce) {\r
634                 throw new RuntimeException(\r
635                         "getAuthorityRefDocs: Problem fetching values from repo: " + ce.getLocalizedMessage());\r
636             }\r
637             if (nRefsFoundInDoc == 0) {\r
638                 logger.warn(\r
639                         "getAuthorityRefDocs: Result: "\r
640                         + docType + " [" + NuxeoUtils.getCsid(docModel)\r
641                         + "] does not reference ["\r
642                         + refName + "]");\r
643             }\r
644             nRefsFoundTotal += nRefsFoundInDoc;\r
645         }\r
646         return nRefsFoundTotal;\r
647     }\r
648 \r
649     private static AuthorityRefDocList.AuthorityRefDocItem cloneAuthRefDocItem(\r
650             AuthorityRefDocList.AuthorityRefDocItem ilistItem, String sourceField) {\r
651         AuthorityRefDocList.AuthorityRefDocItem newlistItem = new AuthorityRefDocList.AuthorityRefDocItem();\r
652         newlistItem.setDocId(ilistItem.getDocId());\r
653         newlistItem.setDocName(ilistItem.getDocName());\r
654         newlistItem.setDocNumber(ilistItem.getDocNumber());\r
655         newlistItem.setDocType(ilistItem.getDocType());\r
656         newlistItem.setUri(ilistItem.getUri());\r
657         newlistItem.setSourceField(sourceField);\r
658         return newlistItem;\r
659     }\r
660 \r
661     public static List<AuthRefInfo> findAuthRefPropertiesInDoc(\r
662             DocumentModel docModel,\r
663             List<AuthRefConfigInfo> authRefFieldInfo,\r
664             String refNameToMatch,\r
665             List<AuthRefInfo> foundProps) {\r
666         // Assume that authRefFieldInfo is keyed by the field name (possibly mapped for UI)\r
667         // and the values are elPaths to the field, where intervening group structures in\r
668         // lists of complex structures are replaced with "*". Thus, valid paths include\r
669         // the following (note that the ServiceBindingUtils prepend schema names to configured values):\r
670         // "schemaname:fieldname"\r
671         // "schemaname:scalarlistname"\r
672         // "schemaname:complexfieldname/fieldname"\r
673         // "schemaname:complexlistname/*/fieldname"\r
674         // "schemaname:complexlistname/*/scalarlistname"\r
675         // "schemaname:complexlistname/*/complexfieldname/fieldname"\r
676         // "schemaname:complexlistname/*/complexlistname/*/fieldname"\r
677         // etc.\r
678         for (AuthRefConfigInfo arci : authRefFieldInfo) {\r
679             try {\r
680                 // Get first property and work down as needed.\r
681                 Property prop = docModel.getProperty(arci.pathEls[0]);\r
682                 findAuthRefPropertiesInProperty(foundProps, prop, arci, 0, refNameToMatch);\r
683             } catch (Exception e) {\r
684                 logger.error("Problem fetching property: " + arci.pathEls[0]);\r
685             }\r
686         }\r
687         return foundProps;\r
688     }\r
689 \r
690     public static List<AuthRefInfo> findAuthRefPropertiesInProperty(\r
691             List<AuthRefInfo> foundProps,\r
692             Property prop,\r
693             AuthRefConfigInfo arci,\r
694             int pathStartIndex, // Supports recursion and we work down the path\r
695             String refNameToMatch) {\r
696         if (pathStartIndex >= arci.pathEls.length) {\r
697             throw new ArrayIndexOutOfBoundsException("Index = " + pathStartIndex + " for path: "\r
698                     + arci.pathEls.toString());\r
699         }\r
700         AuthRefInfo ari = null;\r
701         if (prop == null) {\r
702             return foundProps;\r
703         }\r
704 \r
705         if (prop instanceof StringProperty) {    // scalar string\r
706             addARIifMatches(refNameToMatch, arci, prop, foundProps);\r
707         } else if (prop instanceof List) {\r
708             List<Property> propList = (List<Property>) prop;\r
709             // run through list. Must either be list of Strings, or Complex\r
710             for (Property listItemProp : propList) {\r
711                 if (listItemProp instanceof StringProperty) {\r
712                     if (arci.pathEls.length - pathStartIndex != 1) {\r
713                         logger.error("Configuration for authRefs does not match schema structure: "\r
714                                 + arci.pathEls.toString());\r
715                         break;\r
716                     } else {\r
717                         addARIifMatches(refNameToMatch, arci, listItemProp, foundProps);\r
718                     }\r
719                 } else if (listItemProp.isComplex()) {\r
720                     // Just recurse to handle this. Note that since this is a list of complex, \r
721                     // which should look like listName/*/... we add 2 to the path start index \r
722                     findAuthRefPropertiesInProperty(foundProps, listItemProp, arci,\r
723                             pathStartIndex + 2, refNameToMatch);\r
724                 } else {\r
725                     logger.error("Configuration for authRefs does not match schema structure: "\r
726                             + arci.pathEls.toString());\r
727                     break;\r
728                 }\r
729             }\r
730         } else if (prop.isComplex()) {\r
731             String localPropName = arci.pathEls[pathStartIndex];\r
732             try {\r
733                 Property localProp = prop.get(localPropName);\r
734                 // Now just recurse, pushing down the path 1 step\r
735                 findAuthRefPropertiesInProperty(foundProps, localProp, arci,\r
736                         pathStartIndex, refNameToMatch);\r
737             } catch (PropertyNotFoundException pnfe) {\r
738                 logger.error("Could not find property: [" + localPropName + "] in path: "\r
739                         + arci.getFullPath());\r
740                 // Fall through - ari will be null and we will continue...\r
741             }\r
742         } else {\r
743             logger.error("Configuration for authRefs does not match schema structure: "\r
744                     + arci.pathEls.toString());\r
745         }\r
746 \r
747         if (ari != null) {\r
748             foundProps.add(ari); //FIXME: REM - This is dead code.  'ari' is never touched after being initalized to null.  Why?\r
749         }\r
750 \r
751         return foundProps;\r
752     }\r
753 \r
754     private static void addARIifMatches(\r
755             String refNameToMatch,\r
756             AuthRefConfigInfo arci,\r
757             Property prop,\r
758             List<AuthRefInfo> foundProps) {\r
759         // Need to either match a passed refName \r
760         // OR have no refName to match but be non-empty\r
761         try {\r
762             String value = (String) prop.getValue();\r
763             if (((refNameToMatch != null) && refNameToMatch.equals(value))\r
764                     || ((refNameToMatch == null) && Tools.notBlank(value))) {\r
765                 // Found a match\r
766                 logger.debug("Found a match on property: " + prop.getPath() + " with value: [" + value + "]");\r
767                 AuthRefInfo ari = new AuthRefInfo(arci, prop);\r
768                 foundProps.add(ari);\r
769             }\r
770         } catch (PropertyException pe) {\r
771             logger.debug("PropertyException on: " + prop.getPath() + pe.getLocalizedMessage());\r
772         }\r
773     }\r
774 \r
775     /*\r
776      * Identifies whether the refName was found in the supplied field. If passed\r
777      * a new RefName, will set that into fields in which the old one was found.\r
778      *\r
779      * Only works for: * Scalar fields * Repeatable scalar fields (aka\r
780      * multi-valued fields)\r
781      *\r
782      * Does not work for: * Structured fields (complexTypes) * Repeatable\r
783      * structured fields (repeatable complexTypes) private static int\r
784      * refNameFoundInField(String oldRefName, Property fieldValue, String\r
785      * newRefName) { int nFound = 0; if (fieldValue instanceof List) {\r
786      * List<Property> fieldValueList = (List) fieldValue; for (Property\r
787      * listItemValue : fieldValueList) { try { if ((listItemValue instanceof\r
788      * StringProperty) &&\r
789      * oldRefName.equalsIgnoreCase((String)listItemValue.getValue())) {\r
790      * nFound++; if(newRefName!=null) { fieldValue.setValue(newRefName); } else\r
791      * { // We cannot quit after the first, if we are replacing values. // If we\r
792      * are just looking (not replacing), finding one is enough. break; } } }\r
793      * catch( PropertyException pe ) {} } } else { try { if ((fieldValue\r
794      * instanceof StringProperty) &&\r
795      * oldRefName.equalsIgnoreCase((String)fieldValue.getValue())) { nFound++;\r
796      * if(newRefName!=null) { fieldValue.setValue(newRefName); } } } catch(\r
797      * PropertyException pe ) {} } return nFound; }\r
798      */\r
799 }\r