]> git.aero2k.de Git - tmp/jakarta-migration.git/commitdiff
CSPACE-5271: More work on returning UriTemplates from resource classes, for inclusion...
authorAron Roberts <aron@socrates.berkeley.edu>
Fri, 8 Jun 2012 19:26:07 +0000 (12:26 -0700)
committerAron Roberts <aron@socrates.berkeley.edu>
Fri, 8 Jun 2012 19:26:07 +0000 (12:26 -0700)
services/JaxRsServiceProvider/src/main/java/org/collectionspace/services/jaxrs/CollectionSpaceJaxRsApplication.java
services/authority/service/src/main/java/org/collectionspace/services/common/vocabulary/AuthorityResource.java
services/common/src/main/java/org/collectionspace/services/common/ResourceBase.java
services/common/src/main/java/org/collectionspace/services/common/StoredValuesUriTemplate.java
services/common/src/main/java/org/collectionspace/services/common/UriTemplateFactory.java
services/contact/service/src/main/java/org/collectionspace/services/contact/AuthorityResourceWithContacts.java

index 56eaf47c88b322f89684938d0b82538571357c2a..b476f562067a71d2c87cf298194a096cc01a942e 100644 (file)
@@ -142,9 +142,7 @@ public class CollectionSpaceJaxRsApplication extends Application
     private void addResourceToMapAndSingletons(ResourceBase resource) {
         singletons.add(resource);
         resourceMap.put(resource.getServiceName(), resource);
-        // FIXME: Accept multiple entries from each resource into the registry.
-        // See also similar comments in ResourceBase.
-        uriTemplateRegistry.put(resource.getDocType(), resource.getUriTemplate());
+        uriTemplateRegistry.putAll(resource.getUriTemplateMap());
     }
 
     @Override
index cdf3b4c8b579872c7db6aa4f0b3f7b801c4f41cd..f8ca689d5f316d5a056d7e948b09cf1c65db3335 100644 (file)
@@ -81,8 +81,11 @@ import javax.ws.rs.core.UriBuilder;
 import javax.ws.rs.core.UriInfo;
 
 import java.util.ArrayList;
+import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
 import org.collectionspace.services.client.*;
