From 631cd8668215da789c7d9ed33457a6185c6ce6a0 Mon Sep 17 00:00:00 2001 From: Costantino Perciante Date: Thu, 3 Aug 2017 10:35:38 +0000 Subject: [PATCH] namespaces are now retrieved per scope git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/data-catalogue/gcubedatacatalogue-metadata-discovery@152442 82a268e6-3cf1-43bd-a215-b396298e98cf --- distro/changelog.xml | 5 ++ pom.xml | 2 +- .../DataCalogueMetadataFormatReader.java | 2 +- .../reader/NamespaceCategoryReader.java | 79 +++++-------------- .../NamespacesCatalogueCategories.properties | 10 --- .../reader/QueryForResourceUtil.java | 20 ++++- 6 files changed, 45 insertions(+), 73 deletions(-) delete mode 100644 src/main/java/org/gcube/datacatalogue/metadatadiscovery/reader/NamespacesCatalogueCategories.properties diff --git a/distro/changelog.xml b/distro/changelog.xml index 9755f85..226e9ef 100644 --- a/distro/changelog.xml +++ b/distro/changelog.xml @@ -1,4 +1,9 @@ + + Namespaces are retrieved per scope + diff --git a/pom.xml b/pom.xml index ab93c30..796be6d 100644 --- a/pom.xml +++ b/pom.xml @@ -13,7 +13,7 @@ org.gcube.data-catalogue gcubedatacatalogue-metadata-discovery jar - 3.1.0-SNAPSHOT + 3.2.0-SNAPSHOT The gCube data catalogue metadata discovery library The gCube data catalogue metadata discovery library diff --git a/src/main/java/org/gcube/datacatalogue/metadatadiscovery/DataCalogueMetadataFormatReader.java b/src/main/java/org/gcube/datacatalogue/metadatadiscovery/DataCalogueMetadataFormatReader.java index 32dbdc0..379b26f 100644 --- a/src/main/java/org/gcube/datacatalogue/metadatadiscovery/DataCalogueMetadataFormatReader.java +++ b/src/main/java/org/gcube/datacatalogue/metadatadiscovery/DataCalogueMetadataFormatReader.java @@ -85,7 +85,7 @@ public class DataCalogueMetadataFormatReader implements DataCatalogueMetadataDis if(namespaceCategories == null) namespaceCategories = new ArrayList(); - NamespaceCategoryReader rd = new NamespaceCategoryReader(scope, NamespaceCategoryReader.NamespacesCatalogueCategoriesResourceID()); + NamespaceCategoryReader rd = new NamespaceCategoryReader(scope); namespaceCategories.addAll(rd.getNamespaces().getNamespaceCategories()); } }catch(Exception e){ diff --git a/src/main/java/org/gcube/datacatalogue/metadatadiscovery/reader/NamespaceCategoryReader.java b/src/main/java/org/gcube/datacatalogue/metadatadiscovery/reader/NamespaceCategoryReader.java index 479a3e5..e8f6e4a 100644 --- a/src/main/java/org/gcube/datacatalogue/metadatadiscovery/reader/NamespaceCategoryReader.java +++ b/src/main/java/org/gcube/datacatalogue/metadatadiscovery/reader/NamespaceCategoryReader.java @@ -6,11 +6,9 @@ package org.gcube.datacatalogue.metadatadiscovery.reader; import static org.gcube.resources.discovery.icclient.ICFactory.client; import java.io.ByteArrayInputStream; -import java.io.IOException; import java.io.InputStream; import java.io.StringReader; import java.util.List; -import java.util.Properties; import javax.xml.bind.JAXBContext; import javax.xml.bind.Unmarshaller; @@ -37,13 +35,12 @@ import org.xml.sax.InputSource; */ public class NamespaceCategoryReader { - /** - * - */ - public static final String NAMESPACES_CATALOGUE_CATEGORIES_PROPERTIES = "NamespacesCatalogueCategories.properties"; private static Logger logger = LoggerFactory.getLogger(NamespaceCategoryReader.class); + + private static final String GENERIC_RESOURCE_NAMESPACES_NAME = "Namespaces Catalogue Categories"; + private static final String GENERIC_RESOURCE_NAMESPACES_SECONDARY_TYPE = "DataCatalogueNamespace"; + private ScopeBean scope; - private String resourceID; private NamespaceCategories namespaces; @@ -51,16 +48,11 @@ public class NamespaceCategoryReader { * Instantiates a new medata format reader. * * @param scope the scope - * @param resourceID the resource id * @throws Exception the exception */ - public NamespaceCategoryReader(ScopeBean scope, String resourceID) throws Exception { + public NamespaceCategoryReader(ScopeBean scope) throws Exception { this.scope = scope; - this.resourceID = resourceID; - if(resourceID == null || resourceID.isEmpty()) - throw new Exception("Please, specify a valid ResourceID in the property file: "+NAMESPACES_CATALOGUE_CATEGORIES_PROPERTIES); - - readNamespaceCategoryByResourceID(); + readNamespaceCategory(); } /** @@ -69,8 +61,8 @@ public class NamespaceCategoryReader { * @return the metadata format by id * @throws Exception the exception */ - private void readNamespaceCategoryByResourceID() throws Exception { - logger.trace("Getting Namespace Category for resourceID: "+resourceID); + private void readNamespaceCategory() throws Exception { + logger.trace("Getting Namespace Category"); if(this.scope==null) throw new Exception("Scope is null"); @@ -84,26 +76,23 @@ public class NamespaceCategoryReader { logger.info("Using scope from ScopeProvider: "+scopeString); - String queryString = QueryForResourceUtil.queryForGenericResourceById(resourceID); + String queryString = QueryForResourceUtil.getGcubeGenericQueryStringForSecondaryTypeAndName(GENERIC_RESOURCE_NAMESPACES_NAME, GENERIC_RESOURCE_NAMESPACES_SECONDARY_TYPE); logger.trace("queryString: " +queryString); Query q = new QueryBox(queryString); DiscoveryClient client = client(); - List appProfile = client.submit(q); + List appProfile = client.submit(q); if (appProfile == null || appProfile.size() == 0) - throw new MetadataProfileNotFoundException("Your resourceID "+resourceID+" is not registered in the infrastructure, the scope is "+scopeString); + throw new MetadataProfileNotFoundException("Your resource is not registered in the infrastructure, the scope is "+scopeString); else { String theResource = null; try{ theResource = appProfile.get(0); -// logger.trace("Resource with resourceID "+resourceID+" matched "+theResource); - logger.debug("Resource (Namespaces Catalogue Categories) within resourceID "+resourceID+" found"); + logger.debug("Resource (Namespaces Catalogue Categories) found"); DocumentBuilder docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); Node node = docBuilder.parse(new InputSource(new StringReader(theResource))).getDocumentElement(); XPathHelper helper = new XPathHelper(node); -// List fields = getMetadataFieldsFromResource(helper); -// mf.setMetadataFields(fields); logger.debug("Unmarshalling it.."); readNamespaceCategoryFromResource(helper); }catch(Exception e){ @@ -112,10 +101,11 @@ public class NamespaceCategoryReader { } } catch (Exception e) { - logger.error("Error while trying to fetch resourceID "+resourceID+" from the infrastructure, the scope is "+scopeString,e); - throw new Exception("Error while trying to fetch resourceID "+resourceID+" from the infrastructure, the scope is "+scopeString, e); + logger.error("Error while trying to fetch resource with name " + GENERIC_RESOURCE_NAMESPACES_NAME + " and secondary type " + GENERIC_RESOURCE_NAMESPACES_SECONDARY_TYPE + + " from the infrastructure, the scope is "+scopeString, e); + throw new Exception("Error while trying to fetch resource with name " + GENERIC_RESOURCE_NAMESPACES_NAME + " and secondary type " + GENERIC_RESOURCE_NAMESPACES_SECONDARY_TYPE + + " from the infrastructure, the scope is "+scopeString, e); } finally{ -// ScopeProvider.instance.reset(); } } @@ -133,13 +123,13 @@ public class NamespaceCategoryReader { List namespaceSources = helper.evaluate("/Resource/Profile/Body/namespaces"); JAXBContext jaxbContext = JAXBContext.newInstance(NamespaceCategories.class); - Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller(); + Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller(); - if(namespaceSources==null || namespaceSources.size()==0){ - throw new Exception("Resource does not contain ... in the body"); - } + if(namespaceSources==null || namespaceSources.size()==0){ + throw new Exception("Resource does not contain ... in the body"); + } - InputStream stream = new ByteArrayInputStream(namespaceSources.get(0).getBytes()); + InputStream stream = new ByteArrayInputStream(namespaceSources.get(0).getBytes()); namespaces = (NamespaceCategories) jaxbUnmarshaller.unmarshal(stream); }catch(Exception e){ @@ -149,31 +139,6 @@ public class NamespaceCategoryReader { } } - - /** - * Namespaces catalogue categories resource id. - * - * @return the string - * @throws PropertyFileNotFoundException the property file not found exception - */ - public static String NamespacesCatalogueCategoriesResourceID() throws PropertyFileNotFoundException { - - Properties prop = new Properties(); - - try { - - InputStream in = NamespaceCategoryReader.class.getResourceAsStream(NAMESPACES_CATALOGUE_CATEGORIES_PROPERTIES); - // load a properties file - prop.load(in); - return prop.getProperty("RESOURCE_ID"); - - } catch (IOException e) { - logger.error("An error occurred on read property file "+NAMESPACES_CATALOGUE_CATEGORIES_PROPERTIES, e); - throw new PropertyFileNotFoundException("An error occurred on read property file "+NAMESPACES_CATALOGUE_CATEGORIES_PROPERTIES+": "+e); - } - } - - /** * Gets the namespaces. * @@ -193,8 +158,6 @@ public class NamespaceCategoryReader { StringBuilder builder = new StringBuilder(); builder.append("NamespaceCategoryReader [scope="); builder.append(scope); - builder.append(", resourceID="); - builder.append(resourceID); builder.append(", namespaces="); builder.append(namespaces); builder.append("]"); diff --git a/src/main/java/org/gcube/datacatalogue/metadatadiscovery/reader/NamespacesCatalogueCategories.properties b/src/main/java/org/gcube/datacatalogue/metadatadiscovery/reader/NamespacesCatalogueCategories.properties deleted file mode 100644 index 62ae88b..0000000 --- a/src/main/java/org/gcube/datacatalogue/metadatadiscovery/reader/NamespacesCatalogueCategories.properties +++ /dev/null @@ -1,10 +0,0 @@ -# Property files -# -# author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it -# created 04/2017 -# -# Generic resource id of "Namespaces Calogue Categories" -# -# - -RESOURCE_ID = cdc52c27-6d7b-4d55-b4cd-314d7cc2e224 \ No newline at end of file diff --git a/src/main/java/org/gcube/datacatalogue/metadatadiscovery/reader/QueryForResourceUtil.java b/src/main/java/org/gcube/datacatalogue/metadatadiscovery/reader/QueryForResourceUtil.java index 06d3c52..4b97f10 100644 --- a/src/main/java/org/gcube/datacatalogue/metadatadiscovery/reader/QueryForResourceUtil.java +++ b/src/main/java/org/gcube/datacatalogue/metadatadiscovery/reader/QueryForResourceUtil.java @@ -4,6 +4,7 @@ package org.gcube.datacatalogue.metadatadiscovery.reader; + /** * The Class QueryForResourceUtil. * @@ -22,12 +23,11 @@ public class QueryForResourceUtil { public static synchronized String queryForGenericResourceById(String resourceId){ return String.format("declare namespace ic = 'http://gcube-system.org/namespaces/informationsystem/registry'; " + - "for $resource in collection('/db/Profiles')//Document/Data/ic:Profile/Resource " + - "where ($resource/ID/text() eq '%s') return $resource", resourceId); + "for $resource in collection('/db/Profiles')//Document/Data/ic:Profile/Resource " + + "where ($resource/ID/text() eq '%s') return $resource", resourceId); } - /** * Gets the gcube generic query string for secondary type. * @@ -41,4 +41,18 @@ public class QueryForResourceUtil { " return $profile"; } + /** + * Gets the gcube generic query string for secondary type. + * @param name the resource name + * @param secondaryType the secondary type + * @return the gcube generic query string for secondary type + */ + public static String getGcubeGenericQueryStringForSecondaryTypeAndName(String name, String secondaryType){ + + return "for $profile in collection('/db/Profiles/GenericResource')//Resource " + + "where $profile/Profile/SecondaryType/string() eq '" + secondaryType + "' and $profile/Profile/Name/string() " + + " eq '" + name + "'" + + "return $profile"; + } + }