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
This commit is contained in:
Costantino Perciante 2017-08-03 10:35:38 +00:00
parent 3fd17d33e7
commit 631cd86682
6 changed files with 45 additions and 73 deletions

View File

@ -1,4 +1,9 @@
<ReleaseNotes> <ReleaseNotes>
<Changeset
component="org.gcube.data-catalogue.gcubedatacatalogue-metadata-discovery.3-2-0"
date="2017-04-26">
<Change>Namespaces are retrieved per scope</Change>
</Changeset>
<Changeset <Changeset
component="org.gcube.data-catalogue.gcubedatacatalogue-metadata-discovery.3-1-0" component="org.gcube.data-catalogue.gcubedatacatalogue-metadata-discovery.3-1-0"
date="2017-04-26"> date="2017-04-26">

View File

@ -13,7 +13,7 @@
<groupId>org.gcube.data-catalogue</groupId> <groupId>org.gcube.data-catalogue</groupId>
<artifactId>gcubedatacatalogue-metadata-discovery</artifactId> <artifactId>gcubedatacatalogue-metadata-discovery</artifactId>
<packaging>jar</packaging> <packaging>jar</packaging>
<version>3.1.0-SNAPSHOT</version> <version>3.2.0-SNAPSHOT</version>
<name>The gCube data catalogue metadata discovery library</name> <name>The gCube data catalogue metadata discovery library</name>
<description>The gCube data catalogue metadata discovery library</description> <description>The gCube data catalogue metadata discovery library</description>
<scm> <scm>

View File

