]> git.aero2k.de Git - tmp/jakarta-migration.git/commitdiff
DRYD-202: Misc bug fixes to term-list payloads in PUT requests to vocab resources.
authorremillet <remillet@yahoo.com>
Fri, 26 Jan 2018 06:07:31 +0000 (22:07 -0800)
committerremillet <remillet@yahoo.com>
Fri, 26 Jan 2018 06:07:31 +0000 (22:07 -0800)
services/IntegrationTests/src/test/resources/test-data/xmlreplay/vocabulary/PostWithItems/postWithItems-vocab.xml
services/IntegrationTests/src/test/resources/test-data/xmlreplay/vocabulary/PostWithItems/vocab-Item-no-shortid.xml [new file with mode: 0644]
services/IntegrationTests/src/test/resources/test-data/xmlreplay/vocabulary/UpdateAddDeleteVocabItems/responses/updateAddDeleteVocabItems.res.xml
services/IntegrationTests/src/test/resources/test-data/xmlreplay/vocabulary/UpdateAddDeleteVocabItems/updateAddDeleteWithItems-vocab.xml
services/IntegrationTests/src/test/resources/test-data/xmlreplay/vocabulary/vocabulary.xml
services/authority/service/src/main/java/org/collectionspace/services/common/vocabulary/nuxeo/AuthorityItemDocumentModelHandler.java
services/vocabulary/service/src/main/java/org/collectionspace/services/vocabulary/VocabularyResource.java

index 4bb5382f2ad0dfac68b5ecfd7d3b58112c277cc0..8b4e4b7b8632399d2ac5df8ba740f19ec4879b1b 100644 (file)
@@ -20,7 +20,7 @@
         <list-item>
             <order>3</order>
             <displayName>PostWithItems item PostWithItems3</displayName>
-            <shortIdentifier>PostWithItems3</shortIdentifier>
+            <!-- Intentionally providing no short identifier.  Backend should generate one -->
         </list-item>
     </ns2:abstract-common-list>
 </document>
diff --git a/services/IntegrationTests/src/test/resources/test-data/xmlreplay/vocabulary/PostWithItems/vocab-Item-no-shortid.xml b/services/IntegrationTests/src/test/resources/test-data/xmlreplay/vocabulary/PostWithItems/vocab-Item-no-shortid.xml
new file mode 100644 (file)
index 0000000..76fdc78
--- /dev/null
@@ -0,0 +1,10 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<document name="vocabularyitems">
+    <ns3:vocabularyitems_common xmlns:ns3="http://collectionspace.org/services/vocabulary" xmlns:ns2="http://collectionspace.org/services/jaxb">
+        <displayName>${displayName}</displayName>
+        <order>${order}</order>
+        <description>This is a test vocabulary item for test ${Test.ID}</description>
+        <source>Some mythical book for test ${Test.ID}</source>
+        <sourcePage>The page should match the order:${order}</sourcePage>
+    </ns3:vocabularyitems_common>
+</document>
index fba9c64d9146198f628d563f194434d46cb2253a..eae9013393ab830b669036278d3e4aecdb4dbb9a 100644 (file)
             <shortIdentifier>addeditemb</shortIdentifier>
         </list-item>
         <list-item>
-            <refName>
-                urn:cspace:testsci.collectionspace.org:vocabularies:name(createUpdateAddDeleteVocabItems):item:name(addeditemc)'Added item C'
-            </refName>
             <workflowState>project</workflowState>
             <rev>0</rev>
             <sas>false</sas>
             <proposed>true</proposed>
             <order>4</order>
             <displayName>Added item C</displayName>
-            <shortIdentifier>addeditemc</shortIdentifier>
         </list-item>
         <list-item>
             <refName>
index 6128aceacecfbaade7292a5a678745967778288c..4290080255e2d6784d59aa4b32641284b0823245 100644 (file)
@@ -8,9 +8,11 @@
     </ns3:vocabularies_common>
     <ns2:abstract-common-list xmlns:ns2="http://collectionspace.org/services/jaxb">
         <list-item>
