git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/data-catalogue/gcubedatacatalogue-metadata-discovery@147200 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Francesco Mangiacrapa 2017-04-26 16:18:32 +00:00
parent 83d0640800
commit 05972d72c3
18 changed files with 591 additions and 153 deletions

View File

@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<namespaces xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="NamespacesCatalogueCategories.xsd">
<namespace id="contact">
<name>Contact</name>
<title>Contact Title</title>
<description>This section is about Contact(s)</description>
</namespace>
<namespace id="developer_information">
<name>Developer</name>
<title>Developer Information</title>
<description>This section is about Developer(s)</description>
</namespace>
<namespace id="extra_information">
<name>Extra Information</name>
<title>Extras</title>
<description>This section is about Extra(s)</description>
</namespace>
</namespaces>

View File

@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="namespaces">
<xs:complexType>
<xs:sequence>
<xs:element name="namespace" minOccurs="1" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element type="xs:string" name="name" minOccurs="1" maxOccurs="1"/>
<xs:element type="xs:string" name="title" minOccurs="1" maxOccurs="1"/>
<xs:element type="xs:string" name="description" minOccurs="0" maxOccurs="1"/>
</xs:sequence>
<xs:attribute type="xs:ID" name="id" use="required"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>

View File

@ -1,7 +1,9 @@
<ReleaseNotes> <ReleaseNotes>
<Changeset <Changeset
component="org.gcube.data-catalogue.gcubedatacatalogue-metadata-discovery.3-0-0" component="org.gcube.data-catalogue.gcubedatacatalogue-metadata-discovery.3-0-0"
date="2017-03-21"> date="2017-04-26">
<Change>[Task #8267] Medatada Discovery Library: definition of namespaces and metadata fields must be separated
</Change>
<Change>[Task #7616] Porting library to Metadata Profile model v3.0 <Change>[Task #7616] Porting library to Metadata Profile model v3.0
</Change> </Change>
</Changeset> </Changeset>

View File

@ -1,5 +1,6 @@
package org.gcube.datacatalogue.metadatadiscovery; package org.gcube.datacatalogue.metadatadiscovery;
import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; 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.common.scope.impl.ScopeBean;
import org.gcube.datacatalogue.metadatadiscovery.bean.MetadataProfile; import org.gcube.datacatalogue.metadatadiscovery.bean.MetadataProfile;
import org.gcube.datacatalogue.metadatadiscovery.bean.jaxb.MetadataFormat; 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.MetadataFormatDiscovery;
import org.gcube.datacatalogue.metadatadiscovery.reader.MetadataFormatReader; import org.gcube.datacatalogue.metadatadiscovery.reader.MetadataFormatReader;
import org.gcube.datacatalogue.metadatadiscovery.reader.NamespaceCategoryReader;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -24,8 +27,9 @@ import org.slf4j.LoggerFactory;
public class DataCalogueMetadataFormatReader implements DataCatalogueMetadataDiscovery{ public class DataCalogueMetadataFormatReader implements DataCatalogueMetadataDiscovery{
private MetadataFormatDiscovery medataFormatDiscovery; private MetadataFormatDiscovery medataFormatDiscovery;
private final ScopeBean scope; private ScopeBean scope;
private Map<String, MetadataFormat> hash = new HashMap<String, MetadataFormat>(); private Map<String, MetadataFormat> hashMetadataFormats = new HashMap<String, MetadataFormat>();
private List<NamespaceCategory> namespaceCategories = new ArrayList<NamespaceCategory>();
private static Logger logger = LoggerFactory.getLogger(DataCalogueMetadataFormatReader.class); private static Logger logger = LoggerFactory.getLogger(DataCalogueMetadataFormatReader.class);
@ -37,12 +41,24 @@ public class DataCalogueMetadataFormatReader implements DataCatalogueMetadataDis
public DataCalogueMetadataFormatReader() throws Exception { public DataCalogueMetadataFormatReader() throws Exception {
String scopeString = ScopeProvider.instance.get(); 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()) if(scopeString==null || scopeString.isEmpty())
throw new Exception("Please set a valid scope into ScopeProvider"); throw new Exception("Please set a valid scope into ScopeProvider");
scope = new ScopeBean(scopeString); scope = new ScopeBean(scopeString);
//readMetadaFormats();
readNamespaces();
}
/**
* Read metada formats.
*
* @throws Exception the exception
*/
private void readMetadaFormats() throws Exception{
medataFormatDiscovery = new MetadataFormatDiscovery(scope); medataFormatDiscovery = new MetadataFormatDiscovery(scope);
logger.info("MedataFormatDiscovery has retrieved: "+medataFormatDiscovery.getMetadataProfiles().size() +" metadata type/s"); logger.info("MedataFormatDiscovery has retrieved: "+medataFormatDiscovery.getMetadataProfiles().size() +" metadata type/s");
logger.debug("filling cache for MedataFormat"); logger.debug("filling cache for MedataFormat");
@ -51,18 +67,25 @@ public class DataCalogueMetadataFormatReader implements DataCatalogueMetadataDis
continue; continue;
MetadataFormatReader reader = new MetadataFormatReader(scope, mT.getId()); 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()); logger.debug("MetadataType id: "+mT.getId() +" cached as: "+reader.getMetadataFormat());
} }
} }
/* (non-Javadoc)
* @see org.gcube.datacatalogue.metadatadiscovery.DataCatalogueMetadataDiscovery#getListOfMetadataTypes()
*/
@Override
public List<MetadataProfile> 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) /* (non-Javadoc)
@ -74,7 +97,7 @@ public class DataCalogueMetadataFormatReader implements DataCatalogueMetadataDis
if(profile==null) if(profile==null)
throw new Exception("Input "+ MetadataProfile.class.getSimpleName() + " is 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) if(format!=null)
return format; return format;
@ -82,4 +105,28 @@ public class DataCalogueMetadataFormatReader implements DataCatalogueMetadataDis
return reader.getMetadataFormat(); return reader.getMetadataFormat();
} }
/* (non-Javadoc)
* @see org.gcube.datacatalogue.metadatadiscovery.DataCatalogueMetadataDiscovery#getListOfMetadataTypes()
*/
@Override
public List<MetadataProfile> getListOfMetadataProfiles() throws Exception {
if(medataFormatDiscovery==null)
readMetadaFormats();
return medataFormatDiscovery.getMetadataProfiles();
}
/* (non-Javadoc)
* @see org.gcube.datacatalogue.metadatadiscovery.DataCatalogueMetadataDiscovery#getListOfNamespaceCategories()
*/
@Override
public List<NamespaceCategory> getListOfNamespaceCategories() throws Exception {
if(namespaceCategories == null)
readNamespaces();
return namespaceCategories;
}
} }