+import org.collectionspace.services.common.*;
 import org.collectionspace.services.nuxeo.util.NuxeoUtils;
 
 /**
@@ -957,4 +960,36 @@ public abstract class AuthorityResource<AuthCommon, AuthItemHandler>
             throw bigReThrow(e, "Error showing hierarchy", itemcsid);
         }
     }
+    
+    public String getItemDocType() {
+        // FIXME: Proof of concept placeholder
+        return getServiceName();
+    }
+
+    @Override
+    public Map<String,StoredValuesUriTemplate> getUriTemplateMap() {
+        // Get resource URI template from superclass
+        Map<String,StoredValuesUriTemplate> uriTemplateMap = super.getUriTemplateMap();
+        // Add item URI template
+        String itemDocType = getItemDocType();
+        StoredValuesUriTemplate itemUriTemplate = getItemUriTemplate();
+        if (itemDocType == null) {
+            return uriTemplateMap; // return map as obtained from superclass
+        }
+        if (itemUriTemplate == null) {
+            return uriTemplateMap; // return map as obtained from superclass
+        }
+        uriTemplateMap.put(itemDocType, itemUriTemplate);
+        cacheUriTemplateMap(uriTemplateMap);
+        return uriTemplateMap;
+    }
+    
+    private StoredValuesUriTemplate getItemUriTemplate() {
+        Map<String,String> storedValuesMap = new HashMap<String,String>();
+        storedValuesMap.put(UriTemplateFactory.SERVICENAME_VAR, getServiceName());
+        StoredValuesUriTemplate template =
+                UriTemplateFactory.getURITemplate(UriTemplateFactory.ITEM,
+                storedValuesMap);
+        return template;
+    }
 }
index 8ba0c0cf65d8bae061a7a2a63d2a91c1ec02409c..433bc9956aa6869fc7acc018b45fac1cdc5a5b82 100644 (file)
@@ -66,6 +66,8 @@ public abstract class ResourceBase
     public static final String LIST = "list";\r
     //FIXME retrieve client type from configuration\r
     static ClientType CLIENT_TYPE;\r
+    \r
+    Map<String,StoredValuesUriTemplate> cachedUriTemplateMap = new HashMap<String,StoredValuesUriTemplate>();\r
 \r
     /*\r
      * REM - 11/14/2011 - I discovered this static block of code and don't understand why it exists.  However, a side-effect of this static block is that ServiceMain is trying\r
@@ -393,21 +395,26 @@ public abstract class ResourceBase
         return getServiceName();\r
     }\r
     \r
-    // As generally mentioned by Patrick, we will want to cache generated values, and\r
-    // only generate one time if the cached value has not yet been populated. \r
-\r
     public Map<String,StoredValuesUriTemplate> getUriTemplateMap() {\r
+        // Return cached copy of URI template map, if available\r
         Map<String,StoredValuesUriTemplate> uriTemplateMap = new HashMap<String,StoredValuesUriTemplate>();\r
-        String docType = getDocType();\r
-        StoredValuesUriTemplate resourceUriTemplate = getResourceUriTemplate();\r
-        if (docType == null) {\r
-            return uriTemplateMap; // return an empty map\r
-        }\r
-        if (resourceUriTemplate == null) {\r
-            return uriTemplateMap; // return an empty map\r
+        if (this.cachedUriTemplateMap != null && !this.cachedUriTemplateMap.isEmpty()) {\r
+            uriTemplateMap.putAll(cachedUriTemplateMap);\r
+            return uriTemplateMap;\r
+        } else {\r
+            // Construct and return a resource URI template as the sole item in the map\r
+            String docType = getDocType();\r
+            StoredValuesUriTemplate resourceUriTemplate = getResourceUriTemplate();\r
+            if (docType == null) {\r
+                return uriTemplateMap; // return an empty map\r
+            }\r
+            if (resourceUriTemplate == null) {\r
+                return uriTemplateMap; // return an empty map\r
+            }\r
+            uriTemplateMap.put(docType, resourceUriTemplate);\r
+            cacheUriTemplateMap(uriTemplateMap);\r
+            return uriTemplateMap;\r
         }\r
-        uriTemplateMap.put(docType, resourceUriTemplate);\r
-        return uriTemplateMap;\r
     }\r
     \r
     private StoredValuesUriTemplate getResourceUriTemplate() {\r
@@ -419,4 +426,8 @@ public abstract class ResourceBase
         return template;\r
     }\r
 \r
+    public void cacheUriTemplateMap(Map<String, StoredValuesUriTemplate> uriTemplateMap) {\r
+            this.cachedUriTemplateMap = uriTemplateMap;\r
+    }\r
+\r
 }\r
index 47efe1f75adb567caebc1bbdbc1d2ae371a0c6b5..2769945fae15ad70cf875edaf4b544e3c4d38f5d 100644 (file)
@@ -35,10 +35,10 @@ import org.slf4j.LoggerFactory;
  * replace variables within the template.
  *
  * In this subclass of UriTemplate, some of the values which will replace
- * variables in a URI template are static, and can be stored alongside the
- * template for reuse. Additional values that will replace variables within the
- * template are also dynamically accepted, and are merged with stored values
- * when building URIs.
+ * variables in an internal URI template are static, and can be stored alongside
+ * the URI template for reuse. Additional values that will replace variables
+ * within the template are also dynamically accepted, and will be merged with
+ * stored values when building URIs.
  */
 public class StoredValuesUriTemplate extends UriTemplate {
 
@@ -54,13 +54,14 @@ public class StoredValuesUriTemplate extends UriTemplate {
         setStoredValuesMap(storedValuesMap);
     }
 
-    final public void setStoredValuesMap(Map<String, String> storedValuesMap) {
+    // Private access ensures that stored values can only be set via a constructor
+    private void setStoredValuesMap(Map<String, String> storedValuesMap) {
         if (storedValuesMap != null && !storedValuesMap.isEmpty()) {
             this.storedValuesMap = storedValuesMap;
         }
     }
 
-    private Map<String, String> getStoredValuesMap() {
+    public Map<String, String> getStoredValuesMap() {
         return this.storedValuesMap;
     }
 
index 75755816197e6448d2e238d40878232d5da084f7..72dedc4f1212336ab07da373e7cfad0fc8bf7d70 100644 (file)
@@ -64,7 +64,7 @@ public class UriTemplateFactory {
             + "/contacts/"\r
             + "{" + CONTACT_IDENTIFIER_VAR + "}";\r
 \r
-    public static StoredValuesUriTemplate getURITemplate(UriTemplateType type) {\r
+    public static UriTemplate getURITemplate(UriTemplateType type) {\r
         return new StoredValuesUriTemplate(type, getUriPathPattern(type));\r
     }\r
     \r
index cd2c047a0f7703035e8ef8b9de17ac0e3c98c009..fa381b66c09885d642ad5c2d11f8d3da78cf31ed 100644 (file)
@@ -23,6 +23,8 @@
  */
 package org.collectionspace.services.contact;
 
+import java.util.HashMap;
+import java.util.Map;
 import javax.ws.rs.Consumes;
 import javax.ws.rs.DELETE;
 import javax.ws.rs.GET;
@@ -41,6 +43,8 @@ import javax.ws.rs.core.UriInfo;
 import org.collectionspace.services.client.IQueryManager;
 import org.collectionspace.services.client.PoxPayloadIn;
 import org.collectionspace.services.client.PoxPayloadOut;
+import org.collectionspace.services.common.StoredValuesUriTemplate;
+import org.collectionspace.services.common.UriTemplateFactory;
 import org.collectionspace.services.common.vocabulary.AuthorityResource;
 import org.collectionspace.services.common.context.ServiceContext;
 import org.collectionspace.services.common.document.DocumentFilter;
@@ -294,4 +298,36 @@ public abstract class AuthorityResourceWithContacts<AuthCommon, AuthItemHandler>
                     + ": and item:" + itemspecifier + ": was not found.", csid);
         }
     }
+    
+    public String getContactDocType() {
+        // FIXME: Proof of concept placeholder
+        return getServiceName();
+    }
+
+    @Override
+    public Map<String,StoredValuesUriTemplate> getUriTemplateMap() {
+        // Get resource and item URI templates from superclass
+        Map<String,StoredValuesUriTemplate> uriTemplateMap = super.getUriTemplateMap();
+        // Add item URI template
+        String contactDocType = getContactDocType();
+        StoredValuesUriTemplate itemUriTemplate = getItemUriTemplate();
+        if (contactDocType == null) {
+            return uriTemplateMap; // return map as obtained from superclass
+        }
+        if (itemUriTemplate == null) {
+            return uriTemplateMap; // return map as obtained from superclass
+        }
+        uriTemplateMap.put(contactDocType, itemUriTemplate);
+        cacheUriTemplateMap(uriTemplateMap);
+        return uriTemplateMap;
+    }
+    
+    private StoredValuesUriTemplate getItemUriTemplate() {
+        Map<String,String> storedValuesMap = new HashMap<String,String>();
+        storedValuesMap.put(UriTemplateFactory.SERVICENAME_VAR, getServiceName());
+        StoredValuesUriTemplate template =
+                UriTemplateFactory.getURITemplate(UriTemplateFactory.ITEM,
+                storedValuesMap);
+        return template;
+    }
 }