+            <csid>${item301csid}</csid>
             <order>1</order>
             <displayName>Updated createItem301</displayName>
             <shortIdentifier>createItem301</shortIdentifier>
+            <termStatus>inactive</termStatus>
         </list-item>
         <list-item>
             <order>2</order>
@@ -25,7 +27,7 @@
         <list-item>
             <order>4</order>
             <displayName>Added item C</displayName>
-            <shortIdentifier>addeditemc</shortIdentifier>
+            <!-- Test providing no short ID; should get generated. -->
         </list-item>
     </ns2:abstract-common-list>
 </document>
index b02a86342d4265f7f9ca8c7c4664cd358fd1d3ec..740711e3572b63870a96756bae29ca5df1a699e8 100644 (file)
             <method>PUT</method>
             <uri>/cspace-services/vocabularies/${createUpdateAddDeleteVocabItems.CSID}?omittedItemAction=delete</uri>
             <filename>vocabulary/UpdateAddDeleteVocabItems/updateAddDeleteWithItems-vocab.xml</filename>
+            <vars>
+                <var ID="item301csid">${createItem301.CSID}</var>
+            </vars>
         </test>
         <test ID="getUpdateAddDeleteVocabItems">
             <method>GET</method>
             <uri>/cspace-services/vocabularies/</uri>
             <filename>vocabulary/PostWithItems/postWithItems-vocab.xml</filename>            
         </test>
+        <test ID="Item4">
+            <method>POST</method>
+            <uri>/cspace-services/vocabularies/${createPostWithItems.CSID}/items/</uri>
+            <filename>vocabulary/PostWithItems/vocab-Item-no-shortid.xml</filename>
+            <vars>
+                <var ID="order">4</var>
+                <var ID="displayName">TestOrder item ${itemID} order ${order}</var>
+            </vars>
+        </test>
     </testGroup>
     
     <!--
index 7a60881e54d6507ee6e82615135190c208e07254..dbd5628176bd49684ecba78270536b4cfd181341 100644 (file)
@@ -994,13 +994,12 @@ public abstract class AuthorityItemDocumentModelHandler<AICommon>
      * 
      * @throws Exception the exception
      */