View File

@ -7,6 +7,7 @@ import java.util.List;
import org.gcube.datacatalogue.metadatadiscovery.bean.MetadataProfile; import org.gcube.datacatalogue.metadatadiscovery.bean.MetadataProfile;
import org.gcube.datacatalogue.metadatadiscovery.bean.jaxb.MetadataFormat; import org.gcube.datacatalogue.metadatadiscovery.bean.jaxb.MetadataFormat;
import org.gcube.datacatalogue.metadatadiscovery.bean.jaxb.NamespaceCategory;
@ -29,6 +30,16 @@ public interface DataCatalogueMetadataDiscovery {
List<MetadataProfile> getListOfMetadataProfiles() throws Exception; List<MetadataProfile> getListOfMetadataProfiles() throws Exception;
/**
* Gets the list of namespace categories.
*
* @return the list of namespace categories
* @throws Exception the exception
*/
List<NamespaceCategory> getListOfNamespaceCategories() throws Exception;
/** /**
* Gets the metadata format for metadata type. * Gets the metadata format for metadata type.
* *

View File

@ -3,13 +3,17 @@
*/ */
package org.gcube.datacatalogue.metadatadiscovery; package org.gcube.datacatalogue.metadatadiscovery;
/** /**
* The Interface Namespace.
* *
* @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it * @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 static final String Separator = ":";
public String id = null;
public String name = null;
public String title = null;
public String description = "";
} }

View File

@ -13,7 +13,7 @@ import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient; import javax.xml.bind.annotation.XmlTransient;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; 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; import org.gcube.datacatalogue.metadatadiscovery.adapter.DataTypeAdapter;
@ -115,7 +115,7 @@ public class MetadataField implements Serializable{
*/ */
private void setNamespaceFieldToCategoryRef() { 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. * 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 * Otherwise returns the fieldName
* *
* @return the category q name * @return the category q name
@ -154,7 +154,7 @@ public class MetadataField implements Serializable{
public String getCategoryFieldQName(){ public String getCategoryFieldQName(){
initNamespaceFieldToCategoryReference(); initNamespaceFieldToCategoryReference();
return namespaceFieldToCategoryReference==null?fieldName:namespaceFieldToCategoryReference+MetadataNamespace.Separator+fieldName; return namespaceFieldToCategoryReference==null?fieldName:namespaceFieldToCategoryReference+Namespace.Separator+fieldName;
} }

View File

@ -31,25 +31,22 @@ public class MetadataFormat implements Serializable{
*/ */
private static final long serialVersionUID = -5881074882343424963L; 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"; public static final String LOCAL_NAME_METADATA_FORMAT = "metadataformat";
/** /**
* Instantiates a new metadata format. * Instantiates a new metadata format.
*/ */
public MetadataFormat() { public MetadataFormat() {
} }
@XmlElement(name = "metadatacategory")
private List<MetadataCategory> metadataCategories;
@XmlElement(name = "metadatafield") @XmlElement(name = "metadatafield")
private List<MetadataField> metadataFields; private List<MetadataField> metadataFields;
@XmlAttribute(name = LOCAL_NAME_METADATA_TYPE, required=true) @XmlAttribute(name = LOCAL_NAME_METADATA_TYPE, required=true)
private String metadataType = ""; private String type = "";
@XmlTransient @XmlTransient
private String metadataSource = null; private String metadataSource = null;
@ -65,25 +62,27 @@ public class MetadataFormat implements Serializable{
return metadataFields; 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<MetadataCategory> 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<MetadataCategory>();
this.metadataCategories.add(metadataCategory);
}
/* (non-Javadoc) /* (non-Javadoc)
* @see java.lang.Object#toString() * @see java.lang.Object#toString()
*/ */
@ -141,12 +128,12 @@ public class MetadataFormat implements Serializable{
public String toString() { public String toString() {
StringBuilder builder = new StringBuilder(); StringBuilder builder = new StringBuilder();
builder.append("MetadataFormat [metadataCategories="); builder.append("MetadataFormat [metadataFields=");
builder.append(metadataCategories);
builder.append(", metadataFields=");
builder.append(metadataFields); builder.append(metadataFields);
builder.append(", metadataType="); builder.append(", type=");
builder.append(metadataType); builder.append(type);
builder.append(", metadataSource=");
builder.append(metadataSource);
builder.append("]"); builder.append("]");
return builder.toString(); return builder.toString();
} }

View File

@ -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<NamespaceCategory> namespaceCategories;
/**
* Instantiates a new namespace categories.
*/
public NamespaceCategories() {
}
/**
* @param namespaceCategories
*/
public NamespaceCategories(List<NamespaceCategory> namespaceCategories) {
this.namespaceCategories = namespaceCategories;
}
/**
* @return the namespaceCategories
*/
public List<NamespaceCategory> getNamespaceCategories() {
return namespaceCategories;
}
/**
* @param namespaceCategories the namespaceCategories to set
*/
public void setNamespaceCategories(List<NamespaceCategory> 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();
}
}

View File

@ -10,73 +10,72 @@ import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute; import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement; 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 * @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it
* Mar 21, 2017 * Apr 26, 2017
*/ */
@XmlRootElement(name = "metadatacategory") @XmlRootElement(name = "namespace")
@XmlAccessorType (XmlAccessType.FIELD) @XmlAccessorType (XmlAccessType.FIELD)
public class MetadataCategory implements Serializable{ public class NamespaceCategory implements Namespace, Serializable{
/**
*
*/
private static final String NAME_ELEMENT_METADATACATEGORY = "metadatacategory";
/** /** The Constant serialVersionUID. */
* private static final long serialVersionUID = -1632300992065668875L;
*/
private static final long serialVersionUID = -7282676787740920115L;
@XmlAttribute(required = true) @XmlAttribute(required = true)
private String id = null; private String id = null;
@XmlElement(required = true) @XmlElement(required = true)
private String title = ""; private String name = null;
private String description = null; @XmlElement(required = true)
private String title = null;
@XmlTransient private String description = "";
private String namespaceCategory = NAME_ELEMENT_METADATACATEGORY;
/** /**
* 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 id the id
* @param name the name
* @param title the title * @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(); super();
this.id = id; 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.title = title;
this.description = description; 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. * Gets the namespace category qualifier name. The QName is a unique name to identify the category. It corresponds to {@link NamespaceCategory#id}
* It is the identifier: {@link MetadataCategory#namespaceCategory} + {@link MetadataNamespace#Separator} + {@link MetadataCategory#id} * @return the namespace category q name
*
* @return the 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) /* (non-Javadoc)
* @see java.lang.Object#toString() * @see java.lang.Object#toString()
@ -177,18 +164,16 @@ public class MetadataCategory implements Serializable{
public String toString() { public String toString() {
StringBuilder builder = new StringBuilder(); StringBuilder builder = new StringBuilder();
builder.append("MetadataCategory [id="); builder.append("NamespaceCategory [id=");
builder.append(id); builder.append(id);
builder.append(", name=");
builder.append(name);
builder.append(", title="); builder.append(", title=");
builder.append(title); builder.append(title);
builder.append(", description="); builder.append(", description=");
builder.append(description); builder.append(description);
builder.append(", namespaceCategory=");
builder.append(namespaceCategory);
builder.append("]"); builder.append("]");
return builder.toString(); return builder.toString();
} }
} }

View File

@ -78,7 +78,7 @@ public class MetadataFormatDiscovery {
// ScopeProvider.instance.set(scopeString); // ScopeProvider.instance.set(scopeString);
logger.info("Using scope from ScopeProvider: "+scopeString); logger.info("Using scope from ScopeProvider: "+scopeString);
String queryString = getGcubeGenericQueryString(secondaryType); String queryString = QueryForResourceUtil.getGcubeGenericQueryStringForSecondaryType(secondaryType);
logger.trace("queryString: " +queryString); logger.trace("queryString: " +queryString);
Query q = new QueryBox(queryString); Query q = new QueryBox(queryString);
@ -121,11 +121,9 @@ public class MetadataFormatDiscovery {
*/ */
private MetadataProfile getMetadataProfileFromResource(Document doc, XPathHelper helper) throws MetadataProfileNotFoundException{ private MetadataProfile getMetadataProfileFromResource(Document doc, XPathHelper helper) throws MetadataProfileNotFoundException{
try { try {
MetadataProfile metadataProfile = new MetadataProfile(); MetadataProfile metadataProfile = new MetadataProfile();
List<String> id = helper.evaluate("/Resource/ID/text()"); List<String> id = helper.evaluate("/Resource/ID/text()");
if(id==null || id.isEmpty()) 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. * Gets the secondary type.
* *

View File

@ -49,7 +49,7 @@ public class MetadataFormatReader {
public MetadataFormatReader(ScopeBean scope, String resourceID) throws Exception { public MetadataFormatReader(ScopeBean scope, String resourceID) throws Exception {
this.scope = scope; this.scope = scope;
this.resourceID = resourceID; this.resourceID = resourceID;
this.metadataFormat = getMetadataFormatByID(resourceID); this.metadataFormat = getMetadataFormatByResourceID(resourceID);
} }
@ -61,8 +61,8 @@ public class MetadataFormatReader {
* @return the metadata format by id * @return the metadata format by id
* @throws Exception the exception * @throws Exception the exception
*/ */
private MetadataFormat getMetadataFormatByID(String resourceID) throws Exception { private MetadataFormat getMetadataFormatByResourceID(String resourceID) throws Exception {
logger.trace("Getting MedataFormat with resourceID: "+resourceID); logger.trace("Getting MedataFormat for resourceID: "+resourceID);
if(this.scope==null) if(this.scope==null)
throw new Exception("Scope is null"); throw new Exception("Scope is null");
@ -76,7 +76,7 @@ public class MetadataFormatReader {
logger.info("Using scope from ScopeProvider: "+scopeString); logger.info("Using scope from ScopeProvider: "+scopeString);
String queryString = getQuesryStringForGenericResourceById(resourceID); String queryString = QueryForResourceUtil.queryForGenericResourceById(resourceID);
logger.trace("queryString: " +queryString); logger.trace("queryString: " +queryString);
Query q = new QueryBox(queryString); Query q = new QueryBox(queryString);
@ -163,7 +163,7 @@ public class MetadataFormatReader {
InputStream stream = new ByteArrayInputStream(metadataFormatSource.get(0).getBytes()); InputStream stream = new ByteArrayInputStream(metadataFormatSource.get(0).getBytes());
MetadataFormat mtdf = (MetadataFormat) jaxbUnmarshaller.unmarshal(stream); 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; 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);
}
/** /**

View File

@ -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<String> client = client();
List<String> 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<MetadataField> 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<String> 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 <namespaces> <namespace>...</namespace> </namespaces> 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();
}
}

View File

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

View File

@ -0,0 +1,8 @@
package org.gcube.datacatalogue.metadatadiscovery.reader;
@SuppressWarnings("serial")
public class PropertyFileNotFoundException extends Exception {
public PropertyFileNotFoundException(String message) {
super(message);
}
}

View File

@ -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";
}
}

View File

@ -3,9 +3,10 @@
*/ */
package org.gcube.datacatalogue.metadatadiscovery; package org.gcube.datacatalogue.metadatadiscovery;
import java.util.List;
import org.gcube.common.scope.api.ScopeProvider; import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.datacatalogue.metadatadiscovery.bean.MetadataProfile; import org.gcube.datacatalogue.metadatadiscovery.bean.jaxb.NamespaceCategory;
import org.gcube.datacatalogue.metadatadiscovery.bean.jaxb.MetadataFormat;
import org.junit.Test; import org.junit.Test;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -31,15 +32,22 @@ public class TestDataCatalogueMetadataFormatReader {
ScopeProvider.instance.set(scopeString); ScopeProvider.instance.set(scopeString);
DataCalogueMetadataFormatReader reader = new DataCalogueMetadataFormatReader(); DataCalogueMetadataFormatReader reader = new DataCalogueMetadataFormatReader();
int i = 0; int i = 0;
for (MetadataProfile mt : reader.getListOfMetadataProfiles()) {
if(mt==null) List<NamespaceCategory> categs = reader.getListOfNamespaceCategories();
continue; for (NamespaceCategory namespaceCategory : categs) {
logger.trace("\n\n "+ ++i +".) Category: "+namespaceCategory);
MetadataFormat metadataFormat = reader.getMetadataFormatForMetadataProfile(mt);
logger.trace("\n\n "+ ++i +".) Metadata source: "+metadataFormat.getMetadataSource().substring(0, 100));
} }
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) { } catch (Exception e) {
// TODO Auto-generated catch block // TODO Auto-generated catch block
e.printStackTrace(); e.printStackTrace();

View File

@ -11,9 +11,10 @@ import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller; import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller; 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.MetadataField;
import org.gcube.datacatalogue.metadatadiscovery.bean.jaxb.MetadataFormat; 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; import org.junit.Test;
@ -28,7 +29,8 @@ public class TestJaxbMetadataMarshUnmarsh {
static MetadataField ckanMetadata; static MetadataField ckanMetadata;
static MetadataFormat metadatas; static MetadataFormat metadatas;
static String tmpFileXML = "." + File.separator + "CkanMetadatas.xml"; static String tmpFileXML = "." + File.separator + "CkanMetadatas.xml";
private ArrayList<MetadataCategory> ckanCategories; static String tmpCategoriesXML = "." + File.separator + "NamespacesCatalogueCategories.xml";
private ArrayList<NamespaceCategory> ckanCategories;
public static int MAX_CATEGORIES = 3; public static int MAX_CATEGORIES = 3;
/** /**
@ -93,7 +95,8 @@ public class TestJaxbMetadataMarshUnmarsh {
try { try {
//marshalingExample(); //marshalingExample();
unMarshalingExample(); //unMarshalingExample();
unMarshalingCategories();
} }
catch (JAXBException e) { catch (JAXBException e) {
// TODO Auto-generated catch block // 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) { public static void main(String[] args) {
//System.out.println(DataType.valueOf("aa")); //System.out.println(DataType.valueOf("aa"));