]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
967661d9a9f0d3b052bbf6b4f6e9b0839685b20e
[tmp/jakarta-migration.git] /
1 /**
2  *  This document is a part of the source code and related artifacts
3  *  for CollectionSpace, an open source collections management system
4  *  for museums and related institutions:
5
6  *  http://www.collectionspace.org
7  *  http://wiki.collectionspace.org
8
9  *  Copyright 2009 University of California at Berkeley
10
11  *  Licensed under the Educational Community License (ECL), Version 2.0.
12  *  You may not use this file except in compliance with this License.
13
14  *  You may obtain a copy of the ECL 2.0 License at
15
16  *  https://source.collectionspace.org/collection-space/LICENSE.txt
17
18  *  Unless required by applicable law or agreed to in writing, software
19  *  distributed under the License is distributed on an "AS IS" BASIS,
20  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21  *  See the License for the specific language governing permissions and
22  *  limitations under the License.
23  */
24 package org.collectionspace.services.common.vocabulary.nuxeo;
25
26 import org.collectionspace.services.client.AuthorityClient;
27 import org.collectionspace.services.client.IQueryManager;
28 import org.collectionspace.services.client.PoxPayloadIn;
29 import org.collectionspace.services.client.PoxPayloadOut;
30
31 import org.collectionspace.services.common.UriTemplateRegistry;
32 import org.collectionspace.services.common.api.RefName;
33 import org.collectionspace.services.common.api.Tools;
34 import org.collectionspace.services.common.authorityref.AuthorityRefDocList;
35 import org.collectionspace.services.common.context.MultipartServiceContext;
36 import org.collectionspace.services.common.context.ServiceContext;
37 import org.collectionspace.services.common.document.DocumentException;
38 import org.collectionspace.services.common.document.DocumentFilter;
39 import org.collectionspace.services.common.document.DocumentWrapper;
40 import org.collectionspace.services.common.repository.RepositoryClient;
41 import org.collectionspace.services.common.vocabulary.AuthorityJAXBSchema;
42 import org.collectionspace.services.common.vocabulary.AuthorityItemJAXBSchema;
43 import org.collectionspace.services.common.vocabulary.RefNameServiceUtils;
44
45 import org.collectionspace.services.config.service.ListResultField;
46 import org.collectionspace.services.config.service.ObjectPartType;
47
48 import org.collectionspace.services.nuxeo.client.java.DocHandlerBase;
49 import org.collectionspace.services.nuxeo.client.java.RepositoryJavaClientImpl;
50 import org.collectionspace.services.nuxeo.util.NuxeoUtils;
51
52 import org.collectionspace.services.relation.RelationsCommonList;
53 import org.collectionspace.services.relation.RelationsDocListItem;
54
55 import org.collectionspace.services.vocabulary.VocabularyItemJAXBSchema;
56
57 import org.nuxeo.ecm.core.api.ClientException;
58 import org.nuxeo.ecm.core.api.DocumentModel;
59 import org.nuxeo.ecm.core.api.model.PropertyException;
60 import org.nuxeo.ecm.core.api.repository.RepositoryInstance;
61
62 import org.slf4j.Logger;
63 import org.slf4j.LoggerFactory;
64
65 import javax.ws.rs.core.MultivaluedMap;
66
67 import java.util.ArrayList;
68 import java.util.HashMap;
69 import java.util.List;
70 import java.util.Map;
71 import java.util.regex.Matcher;
72 import java.util.regex.Pattern;
73
74 //import org.collectionspace.services.common.authority.AuthorityItemRelations;
75 /**
76  * AuthorityItemDocumentModelHandler
77  *
78  * $LastChangedRevision: $
79  * $LastChangedDate: $
80  */
81 public abstract class AuthorityItemDocumentModelHandler<AICommon>
82         extends DocHandlerBase<AICommon> {
83
84     private final Logger logger = LoggerFactory.getLogger(AuthorityItemDocumentModelHandler.class);
85     private String authorityItemCommonSchemaName;
86     private String authorityItemTermGroupXPathBase;
87     /**
88      * inVocabulary is the parent Authority for this context
89      */
90     protected String inAuthority = null;
91     protected String authorityRefNameBase = null;
92     // Used to determine when the displayName changes as part of the update.
93     protected String oldDisplayNameOnUpdate = null;
94     private final static String LIST_SUFFIX = "List";
95
96     public AuthorityItemDocumentModelHandler(String authorityItemCommonSchemaName) {
97         this.authorityItemCommonSchemaName = authorityItemCommonSchemaName;
98     }
99
100     @Override
101     protected String getRefnameDisplayName(DocumentWrapper<DocumentModel> docWrapper) {
102         String result = null;
103         
104         DocumentModel docModel = docWrapper.getWrappedObject();
105         ServiceContext ctx = this.getServiceContext();
106         RefName.AuthorityItem refname = (RefName.AuthorityItem)getRefName(ctx, docModel);
107         result = refname.getDisplayName();
108         
109         return result;
110     }
111     
112     /*
113      * After calling this method successfully, the document model will contain an updated refname and short ID
114      * (non-Javadoc)
115      * @see org.collectionspace.services.nuxeo.client.java.DocumentModelHandler#getRefName(org.collectionspace.services.common.context.ServiceContext, org.nuxeo.ecm.core.api.DocumentModel)
116      */
117     @Override
118     public RefName.RefNameInterface getRefName(ServiceContext ctx,
119                 DocumentModel docModel) {
120         RefName.RefNameInterface refname = null;
121         
122         try {
123                 String displayName = getPrimaryDisplayName(docModel, authorityItemCommonSchemaName,
124                     getItemTermInfoGroupXPathBase(), AuthorityItemJAXBSchema.TERM_DISPLAY_NAME);
125                 if (Tools.isEmpty(displayName)) {
126                     throw new Exception("The displayName for this authority term was empty or not set.");
127                 }
128         
129                 String shortIdentifier = (String) docModel.getProperty(authorityItemCommonSchemaName, AuthorityItemJAXBSchema.SHORT_IDENTIFIER);
130                 if (Tools.isEmpty(shortIdentifier)) {
131                         // We didn't find a short ID in the payload request, so we need to synthesize one.
132                         shortIdentifier = handleDisplayNameAsShortIdentifier(docModel); // updates the document model with the new short ID as a side-effect
133                 }
134                 
135                 String authorityRefBaseName = getAuthorityRefNameBase();
136                 if (Tools.isEmpty(authorityRefBaseName)) {
137                     throw new Exception("Could not create the refName for this authority term, because the refName for its authority parent was empty.");
138                 }
139                 
140                 // Create the items refname using the parent's as a base
141                 RefName.Authority parentsRefName = RefName.Authority.parse(authorityRefBaseName);
142                 refname = RefName.buildAuthorityItem(parentsRefName, shortIdentifier, displayName);
143                 // Now update the document model with the refname value
144                 String refNameStr = refname.toString();
145                 docModel.setProperty(authorityItemCommonSchemaName, AuthorityItemJAXBSchema.REF_NAME, refNameStr); // REM - This field is deprecated now that the refName is part of the collection_space core schema
146
147         } catch (Exception e) {
148                 logger.error(e.getMessage(), e);
149         }
150
151         return refname;
152     }
153     
154     public void setInAuthority(String inAuthority) {
155         this.inAuthority = inAuthority;
156     }
157     
158    public String getInAuthority() {
159         return this.inAuthority;
160     }
161
162     /** Subclasses may override this to customize the URI segment. */
163     public String getAuthorityServicePath() {
164         return getServiceContext().getServiceName().toLowerCase();    // Laramie20110510 CSPACE-3932
165     }
166
167     @Override
168     public String getUri(DocumentModel docModel) {
169         // Laramie20110510 CSPACE-3932
170         String authorityServicePath = getAuthorityServicePath();
171         if(inAuthority==null) { // Only happens on queries to wildcarded authorities
172                 try {
173                         inAuthority = (String) docModel.getProperty(authorityItemCommonSchemaName,
174                         AuthorityItemJAXBSchema.IN_AUTHORITY);
175                 } catch (ClientException pe) {
176                         throw new RuntimeException("Could not get parent specifier for item!");
177                 }
178         }
179         return "/" + authorityServicePath + '/' + inAuthority + '/' + AuthorityClient.ITEMS + '/' + getCsid(docModel);
180     }
181
182     protected String getAuthorityRefNameBase() {
183         return this.authorityRefNameBase;
184     }
185
186     public void setAuthorityRefNameBase(String value) {
187         this.authorityRefNameBase = value;
188     }
189
190     /*
191      * Note: the Vocabulary service's VocabularyItemDocumentModelHandler class overrides this method.
192      */
193     protected ListResultField getListResultsDisplayNameField() {
194         ListResultField result = new ListResultField();
195         // Per CSPACE-5132, the name of this element remains 'displayName'
196         // for backwards compatibility, although its value is obtained
197         // from the termDisplayName field.
198         //
199         // Update: this name is now being changed to 'termDisplayName', both
200         // because this is the actual field name and because the app layer
201         // work to convert over to this field is underway. Per Patrick, the
202         // app layer treats lists, in at least some context(s), as sparse record
203         // payloads, and thus fields in list results must all be present in
204         // (i.e. represent a strict subset of the fields in) record schemas.
205         // - ADR 2012-05-11
206         // 
207         //
208         // In CSPACE-5134, these list results will change substantially
209         // to return display names for both the preferred term and for
210         // each non-preferred term (if any).
211         result.setElement(AuthorityItemJAXBSchema.TERM_DISPLAY_NAME);
212         result.setXpath(NuxeoUtils.getPrimaryXPathPropertyName(
213                 authorityItemCommonSchemaName, getItemTermInfoGroupXPathBase(), AuthorityItemJAXBSchema.TERM_DISPLAY_NAME));
214         
215         return result;
216     }
217     
218     /*
219      * Note: the Vocabulary service's VocabularyItemDocumentModelHandler class overrides this method.
220      */    
221     protected ListResultField getListResultsTermStatusField() {
222         ListResultField result = new ListResultField();
223         
224         result.setElement(AuthorityItemJAXBSchema.TERM_STATUS);
225         result.setXpath(NuxeoUtils.getPrimaryXPathPropertyName(
226                 authorityItemCommonSchemaName, getItemTermInfoGroupXPathBase(), AuthorityItemJAXBSchema.TERM_STATUS));
227
228         return result;
229     }    
230     
231     private boolean isTermDisplayName(String elName) {
232         return AuthorityItemJAXBSchema.TERM_DISPLAY_NAME.equals(elName) || VocabularyItemJAXBSchema.DISPLAY_NAME.equals(elName);
233     }
234     
235     /*
236      * (non-Javadoc)
237      * @see org.collectionspace.services.nuxeo.client.java.DocHandlerBase#getListItemsArray()
238      * 
239      * Note: We're updating the "global" service and tenant bindings instance here -the list instance here is
240      * a reference to the tenant bindings instance in the singleton ServiceMain.
241      */
242     @Override
243     public List<ListResultField> getListItemsArray() throws DocumentException {
244         List<ListResultField> list = super.getListItemsArray();
245         
246         // One-time initialization for each authority item service.
247         if (isListItemArrayExtended() == false) {
248                 synchronized(AuthorityItemDocumentModelHandler.class) {
249                         if (isListItemArrayExtended() == false) {                               
250                         int nFields = list.size();
251                         // Ensure that each item in a list of Authority items includes
252                         // a set of common fields, so we do not depend upon configuration
253                         // for general logic.
254                         boolean hasDisplayName = false;
255                         boolean hasShortId = false;
256                         boolean hasTermStatus = false;
257                         for (int i = 0; i < nFields; i++) {
258                             ListResultField field = list.get(i);
259                             String elName = field.getElement();
260                             if (isTermDisplayName(elName) == true) {
261                                 hasDisplayName = true;
262                             } else if (AuthorityItemJAXBSchema.SHORT_IDENTIFIER.equals(elName)) {
263                                 hasShortId = true;
264                             } else if (AuthorityItemJAXBSchema.TERM_STATUS.equals(elName)) {
265                                 hasTermStatus = true;
266                             }
267                         }
268                                 
269                         ListResultField field;
270                         if (!hasDisplayName) {
271                                 field = getListResultsDisplayNameField();
272                             list.add(field);
273                         }
274                         if (!hasShortId) {
275                             field = new ListResultField();
276                             field.setElement(AuthorityItemJAXBSchema.SHORT_IDENTIFIER);
277                             field.setXpath(AuthorityItemJAXBSchema.SHORT_IDENTIFIER);
278                             list.add(field);
279                         }
280                         if (!hasTermStatus) {
281                             field = getListResultsTermStatusField();
282                             list.add(field);
283                         }
284                         }
285                         
286                         setListItemArrayExtended(true);
287                 } // end of synchronized block
288         }
289
290         return list;
291     }
292     
293     /* (non-Javadoc)
294      * @see org.collectionspace.services.nuxeo.client.java.DocumentModelHandler#handleCreate(org.collectionspace.services.common.document.DocumentWrapper)
295      */
296     @Override
297     public void handleCreate(DocumentWrapper<DocumentModel> wrapDoc) throws Exception {
298         // first fill all the parts of the document, refname and short ID get set as well
299         super.handleCreate(wrapDoc);
300         // Ensure we have required fields set properly
301         handleInAuthority(wrapDoc.getWrappedObject());        
302     }
303
304     /*
305      * This method gets called after the primary update to an authority item has happened.  If the authority item's refName
306      * has changed, then we need to updated all the records that use that refname with the new/updated version
307      * 
308      * (non-Javadoc)
309      * @see org.collectionspace.services.nuxeo.client.java.RemoteDocumentModelHandlerImpl#completeUpdate(org.collectionspace.services.common.document.DocumentWrapper)
310      */
311     public void completeUpdate(DocumentWrapper<DocumentModel> wrapDoc) throws Exception {
312         // Must call our super class' version first
313         super.completeUpdate(wrapDoc);
314         
315         //
316         // Look for and update authority references with the updated refName
317         //
318         if (hasRefNameUpdate() == true) {
319             // We have work to do.
320             if (logger.isDebugEnabled()) {
321                 final String EOL = System.getProperty("line.separator");
322                 logger.debug("Need to find and update references to authority item." + EOL
323                         + "   Old refName" + oldRefNameOnUpdate + EOL
324                         + "   New refName" + newRefNameOnUpdate);
325             }
326             ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = getServiceContext();
327             RepositoryClient<PoxPayloadIn, PoxPayloadOut> repoClient = getRepositoryClient(ctx);
328             RepositoryInstance repoSession = this.getRepositorySession();
329             
330             // Update all the existing records that have a field with the old refName in it
331             int nUpdated = RefNameServiceUtils.updateAuthorityRefDocs(ctx, repoClient, repoSession,
332                     oldRefNameOnUpdate, newRefNameOnUpdate, getRefPropName());
333             
334             // Finished so log a message.
335             if (logger.isDebugEnabled()) {
336                 logger.debug("Updated " + nUpdated + " instances of oldRefName to newRefName");
337             }
338         }
339     }
340     
341     /*
342      * Note that the Vocabulary service's document-model for items overrides this method.
343      */
344         protected String getPrimaryDisplayName(DocumentModel docModel, String schema,
345                         String complexPropertyName, String fieldName) {
346                 String result = null;
347
348                 result = getStringValueInPrimaryRepeatingComplexProperty(docModel, schema, complexPropertyName, fieldName);
349                 
350                 return result;
351         }
352     
353     /* (non-Javadoc)
354      * @see org.collectionspace.services.nuxeo.client.java.DocumentModelHandler#handleUpdate(org.collectionspace.services.common.document.DocumentWrapper)
355      */
356     @Override
357     // FIXME: Once we remove the refName field from the authority item schemas, we can remove this override method since our super does everthing for us now.
358     @Deprecated
359     public void handleUpdate(DocumentWrapper<DocumentModel> wrapDoc) throws Exception {
360         // Must call our super's version first, this updates the core schema and the relationship records to deal with possible refName changes/update
361         super.handleUpdate(wrapDoc);
362         if (this.hasRefNameUpdate() == true) {
363                 DocumentModel docModel = wrapDoc.getWrappedObject();
364             docModel.setProperty(authorityItemCommonSchemaName, AuthorityItemJAXBSchema.REF_NAME, this.newRefNameOnUpdate); // This field is deprecated since it is now a duplicate of what is in the collectionspace_core:refName field                
365         }
366     }
367     
368     /**
369      * If no short identifier was provided in the input payload, generate a
370      * short identifier from the preferred term display name or term name.
371      */
372         private String handleDisplayNameAsShortIdentifier(DocumentModel docModel) throws Exception {
373                 String result = (String) docModel.getProperty(authorityItemCommonSchemaName,
374                                 AuthorityItemJAXBSchema.SHORT_IDENTIFIER);
375
376                 if (Tools.isEmpty(result)) {
377                         String termDisplayName = getPrimaryDisplayName(
378                                         docModel, authorityItemCommonSchemaName,
379                                         getItemTermInfoGroupXPathBase(),
380                                         AuthorityItemJAXBSchema.TERM_DISPLAY_NAME);
381
382                         String termName = getPrimaryDisplayName(
383                                         docModel, authorityItemCommonSchemaName,
384                                         getItemTermInfoGroupXPathBase(),
385                                         AuthorityItemJAXBSchema.TERM_NAME);
386
387                         String generatedShortIdentifier = AuthorityIdentifierUtils.generateShortIdentifierFromDisplayName(termDisplayName,
388                                                         termName);
389                         docModel.setProperty(authorityItemCommonSchemaName, AuthorityItemJAXBSchema.SHORT_IDENTIFIER,
390                                         generatedShortIdentifier);
391                         result = generatedShortIdentifier;
392                 }
393                 
394                 return result;
395         }
396
397     /**
398      * Generate a refName for the authority item from the short identifier
399      * and display name.
400      * 
401      * All refNames for authority items are generated.  If a client supplies
402      * a refName, it will be overwritten during create (per this method) 
403      * or discarded during update (per filterReadOnlyPropertiesForPart).
404      * 
405      * @see #filterReadOnlyPropertiesForPart(Map<String, Object>, org.collectionspace.services.common.service.ObjectPartType)
406      * 
407      */
408     protected String updateRefnameForAuthorityItem(DocumentModel docModel,
409             String schemaName) throws Exception {
410         String result = null;
411         
412         RefName.RefNameInterface refname = getRefName(getServiceContext(), docModel);
413         String refNameStr = refname.toString();
414         docModel.setProperty(schemaName, AuthorityItemJAXBSchema.REF_NAME, refNameStr);
415         result = refNameStr;
416         
417         return result;
418     }
419
420     /**
421      * Check the logic around the parent pointer. Note that we only need do this on
422      * create, since we have logic to make this read-only on update. 
423      * 
424      * @param docModel
425      * 
426      * @throws Exception the exception
427      */
428     private void handleInAuthority(DocumentModel docModel) throws Exception {
429         if(inAuthority==null) { // Only happens on queries to wildcarded authorities
430                 throw new IllegalStateException("Trying to Create an object with no inAuthority value!");
431         }
432         docModel.setProperty(authorityItemCommonSchemaName,
433                 AuthorityItemJAXBSchema.IN_AUTHORITY, inAuthority);
434     }
435     
436     public AuthorityRefDocList getReferencingObjects(
437                 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx,
438                 UriTemplateRegistry uriTemplateRegistry, 
439                 List<String> serviceTypes,
440                 String propertyName,
441             String itemcsid) throws Exception {
442         AuthorityRefDocList authRefDocList = null;
443         RepositoryInstance repoSession = null;
444         boolean releaseRepoSession = false;
445         
446         try {
447                 RepositoryJavaClientImpl repoClient = (RepositoryJavaClientImpl)this.getRepositoryClient(ctx);
448                 repoSession = this.getRepositorySession();
449                 if (repoSession == null) {
450                         repoSession = repoClient.getRepositorySession(ctx);
451                         releaseRepoSession = true;
452                 }
453             DocumentFilter myFilter = getDocumentFilter();
454
455                 try {
456                         DocumentWrapper<DocumentModel> wrapper = repoClient.getDoc(repoSession, ctx, itemcsid);
457                         DocumentModel docModel = wrapper.getWrappedObject();
458                         String refName = (String) docModel.getPropertyValue(AuthorityItemJAXBSchema.REF_NAME);
459                 authRefDocList = RefNameServiceUtils.getAuthorityRefDocs(
460                                 repoSession, ctx, uriTemplateRegistry, repoClient,
461                         serviceTypes,
462                         refName,
463                         propertyName,
464                         myFilter, true /*computeTotal*/);
465                 } catch (PropertyException pe) {
466                         throw pe;
467                 } catch (DocumentException de) {
468                         throw de;
469                 } catch (Exception e) {
470                         if (logger.isDebugEnabled()) {
471                                 logger.debug("Caught exception ", e);
472                         }
473                         throw new DocumentException(e);
474                 } finally {
475                         // If we got/aquired a new seesion then we're responsible for releasing it.
476                         if (releaseRepoSession && repoSession != null) {
477                                 repoClient.releaseRepositorySession(ctx, repoSession);
478                         }
479                 }
480         } catch (Exception e) {
481                 if (logger.isDebugEnabled()) {
482                         logger.debug("Caught exception ", e);
483                 }
484                 throw new DocumentException(e);
485         }
486         
487         return authRefDocList;
488     }
489
490     /* (non-Javadoc)
491      * @see org.collectionspace.services.nuxeo.client.java.RemoteDocumentModelHandlerImpl#extractPart(org.nuxeo.ecm.core.api.DocumentModel, java.lang.String, org.collectionspace.services.common.service.ObjectPartType)
492      */
493     @Override
494     protected Map<String, Object> extractPart(DocumentModel docModel, String schema, ObjectPartType partMeta)
495             throws Exception {
496         Map<String, Object> unQObjectProperties = super.extractPart(docModel, schema, partMeta);
497
498         // Add the CSID to the common part, since they may have fetched via the shortId.
499         if (partMeta.getLabel().equalsIgnoreCase(authorityItemCommonSchemaName)) {
500             String csid = getCsid(docModel);//NuxeoUtils.extractId(docModel.getPathAsString());
501             unQObjectProperties.put("csid", csid);
502         }
503
504         return unQObjectProperties;
505     }
506
507     /**
508      * Filters out selected values supplied in an update request.
509      * 
510      * For example, filters out AuthorityItemJAXBSchema.IN_AUTHORITY, to ensure
511      * that the link to the item's parent remains untouched.
512      * 
513      * @param objectProps the properties filtered out from the update payload
514      * @param partMeta metadata for the object to fill
515      */
516     @Override
517     public void filterReadOnlyPropertiesForPart(
518             Map<String, Object> objectProps, ObjectPartType partMeta) {
519         super.filterReadOnlyPropertiesForPart(objectProps, partMeta);
520         String commonPartLabel = getServiceContext().getCommonPartLabel();
521         if (partMeta.getLabel().equalsIgnoreCase(commonPartLabel)) {
522             objectProps.remove(AuthorityItemJAXBSchema.IN_AUTHORITY);
523             objectProps.remove(AuthorityItemJAXBSchema.CSID);
524             objectProps.remove(AuthorityJAXBSchema.SHORT_IDENTIFIER);
525             objectProps.remove(AuthorityItemJAXBSchema.REF_NAME);
526         }
527     }
528     
529     protected List<String> getPartialTermDisplayNameMatches(List<String> termDisplayNameList, String partialTerm) {
530         List<String> result = new ArrayList<String>();
531         
532         for (String termDisplayName : termDisplayNameList) {
533                 if (termDisplayName.toLowerCase().contains(partialTerm.toLowerCase()) == true) {
534                         result.add(termDisplayName);
535                 }
536         }
537         
538         return result;
539     }
540     
541     @SuppressWarnings("unchecked")
542         private List<String> getPartialTermDisplayNameMatches(DocumentModel docModel, // REM - CSPACE-5133
543                         String schema, ListResultField field, String partialTerm) {
544         List<String> result = null;
545           
546         String xpath = field.getXpath(); // results in something like "persons_common:personTermGroupList/[0]/termDisplayName"
547         int endOfTermGroup = xpath.lastIndexOf("/[0]/");
548         String propertyName = endOfTermGroup != -1 ? xpath.substring(0, endOfTermGroup) : xpath; // it may not be multivalued so the xpath passed in would be the property name
549         Object value = null;
550         
551                 try {
552                         value = docModel.getProperty(schema, propertyName);
553                 } catch (Exception e) {
554                         logger.error("Could not extract term display name with property = "
555                                         + propertyName, e);
556                 }
557                 
558                 if (value != null && value instanceof ArrayList) {
559                         ArrayList<HashMap<String, Object>> termGroupList = (ArrayList<HashMap<String, Object>>)value;
560                         int arrayListSize = termGroupList.size();
561                         if (arrayListSize > 1) { // if there's only 1 element in the list then we've already matched the primary term's display name
562                                 List<String> displayNameList = new ArrayList<String>();
563                                 for (int i = 1; i < arrayListSize; i++) { // start at 1, skip the primary term's displayName since we will always return it
564                                         HashMap<String, Object> map = (HashMap<String, Object>)termGroupList.get(i);
565                                         String termDisplayName = (String) map.get(AuthorityItemJAXBSchema.TERM_DISPLAY_NAME);
566                                         displayNameList.add(i - 1, termDisplayName);
567                                 }
568                                 
569                                 result = getPartialTermDisplayNameMatches(displayNameList, partialTerm);
570                         }
571                 }
572
573         return result;
574     }
575
576     @Override
577         protected Object getListResultValue(DocumentModel docModel, // REM - CSPACE-5133
578                         String schema, ListResultField field) {
579                 Object result = null;           
580
581                 result = NuxeoUtils.getXPathValue(docModel, schema, field.getXpath());
582                 String elName = field.getElement();
583                 //
584                 // If the list result value is the termDisplayName element, we need to check to see if a partial term query was made.
585                 //
586                 if (isTermDisplayName(elName) == true) {
587                         MultivaluedMap<String, String> queryParams = this.getServiceContext().getQueryParams();
588                 String partialTerm = queryParams != null ? queryParams.getFirst(IQueryManager.SEARCH_TYPE_PARTIALTERM) : null;
589                 if (partialTerm != null && partialTerm.trim().isEmpty() == false) {
590                                 String primaryTermDisplayName = (String)result;
591                         List<String> matches = getPartialTermDisplayNameMatches(docModel, schema, field, partialTerm);
592                         if (matches != null && matches.isEmpty() == false) {
593                                 matches.add(0, primaryTermDisplayName); // insert the primary term's display name at the beginning of the list
594                                 result = matches; // set the result to a list of matching term display names with the primary term's display name at the beginning
595                         }
596                 }
597                 }
598                 
599                 return result;
600         }
601     
602     @Override
603     public void extractAllParts(DocumentWrapper<DocumentModel> wrapDoc) throws Exception {
604         MultipartServiceContext ctx = (MultipartServiceContext) getServiceContext();
605         super.extractAllParts(wrapDoc);
606     }
607
608     @Override
609     public void fillAllParts(DocumentWrapper<DocumentModel> wrapDoc, Action action) throws Exception {
610         //
611         // We currently don't override this method with any AuthorityItemDocumentModelHandler specific functionality, so
612         // we could remove this method.
613         //
614         super.fillAllParts(wrapDoc, action);
615     }
616
617     protected List<RelationsCommonList.RelationListItem> cloneList(List<RelationsCommonList.RelationListItem> inboundList) {
618         List<RelationsCommonList.RelationListItem> result = newRelationsCommonList();
619         for (RelationsCommonList.RelationListItem item : inboundList) {
620             result.add(item);
621         }
622         return result;
623     }
624
625
626     /* don't even THINK of re-using this method.
627      * String example_uri = "/locationauthorities/7ec60f01-84ab-4908-9a6a/items/a5466530-713f-43b4-bc05";
628      */
629     private String extractInAuthorityCSID(String uri) {
630         String IN_AUTHORITY_REGEX = "/(.*?)/(.*?)/(.*)";
631         Pattern p = Pattern.compile(IN_AUTHORITY_REGEX);
632         Matcher m = p.matcher(uri);
633         if (m.find()) {
634             if (m.groupCount() < 3) {
635                 logger.warn("REGEX-WRONG-GROUPCOUNT looking in " + uri);
636                 return "";
637             } else {
638                 //String service = m.group(1);
639                 String inauth = m.group(2);
640                 //String theRest = m.group(3);
641                 return inauth;
642                 //print("service:"+service+", inauth:"+inauth+", rest:"+rest);
643             }
644         } else {
645             logger.warn("REGEX-NOT-MATCHED looking in " + uri);
646             return "";
647         }
648     }
649
650     //ensures CSPACE-4042
651     protected void uriPointsToSameAuthority(String thisURI, String inboundItemURI) throws Exception {
652         String authorityCSID = extractInAuthorityCSID(thisURI);
653         String authorityCSIDForInbound = extractInAuthorityCSID(inboundItemURI);
654         if (Tools.isBlank(authorityCSID)
655                 || Tools.isBlank(authorityCSIDForInbound)
656                 || (!authorityCSID.equalsIgnoreCase(authorityCSIDForInbound))) {
657             throw new Exception("Item URI " + thisURI + " must point to same authority as related item: " + inboundItemURI);
658         }
659     }
660
661     public String getItemTermInfoGroupXPathBase() {
662         return authorityItemTermGroupXPathBase;
663     }
664         
665     public void setItemTermInfoGroupXPathBase(String itemTermInfoGroupXPathBase) {
666         authorityItemTermGroupXPathBase = itemTermInfoGroupXPathBase;
667     }
668     
669     protected String getAuthorityItemCommonSchemaName() {
670         return authorityItemCommonSchemaName;
671     }
672     
673     // @Override
674     public boolean isJDBCQuery() {
675         boolean result = false;
676         
677         MultivaluedMap<String, String> queryParams = getServiceContext().getQueryParams();
678         //
679         // Look the query params to see if we need to make a SQL query.
680         //
681         String partialTerm = queryParams.getFirst(IQueryManager.SEARCH_TYPE_PARTIALTERM);
682         if (partialTerm != null && partialTerm.trim().isEmpty() == false) {
683                 result = true;
684         }
685         
686         return result;
687     }
688     
689     // By convention, the name of the database table that contains
690     // repeatable term information group records is derivable from
691     // an XPath base value by removing a suffix and converting to lowercase
692     protected String getTermGroupTableName() {
693         String termInfoGroupListName = getItemTermInfoGroupXPathBase();
694         return termInfoGroupListName.substring(0, termInfoGroupListName.lastIndexOf(LIST_SUFFIX)).toLowerCase();
695     }
696     
697     @Override
698     public Map<String,String> getJDBCQueryParams() {
699         Map<String,String> params = super.getJDBCQueryParams();
700         params.put(RepositoryJavaClientImpl.JDBC_TABLE_NAME_PARAM, getTermGroupTableName());
701         return params;
702     }
703     
704 }