diff --git a/NamespacesCatalogueCategories.xml b/NamespacesCatalogueCategories.xml new file mode 100644 index 0000000..ef47f33 --- /dev/null +++ b/NamespacesCatalogueCategories.xml @@ -0,0 +1,19 @@ + + + + Contact + Contact Title + This section is about Contact(s) + + + Developer + Developer Information + This section is about Developer(s) + + + Extra Information + Extras + This section is about Extra(s) + + diff --git a/NamespacesCatalogueCategories.xsd b/NamespacesCatalogueCategories.xsd new file mode 100644 index 0000000..354b315 --- /dev/null +++ b/NamespacesCatalogueCategories.xsd @@ -0,0 +1,19 @@ + + + + + + + + + + + + + + + + + + + diff --git a/distro/changelog.xml b/distro/changelog.xml index 6d5cf0a..a829c21 100644 --- a/distro/changelog.xml +++ b/distro/changelog.xml @@ -1,7 +1,9 @@ + date="2017-04-26"> + [Task #8267] Medatada Discovery Library: definition of namespaces and metadata fields must be separated + [Task #7616] Porting library to Metadata Profile model v3.0 diff --git a/src/main/java/org/gcube/datacatalogue/metadatadiscovery/DataCalogueMetadataFormatReader.java b/src/main/java/org/gcube/datacatalogue/metadatadiscovery/DataCalogueMetadataFormatReader.java index 2350738..e567e6e 100644 --- a/src/main/java/org/gcube/datacatalogue/metadatadiscovery/DataCalogueMetadataFormatReader.java +++ b/src/main/java/org/gcube/datacatalogue/metadatadiscovery/DataCalogueMetadataFormatReader.java @@ -1,5 +1,6 @@ package org.gcube.datacatalogue.metadatadiscovery; +import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -8,8 +9,10 @@ import org.gcube.common.scope.api.ScopeProvider; import org.gcube.common.scope.impl.ScopeBean; import org.gcube.datacatalogue.metadatadiscovery.bean.MetadataProfile; import org.gcube.datacatalogue.metadatadiscovery.bean.jaxb.MetadataFormat; +import org.gcube.datacatalogue.metadatadiscovery.bean.jaxb.NamespaceCategory; import org.gcube.datacatalogue.metadatadiscovery.reader.MetadataFormatDiscovery; import org.gcube.datacatalogue.metadatadiscovery.reader.MetadataFormatReader; +import org.gcube.datacatalogue.metadatadiscovery.reader.NamespaceCategoryReader; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -24,8 +27,9 @@ import org.slf4j.LoggerFactory; public class DataCalogueMetadataFormatReader implements DataCatalogueMetadataDiscovery{ private MetadataFormatDiscovery medataFormatDiscovery; - private final ScopeBean scope; - private Map hash = new HashMap(); + private ScopeBean scope; + private Map hashMetadataFormats = new HashMap(); + private List namespaceCategories = new ArrayList(); private static Logger logger = LoggerFactory.getLogger(DataCalogueMetadataFormatReader.class); @@ -37,12 +41,24 @@ public class DataCalogueMetadataFormatReader implements DataCatalogueMetadataDis public DataCalogueMetadataFormatReader() throws Exception { String scopeString = ScopeProvider.instance.get(); - logger.debug("DataCalogueMetadataFormatReader read scope "+scopeString +" from ScopeProvider"); + logger.debug("Read scope "+scopeString +" from ScopeProvider"); if(scopeString==null || scopeString.isEmpty()) throw new Exception("Please set a valid scope into ScopeProvider"); scope = new ScopeBean(scopeString); + //readMetadaFormats(); + readNamespaces(); + } + + + /** + * Read metada formats. + * + * @throws Exception the exception + */ + private void readMetadaFormats() throws Exception{ + medataFormatDiscovery = new MetadataFormatDiscovery(scope); logger.info("MedataFormatDiscovery has retrieved: "+medataFormatDiscovery.getMetadataProfiles().size() +" metadata type/s"); logger.debug("filling cache for MedataFormat"); @@ -51,18 +67,25 @@ public class DataCalogueMetadataFormatReader implements DataCatalogueMetadataDis continue; MetadataFormatReader reader = new MetadataFormatReader(scope, mT.getId()); - hash.put(mT.getId(), reader.getMetadataFormat()); + hashMetadataFormats.put(mT.getId(), reader.getMetadataFormat()); logger.debug("MetadataType id: "+mT.getId() +" cached as: "+reader.getMetadataFormat()); } } - /* (non-Javadoc) - * @see org.gcube.datacatalogue.metadatadiscovery.DataCatalogueMetadataDiscovery#getListOfMetadataTypes() - */ - @Override - public List getListOfMetadataProfiles() throws Exception { - return medataFormatDiscovery.getMetadataProfiles(); + /** + * Read namespaces. + */ + private void readNamespaces() { + + try{ + if(namespaceCategories == null || namespaceCategories.isEmpty()){ + NamespaceCategoryReader rd = new NamespaceCategoryReader(scope, NamespaceCategoryReader.NamespacesCatalogueCategoriesResourceID()); + namespaceCategories.addAll(rd.getNamespaces().getNamespaceCategories()); + } + }catch(Exception e){ + logger.debug("An error occurred during read namespaces for categories: ", e); + } } /* (non-Javadoc) @@ -74,7 +97,7 @@ public class DataCalogueMetadataFormatReader implements DataCatalogueMetadataDis if(profile==null) throw new Exception("Input "+ MetadataProfile.class.getSimpleName() + " is null"); - MetadataFormat format = hash.get(profile.getId()); + MetadataFormat format = hashMetadataFormats.get(profile.getId()); if(format!=null) return format; @@ -82,4 +105,28 @@ public class DataCalogueMetadataFormatReader implements DataCatalogueMetadataDis return reader.getMetadataFormat(); } + /* (non-Javadoc) + * @see org.gcube.datacatalogue.metadatadiscovery.DataCatalogueMetadataDiscovery#getListOfMetadataTypes() + */ + @Override + public List getListOfMetadataProfiles() throws Exception { + + if(medataFormatDiscovery==null) + readMetadaFormats(); + + return medataFormatDiscovery.getMetadataProfiles(); + } + + /* (non-Javadoc) + * @see org.gcube.datacatalogue.metadatadiscovery.DataCatalogueMetadataDiscovery#getListOfNamespaceCategories() + */ + @Override + public List getListOfNamespaceCategories() throws Exception { + + if(namespaceCategories == null) + readNamespaces(); + + return namespaceCategories; + } + } diff --git a/src/main/java/org/gcube/datacatalogue/metadatadiscovery/DataCatalogueMetadataDiscovery.java b/src/main/java/org/gcube/datacatalogue/metadatadiscovery/DataCatalogueMetadataDiscovery.java index 492852c..908f7c5 100644 --- a/src/main/java/org/gcube/datacatalogue/metadatadiscovery/DataCatalogueMetadataDiscovery.java +++ b/src/main/java/org/gcube/datacatalogue/metadatadiscovery/DataCatalogueMetadataDiscovery.java @@ -7,6 +7,7 @@ import java.util.List; import org.gcube.datacatalogue.metadatadiscovery.bean.MetadataProfile; import org.gcube.datacatalogue.metadatadiscovery.bean.jaxb.MetadataFormat; +import org.gcube.datacatalogue.metadatadiscovery.bean.jaxb.NamespaceCategory; @@ -29,6 +30,16 @@ public interface DataCatalogueMetadataDiscovery { List getListOfMetadataProfiles() throws Exception; + + /** + * Gets the list of namespace categories. + * + * @return the list of namespace categories + * @throws Exception the exception + */ + List getListOfNamespaceCategories() throws Exception; + + /** * Gets the metadata format for metadata type. * diff --git a/src/main/java/org/gcube/datacatalogue/metadatadiscovery/MetadataNamespace.java b/src/main/java/org/gcube/datacatalogue/metadatadiscovery/Namespace.java similarity index 50% rename from src/main/java/org/gcube/datacatalogue/metadatadiscovery/MetadataNamespace.java rename to src/main/java/org/gcube/datacatalogue/metadatadiscovery/Namespace.java index 998c818..ca7b439 100644 --- a/src/main/java/org/gcube/datacatalogue/metadatadiscovery/MetadataNamespace.java +++ b/src/main/java/org/gcube/datacatalogue/metadatadiscovery/Namespace.java @@ -3,13 +3,17 @@ */ package org.gcube.datacatalogue.metadatadiscovery; - /** + * The Interface Namespace. * * @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it - * Mar 23, 2017 + * Apr 26, 2017 */ -public interface MetadataNamespace { +public interface Namespace { public static final String Separator = ":"; + public String id = null; + public String name = null; + public String title = null; + public String description = ""; } diff --git a/src/main/java/org/gcube/datacatalogue/metadatadiscovery/bean/jaxb/MetadataField.java b/src/main/java/org/gcube/datacatalogue/metadatadiscovery/bean/jaxb/MetadataField.java index 7d6d982..73e6ec0 100644 --- a/src/main/java/org/gcube/datacatalogue/metadatadiscovery/bean/jaxb/MetadataField.java +++ b/src/main/java/org/gcube/datacatalogue/metadatadiscovery/bean/jaxb/MetadataField.java @@ -13,7 +13,7 @@ import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlTransient; import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; -import org.gcube.datacatalogue.metadatadiscovery.MetadataNamespace; +import org.gcube.datacatalogue.metadatadiscovery.Namespace; import org.gcube.datacatalogue.metadatadiscovery.adapter.DataTypeAdapter; @@ -115,7 +115,7 @@ public class MetadataField implements Serializable{ */ private void setNamespaceFieldToCategoryRef() { - this.namespaceFieldToCategoryReference = LOCAL_NAME_CATEGORYREF+MetadataNamespace.Separator+categoryRef; + this.namespaceFieldToCategoryReference = LOCAL_NAME_CATEGORYREF+Namespace.Separator+categoryRef; } @@ -146,7 +146,7 @@ public class MetadataField implements Serializable{ /** * Gets the category field qualified name. * - * If the Metadata Field belongs to a category returns the qualified name: {@link MetadataField#LOCAL_NAME_CATEGORYREF} {@link MetadataNamespace#Separator}} fieldName; + * If the Metadata Field belongs to a category returns the qualified name: {@link MetadataField#LOCAL_NAME_CATEGORYREF} {@link Namespace#Separator}} fieldName; * Otherwise returns the fieldName * * @return the category q name @@ -154,7 +154,7 @@ public class MetadataField implements Serializable{ public String getCategoryFieldQName(){ initNamespaceFieldToCategoryReference(); - return namespaceFieldToCategoryReference==null?fieldName:namespaceFieldToCategoryReference+MetadataNamespace.Separator+fieldName; + return namespaceFieldToCategoryReference==null?fieldName:namespaceFieldToCategoryReference+Namespace.Separator+fieldName; } diff --git a/src/main/java/org/gcube/datacatalogue/metadatadiscovery/bean/jaxb/MetadataFormat.java b/src/main/java/org/gcube/datacatalogue/metadatadiscovery/bean/jaxb/MetadataFormat.java index 2b9c1ca..2d79caf 100644 --- a/src/main/java/org/gcube/datacatalogue/metadatadiscovery/bean/jaxb/MetadataFormat.java +++ b/src/main/java/org/gcube/datacatalogue/metadatadiscovery/bean/jaxb/MetadataFormat.java @@ -31,25 +31,22 @@ public class MetadataFormat implements Serializable{ */ private static final long serialVersionUID = -5881074882343424963L; - public static final String LOCAL_NAME_METADATA_TYPE = "metadatatype"; + public static final String LOCAL_NAME_METADATA_TYPE = "type"; public static final String LOCAL_NAME_METADATA_FORMAT = "metadataformat"; + /** * Instantiates a new metadata format. */ public MetadataFormat() { } - @XmlElement(name = "metadatacategory") - private List metadataCategories; - - @XmlElement(name = "metadatafield") private List metadataFields; @XmlAttribute(name = LOCAL_NAME_METADATA_TYPE, required=true) - private String metadataType = ""; + private String type = ""; @XmlTransient private String metadataSource = null; @@ -65,25 +62,27 @@ public class MetadataFormat implements Serializable{ return metadataFields; } - /** - * Gets the metadata type. - * - * @return the metadataType - */ - public String getMetadataType() { - return metadataType; + + /** + * Gets the type. + * + * @return the type + */ + public String getType() { + + return type; } /** - * Gets the metadata categories. + * Sets the type. * - * @return the metadataCategories + * @param type the type to set */ - public List getMetadataCategories() { + public void setType(String type) { - return metadataCategories; + this.type = type; } /** @@ -122,18 +121,6 @@ public class MetadataFormat implements Serializable{ - /** - * Adds the category. - * - * @param metadataCategory the metadata category - */ - public void addCategory(MetadataCategory metadataCategory){ - if(this.metadataCategories==null) - this.metadataCategories = new ArrayList(); - - this.metadataCategories.add(metadataCategory); - } - /* (non-Javadoc) * @see java.lang.Object#toString() */ @@ -141,12 +128,12 @@ public class MetadataFormat implements Serializable{ public String toString() { StringBuilder builder = new StringBuilder(); - builder.append("MetadataFormat [metadataCategories="); - builder.append(metadataCategories); - builder.append(", metadataFields="); + builder.append("MetadataFormat [metadataFields="); builder.append(metadataFields); - builder.append(", metadataType="); - builder.append(metadataType); + builder.append(", type="); + builder.append(type); + builder.append(", metadataSource="); + builder.append(metadataSource); builder.append("]"); return builder.toString(); } diff --git a/src/main/java/org/gcube/datacatalogue/metadatadiscovery/bean/jaxb/NamespaceCategories.java b/src/main/java/org/gcube/datacatalogue/metadatadiscovery/bean/jaxb/NamespaceCategories.java new file mode 100644 index 0000000..8291ca3 --- /dev/null +++ b/src/main/java/org/gcube/datacatalogue/metadatadiscovery/bean/jaxb/NamespaceCategories.java @@ -0,0 +1,80 @@ +/** + * + */ +package org.gcube.datacatalogue.metadatadiscovery.bean.jaxb; + +import java.util.List; + +import javax.xml.bind.annotation.XmlAccessType; +import javax.xml.bind.annotation.XmlAccessorType; +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlRootElement; + + + +/** + * The Class NamespaceCategories. + * + * @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it + * Apr 26, 2017 + */ +@XmlRootElement(name="namespaces") +@XmlAccessorType(XmlAccessType.FIELD) +public class NamespaceCategories { + + @XmlElement(name = "namespace") + private List namespaceCategories; + + /** + * Instantiates a new namespace categories. + */ + public NamespaceCategories() { + + } + + /** + * @param namespaceCategories + */ + public NamespaceCategories(List namespaceCategories) { + this.namespaceCategories = namespaceCategories; + } + + + + + /** + * @return the namespaceCategories + */ + public List getNamespaceCategories() { + + return namespaceCategories; + } + + + + + /** + * @param namespaceCategories the namespaceCategories to set + */ + public void setNamespaceCategories(List namespaceCategories) { + + this.namespaceCategories = namespaceCategories; + } + + + + /* (non-Javadoc) + * @see java.lang.Object#toString() + */ + @Override + public String toString() { + + StringBuilder builder = new StringBuilder(); + builder.append("NamespaceCategories [namespaceCategories="); + builder.append(namespaceCategories); + builder.append("]"); + return builder.toString(); + } + + +} diff --git a/src/main/java/org/gcube/datacatalogue/metadatadiscovery/bean/jaxb/MetadataCategory.java b/src/main/java/org/gcube/datacatalogue/metadatadiscovery/bean/jaxb/NamespaceCategory.java similarity index 56% rename from src/main/java/org/gcube/datacatalogue/metadatadiscovery/bean/jaxb/MetadataCategory.java rename to src/main/java/org/gcube/datacatalogue/metadatadiscovery/bean/jaxb/NamespaceCategory.java index b0fa25f..905dc3c 100644 --- a/src/main/java/org/gcube/datacatalogue/metadatadiscovery/bean/jaxb/MetadataCategory.java +++ b/src/main/java/org/gcube/datacatalogue/metadatadiscovery/bean/jaxb/NamespaceCategory.java @@ -10,73 +10,72 @@ import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlAttribute; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; -import javax.xml.bind.annotation.XmlTransient; -import org.gcube.datacatalogue.metadatadiscovery.MetadataNamespace; +import org.gcube.datacatalogue.metadatadiscovery.Namespace; /** - * The Class MetadataCategory. + * The Class Namespace. * * @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it - * Mar 21, 2017 + * Apr 26, 2017 */ -@XmlRootElement(name = "metadatacategory") +@XmlRootElement(name = "namespace") @XmlAccessorType (XmlAccessType.FIELD) -public class MetadataCategory implements Serializable{ +public class NamespaceCategory implements Namespace, Serializable{ - /** - * - */ - private static final String NAME_ELEMENT_METADATACATEGORY = "metadatacategory"; - /** - * - */ - private static final long serialVersionUID = -7282676787740920115L; + /** The Constant serialVersionUID. */ + private static final long serialVersionUID = -1632300992065668875L; @XmlAttribute(required = true) private String id = null; @XmlElement(required = true) - private String title = ""; + private String name = null; - private String description = null; + @XmlElement(required = true) + private String title = null; - @XmlTransient - private String namespaceCategory = NAME_ELEMENT_METADATACATEGORY; + private String description = ""; /** - * Instantiates a new metadata category. + * Instantiates a new namespace category. */ - public MetadataCategory() { + public NamespaceCategory() { } - /** - * Instantiates a new metadata category. - * - * @param id the id - * @param title the title - */ - public MetadataCategory(String id, String title) { - - super(); - setId(id); - this.title = title; - } /** - * Instantiates a new metadata category. + * Instantiates a new namespace category. * * @param id the id + * @param name the name * @param title the title - * @param description the description */ - public MetadataCategory(String id, String title, String description) { + public NamespaceCategory(String id, String name, String title) { super(); this.id = id; + this.name = name; + this.title = title; + } + + + /** + * Instantiates a new namespace category. + * + * @param id the id + * @param name the name + * @param title the title + * @param description the description + */ + public NamespaceCategory(String id, String name, String title, String description) { + + super(); + this.id = id; + this.name = name; this.title = title; this.description = description; } @@ -126,14 +125,12 @@ public class MetadataCategory implements Serializable{ /** - * Gets the category qualifier name. The QName is a unique name to identify the category. - * It is the identifier: {@link MetadataCategory#namespaceCategory} + {@link MetadataNamespace#Separator} + {@link MetadataCategory#id} - * - * @return the category q name + * Gets the namespace category qualifier name. The QName is a unique name to identify the category. It corresponds to {@link NamespaceCategory#id} + * @return the namespace category q name */ - public String getCategoryQName(){ + public String getNamespaceCategoryQName(){ - return namespaceCategory+MetadataNamespace.Separator+id; + return id; } @@ -159,16 +156,6 @@ public class MetadataCategory implements Serializable{ } - /** - * Gets the namespace category. - * - * @return the namespace category - */ - public String getNamespaceCategory() { - - return namespaceCategory; - } - /* (non-Javadoc) * @see java.lang.Object#toString() @@ -177,18 +164,16 @@ public class MetadataCategory implements Serializable{ public String toString() { StringBuilder builder = new StringBuilder(); - builder.append("MetadataCategory [id="); + builder.append("NamespaceCategory [id="); builder.append(id); + builder.append(", name="); + builder.append(name); builder.append(", title="); builder.append(title); builder.append(", description="); builder.append(description); - builder.append(", namespaceCategory="); - builder.append(namespaceCategory); builder.append("]"); return builder.toString(); } - - } diff --git a/src/main/java/org/gcube/datacatalogue/metadatadiscovery/reader/MetadataFormatDiscovery.java b/src/main/java/org/gcube/datacatalogue/metadatadiscovery/reader/MetadataFormatDiscovery.java index a06c9bc..db6dca5 100644 --- a/src/main/java/org/gcube/datacatalogue/metadatadiscovery/reader/MetadataFormatDiscovery.java +++ b/src/main/java/org/gcube/datacatalogue/metadatadiscovery/reader/MetadataFormatDiscovery.java @@ -78,7 +78,7 @@ public class MetadataFormatDiscovery { // ScopeProvider.instance.set(scopeString); logger.info("Using scope from ScopeProvider: "+scopeString); - String queryString = getGcubeGenericQueryString(secondaryType); + String queryString = QueryForResourceUtil.getGcubeGenericQueryStringForSecondaryType(secondaryType); logger.trace("queryString: " +queryString); Query q = new QueryBox(queryString); @@ -121,11 +121,9 @@ public class MetadataFormatDiscovery { */ private MetadataProfile getMetadataProfileFromResource(Document doc, XPathHelper helper) throws MetadataProfileNotFoundException{ - try { MetadataProfile metadataProfile = new MetadataProfile(); - List id = helper.evaluate("/Resource/ID/text()"); if(id==null || id.isEmpty()) @@ -167,19 +165,6 @@ public class MetadataFormatDiscovery { } - /** - * Gets the gcube generic query string. - * - * @param secondaryType the secondary type - * @return the gcube generic query string - */ - public synchronized String getGcubeGenericQueryString(String secondaryType){ - - return "for $profile in collection('/db/Profiles/GenericResource')//Resource" + - " where $profile/Profile/SecondaryType/string() eq '"+secondaryType+"'" + - " return $profile"; - } - /** * Gets the secondary type. * diff --git a/src/main/java/org/gcube/datacatalogue/metadatadiscovery/reader/MetadataFormatReader.java b/src/main/java/org/gcube/datacatalogue/metadatadiscovery/reader/MetadataFormatReader.java index f679b4a..e84412b 100644 --- a/src/main/java/org/gcube/datacatalogue/metadatadiscovery/reader/MetadataFormatReader.java +++ b/src/main/java/org/gcube/datacatalogue/metadatadiscovery/reader/MetadataFormatReader.java @@ -49,7 +49,7 @@ public class MetadataFormatReader { public MetadataFormatReader(ScopeBean scope, String resourceID) throws Exception { this.scope = scope; this.resourceID = resourceID; - this.metadataFormat = getMetadataFormatByID(resourceID); + this.metadataFormat = getMetadataFormatByResourceID(resourceID); } @@ -61,8 +61,8 @@ public class MetadataFormatReader { * @return the metadata format by id * @throws Exception the exception */ - private MetadataFormat getMetadataFormatByID(String resourceID) throws Exception { - logger.trace("Getting MedataFormat with resourceID: "+resourceID); + private MetadataFormat getMetadataFormatByResourceID(String resourceID) throws Exception { + logger.trace("Getting MedataFormat for resourceID: "+resourceID); if(this.scope==null) throw new Exception("Scope is null"); @@ -76,7 +76,7 @@ public class MetadataFormatReader { logger.info("Using scope from ScopeProvider: "+scopeString); - String queryString = getQuesryStringForGenericResourceById(resourceID); + String queryString = QueryForResourceUtil.queryForGenericResourceById(resourceID); logger.trace("queryString: " +queryString); Query q = new QueryBox(queryString); @@ -163,7 +163,7 @@ public class MetadataFormatReader { InputStream stream = new ByteArrayInputStream(metadataFormatSource.get(0).getBytes()); MetadataFormat mtdf = (MetadataFormat) jaxbUnmarshaller.unmarshal(stream); - logger.debug("returning metadata format with medata type: "+mtdf.getMetadataType()); + logger.debug("returning metadata format with medata type: "+mtdf.getType()); return mtdf; @@ -221,18 +221,6 @@ public class MetadataFormatReader { } - /** - * Gets the quesry string for generic resource by id. - * - * @param resourceId the resource id - * @return the quesry string for generic resource by id - */ - public synchronized String getQuesryStringForGenericResourceById(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); - } /** diff --git a/src/main/java/org/gcube/datacatalogue/metadatadiscovery/reader/NamespaceCategoryReader.java b/src/main/java/org/gcube/datacatalogue/metadatadiscovery/reader/NamespaceCategoryReader.java new file mode 100644 index 0000000..479a3e5 --- /dev/null +++ b/src/main/java/org/gcube/datacatalogue/metadatadiscovery/reader/NamespaceCategoryReader.java @@ -0,0 +1,204 @@ +/** + * + */ +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; +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; + +import org.gcube.common.resources.gcore.utils.XPathHelper; +import org.gcube.common.scope.impl.ScopeBean; +import org.gcube.datacatalogue.metadatadiscovery.bean.jaxb.NamespaceCategories; +import org.gcube.resources.discovery.client.api.DiscoveryClient; +import org.gcube.resources.discovery.client.queries.api.Query; +import org.gcube.resources.discovery.client.queries.impl.QueryBox; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.w3c.dom.Node; +import org.xml.sax.InputSource; + + +/** + * The Class NamespaceCategoryReader. + * + * @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it + * Apr 26, 2017 + */ +public class NamespaceCategoryReader { + + /** + * + */ + public static final String NAMESPACES_CATALOGUE_CATEGORIES_PROPERTIES = "NamespacesCatalogueCategories.properties"; + private static Logger logger = LoggerFactory.getLogger(NamespaceCategoryReader.class); + private ScopeBean scope; + private String resourceID; + + private NamespaceCategories namespaces; + + /** + * 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 { + 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(); + } + + /** + * Gets the metadata format by id. + * + * @return the metadata format by id + * @throws Exception the exception + */ + private void readNamespaceCategoryByResourceID() throws Exception { + logger.trace("Getting Namespace Category for resourceID: "+resourceID); + + if(this.scope==null) + throw new Exception("Scope is null"); + + String scopeString = this.scope.toString(); + logger.trace("read scope: "+scopeString); + + namespaces = new NamespaceCategories(); + + try { + + logger.info("Using scope from ScopeProvider: "+scopeString); + + String queryString = QueryForResourceUtil.queryForGenericResourceById(resourceID); + logger.trace("queryString: " +queryString); + Query q = new QueryBox(queryString); + + DiscoveryClient client = client(); + 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); + 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"); + 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){ + logger.error("Error while parsing Resource "+theResource+" from the infrastructure, the scope is "+scopeString,e); + } + } + + } 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); + } finally{ +// ScopeProvider.instance.reset(); + } + } + + + /** + * Read namespace category from resource. + * + * @param helper the helper + * @throws Exception the exception + */ + private void readNamespaceCategoryFromResource(XPathHelper helper) throws Exception{ + + try { + + List namespaceSources = helper.evaluate("/Resource/Profile/Body/namespaces"); + + JAXBContext jaxbContext = JAXBContext.newInstance(NamespaceCategories.class); + Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller(); + + if(namespaceSources==null || namespaceSources.size()==0){ + throw new Exception("Resource does not contain ... in the body"); + } + + InputStream stream = new ByteArrayInputStream(namespaceSources.get(0).getBytes()); + namespaces = (NamespaceCategories) jaxbUnmarshaller.unmarshal(stream); + + }catch(Exception e){ + String error = "An error occurred in readNamespaceCategoryFromResource " + e.getMessage(); + logger.error("An error occurred in readNamespaceCategoryFromResource ", e); + throw new Exception(error); + } + } + + + /** + * 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. + * + * @return the namespaces + */ + public NamespaceCategories getNamespaces() { + + return namespaces; + } + + /* (non-Javadoc) + * @see java.lang.Object#toString() + */ + @Override + public String toString() { + + 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("]"); + return builder.toString(); + } + +} diff --git a/src/main/java/org/gcube/datacatalogue/metadatadiscovery/reader/NamespacesCatalogueCategories.properties b/src/main/java/org/gcube/datacatalogue/metadatadiscovery/reader/NamespacesCatalogueCategories.properties new file mode 100644 index 0000000..62ae88b --- /dev/null +++ b/src/main/java/org/gcube/datacatalogue/metadatadiscovery/reader/NamespacesCatalogueCategories.properties @@ -0,0 +1,10 @@ +# 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/PropertyFileNotFoundException.java b/src/main/java/org/gcube/datacatalogue/metadatadiscovery/reader/PropertyFileNotFoundException.java new file mode 100644 index 0000000..3675a2e --- /dev/null +++ b/src/main/java/org/gcube/datacatalogue/metadatadiscovery/reader/PropertyFileNotFoundException.java @@ -0,0 +1,8 @@ +package org.gcube.datacatalogue.metadatadiscovery.reader; + +@SuppressWarnings("serial") +public class PropertyFileNotFoundException extends Exception { + public PropertyFileNotFoundException(String message) { + super(message); + } +} \ 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 new file mode 100644 index 0000000..06d3c52 --- /dev/null +++ b/src/main/java/org/gcube/datacatalogue/metadatadiscovery/reader/QueryForResourceUtil.java @@ -0,0 +1,44 @@ +/** + * + */ +package org.gcube.datacatalogue.metadatadiscovery.reader; + + +/** + * The Class QueryForResourceUtil. + * + * @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it + * Apr 26, 2017 + */ +public class QueryForResourceUtil { + + + /** + * Query for generic resource by id. Returns a query string to get a generic resource by input resource id + * + * @param resourceId the resource id + * @return the string + */ + 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); + } + + + + /** + * Gets the gcube generic query string for secondary type. + * + * @param secondaryType the secondary type + * @return the gcube generic query string for secondary type + */ + public static synchronized String getGcubeGenericQueryStringForSecondaryType(String secondaryType){ + + return "for $profile in collection('/db/Profiles/GenericResource')//Resource" + + " where $profile/Profile/SecondaryType/string() eq '"+secondaryType+"'" + + " return $profile"; + } + +} diff --git a/src/test/java/org/gcube/datacatalogue/metadatadiscovery/TestDataCatalogueMetadataFormatReader.java b/src/test/java/org/gcube/datacatalogue/metadatadiscovery/TestDataCatalogueMetadataFormatReader.java index b4517fc..d956a80 100644 --- a/src/test/java/org/gcube/datacatalogue/metadatadiscovery/TestDataCatalogueMetadataFormatReader.java +++ b/src/test/java/org/gcube/datacatalogue/metadatadiscovery/TestDataCatalogueMetadataFormatReader.java @@ -3,9 +3,10 @@ */ package org.gcube.datacatalogue.metadatadiscovery; +import java.util.List; + import org.gcube.common.scope.api.ScopeProvider; -import org.gcube.datacatalogue.metadatadiscovery.bean.MetadataProfile; -import org.gcube.datacatalogue.metadatadiscovery.bean.jaxb.MetadataFormat; +import org.gcube.datacatalogue.metadatadiscovery.bean.jaxb.NamespaceCategory; import org.junit.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -31,15 +32,22 @@ public class TestDataCatalogueMetadataFormatReader { ScopeProvider.instance.set(scopeString); DataCalogueMetadataFormatReader reader = new DataCalogueMetadataFormatReader(); int i = 0; - for (MetadataProfile mt : reader.getListOfMetadataProfiles()) { - if(mt==null) - continue; - - MetadataFormat metadataFormat = reader.getMetadataFormatForMetadataProfile(mt); - logger.trace("\n\n "+ ++i +".) Metadata source: "+metadataFormat.getMetadataSource().substring(0, 100)); + List categs = reader.getListOfNamespaceCategories(); + for (NamespaceCategory namespaceCategory : categs) { + logger.trace("\n\n "+ ++i +".) Category: "+namespaceCategory); } + i = 0; +// for (MetadataProfile mt : reader.getListOfMetadataProfiles()) { +// +// if(mt==null) +// continue; +// +// MetadataFormat metadataFormat = reader.getMetadataFormatForMetadataProfile(mt); +// logger.trace("\n\n "+ ++i +".) Metadata source: "+metadataFormat.getMetadataSource().substring(0, 100)); +// } + } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); diff --git a/src/test/java/org/gcube/datacatalogue/metadatadiscovery/TestJaxbMetadataMarshUnmarsh.java b/src/test/java/org/gcube/datacatalogue/metadatadiscovery/TestJaxbMetadataMarshUnmarsh.java index 7fd855d..a020ce9 100644 --- a/src/test/java/org/gcube/datacatalogue/metadatadiscovery/TestJaxbMetadataMarshUnmarsh.java +++ b/src/test/java/org/gcube/datacatalogue/metadatadiscovery/TestJaxbMetadataMarshUnmarsh.java @@ -11,9 +11,10 @@ import javax.xml.bind.JAXBException; import javax.xml.bind.Marshaller; import javax.xml.bind.Unmarshaller; -import org.gcube.datacatalogue.metadatadiscovery.bean.jaxb.MetadataCategory; import org.gcube.datacatalogue.metadatadiscovery.bean.jaxb.MetadataField; import org.gcube.datacatalogue.metadatadiscovery.bean.jaxb.MetadataFormat; +import org.gcube.datacatalogue.metadatadiscovery.bean.jaxb.NamespaceCategories; +import org.gcube.datacatalogue.metadatadiscovery.bean.jaxb.NamespaceCategory; import org.junit.Test; @@ -28,7 +29,8 @@ public class TestJaxbMetadataMarshUnmarsh { static MetadataField ckanMetadata; static MetadataFormat metadatas; static String tmpFileXML = "." + File.separator + "CkanMetadatas.xml"; - private ArrayList ckanCategories; + static String tmpCategoriesXML = "." + File.separator + "NamespacesCatalogueCategories.xml"; + private ArrayList ckanCategories; public static int MAX_CATEGORIES = 3; /** @@ -93,7 +95,8 @@ public class TestJaxbMetadataMarshUnmarsh { try { //marshalingExample(); - unMarshalingExample(); + //unMarshalingExample(); + unMarshalingCategories(); } catch (JAXBException e) { // TODO Auto-generated catch block @@ -159,6 +162,40 @@ public class TestJaxbMetadataMarshUnmarsh { } + /** + * Un marshaling example. + * + * @throws JAXBException the JAXB exception + */ + private static void unMarshalingCategories() throws JAXBException + { + JAXBContext jaxbContext = JAXBContext.newInstance(NamespaceCategories.class); + Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller(); + + //We had written this file in marshalling example + NamespaceCategories mtds = (NamespaceCategories) jaxbUnmarshaller.unmarshal(new File(tmpCategoriesXML)); + + System.out.println("NamespaceCategories: "); + System.out.println(mtds); + +// System.out.println("Metadata Categories"); +// if(mtds.getMetadataCategories()!=null){ +// for (MetadataCategory cat : mtds.getMetadataCategories()) { +// System.out.println(cat); +// System.out.println(cat.getCategoryQName()); +// } +// } + + System.out.println("\nCategories: "); + for (NamespaceCategory category : mtds.getNamespaceCategories()) { + System.out.println(category); + +// System.out.println(field.getGrouping()); +// System.out.println(field.getTagging()); + } + } + + public static void main(String[] args) { //System.out.println(DataType.valueOf("aa"));