]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
e692420dc85f50427d95ca7df3f57a4c76fefdfe
[tmp/jakarta-migration.git] /
1 /**
2  * This document is a part of the source code and related artifacts for
3  * CollectionSpace, an open source collections management system for museums and
4  * related institutions:
5  *
6  * http://www.collectionspace.org http://wiki.collectionspace.org
7  *
8  * Copyright 2009 University of California at Berkeley
9  *
10  * Licensed under the Educational Community License (ECL), Version 2.0. You may
11  * not use this file except in compliance with this License.
12  *
13  * You may obtain a copy of the ECL 2.0 License at
14  *
15  * https://source.collectionspace.org/collection-space/LICENSE.txt
16  *
17  * Unless required by applicable law or agreed to in writing, software
18  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
19  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
20  * License for the specific language governing permissions and limitations under
21  * the License.
22  */
23 package org.collectionspace.services.common.vocabulary;
24
25 import java.util.ArrayList;
26 import java.util.HashMap;
27 import java.util.Iterator;
28 import java.util.List;
29 import java.util.Map;
30
31 import javax.ws.rs.core.Response;
32
33 import org.nuxeo.ecm.core.api.ClientException;
34 import org.nuxeo.ecm.core.api.DocumentModel;
35 import org.nuxeo.ecm.core.api.DocumentModelList;
36 import org.nuxeo.ecm.core.api.model.Property;
37 import org.nuxeo.ecm.core.api.model.PropertyException;
38 import org.nuxeo.ecm.core.api.model.PropertyNotFoundException;
39 import org.nuxeo.ecm.core.api.model.impl.primitives.StringProperty;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42 import org.collectionspace.services.client.CollectionSpaceClient;
43 import org.collectionspace.services.client.IQueryManager;
44 import org.collectionspace.services.client.IRelationsManager;
45 import org.collectionspace.services.client.PoxPayloadIn;
46 import org.collectionspace.services.client.PoxPayloadOut;
47 import org.collectionspace.services.common.CSWebApplicationException;
48 import org.collectionspace.services.common.ServiceMain;
49 import org.collectionspace.services.common.StoredValuesUriTemplate;
50 import org.collectionspace.services.common.UriTemplateFactory;
51 import org.collectionspace.services.common.UriTemplateRegistry;
52 import org.collectionspace.services.common.UriTemplateRegistryKey;
53 import org.collectionspace.services.common.context.ServiceContext;
54 import org.collectionspace.services.common.context.AbstractServiceContextImpl;
55 import org.collectionspace.services.common.api.RefNameUtils;
56 import org.collectionspace.services.common.api.Tools;
57 import org.collectionspace.services.common.api.RefNameUtils.AuthorityTermInfo;
58 import org.collectionspace.services.common.authorityref.AuthorityRefDocList;
59 import org.collectionspace.services.common.config.TenantBindingConfigReaderImpl;
60 import org.collectionspace.services.common.context.ServiceBindingUtils;
61 import org.collectionspace.services.common.document.DocumentException;
62 import org.collectionspace.services.common.document.DocumentFilter;
63 import org.collectionspace.services.common.document.DocumentNotFoundException;
64 import org.collectionspace.services.common.document.DocumentUtils;
65 import org.collectionspace.services.common.document.DocumentWrapper;
66 import org.collectionspace.services.common.query.QueryManager;
67 import org.collectionspace.services.common.relation.RelationUtils;
68 import org.collectionspace.services.common.repository.RepositoryClient;
69 import org.collectionspace.services.nuxeo.client.java.CoreSessionInterface;
70 import org.collectionspace.services.nuxeo.client.java.NuxeoDocumentModelHandler;
71 import org.collectionspace.services.nuxeo.client.java.RepositoryClientImpl;
72 import org.collectionspace.services.common.security.SecurityUtils;
73 import org.collectionspace.services.config.service.ServiceBindingType;
74 import org.collectionspace.services.jaxb.AbstractCommonList;
75 import org.collectionspace.services.nuxeo.util.NuxeoUtils;
76
77 /**
78  * RefNameServiceUtils is a collection of services utilities related to refName
79  * usage.
80  *
81  * $LastChangedRevision: $ $LastChangedDate: $
82  */
83 public class RefNameServiceUtils {
84
85     public static enum SpecifierForm {
86         CSID, URN_NAME // Either a CSID or a short ID
87     };
88
89     public static class Specifier {
90         //
91         // URN statics for things like urn:cspace:name(grover)
92         //
93         final static String URN_PREFIX = "urn:cspace:";
94         final static int URN_PREFIX_LEN = URN_PREFIX.length();
95         final static String URN_PREFIX_NAME = "name(";
96         final static int URN_NAME_PREFIX_LEN = URN_PREFIX_LEN + URN_PREFIX_NAME.length();
97         final static String URN_PREFIX_ID = "id(";
98         final static int URN_ID_PREFIX_LEN = URN_PREFIX_LEN + URN_PREFIX_ID.length();
99         
100         public SpecifierForm form;
101         public String value;
102
103         public Specifier(SpecifierForm form, String value) {
104             this.form = form;
105             this.value = value;
106         }
107         
108         /*
109          *  identifier can be a CSID form like a8ad38ec-1d7d-4bf2-bd31 or a URN form like urn:cspace:name(shortid) or urn:cspace:id(a8ad38ec-1d7d-4bf2-bd31)
110          *
111          */
112         public static Specifier getSpecifier(String identifier) throws CSWebApplicationException {
113                 return getSpecifier(identifier, "NO-OP", "NO-OP");
114         }
115
116         /*
117          *  identifier can be a CSID form like a8ad38ec-1d7d-4bf2-bd31 or a URN form like urn:cspace:name(shortid) or urn:cspace:id(a8ad38ec-1d7d-4bf2-bd31)
118          *
119          */
120         public static Specifier getSpecifier(String identifier, String method, String op) throws CSWebApplicationException {
121                 Specifier result = null;
122
123                 if (identifier != null) {
124                 if (!identifier.startsWith(URN_PREFIX)) {
125                     // We'll assume it is a CSID and complain if it does not match
126                     result = new Specifier(SpecifierForm.CSID, identifier);
127                 } else {
128                     if (identifier.startsWith(URN_PREFIX_NAME, URN_PREFIX_LEN)) {
129                         int closeParen = identifier.indexOf(')', URN_NAME_PREFIX_LEN);
130                         if (closeParen >= 0) {
131                             result = new Specifier(SpecifierForm.URN_NAME,
132                                     identifier.substring(URN_NAME_PREFIX_LEN, closeParen));
133                         }
134                     } else if (identifier.startsWith(URN_PREFIX_ID, URN_PREFIX_LEN)) {
135                         int closeParen = identifier.indexOf(')', URN_ID_PREFIX_LEN);
136                         if (closeParen >= 0) {
137                             result = new Specifier(SpecifierForm.CSID,
138                                     identifier.substring(URN_ID_PREFIX_LEN, closeParen));
139                         }
140                     } else {
141                         logger.error(method + ": bad or missing specifier!");
142                         Response response = Response.status(Response.Status.BAD_REQUEST).entity(
143                                 op + " failed on bad or missing Authority specifier").type(
144                                 "text/plain").build();
145                         throw new CSWebApplicationException(response);
146                     }
147                 }
148             }
149             
150             return result;
151         }
152         
153         /**
154          * Creates a refName in the name / shortIdentifier form.
155          *
156          * @param shortId a shortIdentifier for an authority or one of its terms
157          * @return a refName for that authority or term, in the name / shortIdentifier form.
158          *         If the provided shortIdentifier is null or empty, returns
159          *         the empty string.
160          */
161         public static String createShortIdURNValue(String shortId) {
162                 String result = null;
163                 
164             if (shortId != null || !shortId.trim().isEmpty()) {
165                 result = String.format("urn:cspace:name(%s)", shortId);
166             }
167             
168             return result;
169         }        
170         
171         /**
172          * Returns a URN string identifier -e.g., urn:cspace:name(patrick) or urn:cspace:id(579d18a6-b464-4b11-ba3a)
173          * 
174          * @return
175          * @throws Exception
176          */
177         public String getURNValue() throws Exception {
178                 String result = null;
179                 
180                 if (form == SpecifierForm.CSID) {
181                         result = String.format("urn:cspace:id(%s)", value);
182                 } else if (form == SpecifierForm.URN_NAME) {
183                         result = String.format("urn:cspace:name(%s)", value);
184                 } else {
185                         throw new Exception(String.format("Unknown specifier form '%s'.", form));
186                 }
187                 
188                 return result;
189         }
190     }
191     
192     public static class AuthorityItemSpecifier {
193         private Specifier parentSpecifier;
194         private Specifier itemSpecifier;
195         
196         public AuthorityItemSpecifier(Specifier parentSpecifier, Specifier itemSpecifier) {
197                 this.parentSpecifier = parentSpecifier;
198                 this.itemSpecifier = itemSpecifier;
199         }
200         
201         public AuthorityItemSpecifier(SpecifierForm form, String parentCsidOrShortId, String itemCsidOrShortId) {
202                 this.parentSpecifier = new Specifier(form, parentCsidOrShortId);
203                 this.itemSpecifier = new Specifier(form, itemCsidOrShortId);
204         }       
205         
206         public Specifier getParentSpecifier() {
207                 return this.parentSpecifier;
208         }
209         
210         public Specifier getItemSpecifier() {
211                 return this.itemSpecifier;
212         }
213         
214         @Override
215         public String toString() {
216                 String result = "%s/items/%s";
217                 
218                 try {
219                                 result = String.format(result, this.parentSpecifier.getURNValue(), this.itemSpecifier.getURNValue());
220                         } catch (Exception e) {
221                                 result = "Unknown error trying to get string representation of Specifier.";
222                                 logger.error(result, e);
223                         }
224                 
225                 return result;
226         }
227     }
228
229     public static class AuthRefConfigInfo {
230
231         public String getQualifiedDisplayName() {
232             return (Tools.isBlank(schema))
233                     ? displayName : DocumentUtils.appendSchemaName(schema, displayName);
234         }
235
236         public String getDisplayName() {
237             return displayName;
238         }
239
240         public void setDisplayName(String displayName) {
241             this.displayName = displayName;
242         }
243         String displayName;
244         String schema;
245
246         public String getSchema() {
247             return schema;
248         }
249
250         public void setSchema(String schema) {
251             this.schema = schema;
252         }
253
254         public String getFullPath() {
255             return fullPath;
256         }
257
258         public void setFullPath(String fullPath) {
259             this.fullPath = fullPath;
260         }
261         String fullPath;
262         protected String[] pathEls;
263
264         public AuthRefConfigInfo(AuthRefConfigInfo arci) {
265             this.displayName = arci.displayName;
266             this.schema = arci.schema;
267             this.fullPath = arci.fullPath;
268             this.pathEls = arci.pathEls;
269             // Skip the pathElse check, since we are creatign from another (presumably valid) arci.
270         }
271
272         public AuthRefConfigInfo(String displayName, String schema, String fullPath, String[] pathEls) {
273             this.displayName = displayName;
274             this.schema = schema;
275             this.fullPath = fullPath;
276             this.pathEls = pathEls;
277             checkPathEls();
278         }
279
280         // Split a config value string like "intakes_common:collector", or
281         // "collectionobjects_common:contentPeoples|contentPeople"
282         // "collectionobjects_common:assocEventGroupList/*/assocEventPlace"
283         // If has a pipe ('|') second part is a displayLabel, and first is path
284         // Otherwise, entry is a path, and can use the last pathElement as displayName
285         // Should be schema qualified.
286         public AuthRefConfigInfo(String configString) {
287             String[] pair = configString.split("\\|", 2);
288             String[] pathEls;
289             String displayName, fullPath;
290             if (pair.length == 1) {
291                 // no label specifier, so we'll defer getting label
292                 fullPath = pair[0];
293                 pathEls = pair[0].split("/");
294                 displayName = pathEls[pathEls.length - 1];
295             } else {
296                 fullPath = pair[0];
297                 pathEls = pair[0].split("/");
298                 displayName = pair[1];
299             }
300             String[] schemaSplit = pathEls[0].split(":", 2);
301             String schema;
302             if (schemaSplit.length == 1) {    // schema not specified
303                 schema = null;
304             } else {
305                 schema = schemaSplit[0];
306                 if (pair.length == 1 && pathEls.length == 1) {    // simplest case of field in top level schema, no labelll
307                     displayName = schemaSplit[1];    // Have to fix up displayName to have no schema
308                 }
309             }
310             this.displayName = displayName;
311             this.schema = schema;
312             this.fullPath = fullPath;
313             this.pathEls = pathEls;
314             checkPathEls();
315         }
316
317         protected void checkPathEls() {
318             int len = pathEls.length;
319             if (len < 1) {
320                 throw new InternalError("Bad values in authRef info - caller screwed up:" + fullPath);
321             }
322             // Handle case of them putting a leading slash on the path
323             if (len > 1 && pathEls[0].endsWith(":")) {
324                 len--;
325                 String[] newArray = new String[len];
326                 newArray[0] = pathEls[0] + pathEls[1];
327                 if (len >= 2) {
328                     System.arraycopy(pathEls, 2, newArray, 1, len - 1);
329                 }
330                 pathEls = newArray;
331             }
332         }
333     }
334
335     public static class AuthRefInfo extends AuthRefConfigInfo {
336
337         public Property getProperty() {
338             return property;
339         }
340
341         public void setProperty(Property property) {
342             this.property = property;
343         }
344         Property property;
345
346         public AuthRefInfo(String displayName, String schema, String fullPath, String[] pathEls, Property prop) {
347             super(displayName, schema, fullPath, pathEls);
348             this.property = prop;
349         }
350
351         public AuthRefInfo(AuthRefConfigInfo arci, Property prop) {
352             super(arci);
353             this.property = prop;
354         }
355     }
356     
357     private static final Logger logger = LoggerFactory.getLogger(RefNameServiceUtils.class);
358     private static ArrayList<String> refNameServiceTypes = null;
359
360     public static void updateRefNamesInRelations(
361             ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx,
362             RepositoryClient<PoxPayloadIn, PoxPayloadOut> repoClient,
363             CoreSessionInterface repoSession,
364             String oldRefName,
365             String newRefName) throws Exception {
366         //
367         // First, look for and update all the places where the refName is the "subject" of the relationship
368         //
369         RelationUtils.updateRefNamesInRelations(ctx, repoClient, repoSession, IRelationsManager.SUBJECT_REFNAME, oldRefName, newRefName);
370         
371         //
372         // Next, look for and update all the places where the refName is the "object" of the relationship
373         //
374         RelationUtils.updateRefNamesInRelations(ctx, repoClient, repoSession, IRelationsManager.OBJECT_REFNAME, oldRefName, newRefName);
375     }
376     
377         public static List<AuthRefConfigInfo> getConfiguredAuthorityRefs(ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx) {
378                 List<String> authRefFields = ((AbstractServiceContextImpl) ctx).getAllPartsPropertyValues(
379                                 ServiceBindingUtils.AUTH_REF_PROP, ServiceBindingUtils.QUALIFIED_PROP_NAMES);
380                 ArrayList<AuthRefConfigInfo> authRefsInfo = new ArrayList<AuthRefConfigInfo>(authRefFields.size());
381                 for (String spec : authRefFields) {
382                         AuthRefConfigInfo arci = new AuthRefConfigInfo(spec);
383                         authRefsInfo.add(arci);
384                 }
385                 return authRefsInfo;
386         }
387
388     public static AuthorityRefDocList getAuthorityRefDocs(
389                 CoreSessionInterface repoSession,
390             ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx,
391             UriTemplateRegistry uriTemplateRegistry,
392             RepositoryClient<PoxPayloadIn, PoxPayloadOut> repoClient,
393             List<String> serviceTypes,
394             String refName,
395             String refPropName, // authRef or termRef, authorities or vocab terms.
396             DocumentFilter filter, boolean computeTotal)
397             throws DocumentException, DocumentNotFoundException {
398         AuthorityRefDocList wrapperList = new AuthorityRefDocList();
399         AbstractCommonList commonList = (AbstractCommonList) wrapperList;
400         int pageNum = filter.getStartPage();
401         int pageSize = filter.getPageSize();
402         
403         List<AuthorityRefDocList.AuthorityRefDocItem> list =
404                 wrapperList.getAuthorityRefDocItem();
405
406         Map<String, ServiceBindingType> queriedServiceBindings = new HashMap<String, ServiceBindingType>();
407         Map<String, List<AuthRefConfigInfo>> authRefFieldsByService = new HashMap<String, List<AuthRefConfigInfo>>();
408
409         RepositoryClientImpl nuxeoRepoClient = (RepositoryClientImpl) repoClient;
410         try {
411             // Ignore any provided page size and number query parameters in
412             // the following call, as they pertain to the list of authority
413             // references to be returned, not to the list of documents to be
414             // scanned for those references.
415             
416             // Get a list of possibly referencing documents. This list is
417             // lazily loaded, page by page. Ideally, only one page will
418             // need to be loaded to fill one page of results. Some number
419             // of possibly referencing documents will be false positives,
420             // so use a page size of double the requested page size to
421             // account for those.
422             DocumentModelList docList = findAllAuthorityRefDocs(ctx, repoClient, repoSession,
423                     serviceTypes, refName, refPropName, queriedServiceBindings, authRefFieldsByService,
424                     filter.getWhereClause(), null, 2*pageSize, computeTotal);
425
426             if (docList == null) { // found no authRef fields - nothing to process
427                 return wrapperList;
428             }
429
430             // set the fieldsReturned list. Even though this is a fixed schema, app layer treats
431             // this like other abstract common lists
432             /*
433              * <xs:element name="docType" type="xs:string" minOccurs="1" />
434              * <xs:element name="docId" type="xs:string" minOccurs="1" />
435              * <xs:element name="docNumber" type="xs:string" minOccurs="0" />
436              * <xs:element name="docName" type="xs:string" minOccurs="0" />
437              * <xs:element name="sourceField" type="xs:string" minOccurs="1" />
438              * <xs:element name="uri" type="xs:anyURI" minOccurs="1" />
439              * <xs:element name="refName" type="xs:String" minOccurs="1" />
440              * <xs:element name="updatedAt" type="xs:string" minOccurs="1" />
441              * <xs:element name="workflowState" type="xs:string" minOccurs="1"
442              * />
443              */
444             String fieldList = "docType|docId|docNumber|docName|sourceField|uri|refName|updatedAt|workflowState";
445             commonList.setFieldsReturned(fieldList);
446
447             // As a side-effect, the method called below modifies the value of
448             // the 'list' variable, which holds the list of references to
449             // an authority item.
450             //
451             // There can be more than one reference to a particular authority
452             // item within any individual document scanned, so the number of
453             // authority references may potentially exceed the total number
454             // of documents scanned.
455
456             // Strip off displayName and only match the base, so we get references to all 
457             // the NPTs as well as the PT.
458                 String strippedRefName = RefNameUtils.stripAuthorityTermDisplayName(refName);
459                 
460                 // *** Need to pass in pagination info here. 
461             int nRefsFound = processRefObjsDocListForList(docList, ctx.getTenantId(), strippedRefName, 
462                         queriedServiceBindings, authRefFieldsByService, // the actual list size needs to be updated to the size of "list"
463                     list, pageSize, pageNum);
464                 
465             commonList.setPageSize(pageSize);
466             
467             // Values returned in the pagination block above the list items
468             // need to reflect the number of references to authority items
469             // returned, rather than the number of documents originally scanned
470             // to find such references.
471             // This will be an estimate only...
472             commonList.setPageNum(pageNum);
473                 commonList.setTotalItems(nRefsFound);   // Accurate if total was scanned, otherwise, just an estimate
474             commonList.setItemsInPage(list.size());
475
476             /* Pagination is now handled in the processing step
477             // Slice the list to return only the specified page of items
478             // in the list results.
479             //
480             // FIXME: There may well be a pattern-based way to do this
481             // in our framework, and if we can eliminate much of the
482             // non-DRY code below, that would be desirable.
483             
484             int startIndex = 0;
485             int endIndex = 0;
486             
487             // Return all results if pageSize is 0.
488             if (pageSize == 0) {
489                 startIndex = 0;
490                 endIndex = list.size();
491             } else {
492                startIndex = pageNum * pageSize;
493             }
494             
495             // Return an empty list when the start of the requested page is
496             // beyond the last item in the list.
497             if (startIndex > list.size()) {
498                 wrapperList.getAuthorityRefDocItem().clear();
499                 commonList.setItemsInPage(wrapperList.getAuthorityRefDocItem().size());
500                 return wrapperList;
501             }
502
503             // Otherwise, return a list of items from the start of the specified
504             // page through the last item on that page, or otherwise through the
505             // last item in the entire list, if that occurs earlier than the end
506             // of the specified page.
507             if (endIndex == 0) {
508                 int pageEndIndex = ((startIndex + pageSize));
509                 endIndex = (pageEndIndex > list.size()) ? list.size() : pageEndIndex;
510             }
511             
512             // Slice the list to return only the specified page of results.
513             // Note: the second argument to List.subList(), endIndex, is
514             // exclusive of the item at its index position, reflecting the
515             // zero-index nature of the list.
516             List<AuthorityRefDocList.AuthorityRefDocItem> currentPageList =
517                     new ArrayList<AuthorityRefDocList.AuthorityRefDocItem>(list.subList(startIndex, endIndex));
518             wrapperList.getAuthorityRefDocItem().clear();
519             wrapperList.getAuthorityRefDocItem().addAll(currentPageList);
520             commonList.setItemsInPage(currentPageList.size());
521             */
522             
523             if (logger.isDebugEnabled() && (nRefsFound < docList.size())) {
524                 logger.debug("Internal curiosity: got fewer matches of refs than # docs matched..."); // We found a ref to ourself and have excluded it.
525             }
526         } catch (Exception e) {
527             logger.error("Could not retrieve a list of documents referring to the specified authority item", e);
528             wrapperList = null;
529         }
530
531         return wrapperList;
532     }
533
534     private static ArrayList<String> getRefNameServiceTypes() {
535         if (refNameServiceTypes == null) {
536             refNameServiceTypes = new ArrayList<String>();
537             refNameServiceTypes.add(ServiceBindingUtils.SERVICE_TYPE_AUTHORITY);
538             refNameServiceTypes.add(ServiceBindingUtils.SERVICE_TYPE_OBJECT);
539             refNameServiceTypes.add(ServiceBindingUtils.SERVICE_TYPE_PROCEDURE);
540         }
541         return refNameServiceTypes;
542     }
543     
544     // Seems like a good value - no real data to set this well.
545     // Note: can set this value lower during debugging; e.g. to 3 - ADR 2012-07-10
546     private static final int N_OBJS_TO_UPDATE_PER_LOOP = 100;
547
548     public static int updateAuthorityRefDocs(
549             ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx,
550             RepositoryClient<PoxPayloadIn, PoxPayloadOut> repoClient,
551             CoreSessionInterface repoSession,
552             String oldRefName,
553             String newRefName,
554             String refPropName) throws Exception {
555         Map<String, ServiceBindingType> queriedServiceBindings = new HashMap<String, ServiceBindingType>();
556         Map<String, List<AuthRefConfigInfo>> authRefFieldsByService = new HashMap<String, List<AuthRefConfigInfo>>();
557
558         int docsScanned = 0;
559         int nRefsFound = 0;
560         int currentPage = 0;
561         int docsInCurrentPage = 0;
562         final String WHERE_CLAUSE_ADDITIONS_VALUE = null;
563         final String ORDER_BY_VALUE = CollectionSpaceClient.CORE_CREATED_AT  // "collectionspace_core:createdAt";
564                                           + ", " + IQueryManager.NUXEO_UUID; // CSPACE-6333: Add secondary sort on uuid, in case records have the same createdAt timestamp.
565
566         if (repoClient instanceof RepositoryClientImpl == false) {
567             throw new InternalError("updateAuthorityRefDocs() called with unknown repoClient type!");
568         }
569         
570         try { // REM - How can we deal with transaction and timeout issues here?
571             final int pageSize = N_OBJS_TO_UPDATE_PER_LOOP;
572             DocumentModelList docList;
573             boolean morePages = true;
574             while (morePages) {
575
576                 docList = findAuthorityRefDocs(ctx, repoClient, repoSession,
577                         getRefNameServiceTypes(), oldRefName, refPropName,
578                         queriedServiceBindings, authRefFieldsByService, WHERE_CLAUSE_ADDITIONS_VALUE, ORDER_BY_VALUE, pageSize, currentPage, false);
579
580                 if (docList == null) {
581                     logger.debug("updateAuthorityRefDocs: no documents could be found that referenced the old refName");
582                     break;
583                 }
584                 docsInCurrentPage = docList.size();
585                 logger.debug("updateAuthorityRefDocs: current page=" + currentPage + " documents included in page=" + docsInCurrentPage);
586                 if (docsInCurrentPage == 0) {
587                     logger.debug("updateAuthorityRefDocs: no more documents requiring refName updates could be found");
588                     break;
589                 }
590                 if (docsInCurrentPage < pageSize) {
591                     logger.debug("updateAuthorityRefDocs: assuming no more documents requiring refName updates will be found, as docsInCurrentPage < pageSize");
592                     morePages = false;
593                 }
594
595                 // Only match complete refNames - unless and until we decide how to resolve changes
596                 // to NPTs we will defer that and only change PTs or refNames as passed in.
597                 int nRefsFoundThisPage = processRefObjsDocListForUpdate(docList, ctx.getTenantId(), oldRefName, 
598                                 queriedServiceBindings, authRefFieldsByService, // Perform the refName updates on the list of document models
599                         newRefName);
600                 if (nRefsFoundThisPage > 0) {
601                     ((RepositoryClientImpl) repoClient).saveDocListWithoutHandlerProcessing(ctx, repoSession, docList, true); // Flush the document model list out to Nuxeo storage
602                     nRefsFound += nRefsFoundThisPage;
603                 }
604
605                 // FIXME: Per REM, set a limit of num objects - something like
606                 // 1000K objects - and also add a log Warning after some threshold
607                 docsScanned += docsInCurrentPage;
608                 if (morePages) {
609                     currentPage++;
610                 }
611
612             }
613         } catch (Exception e) {
614             logger.error("Internal error updating the AuthorityRefDocs: " + e.getLocalizedMessage());
615             logger.debug(Tools.errorToString(e, true));
616             throw e;
617         }
618         logger.debug("updateAuthorityRefDocs replaced a total of " + nRefsFound + " authority references, within as many as " + docsScanned + " scanned document(s)");
619         return nRefsFound;
620     }
621
622     private static DocumentModelList findAllAuthorityRefDocs(
623             ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx,
624             RepositoryClient<PoxPayloadIn, PoxPayloadOut> repoClient,
625             CoreSessionInterface repoSession, List<String> serviceTypes,
626             String refName,
627             String refPropName,
628             Map<String, ServiceBindingType> queriedServiceBindings,
629             Map<String, List<AuthRefConfigInfo>> authRefFieldsByService,
630             String whereClauseAdditions,
631             String orderByClause,
632             int pageSize,
633             boolean computeTotal) throws DocumentException, DocumentNotFoundException {
634                 
635         return new LazyAuthorityRefDocList(ctx, repoClient, repoSession,
636                         serviceTypes, refName, refPropName, queriedServiceBindings, authRefFieldsByService,
637                         whereClauseAdditions, orderByClause, pageSize, computeTotal);
638     }
639     
640     protected static DocumentModelList findAuthorityRefDocs(
641             ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx,
642             RepositoryClient<PoxPayloadIn, PoxPayloadOut> repoClient,
643             CoreSessionInterface repoSession, List<String> serviceTypes,
644             String refName,
645             String refPropName,
646             Map<String, ServiceBindingType> queriedServiceBindings,
647             Map<String, List<AuthRefConfigInfo>> authRefFieldsByService,
648             String whereClauseAdditions,
649             String orderByClause,
650             int pageSize,
651             int pageNum,
652             boolean computeTotal) throws DocumentException, DocumentNotFoundException {
653
654         // Get the service bindings for this tenant
655         TenantBindingConfigReaderImpl tReader =
656                 ServiceMain.getInstance().getTenantBindingConfigReader();
657         // We need to get all the procedures, authorities, and objects.
658         List<ServiceBindingType> servicebindings = tReader.getServiceBindingsByType(ctx.getTenantId(), serviceTypes);
659         if (servicebindings == null || servicebindings.isEmpty()) {
660             logger.error("RefNameServiceUtils.getAuthorityRefDocs: No services bindings found, cannot proceed!");
661             return null;
662         }
663         // Filter the list for current user rights
664         servicebindings = SecurityUtils.getReadableServiceBindingsForCurrentUser(servicebindings);
665
666         ArrayList<String> docTypes = new ArrayList<String>();
667
668         String query = computeWhereClauseForAuthorityRefDocs(refName, refPropName, docTypes, servicebindings, // REM - Side effect that docTypes, authRefFieldsByService, and queriedServiceBindings get set/change.  Any others?
669                 queriedServiceBindings, authRefFieldsByService);
670         if (query == null) { // found no authRef fields - nothing to query
671             return null;
672         }
673         // Additional qualifications, like workflow state
674         if (Tools.notBlank(whereClauseAdditions)) {
675             query += " AND " + whereClauseAdditions;
676         }
677         // Now we have to issue the search
678         RepositoryClientImpl nuxeoRepoClient = (RepositoryClientImpl) repoClient;
679         DocumentWrapper<DocumentModelList> docListWrapper = nuxeoRepoClient.findDocs(ctx, repoSession,
680                 docTypes, query, orderByClause, pageSize, pageNum, computeTotal);
681         // Now we gather the info for each document into the list and return
682         DocumentModelList docList = docListWrapper.getWrappedObject();
683         return docList;
684     }
685     private static final boolean READY_FOR_COMPLEX_QUERY = true;
686
687     private static String computeWhereClauseForAuthorityRefDocs(
688             String refName,
689             String refPropName,
690             ArrayList<String> docTypes,
691             List<ServiceBindingType> servicebindings,
692             Map<String, ServiceBindingType> queriedServiceBindings,
693             Map<String, List<AuthRefConfigInfo>> authRefFieldsByService) {
694
695         boolean fFirst = true;
696         List<String> authRefFieldPaths;
697         for (ServiceBindingType sb : servicebindings) {
698             // Gets the property names for each part, qualified with the part label (which
699             // is also the table name, the way that the repository works).
700             authRefFieldPaths =
701                     ServiceBindingUtils.getAllPartsPropertyValues(sb,
702                     refPropName, ServiceBindingUtils.QUALIFIED_PROP_NAMES);
703             if (authRefFieldPaths.isEmpty()) {
704                 continue;
705             }
706             ArrayList<AuthRefConfigInfo> authRefsInfo = new ArrayList<AuthRefConfigInfo>();
707             for (String spec : authRefFieldPaths) {
708                 AuthRefConfigInfo arci = new AuthRefConfigInfo(spec);
709                 authRefsInfo.add(arci);
710             }
711
712             String docType = sb.getObject().getName();
713             queriedServiceBindings.put(docType, sb);
714             authRefFieldsByService.put(docType, authRefsInfo);
715             docTypes.add(docType);
716             fFirst = false;
717         }
718         if (fFirst) { // found no authRef fields - nothing to query
719             return null;
720         }
721         // We used to build a complete matches query, but that was too complex.
722         // Just build a keyword query based upon some key pieces - the urn syntax elements and the shortID
723         // Note that this will also match the Item itself, but that will get filtered out when
724         // we compute actual matches.
725         AuthorityTermInfo authTermInfo = RefNameUtils.parseAuthorityTermInfo(refName);
726
727         String keywords = RefNameUtils.URN_PREFIX
728                 + " AND " + (authTermInfo.inAuthority.name != null
729                 ? authTermInfo.inAuthority.name : authTermInfo.inAuthority.csid)
730                 + " AND " + (authTermInfo.name != null
731                 ? authTermInfo.name : authTermInfo.csid); // REM - This seems likely to cause trouble?  We should consider searching for the full refname -excluding the display name suffix?
732
733         String whereClauseStr = QueryManager.createWhereClauseFromKeywords(keywords);
734
735         if (logger.isTraceEnabled()) {
736             logger.trace("The 'where' clause to find refObjs is: ", whereClauseStr);
737         }
738
739         return whereClauseStr;
740     }
741     
742     // TODO there are multiple copies of this that should be put somewhere common.
743         protected static String getRefname(DocumentModel docModel) throws ClientException {
744                 String result = (String)docModel.getProperty(CollectionSpaceClient.COLLECTIONSPACE_CORE_SCHEMA,
745                                 CollectionSpaceClient.COLLECTIONSPACE_CORE_REFNAME);
746                 return result;
747         }
748
749     private static int processRefObjsDocListForUpdate(
750             DocumentModelList docList,
751             String tenantId,
752             String refName,
753             Map<String, ServiceBindingType> queriedServiceBindings,
754             Map<String, List<AuthRefConfigInfo>> authRefFieldsByService,
755             String newAuthorityRefName) {
756         return processRefObjsDocList(docList, tenantId, refName, false, queriedServiceBindings,
757                         authRefFieldsByService, null, 0, 0, newAuthorityRefName);
758     }
759                         
760     private static int processRefObjsDocListForList(
761             DocumentModelList docList,
762             String tenantId,
763             String refName,
764             Map<String, ServiceBindingType> queriedServiceBindings,
765             Map<String, List<AuthRefConfigInfo>> authRefFieldsByService,
766             List<AuthorityRefDocList.AuthorityRefDocItem> list, 
767             int pageSize, int pageNum) {
768         return processRefObjsDocList(docList, tenantId, refName, true, queriedServiceBindings,
769                         authRefFieldsByService, list, pageSize, pageNum, null);
770     }
771                         
772
773         /*
774      * Runs through the list of found docs, processing them. If list is
775      * non-null, then processing means gather the info for items. If list is
776      * null, and newRefName is non-null, then processing means replacing and
777      * updating. If processing/updating, this must be called in the context of
778      * an open session, and caller must release Session after calling this.
779      *
780      */
781     private static int processRefObjsDocList(
782             DocumentModelList docList,
783             String tenantId,
784             String refName,
785             boolean matchBaseOnly,
786             Map<String, ServiceBindingType> queriedServiceBindings,
787             Map<String, List<AuthRefConfigInfo>> authRefFieldsByService,
788             List<AuthorityRefDocList.AuthorityRefDocItem> list,
789             int pageSize, int pageNum,  // Only used when constructing a list.
790             String newAuthorityRefName) {
791         UriTemplateRegistry registry = ServiceMain.getInstance().getUriTemplateRegistry();
792         Iterator<DocumentModel> iter = docList.iterator();
793         int nRefsFoundTotal = 0;
794         boolean foundSelf = false;
795
796         // When paginating results, we have to guess at the total. First guess is the number of docs returned
797         // by the query. However, this returns some false positives, so may be high. 
798         // In addition, we can match multiple fields per doc, so this may be low. Fun, eh?
799         int nDocsReturnedInQuery = (int)docList.totalSize();
800         int nDocsProcessed = 0;
801         int firstItemInPage = pageNum*pageSize;
802         while (iter.hasNext()) {
803             DocumentModel docModel = iter.next();
804             AuthorityRefDocList.AuthorityRefDocItem ilistItem;
805
806             String docType = docModel.getDocumentType().getName(); // REM - This will be a tentant qualified document type
807             docType = ServiceBindingUtils.getUnqualifiedTenantDocType(docType);
808             ServiceBindingType sb = queriedServiceBindings.get(docType);
809             if (sb == null) {
810                 throw new RuntimeException(
811                         "getAuthorityRefDocs: No Service Binding for docType: " + docType);
812             }
813
814             if (list == null) { // no list - should be update refName case.
815                 if (newAuthorityRefName == null) {
816                     throw new InternalError("processRefObjsDocList() called with neither an itemList nor a new RefName!");
817                 }
818                 ilistItem = null;
819                 pageSize = 0;
820                 firstItemInPage = 0;    // Do not paginate if updating, rather than building list
821             } else {    // Have a list - refObjs case
822                 if (newAuthorityRefName != null) {
823                     throw new InternalError("processRefObjsDocList() called with both an itemList and a new RefName!");
824                 }
825                 if(firstItemInPage > 100) {
826                         logger.warn("Processing a large offset (size:{}, num:{}) for refObjs - will be expensive!!!",
827                                                 pageSize, pageNum);
828                 }
829                 // Note that we have to go through check all the fields to determine the actual page start
830                 ilistItem = new AuthorityRefDocList.AuthorityRefDocItem();
831                 String csid = NuxeoUtils.getCsid(docModel);//NuxeoUtils.extractId(docModel.getPathAsString());
832                 try {
833                         String itemRefName = getRefname(docModel);
834                         ilistItem.setRefName(itemRefName);
835                 } catch (ClientException ce) {
836                     throw new RuntimeException(
837                             "processRefObjsDocList: Problem fetching refName from item Object: " 
838                                         + ce.getLocalizedMessage());
839                 }
840                 ilistItem.setDocId(csid);
841                 String uri = "";
842                 UriTemplateRegistryKey key = new UriTemplateRegistryKey(tenantId, docType);
843                 StoredValuesUriTemplate template = registry.get(key);
844                 if (template != null) {
845                     Map<String, String> additionalValues = new HashMap<String, String>();
846                     if (template.getUriTemplateType() == UriTemplateFactory.RESOURCE) {
847                         additionalValues.put(UriTemplateFactory.IDENTIFIER_VAR, csid);
848                         uri = template.buildUri(additionalValues);
849                     } else if (template.getUriTemplateType() == UriTemplateFactory.ITEM) {
850                         try {
851                             String inAuthorityCsid = (String) NuxeoUtils.getProperyValue(docModel, "inAuthority"); //docModel.getPropertyValue("inAuthority"); // AuthorityItemJAXBSchema.IN_AUTHORITY
852                             additionalValues.put(UriTemplateFactory.IDENTIFIER_VAR, inAuthorityCsid);
853                             additionalValues.put(UriTemplateFactory.ITEM_IDENTIFIER_VAR, csid);
854                             uri = template.buildUri(additionalValues);
855                         } catch (Exception e) {
856                             logger.warn("Could not extract inAuthority property from authority item record: " + e.getMessage());
857                         }
858                     } else if (template.getUriTemplateType() == UriTemplateFactory.CONTACT) {
859                         // FIXME: Generating contact sub-resource URIs requires additional work,
860                         // as a follow-on to CSPACE-5271 - ADR 2012-08-16
861                         // Sets the default (empty string) value for uri, for now
862                     } else {
863                         logger.warn("Unrecognized URI template type = " + template.getUriTemplateType());
864                         // Sets the default (empty string) value for uri
865                     }
866                 } else { // (if template == null)
867                     logger.warn("Could not retrieve URI template from registry via tenant ID "
868                             + tenantId + " and docType " + docType);
869                     // Sets the default (empty string) value for uri
870                 }
871                 ilistItem.setUri(uri);
872                 try {
873                     ilistItem.setWorkflowState(docModel.getCurrentLifeCycleState());
874                     ilistItem.setUpdatedAt(NuxeoDocumentModelHandler.getUpdatedAtAsString(docModel));
875                 } catch (Exception e) {
876                     logger.error("Error getting core values for doc [" + csid + "]: " + e.getLocalizedMessage());
877                 }
878                 ilistItem.setDocType(docType);
879                 ilistItem.setDocNumber(
880                         ServiceBindingUtils.getMappedFieldInDoc(sb, ServiceBindingUtils.OBJ_NUMBER_PROP, docModel));
881                 ilistItem.setDocName(
882                         ServiceBindingUtils.getMappedFieldInDoc(sb, ServiceBindingUtils.OBJ_NAME_PROP, docModel));
883             }
884             // Now, we have to loop over the authRefFieldsByService to figure
885             // out which field(s) matched this.
886             List<AuthRefConfigInfo> matchingAuthRefFields = authRefFieldsByService.get(docType);
887             if (matchingAuthRefFields == null || matchingAuthRefFields.isEmpty()) {
888                 throw new RuntimeException(
889                         "getAuthorityRefDocs: internal logic error: can't fetch authRefFields for DocType.");
890             }
891             //String authRefAncestorField = "";
892             //String authRefDescendantField = "";
893             //String sourceField = "";
894
895             ArrayList<RefNameServiceUtils.AuthRefInfo> foundProps = new ArrayList<RefNameServiceUtils.AuthRefInfo>();
896             try {
897                 findAuthRefPropertiesInDoc(docModel, matchingAuthRefFields, refName, matchBaseOnly, foundProps); // REM - side effect that foundProps is set
898                 if(!foundProps.isEmpty()) {
899                     int nRefsFoundInDoc = 0;
900                         for (RefNameServiceUtils.AuthRefInfo ari : foundProps) {
901                                 if (ilistItem != null) {
902                                         // So this is a true positive, and not a false one. We have to consider pagination now.
903                                         if(nRefsFoundTotal >= firstItemInPage) {        // skipped enough already
904                                                 if (nRefsFoundInDoc == 0) {    // First one?
905                                                         ilistItem.setSourceField(ari.getQualifiedDisplayName());
906                                                 } else {    // duplicates from one object
907                                                         ilistItem = cloneAuthRefDocItem(ilistItem, ari.getQualifiedDisplayName());
908                                                 }
909                                                 list.add(ilistItem);
910                                         nRefsFoundInDoc++;      // Only increment if processed, or clone logic above will fail
911                                         }
912                                 } else {    // update refName case
913                                         Property propToUpdate = ari.getProperty();
914                                         propToUpdate.setValue(newAuthorityRefName);
915                                 }
916                                 nRefsFoundTotal++;              // Whether we processed or not, we found - essential to pagination logic
917                         }
918                 } else if(ilistItem != null) {
919                         String docRefName = ilistItem.getRefName();
920                     if (matchBaseOnly?
921                                         (docRefName!=null && docRefName.startsWith(refName))
922                                         :refName.equals(docRefName)) {
923                                 // We found the self for an item
924                                 foundSelf = true;
925                                 logger.debug("getAuthorityRefDocs: Result: "
926                                                                 + docType + " [" + NuxeoUtils.getCsid(docModel)
927                                                                 + "] appears to be self for: ["
928                                                                 + refName + "]");
929                         } else {
930                                 logger.debug("getAuthorityRefDocs: Result: "
931                                                                 + docType + " [" + NuxeoUtils.getCsid(docModel)
932                                                                 + "] does not reference ["
933                                                                 + refName + "]");
934                         }
935                 }
936             } catch (ClientException ce) {
937                 throw new RuntimeException(
938                                 "getAuthorityRefDocs: Problem fetching values from repo: " + ce.getLocalizedMessage());
939             }
940             nDocsProcessed++;
941             // Done processing that doc. Are we done with the whole page?
942             // Note pageSize <=0 means do them all
943             if((pageSize > 0) && ((nRefsFoundTotal-firstItemInPage)>=pageSize)) {
944                 // Quitting early, so we need to estimate the total. Assume one per doc
945                 // for the rest of the docs we matched in the query
946                 int unprocessedDocs = nDocsReturnedInQuery - nDocsProcessed;
947                 if(unprocessedDocs>0) {
948                         // We generally match ourselves in the keyword search. If we already saw ourselves
949                         // then do not try to correct for this. Otherwise, decrement the total.
950                         // Yes, this is fairly goofy, but the whole estimation mechanism is goofy. 
951                         if(!foundSelf)
952                                 unprocessedDocs--;
953                         nRefsFoundTotal += unprocessedDocs;
954                 }
955                 break;
956             }
957         } // close while(iterator)
958         return nRefsFoundTotal;
959     }
960
961     private static AuthorityRefDocList.AuthorityRefDocItem cloneAuthRefDocItem(
962             AuthorityRefDocList.AuthorityRefDocItem ilistItem, String sourceField) {
963         AuthorityRefDocList.AuthorityRefDocItem newlistItem = new AuthorityRefDocList.AuthorityRefDocItem();
964         newlistItem.setDocId(ilistItem.getDocId());
965         newlistItem.setDocName(ilistItem.getDocName());
966         newlistItem.setDocNumber(ilistItem.getDocNumber());
967         newlistItem.setDocType(ilistItem.getDocType());
968         newlistItem.setUri(ilistItem.getUri());
969         newlistItem.setSourceField(sourceField);
970         return newlistItem;
971     }
972
973     public static List<AuthRefInfo> findAuthRefPropertiesInDoc(
974             DocumentModel docModel,
975             List<AuthRefConfigInfo> authRefFieldInfo,
976             String refNameToMatch,
977             List<AuthRefInfo> foundProps) {
978         return findAuthRefPropertiesInDoc(docModel, authRefFieldInfo, 
979                                                                         refNameToMatch, false, foundProps);
980     }
981     
982     public static List<AuthRefInfo> findAuthRefPropertiesInDoc(
983             DocumentModel docModel,
984             List<AuthRefConfigInfo> authRefFieldInfo,
985             String refNameToMatch,
986             boolean matchBaseOnly,
987             List<AuthRefInfo> foundProps) {
988         // Assume that authRefFieldInfo is keyed by the field name (possibly mapped for UI)
989         // and the values are elPaths to the field, where intervening group structures in
990         // lists of complex structures are replaced with "*". Thus, valid paths include
991         // the following (note that the ServiceBindingUtils prepend schema names to configured values):
992         // "schemaname:fieldname"
993         // "schemaname:scalarlistname"
994         // "schemaname:complexfieldname/fieldname"
995         // "schemaname:complexlistname/*/fieldname"
996         // "schemaname:complexlistname/*/scalarlistname"
997         // "schemaname:complexlistname/*/complexfieldname/fieldname"
998         // "schemaname:complexlistname/*/complexlistname/*/fieldname"
999         // etc.
1000         for (AuthRefConfigInfo arci : authRefFieldInfo) {
1001             try {
1002                 // Get first property and work down as needed.
1003                 Property prop = docModel.getProperty(arci.pathEls[0]);
1004                 findAuthRefPropertiesInProperty(foundProps, prop, arci, 0, refNameToMatch, matchBaseOnly);
1005             } catch (Exception e) {
1006                 logger.error("Problem fetching property: " + arci.pathEls[0]);
1007             }
1008         }
1009         return foundProps;
1010     }
1011
1012     private static List<AuthRefInfo> findAuthRefPropertiesInProperty(
1013             List<AuthRefInfo> foundProps,
1014             Property prop,
1015             AuthRefConfigInfo arci,
1016             int pathStartIndex, // Supports recursion and we work down the path
1017             String refNameToMatch,
1018             boolean matchBaseOnly ) {
1019         if (pathStartIndex >= arci.pathEls.length) {
1020             throw new ArrayIndexOutOfBoundsException("Index = " + pathStartIndex + " for path: "
1021                     + arci.pathEls.toString());
1022         }
1023         AuthRefInfo ari = null;
1024         if (prop == null) {
1025             return foundProps;
1026         }
1027
1028         if (prop instanceof StringProperty) {    // scalar string
1029             addARIifMatches(refNameToMatch, matchBaseOnly, arci, prop, foundProps); // REM - Side effect that foundProps gets changed/updated
1030         } else if (prop instanceof List) {
1031             List<Property> propList = (List<Property>) prop;
1032             // run through list. Must either be list of Strings, or Complex
1033             for (Property listItemProp : propList) {
1034                 if (listItemProp instanceof StringProperty) {
1035                     if (arci.pathEls.length - pathStartIndex != 1) {
1036                         logger.error("Configuration for authRefs does not match schema structure: "
1037                                 + arci.pathEls.toString());
1038                         break;
1039                     } else {
1040                         addARIifMatches(refNameToMatch, matchBaseOnly, arci, listItemProp, foundProps);
1041                     }
1042                 } else if (listItemProp.isComplex()) {
1043                     // Just recurse to handle this. Note that since this is a list of complex, 
1044                     // which should look like listName/*/... we add 2 to the path start index 
1045                     findAuthRefPropertiesInProperty(foundProps, listItemProp, arci,
1046                             pathStartIndex + 2, refNameToMatch, matchBaseOnly);
1047                 } else {
1048                     logger.error("Configuration for authRefs does not match schema structure: "
1049                             + arci.pathEls.toString());
1050                     break;
1051                 }
1052             }
1053         } else if (prop.isComplex()) {
1054             String localPropName = arci.pathEls[pathStartIndex];
1055             try {
1056                 Property localProp = prop.get(localPropName);
1057                 // Now just recurse, pushing down the path 1 step
1058                 findAuthRefPropertiesInProperty(foundProps, localProp, arci,
1059                         pathStartIndex, refNameToMatch, matchBaseOnly);
1060             } catch (PropertyNotFoundException pnfe) {
1061                 logger.error("Could not find property: [" + localPropName + "] in path: "
1062                         + arci.getFullPath());
1063                 // Fall through - ari will be null and we will continue...
1064             }
1065         } else {
1066             logger.error("Configuration for authRefs does not match schema structure: "
1067                     + arci.pathEls.toString());
1068         }
1069
1070         if (ari != null) {
1071             foundProps.add(ari); //FIXME: REM - This is dead code.  'ari' is never touched after being initalized to null.  Why?
1072         }
1073
1074         return foundProps;
1075     }
1076
1077     private static void addARIifMatches(
1078             String refNameToMatch,
1079             boolean matchBaseOnly,
1080             AuthRefConfigInfo arci,
1081             Property prop,
1082             List<AuthRefInfo> foundProps) {
1083         // Need to either match a passed refName 
1084         // OR have no refName to match but be non-empty
1085         try {
1086             String value = (String) prop.getValue();
1087             if (((refNameToMatch != null) && 
1088                                 (matchBaseOnly?
1089                                         (value!=null && value.startsWith(refNameToMatch))
1090                                         :refNameToMatch.equals(value)))
1091                     || ((refNameToMatch == null) && Tools.notBlank(value))) {
1092                 // Found a match
1093                 logger.debug("Found a match on property: " + prop.getPath() + " with value: [" + value + "]");
1094                 AuthRefInfo ari = new AuthRefInfo(arci, prop);
1095                 foundProps.add(ari);
1096             }
1097         } catch (PropertyException pe) {
1098             logger.debug("PropertyException on: " + prop.getPath() + pe.getLocalizedMessage());
1099         }
1100     }
1101     
1102     public static String buildWhereForAuthByName(String authorityCommonSchemaName, String name) {
1103         return authorityCommonSchemaName
1104                 + ":" + AuthorityJAXBSchema.SHORT_IDENTIFIER
1105                 + "='" + name + "'";
1106     }
1107
1108     /**
1109      * Build an NXQL query for finding an item by its short ID
1110      * 
1111      * @param authorityItemCommonSchemaName
1112      * @param shortId
1113      * @param parentcsid
1114      * @return
1115      */
1116     public static String buildWhereForAuthItemByName(String authorityItemCommonSchemaName, String shortId, String parentcsid) {
1117         String result = null;
1118         
1119         result = String.format("%s:%s='%s'", authorityItemCommonSchemaName, AuthorityItemJAXBSchema.SHORT_IDENTIFIER, shortId);
1120         //
1121         // Technically, we don't need the parent CSID since the short ID is unique so it can be null
1122         //
1123         if (parentcsid != null) {
1124                 result = String.format("%s AND %s:%s='%s'",
1125                                 result, authorityItemCommonSchemaName, AuthorityItemJAXBSchema.IN_AUTHORITY, parentcsid);
1126         }
1127         
1128         return result;
1129     }    
1130
1131     /*
1132      * Identifies whether the refName was found in the supplied field. If passed
1133      * a new RefName, will set that into fields in which the old one was found.
1134      *
1135      * Only works for: * Scalar fields * Repeatable scalar fields (aka
1136      * multi-valued fields)
1137      *
1138      * Does not work for: * Structured fields (complexTypes) * Repeatable
1139      * structured fields (repeatable complexTypes) private static int
1140      * refNameFoundInField(String oldRefName, Property fieldValue, String
1141      * newRefName) { int nFound = 0; if (fieldValue instanceof List) {
1142      * List<Property> fieldValueList = (List) fieldValue; for (Property
1143      * listItemValue : fieldValueList) { try { if ((listItemValue instanceof
1144      * StringProperty) &&
1145      * oldRefName.equalsIgnoreCase((String)listItemValue.getValue())) {
1146      * nFound++; if(newRefName!=null) { fieldValue.setValue(newRefName); } else
1147      * { // We cannot quit after the first, if we are replacing values. // If we
1148      * are just looking (not replacing), finding one is enough. break; } } }
1149      * catch( PropertyException pe ) {} } } else { try { if ((fieldValue
1150      * instanceof StringProperty) &&
1151      * oldRefName.equalsIgnoreCase((String)fieldValue.getValue())) { nFound++;
1152      * if(newRefName!=null) { fieldValue.setValue(newRefName); } } } catch(
1153      * PropertyException pe ) {} } return nFound; }
1154      */
1155 }