@ -85,7 +85,7 @@ public class DataCalogueMetadataFormatReader implements DataCatalogueMetadataDis
if(namespaceCategories == null) if(namespaceCategories == null)
namespaceCategories = new ArrayList<NamespaceCategory>(); namespaceCategories = new ArrayList<NamespaceCategory>();
NamespaceCategoryReader rd = new NamespaceCategoryReader(scope, NamespaceCategoryReader.NamespacesCatalogueCategoriesResourceID()); NamespaceCategoryReader rd = new NamespaceCategoryReader(scope);
namespaceCategories.addAll(rd.getNamespaces().getNamespaceCategories()); namespaceCategories.addAll(rd.getNamespaces().getNamespaceCategories());
} }
}catch(Exception e){ }catch(Exception e){

View File

@ -6,11 +6,9 @@ package org.gcube.datacatalogue.metadatadiscovery.reader;
import static org.gcube.resources.discovery.icclient.ICFactory.client; import static org.gcube.resources.discovery.icclient.ICFactory.client;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.StringReader; import java.io.StringReader;
import java.util.List; import java.util.List;
import java.util.Properties;
import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller; import javax.xml.bind.Unmarshaller;
@ -37,13 +35,12 @@ import org.xml.sax.InputSource;
*/ */
public class NamespaceCategoryReader { public class NamespaceCategoryReader {
/**
*
*/
public static final String NAMESPACES_CATALOGUE_CATEGORIES_PROPERTIES = "NamespacesCatalogueCategories.properties";
private static Logger logger = LoggerFactory.getLogger(NamespaceCategoryReader.class); 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 ScopeBean scope;
private String resourceID;
private NamespaceCategories namespaces; private NamespaceCategories namespaces;
@ -51,16 +48,11 @@ public class NamespaceCategoryReader {
* Instantiates a new medata format reader. * Instantiates a new medata format reader.
* *
* @param scope the scope * @param scope the scope
* @param resourceID the resource id
* @throws Exception the exception * @throws Exception the exception
*/ */
public NamespaceCategoryReader(ScopeBean scope, String resourceID) throws Exception { public NamespaceCategoryReader(ScopeBean scope) throws Exception {
this.scope = scope; this.scope = scope;
this.resourceID = resourceID; readNamespaceCategory();
if(resourceID == null || resourceID.isEmpty())
throw new Exception("Please, specify a valid ResourceID in the property file: "+NAMESPACES_CATALOGUE_CATEGORIES_PROPERTIES);
readNamespaceCategoryByResourceID();
} }
/** /**
@ -69,8 +61,8 @@ public class NamespaceCategoryReader {
* @return the metadata format by id * @return the metadata format by id
* @throws Exception the exception * @throws Exception the exception
*/ */
private void readNamespaceCategoryByResourceID() throws Exception { private void readNamespaceCategory() throws Exception {
logger.trace("Getting Namespace Category for resourceID: "+resourceID); logger.trace("Getting Namespace Category");
if(this.scope==null) if(this.scope==null)
throw new Exception("Scope is null"); throw new Exception("Scope is null");
@ -84,26 +76,23 @@ public class NamespaceCategoryReader {
logger.info("Using scope from ScopeProvider: "+scopeString); 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); logger.trace("queryString: " +queryString);
Query q = new QueryBox(queryString); Query q = new QueryBox(queryString);
DiscoveryClient<String> client = client(); DiscoveryClient<String> client = client();
List<String> appProfile = client.submit(q); List<String> appProfile = client.submit(q);
if (appProfile == null || appProfile.size() == 0) 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 { else {
String theResource = null; String theResource = null;
try{ try{
theResource = appProfile.get(0); theResource = appProfile.get(0);
// logger.trace("Resource with resourceID "+resourceID+" matched "+theResource); logger.debug("Resource (Namespaces Catalogue Categories) found");
logger.debug("Resource (Namespaces Catalogue Categories) within resourceID "+resourceID+" found");
DocumentBuilder docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); DocumentBuilder docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Node node = docBuilder.parse(new InputSource(new StringReader(theResource))).getDocumentElement(); Node node = docBuilder.parse(new InputSource(new StringReader(theResource))).getDocumentElement();
XPathHelper helper = new XPathHelper(node); XPathHelper helper = new XPathHelper(node);
// List<MetadataField> fields = getMetadataFieldsFromResource(helper);
// mf.setMetadataFields(fields);
logger.debug("Unmarshalling it.."); logger.debug("Unmarshalling it..");
readNamespaceCategoryFromResource(helper); readNamespaceCategoryFromResource(helper);
}catch(Exception e){ }catch(Exception e){
@ -112,10 +101,11 @@ public class NamespaceCategoryReader {
} }
} catch (Exception e) { } catch (Exception e) {
logger.error("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 +
throw new Exception("Error while trying to fetch resourceID "+resourceID+" from the infrastructure, the scope is "+scopeString, e); " 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{ } finally{
// ScopeProvider.instance.reset();
} }
} }
@ -133,13 +123,13 @@ public class NamespaceCategoryReader {
List<String> namespaceSources = helper.evaluate("/Resource/Profile/Body/namespaces"); List<String> namespaceSources = helper.evaluate("/Resource/Profile/Body/namespaces");
JAXBContext jaxbContext = JAXBContext.newInstance(NamespaceCategories.class); JAXBContext jaxbContext = JAXBContext.newInstance(NamespaceCategories.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller(); Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
if(namespaceSources==null || namespaceSources.size()==0){ if(namespaceSources==null || namespaceSources.size()==0){
throw new Exception("Resource does not contain <namespaces> <namespace>...</namespace> </namespaces> in the body"); throw new Exception("Resource does not contain <namespaces> <namespace>...</namespace> </namespaces> in the body");
} }
InputStream stream = new ByteArrayInputStream(namespaceSources.get(0).getBytes()); InputStream stream = new ByteArrayInputStream(namespaceSources.get(0).getBytes());
namespaces = (NamespaceCategories) jaxbUnmarshaller.unmarshal(stream); namespaces = (NamespaceCategories) jaxbUnmarshaller.unmarshal(stream);
}catch(Exception e){ }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. * Gets the namespaces.
* *
@ -193,8 +158,6 @@ public class NamespaceCategoryReader {
StringBuilder builder = new StringBuilder(); StringBuilder builder = new StringBuilder();
builder.append("NamespaceCategoryReader [scope="); builder.append("NamespaceCategoryReader [scope=");
builder.append(scope); builder.append(scope);
builder.append(", resourceID=");
builder.append(resourceID);
builder.append(", namespaces="); builder.append(", namespaces=");
builder.append(namespaces); builder.append(namespaces);
builder.append("]"); builder.append("]");

View File

@ -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

View File

@ -4,6 +4,7 @@
package org.gcube.datacatalogue.metadatadiscovery.reader; package org.gcube.datacatalogue.metadatadiscovery.reader;
/** /**
* The Class QueryForResourceUtil. * The Class QueryForResourceUtil.
* *
@ -22,12 +23,11 @@ public class QueryForResourceUtil {
public static synchronized String queryForGenericResourceById(String resourceId){ public static synchronized String queryForGenericResourceById(String resourceId){
return String.format("declare namespace ic = 'http://gcube-system.org/namespaces/informationsystem/registry'; " + return String.format("declare namespace ic = 'http://gcube-system.org/namespaces/informationsystem/registry'; " +
"for $resource in collection('/db/Profiles')//Document/Data/ic:Profile/Resource " + "for $resource in collection('/db/Profiles')//Document/Data/ic:Profile/Resource " +
"where ($resource/ID/text() eq '%s') return $resource", resourceId); "where ($resource/ID/text() eq '%s') return $resource", resourceId);
} }
/** /**
* Gets the gcube generic query string for secondary type. * Gets the gcube generic query string for secondary type.
* *
@ -41,4 +41,18 @@ public class QueryForResourceUtil {
" return $profile"; " 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";
}
} }