-    private void handleInAuthority(DocumentModel docModel) throws Exception {
-        if(inAuthority==null) {        // Only happens on queries to wildcarded authorities
-               throw new IllegalStateException("Trying to Create an object with no inAuthority value!");
-        }
-        docModel.setProperty(authorityItemCommonSchemaName,
-                AuthorityItemJAXBSchema.IN_AUTHORITY, inAuthority);
-    }
+       private void handleInAuthority(DocumentModel docModel) throws Exception {
+               if (inAuthority == null) { // Only happens on queries to wildcarded authorities
+                       throw new IllegalStateException("Trying to Create an object with no inAuthority value!");
+               }
+               docModel.setProperty(authorityItemCommonSchemaName, AuthorityItemJAXBSchema.IN_AUTHORITY, inAuthority);
+       }
     
     /**
      * Returns a list of records that reference this authority item
index 64a5f9e3890387df61a223d5da4911c840c0e236..1ae94b8c151ff22a384eb70fb4acb4d162515ce1 100644 (file)
@@ -40,7 +40,6 @@ import org.collectionspace.services.common.context.ServiceContext;
 import org.collectionspace.services.common.document.DocumentException;
 import org.collectionspace.services.common.document.DocumentHandler;
 import org.collectionspace.services.common.document.DocumentNotFoundException;
-import org.collectionspace.services.common.document.JaxbUtils;
 import org.collectionspace.services.common.repository.RepositoryClient;
 import org.collectionspace.services.common.vocabulary.AuthorityResource;
 import org.collectionspace.services.common.vocabulary.AuthorityServiceUtils;
@@ -50,17 +49,15 @@ import org.collectionspace.services.common.vocabulary.RefNameServiceUtils.Specif
 import org.collectionspace.services.jaxb.AbstractCommonList;
 import org.collectionspace.services.jaxb.AbstractCommonList.ListItem;
 import org.collectionspace.services.nuxeo.client.java.CoreSessionInterface;
-import org.collectionspace.services.nuxeo.client.java.NuxeoRepositoryClientImpl;
 import org.collectionspace.services.vocabulary.nuxeo.VocabularyItemDocumentModelHandler;
-import org.collectionspace.services.workflow.WorkflowCommon;
 import org.nuxeo.ecm.core.api.DocumentModel;
 import org.nuxeo.ecm.core.api.DocumentModelList;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.w3c.dom.Element;
 
+import java.util.Base64;
 import java.util.HashSet;
-import java.util.List;
 import java.util.Set;
 
 import javax.ws.rs.GET;
@@ -74,6 +71,7 @@ import javax.ws.rs.core.Request;
 import javax.ws.rs.core.Response;
 import javax.ws.rs.core.UriBuilder;
 import javax.ws.rs.core.UriInfo;
+import javax.xml.bind.DatatypeConverter;
 
 @Path("/" + VocabularyClient.SERVICE_PATH_COMPONENT)
 public class VocabularyResource extends 
@@ -190,6 +188,8 @@ public class VocabularyResource extends
                PoxPayloadIn input) throws Exception {
        
        CoreSessionInterface repoSession = (CoreSessionInterface) existingCtx.getCurrentRepositorySession();
+               Set<String> shortIdsInPayload = getListOfShortIds(itemsList); // record the list of existing or new terms/items
+
        //
        // First try to update and/or create items in the incoming payload
        //
@@ -218,9 +218,18 @@ public class VocabularyResource extends
                                }
                                }
                        } else {
-                               success = false;
-                               errMsg = String.format("Could not update the term list payload of vocabuary '%s' because one of the item is missing a CSID or short identifier value.",
-                                               parentIdentifier);
+                               //
+                               // Since the item was supplied with neither a CSID nor a short identifier, we'll assume we're being
+                               // asked to create it.
+                               //
+                       response = this.createAuthorityItem(repoSession, resourceMap, uriInfo, parentIdentifier, itemXmlPayload);
+                       if (response.getStatus() == Response.Status.CREATED.getStatusCode()) {
+                               String shortId = getShortId(itemXmlPayload);
+                               shortIdsInPayload.add(shortId); // add the new short ID to the list of incoming items
+                       } else {
+                               success = false;
+                                       errMsg = String.format("Could not create the term list payload of vocabuary '%s'.", parentIdentifier);
+                       }
                        }
                        //
                        // Throw an exception as soon as we have problems with any item
@@ -235,7 +244,6 @@ public class VocabularyResource extends
                //
                if (shouldDeleteOmittedItems(uriInfo) == true) {
                        String omittedItemAction = getOmittedItemAction(uriInfo);
-                       Set<String> shortIdsInPayload = getListOfShortIds(itemsList);
                        AbstractCommonList abstractCommonList = this.getAuthorityItemList(existingCtx, parentIdentifier, uriInfo);
                        if (abstractCommonList != null && !Tools.isEmpty(abstractCommonList.getListItem())) {
                                if (omittedItemAction.equalsIgnoreCase(VocabularyClient.DELETE_OMITTED_ITEMS)) {
@@ -247,7 +255,16 @@ public class VocabularyResource extends
                }
        }
     
-    private void deleteAuthorityItems(
+    private String getShortId(PoxPayloadIn itemXmlPayload) {
+               String result = null;
+               
+               VocabularyitemsCommon vocabularyItemsCommon = (VocabularyitemsCommon) itemXmlPayload.getPart(VOCABULARYITEMS_COMMON).getBody();
+               result = vocabularyItemsCommon.getShortIdentifier();
+               
+               return result;
+       }
+
+       private void deleteAuthorityItems(
                ServiceContext<PoxPayloadIn, PoxPayloadOut> existingCtx,
                AbstractCommonList abstractCommonList,
                Set<String> shortIdsInPayload,
@@ -312,20 +329,24 @@ public class VocabularyResource extends
                HashSet<String> result = new HashSet<String>();
                
                for (ListItem item : itemsList.getListItem()) {
-                       result.add(getShortId(item));
+                       String shortId = getShortId(item);
+                       if (Tools.isEmpty(shortId) == false) {
+                               result.add(shortId);
+                       }
                }
                
                return result;
        }
 
        private void createWithItemsPayload(
-               AbstractCommonList itemsList,
-               ServiceContext existingCtx,
-               String parentIdentifier,
-               ResourceMap resourceMap,
-               UriInfo uriInfo,
-               PoxPayloadIn input) throws Exception {
-       
+                       AbstractCommonList itemsList,
+                       ServiceContext<PoxPayloadIn, 
+                       PoxPayloadOut> existingCtx, 
+                       String parentIdentifier, 
+                       ResourceMap resourceMap,
+                       UriInfo uriInfo, 
+                       PoxPayloadIn input) throws Exception {
+
                for (ListItem item : itemsList.getListItem()) {
                        String errMsg = null;
                        boolean success = true;
@@ -344,12 +365,12 @@ public class VocabularyResource extends
                        if (success == false) {
                                throw new DocumentException(errMsg);
                        }
-               }       
-       }    
+               }
+       }   
     
     private boolean handleItemsPayload(
                Method method,
-               ServiceContext existingCtx,
+               ServiceContext<PoxPayloadIn, PoxPayloadOut> existingCtx,
                String parentIdentifier,
                ResourceMap resourceMap,
                UriInfo uriInfo,
@@ -372,13 +393,13 @@ public class VocabularyResource extends
        return result;
        }
     
-    private String getCsid(ListItem item) {
+    private String getFieldValue(ListItem item, String lookingFor) {
        String result = null;
        
                for (Element ele : item.getAny()) {
                        String fieldName = ele.getTagName();
                        String fieldValue = ele.getTextContent();
-                       if (fieldName.equalsIgnoreCase("csid")) {
+                       if (fieldName.equalsIgnoreCase(lookingFor)) {
                                result = fieldValue;
                                break;
                        }
@@ -387,19 +408,16 @@ public class VocabularyResource extends
        return result;
     }
     
+    private String getCsid(ListItem item) {
+       return getFieldValue(item, "csid");
+    }
+    
     private String getShortId(ListItem item) {
-       String result = null;
-       
-       for (Element ele : item.getAny()) {
-                       String fieldName = ele.getTagName();
-                       String fieldValue = ele.getTextContent();
-                       if (fieldName.equalsIgnoreCase("shortIdentifier")) {
-                               result = fieldValue;
-                               break;
-                       }
-               }
-       
-       return result;
+       return getFieldValue(item, "shortIdentifier");
+    }
+    
+    private String getDisplayName(ListItem item) {
+       return getFieldValue(item, "displayName");
     }
     
     /**
@@ -411,7 +429,7 @@ public class VocabularyResource extends
     private String getSpecifier(ListItem item) {
                String result = null;
 
-               String csid = getCsid(item);            
+               String csid = result = getCsid(item);
                if (csid == null) {
                        String shortId = getShortId(item);                      
                        if (shortId != null) {
@@ -465,17 +483,30 @@ public class VocabularyResource extends
                                case "csid":
                                        vocabularyItem.setCsid(fieldValue);
                                        break;
+                                       
+                               case "termStatus":
+                                       vocabularyItem.setTermStatus(fieldValue);
+                                       break;
 
                                default:
-                                       throw new DocumentException(String.format("Unknown field '%s' in vocabulary item payload.",
-                                                       fieldName));
+                                       // ignore other fields
+                                       break;
                        }
                }
                
+               if (Tools.isEmpty(vocabularyItem.getShortIdentifier())) {
+                       //
+                       // We need to create a short ID since one wasn't supplied
+                       //
+                       String value = String.format("%s%d", getDisplayName(item), System.currentTimeMillis());
+                       value = DatatypeConverter.printHexBinary(value.getBytes()).toUpperCase();
+                       vocabularyItem.setShortIdentifier(value);
+               }
+               
                result = new PoxPayloadIn(VocabularyClient.SERVICE_ITEM_PAYLOAD_NAME, vocabularyItem, 
                        VOCABULARYITEMS_COMMON);
 
-               return result;
+               return result; 
        }
     
        private Response createAuthorityItem(