From a8501d54f563869b5e37daca48e24e7c000a5eb1 Mon Sep 17 00:00:00 2001 From: Richard Millet Date: Wed, 16 May 2012 23:20:57 -0700 Subject: [PATCH] CSPACE-5133: Final changes for partial term queries that can return multiple display names in list results. --- .../vocabulary/res/GetVocabularyItems.res.xml | 23 +++---- .../AuthorityItemDocumentModelHandler.java | 67 ++++++++++++++++--- .../client/test/AbstractServiceTestImpl.java | 2 +- .../nuxeo/client/java/DocHandlerBase.java | 2 +- .../java/RemoteDocumentModelHandlerImpl.java | 2 +- .../test/PersonAuthorityServiceTest.java | 21 +++++- 6 files changed, 90 insertions(+), 27 deletions(-) diff --git a/services/IntegrationTests/src/test/resources/test-data/xmlreplay/vocabulary/res/GetVocabularyItems.res.xml b/services/IntegrationTests/src/test/resources/test-data/xmlreplay/vocabulary/res/GetVocabularyItems.res.xml index 0f62c72f1..64430e34f 100644 --- a/services/IntegrationTests/src/test/resources/test-data/xmlreplay/vocabulary/res/GetVocabularyItems.res.xml +++ b/services/IntegrationTests/src/test/resources/test-data/xmlreplay/vocabulary/res/GetVocabularyItems.res.xml @@ -8,12 +8,11 @@ 3 csid|uri|updatedAt|workflowState|order|displayName|shortIdentifier|refName|termStatus - ${Item3DupeOrder.displayName} - ${Item3DupeOrder.itemID} - ${Item3DupeOrder.order} - /vocabularies/${Vocabulary1.CSID}/items/${Item3DupeOrder.CSID} - ${Item3DupeOrder.CSID} - + ${Item1.displayName} + ${Item1.itemID} + ${Item1.order} + /vocabularies/${Vocabulary1.CSID}/items/${Item1.CSID} + ${Item1.CSID} ${Item2.displayName} @@ -21,14 +20,12 @@ ${Item2.order} /vocabularies/${Vocabulary1.CSID}/items/${Item2.CSID} ${Item2.CSID} - - ${Item1.displayName} - ${Item1.itemID} - ${Item1.order} - /vocabularies/${Vocabulary1.CSID}/items/${Item1.CSID} - ${Item1.CSID} - + ${Item3DupeOrder.displayName} + ${Item3DupeOrder.itemID} + ${Item3DupeOrder.order} + /vocabularies/${Vocabulary1.CSID}/items/${Item3DupeOrder.CSID} + ${Item3DupeOrder.CSID} diff --git a/services/authority/service/src/main/java/org/collectionspace/services/common/vocabulary/nuxeo/AuthorityItemDocumentModelHandler.java b/services/authority/service/src/main/java/org/collectionspace/services/common/vocabulary/nuxeo/AuthorityItemDocumentModelHandler.java index b2c7febe8..b9979ecb7 100644 --- a/services/authority/service/src/main/java/org/collectionspace/services/common/vocabulary/nuxeo/AuthorityItemDocumentModelHandler.java +++ b/services/authority/service/src/main/java/org/collectionspace/services/common/vocabulary/nuxeo/AuthorityItemDocumentModelHandler.java @@ -63,6 +63,7 @@ import org.collectionspace.services.vocabulary.VocabularyItemJAXBSchema; import org.nuxeo.ecm.core.api.ClientException; import org.nuxeo.ecm.core.api.DocumentModel; import org.nuxeo.ecm.core.api.DocumentModelList; +import org.nuxeo.ecm.core.api.model.Property; import org.nuxeo.ecm.core.api.model.PropertyException; import org.nuxeo.ecm.core.api.repository.RepositoryInstance; @@ -521,24 +522,74 @@ public abstract class AuthorityItemDocumentModelHandler objectProps.remove(AuthorityItemJAXBSchema.REF_NAME); } } + + protected List getPartialTermDisplayNameMatches(List termDisplayNameList, String partialTerm) { + List result = new ArrayList(); + + for (String termDisplayName : termDisplayNameList) { + if (termDisplayName.contains(partialTerm) == true) { + result.add(termDisplayName); + } + } + + return result; + } + + @SuppressWarnings("unchecked") + private List getPartialTermDisplayNameMatches(DocumentModel docModel, // REM - CSPACE-5133 + String schema, ListResultField field, String partialTerm) { + List result = null; + + String xpath = field.getXpath(); // results in something like "persons_common:personTermGroupList/[0]/termDisplayName" + int endOfTermGroup = xpath.lastIndexOf("/[0]/"); + String propertyName = endOfTermGroup != -1 ? xpath.substring(0, endOfTermGroup) : xpath; // it may not be multivalued so the xpath passed in would be the property name + Object value = null; + + try { + value = docModel.getProperty(schema, propertyName); + } catch (Exception e) { + logger.error("Could not extract term display name with property = " + + propertyName, e); + } + + if (value != null && value instanceof ArrayList) { + ArrayList> termGroupList = (ArrayList>)value; + int arrayListSize = termGroupList.size(); + if (arrayListSize > 1) { // if there's only 1 element in the list then we've already matched the primary term's display name + List displayNameList = new ArrayList(); + for (int i = 1; i < arrayListSize; i++) { // start at 1, skip the primary term's displayName since we will always return it + HashMap map = (HashMap)termGroupList.get(i); + String termDisplayName = (String) map.get(AuthorityItemJAXBSchema.TERM_DISPLAY_NAME); + displayNameList.add(i - 1, termDisplayName); + } + + result = getPartialTermDisplayNameMatches(displayNameList, partialTerm); + } + } + + return result; + } @Override - protected Object getXPathValue(DocumentModel docModel, // REM - CSPACE-5133 + protected Object getListResultValue(DocumentModel docModel, // REM - CSPACE-5133 String schema, ListResultField field) { - Object result = null; + Object result = null; result = NuxeoUtils.getXPathValue(docModel, schema, field.getXpath()); String elName = field.getElement(); + // + // If the list result value is the termDisplayName element, we need to check to see if a partial term query was made. + // if (isTermDisplayName(elName) == true) { MultivaluedMap queryParams = this.getServiceContext().getQueryParams(); String partialTerm = queryParams != null ? queryParams.getFirst(IQueryManager.SEARCH_TYPE_PARTIALTERM) : null; if (partialTerm != null && partialTerm.trim().isEmpty() == false) { - List strList = new ArrayList(); - strList.add((String)result); - strList.add("Tiny"); - strList.add("Tucker"); - strList.add("Tim"); - result = strList; + String primaryTermDisplayName = (String)result; + List matches = getPartialTermDisplayNameMatches(docModel, schema, field, partialTerm); + if (matches != null && matches.isEmpty() == false) { + matches.add(0, primaryTermDisplayName); // insert the primary term's display name at the beginning of the list + result = matches; // set the result to a list of matching term display names with the primary term's display name at the beginning + } } } diff --git a/services/client/src/main/java/org/collectionspace/services/client/test/AbstractServiceTestImpl.java b/services/client/src/main/java/org/collectionspace/services/client/test/AbstractServiceTestImpl.java index c3d5b510d..d7c7b723d 100644 --- a/services/client/src/main/java/org/collectionspace/services/client/test/AbstractServiceTestImpl.java +++ b/services/client/src/main/java/org/collectionspace/services/client/test/AbstractServiceTestImpl.java @@ -1024,7 +1024,7 @@ public abstract class AbstractServiceTestImpl extends RemoteDocumentModelHandlerImpl * @param xpath The XPath expression (without schema prefix) * @return value the indicated property value as a String */ - protected Object getXPathValue(DocumentModel docModel, // REM - CSPACE-5133 + protected Object getListResultValue(DocumentModel docModel, // REM - CSPACE-5133 String schema, ListResultField field) { Object result = null; diff --git a/services/person/client/src/test/java/org/collectionspace/services/client/test/PersonAuthorityServiceTest.java b/services/person/client/src/test/java/org/collectionspace/services/client/test/PersonAuthorityServiceTest.java index 07a79270b..e86d8265f 100644 --- a/services/person/client/src/test/java/org/collectionspace/services/client/test/PersonAuthorityServiceTest.java +++ b/services/person/client/src/test/java/org/collectionspace/services/client/test/PersonAuthorityServiceTest.java @@ -186,15 +186,30 @@ public class PersonAuthorityServiceTest extends AbstractAuthorityServiceTest personInfo = new HashMap(); - String shortId = "johnWayneTempActor"; + String shortId = "MarkTwainAuthor"; personInfo.put(PersonJAXBSchema.SHORT_IDENTIFIER, shortId); List terms = new ArrayList(); PersonTermGroup term = new PersonTermGroup(); - term.setTermDisplayName("John Wayne Temp"); - term.setTermName("JohnWayneTemp"); + term.setTermDisplayName("Mark Twain Primary"); + term.setTermName("MarkTwainPrimary"); terms.add(term); + term = new PersonTermGroup(); + term.setTermDisplayName("Samuel Langhorne Clemens"); + term.setTermName("SamuelLanghorneClemens"); + terms.add(term); + + term = new PersonTermGroup(); + term.setTermDisplayName("Sam Clemens"); + term.setTermName("SamClemens"); + terms.add(term); + + term = new PersonTermGroup(); + term.setTermDisplayName("Huck Fin"); + term.setTermName("Huck Fin"); + terms.add(term); + return PersonAuthorityClientUtils.createPersonInstance(parentCsid, identifier, personInfo, terms, headerLabel); } -- 2.47.3