From dd4e8f3c2a3d1ddbf9f20b84ec9c3fd02126cefc Mon Sep 17 00:00:00 2001 From: Aron Roberts Date: Tue, 4 Oct 2011 17:28:48 +0000 Subject: [PATCH] CSPACE-3178: Added convenience methods to Authority, AuthorityItem in common-api to get short identifier and (for items) parent short identifier. --- .../services/common/api/RefName.java | 92 +++++++++++-------- .../services/common/api/test/RefNameTest.java | 26 ++++++ 2 files changed, 78 insertions(+), 40 deletions(-) diff --git a/services/common-api/src/main/java/org/collectionspace/services/common/api/RefName.java b/services/common-api/src/main/java/org/collectionspace/services/common/api/RefName.java index 93e09ca4e..6a9370649 100755 --- a/services/common-api/src/main/java/org/collectionspace/services/common/api/RefName.java +++ b/services/common-api/src/main/java/org/collectionspace/services/common/api/RefName.java @@ -36,27 +36,21 @@ import java.util.regex.Pattern; * * User: laramie */ - public class RefName { + public static final String HACK_VOCABULARIES = "Vocabularies"; //TODO: get rid of these. public static final String HACK_ORGANIZATIONS = "Organizations"; //TODO: get rid of these. public static final String HACK_ORGAUTHORITIES = "Orgauthorities"; //TODO: get rid of these. public static final String HACK_PERSONAUTHORITIES = "Personauthorities"; //TODO: get rid of these. public static final String HACK_LOCATIONAUTHORITIES = "Locationauthorities"; //TODO: get rid of these. - - public static final String URN_PREFIX = "urn:cspace:"; + public static final String URN_PREFIX = "urn:cspace:"; public static final String URN_NAME_PREFIX = "urn:cspace:name"; - public static final String REFNAME = "refName"; - - public static final String AUTHORITY_REGEX = "urn:cspace:(.*):(.*):name\\((.*)\\)\\'?([^\\']*)\\'?"; + public static final String AUTHORITY_REGEX = "urn:cspace:(.*):(.*):name\\((.*)\\)\\'?([^\\']*)\\'?"; public static final String AUTHORITY_ITEM_REGEX = "urn:cspace:(.*):(.*):name\\((.*)\\):item:name\\((.*)\\)\\'?([^\\']*)\\'?"; - - public static final String AUTHORITY_EXAMPLE = "urn:cspace:collectionspace.org:Loansin:name(shortID)'displayName'"; + public static final String AUTHORITY_EXAMPLE = "urn:cspace:collectionspace.org:Loansin:name(shortID)'displayName'"; public static final String AUTHORITY_EXAMPLE2 = "urn:cspace:collectionspace.org:Loansin:name(shortID)"; - - public static final String AUTHORITY_ITEM_EXAMPLE ="urn:cspace:collectionspace.org:Loansin:name(shortID):item:name(itemShortID)'itemDisplayName'"; - + public static final String AUTHORITY_ITEM_EXAMPLE = "urn:cspace:collectionspace.org:Loansin:name(shortID):item:name(itemShortID)'itemDisplayName'"; public static final String EX_tenantName = "collectionspace.org"; public static final String EX_resource = "Loansin"; public static final String EX_shortIdentifier = "shortID"; @@ -64,18 +58,19 @@ public class RefName { public static final String EX_itemShortIdentifier = "itemShortID"; public static final String EX_itemDisplayName = "itemDisplayName"; - public static class Authority { + public String tenantName = ""; public String resource = ""; public String shortIdentifier = ""; public String displayName = ""; + public static Authority parse(String urn) { Authority info = new Authority(); Pattern p = Pattern.compile(AUTHORITY_REGEX); Matcher m = p.matcher(urn); - if (m.find()){ - if (m.groupCount()<4){ + if (m.find()) { + if (m.groupCount() < 4) { return null; } info.tenantName = m.group(1); @@ -86,43 +81,50 @@ public class RefName { } return null; } - public boolean equals(Object other){ - if (other == null){ + + public String getShortIdentifier() { + return this.shortIdentifier; + } + + public boolean equals(Object other) { + if (other == null) { return false; } - if (other instanceof Authority){ - Authority ao = (Authority)other; - return ( this.tenantName.equals(ao.tenantName) + if (other instanceof Authority) { + Authority ao = (Authority) other; + return (this.tenantName.equals(ao.tenantName) && this.resource.equals(ao.resource) - && this.shortIdentifier.equals(ao.shortIdentifier) - ); + && this.shortIdentifier.equals(ao.shortIdentifier)); } else { return false; } } + public String getRelativeUri() { - return "/"+resource+"/"+URN_NAME_PREFIX+"("+shortIdentifier+")"; + return "/" + resource + "/" + URN_NAME_PREFIX + "(" + shortIdentifier + ")"; } + public String toString() { String displaySuffix = (displayName != null && (!displayName.isEmpty())) ? '\'' + displayName + '\'' : ""; return URN_PREFIX + tenantName + ':' + resource + ":" + "name" + "(" + shortIdentifier + ")" + displaySuffix; } - } public static class AuthorityItem { + public Authority inAuthority; public String shortIdentifier = ""; public String displayName = ""; + public static AuthorityItem parse(String urn) { Authority info = new Authority(); AuthorityItem termInfo = new AuthorityItem(); termInfo.inAuthority = info; Pattern p = Pattern.compile(AUTHORITY_ITEM_REGEX); Matcher m = p.matcher(urn); - if (m.find()){ - if (m.groupCount()<5){ - return null; + if (m.find()) { + if (m.groupCount() < 5) { + return null; } termInfo.inAuthority.tenantName = m.group(1); termInfo.inAuthority.resource = m.group(2); @@ -133,12 +135,21 @@ public class RefName { } return null; } - public boolean equals(Object other){ - if (other == null){ + + public String getParentShortIdentifier() { + return this.inAuthority.shortIdentifier; + } + + public String getShortIdentifier() { + return this.shortIdentifier; + } + + public boolean equals(Object other) { + if (other == null) { return false; } - if (other instanceof AuthorityItem){ - AuthorityItem aio = (AuthorityItem)other; + if (other instanceof AuthorityItem) { + AuthorityItem aio = (AuthorityItem) other; boolean ok = true; ok = ok && aio.inAuthority != null; ok = ok && aio.inAuthority.equals(this.inAuthority); @@ -149,21 +160,22 @@ public class RefName { return false; } } + public String getRelativeUri() { - return inAuthority.getRelativeUri()+"/items/"+URN_NAME_PREFIX+"("+shortIdentifier+")"; + return inAuthority.getRelativeUri() + "/items/" + URN_NAME_PREFIX + "(" + shortIdentifier + ")"; } + public String toString() { String displaySuffix = (displayName != null && (!displayName.isEmpty())) ? '\'' + displayName + '\'' : ""; Authority ai = inAuthority; - if (ai==null){ - return URN_PREFIX+"ERROR:inAuthorityNotSet: (" + shortIdentifier + ")" + displaySuffix; + if (ai == null) { + return URN_PREFIX + "ERROR:inAuthorityNotSet: (" + shortIdentifier + ")" + displaySuffix; } else { - String base = URN_PREFIX + ai.tenantName + ':' + ai.resource + ":" + "name" + "(" + ai.shortIdentifier + ")" ; - String refname = base+":item:name("+shortIdentifier+")"+displaySuffix; - return refname; + String base = URN_PREFIX + ai.tenantName + ':' + ai.resource + ":" + "name" + "(" + ai.shortIdentifier + ")"; + String refname = base + ":item:name(" + shortIdentifier + ")" + displaySuffix; + return refname; } } - } public static Authority buildAuthority(String tenantName, String serviceName, String authorityShortIdentifier, String authorityDisplayName) { @@ -179,7 +191,7 @@ public class RefName { } public static AuthorityItem buildAuthorityItem(String tenantName, String serviceName, String authorityShortIdentifier, - String itemShortIdentifier, String itemDisplayName) { + String itemShortIdentifier, String itemDisplayName) { Authority authority = buildAuthority(tenantName, serviceName, authorityShortIdentifier, ""); return buildAuthorityItem(authority, itemShortIdentifier, itemDisplayName); } @@ -204,7 +216,7 @@ public class RefName { * so that calling shortIdToPath("TestAuthority") returns "urn:cspace:name(TestAuthority)", and * then this value may be put into a path, such as "/personauthorities/urn:cspace:name(TestAuthority)/items". */ - public static String shortIdToPath(String shortId){ - return URN_NAME_PREFIX+'('+shortId+')'; + public static String shortIdToPath(String shortId) { + return URN_NAME_PREFIX + '(' + shortId + ')'; } } diff --git a/services/common-api/src/test/java/org/collectionspace/services/common/api/test/RefNameTest.java b/services/common-api/src/test/java/org/collectionspace/services/common/api/test/RefNameTest.java index 2bb2b198f..f6a91344b 100755 --- a/services/common-api/src/test/java/org/collectionspace/services/common/api/test/RefNameTest.java +++ b/services/common-api/src/test/java/org/collectionspace/services/common/api/test/RefNameTest.java @@ -85,5 +85,31 @@ import org.testng.annotations.Test; check(item.toString(), item3.toString(), "buildAuthorityItem(Authority,str,str) from AUTHORITY_EXAMPLE2 vs. AUTHORITY_ITEM_EXAMPLE"); Assert.assertEquals(item, item3); } + + /** + * Test convenience getters that return short identifiers for authorities and authority items. + */ + @Test + public void testShortIDGetters(){ + testBanner("testShortIDGetters"); + RefName.Authority authority = RefName.Authority.parse(RefName.AUTHORITY_EXAMPLE); + Assert.assertEquals(authority.getShortIdentifier(), RefName.EX_shortIdentifier, + "Short identifier from parsing parent authority refName does not match value of Authority.getShortIdentifier()."); + + RefName.AuthorityItem item = RefName.buildAuthorityItem(RefName.AUTHORITY_EXAMPLE, + RefName.EX_itemShortIdentifier, + RefName.EX_itemDisplayName); + Assert.assertEquals(item.getParentShortIdentifier(), RefName.EX_shortIdentifier, + "Parent short identifier from parsing authority refName does not match value of AuthorityItem.getParentShortIdentifier()."); + Assert.assertEquals(item.getShortIdentifier(), RefName.EX_itemShortIdentifier, + "Short identifier from item does not match value of AuthorityItem.getShortIdentifier()."); + + RefName.AuthorityItem parsedItem = RefName.AuthorityItem.parse(RefName.AUTHORITY_ITEM_EXAMPLE); + Assert.assertEquals(parsedItem.getParentShortIdentifier(), RefName.EX_shortIdentifier, + "Parent short identifier from parsing item refName does not match value of AuthorityItem.getParentShortIdentifier()."); + Assert.assertEquals(parsedItem.getShortIdentifier(), RefName.EX_itemShortIdentifier, + "Short identifier from parsing item refName does not match value of AuthorityItem.getShortIdentifier()."); + + } } -- 2.47.3