From 12edeb53833cfade13227893784b25b468977514 Mon Sep 17 00:00:00 2001 From: Aron Roberts Date: Fri, 8 Jun 2012 14:15:08 -0700 Subject: [PATCH] CSPACE-5271: Remove per-resource cacheing of UriTemplateMap; likely would not have worked as designed. Make generic method for getting a UriTemplate by type in ResourceBase and sub-classes. --- .../common/vocabulary/AuthorityResource.java | 15 ++----- .../services/common/ResourceBase.java | 43 ++++++------------- .../common/StoredValuesUriTemplate.java | 12 ++++-- .../services/common/UriTemplateFactory.java | 17 ++++++-- .../AuthorityResourceWithContacts.java | 24 +++-------- 5 files changed, 43 insertions(+), 68 deletions(-) diff --git a/services/authority/service/src/main/java/org/collectionspace/services/common/vocabulary/AuthorityResource.java b/services/authority/service/src/main/java/org/collectionspace/services/common/vocabulary/AuthorityResource.java index f8ca689d5..1ebd88a3b 100644 --- a/services/authority/service/src/main/java/org/collectionspace/services/common/vocabulary/AuthorityResource.java +++ b/services/authority/service/src/main/java/org/collectionspace/services/common/vocabulary/AuthorityResource.java @@ -970,9 +970,9 @@ public abstract class AuthorityResource public Map getUriTemplateMap() { // Get resource URI template from superclass Map uriTemplateMap = super.getUriTemplateMap(); - // Add item URI template + // Add item URI template, and return both templates in the map String itemDocType = getItemDocType(); - StoredValuesUriTemplate itemUriTemplate = getItemUriTemplate(); + StoredValuesUriTemplate itemUriTemplate = getUriTemplate(UriTemplateFactory.ITEM); if (itemDocType == null) { return uriTemplateMap; // return map as obtained from superclass } @@ -980,16 +980,7 @@ public abstract class AuthorityResource return uriTemplateMap; // return map as obtained from superclass } uriTemplateMap.put(itemDocType, itemUriTemplate); - cacheUriTemplateMap(uriTemplateMap); return uriTemplateMap; } - - private StoredValuesUriTemplate getItemUriTemplate() { - Map storedValuesMap = new HashMap(); - storedValuesMap.put(UriTemplateFactory.SERVICENAME_VAR, getServiceName()); - StoredValuesUriTemplate template = - UriTemplateFactory.getURITemplate(UriTemplateFactory.ITEM, - storedValuesMap); - return template; - } + } diff --git a/services/common/src/main/java/org/collectionspace/services/common/ResourceBase.java b/services/common/src/main/java/org/collectionspace/services/common/ResourceBase.java index 433bc9956..578159891 100644 --- a/services/common/src/main/java/org/collectionspace/services/common/ResourceBase.java +++ b/services/common/src/main/java/org/collectionspace/services/common/ResourceBase.java @@ -382,52 +382,33 @@ public abstract class ResourceBase return getDocModelForAuthorityItem(repoSession, RefName.AuthorityItem.parse(refName)); } - // FIXME: For authority items, we will need to return an array / collection of uriTemplate objects, - // since there are two different doctypes, with associated uriTemplates, to insert into the registry. - // (One of those uriTemplates will also need to be of type ITEM, while in AuthorityResourceWithContacts, - // a third uriTemplate will need to be of type CONCEPT.) - - // The following is a placeholder to test proof of concept, adding a single item from each resource - // to the registry. - public String getDocType() { // FIXME: Proof of concept placeholder return getServiceName(); } public Map getUriTemplateMap() { - // Return cached copy of URI template map, if available Map uriTemplateMap = new HashMap(); - if (this.cachedUriTemplateMap != null && !this.cachedUriTemplateMap.isEmpty()) { - uriTemplateMap.putAll(cachedUriTemplateMap); - return uriTemplateMap; - } else { - // Construct and return a resource URI template as the sole item in the map - String docType = getDocType(); - StoredValuesUriTemplate resourceUriTemplate = getResourceUriTemplate(); - if (docType == null) { - return uriTemplateMap; // return an empty map - } - if (resourceUriTemplate == null) { - return uriTemplateMap; // return an empty map - } - uriTemplateMap.put(docType, resourceUriTemplate); - cacheUriTemplateMap(uriTemplateMap); - return uriTemplateMap; + // Construct and return a resource URI template as the sole item in the map + String docType = getDocType(); + StoredValuesUriTemplate resourceUriTemplate = getUriTemplate(UriTemplateFactory.RESOURCE); + if (docType == null) { + return uriTemplateMap; // return an empty map + } + if (resourceUriTemplate == null) { + return uriTemplateMap; // return an empty map } + uriTemplateMap.put(docType, resourceUriTemplate); + return uriTemplateMap; } - private StoredValuesUriTemplate getResourceUriTemplate() { + protected StoredValuesUriTemplate getUriTemplate(UriTemplateFactory.UriTemplateType type) { Map storedValuesMap = new HashMap(); storedValuesMap.put(UriTemplateFactory.SERVICENAME_VAR, getServiceName()); StoredValuesUriTemplate template = - UriTemplateFactory.getURITemplate(UriTemplateFactory.RESOURCE, - storedValuesMap); + UriTemplateFactory.getURITemplate(type, storedValuesMap); return template; } - public void cacheUriTemplateMap(Map uriTemplateMap) { - this.cachedUriTemplateMap = uriTemplateMap; - } } diff --git a/services/common/src/main/java/org/collectionspace/services/common/StoredValuesUriTemplate.java b/services/common/src/main/java/org/collectionspace/services/common/StoredValuesUriTemplate.java index 2769945fa..60fb2340f 100644 --- a/services/common/src/main/java/org/collectionspace/services/common/StoredValuesUriTemplate.java +++ b/services/common/src/main/java/org/collectionspace/services/common/StoredValuesUriTemplate.java @@ -51,11 +51,17 @@ public class StoredValuesUriTemplate extends UriTemplate { public StoredValuesUriTemplate(UriTemplateType type, String path, Map storedValuesMap) { super(type, path); - setStoredValuesMap(storedValuesMap); + setStoredValues(storedValuesMap); } - // Private access ensures that stored values can only be set via a constructor - private void setStoredValuesMap(Map storedValuesMap) { + public void setStoredValuesMap(Map storedValuesMap) { + setStoredValues(storedValuesMap); + } + + // Called by both constructor and public setter + // Avoids having a subclass-overridable call in the constructor + // See http://stackoverflow.com/questions/6104262/java-overridable-call-in-constructor#comment7087131_6104273 + private void setStoredValues(Map storedValuesMap) { if (storedValuesMap != null && !storedValuesMap.isEmpty()) { this.storedValuesMap = storedValuesMap; } diff --git a/services/common/src/main/java/org/collectionspace/services/common/UriTemplateFactory.java b/services/common/src/main/java/org/collectionspace/services/common/UriTemplateFactory.java index 72dedc4f1..1c270fc13 100644 --- a/services/common/src/main/java/org/collectionspace/services/common/UriTemplateFactory.java +++ b/services/common/src/main/java/org/collectionspace/services/common/UriTemplateFactory.java @@ -23,6 +23,7 @@ package org.collectionspace.services.common; import java.util.Map; +import org.collectionspace.services.client.AuthorityClient; /** * UriTemplateFactory.java @@ -47,21 +48,29 @@ public class UriTemplateFactory { public final static String IDENTIFIER_VAR = "identifier"; public final static String ITEM_IDENTIFIER_VAR = "itemIdentifier"; public final static String CONTACT_IDENTIFIER_VAR = "contactIdentifier"; + + public final static String AUTHORITY_ITEM_PATH_COMPONENT = AuthorityClient.ITEMS; + // FIXME: Get this currently hard-coded value from an external authoritative source. + // The only candidate so far identified is ContactClient.SERVICE_PATH_COMPONENT; + // is this appropriate? + public final static String CONTACT_PATH_COMPONENT = "contacts"; public final static String RESOURCE_PATH_PATTERN = "/" + "{" + SERVICENAME_VAR + "}" + "/" + "{" + IDENTIFIER_VAR + "}"; - // FIXME: Get static strings below (e.g. "items", "contacts") from - // already-declared constants elsewhere public final static String ITEM_PATH_PATTERN = RESOURCE_PATH_PATTERN - + "/items/" + + "/" + + AUTHORITY_ITEM_PATH_COMPONENT + + "/" + "{" + ITEM_IDENTIFIER_VAR + "}"; public final static String CONTACT_PATH_PATTERN = ITEM_PATH_PATTERN - + "/contacts/" + + "/" + + CONTACT_PATH_COMPONENT + + "/" + "{" + CONTACT_IDENTIFIER_VAR + "}"; public static UriTemplate getURITemplate(UriTemplateType type) { diff --git a/services/contact/service/src/main/java/org/collectionspace/services/contact/AuthorityResourceWithContacts.java b/services/contact/service/src/main/java/org/collectionspace/services/contact/AuthorityResourceWithContacts.java index fa381b66c..38031d16c 100644 --- a/services/contact/service/src/main/java/org/collectionspace/services/contact/AuthorityResourceWithContacts.java +++ b/services/contact/service/src/main/java/org/collectionspace/services/contact/AuthorityResourceWithContacts.java @@ -39,10 +39,7 @@ import javax.ws.rs.core.MultivaluedMap; import javax.ws.rs.core.Response; import javax.ws.rs.core.UriBuilder; 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.client.*; import org.collectionspace.services.common.StoredValuesUriTemplate; import org.collectionspace.services.common.UriTemplateFactory; import org.collectionspace.services.common.vocabulary.AuthorityResource; @@ -308,26 +305,17 @@ public abstract class AuthorityResourceWithContacts public Map getUriTemplateMap() { // Get resource and item URI templates from superclass Map uriTemplateMap = super.getUriTemplateMap(); - // Add item URI template + // Add contact URI template, and return all three templates in the map String contactDocType = getContactDocType(); - StoredValuesUriTemplate itemUriTemplate = getItemUriTemplate(); + StoredValuesUriTemplate contactUriTemplate = getUriTemplate(UriTemplateFactory.CONTACT); if (contactDocType == null) { return uriTemplateMap; // return map as obtained from superclass } - if (itemUriTemplate == null) { + if (contactUriTemplate == null) { return uriTemplateMap; // return map as obtained from superclass } - uriTemplateMap.put(contactDocType, itemUriTemplate); - cacheUriTemplateMap(uriTemplateMap); + uriTemplateMap.put(contactDocType, contactUriTemplate); return uriTemplateMap; } - - private StoredValuesUriTemplate getItemUriTemplate() { - Map storedValuesMap = new HashMap(); - storedValuesMap.put(UriTemplateFactory.SERVICENAME_VAR, getServiceName()); - StoredValuesUriTemplate template = - UriTemplateFactory.getURITemplate(UriTemplateFactory.ITEM, - storedValuesMap); - return template; - } + } -- 2.47.3