From 9380c394f29e7e857a20906cf0e2c886f26cd759 Mon Sep 17 00:00:00 2001 From: "francesco.mangiacrapa" Date: Wed, 30 Sep 2020 10:36:30 +0200 Subject: [PATCH] created project copying class form gcubedatacatalogue-metadata-discovery --- .classpath | 28 ++ .gitignore | 2 + .project | 29 ++ .settings/org.eclipse.core.resources.prefs | 4 + .settings/org.eclipse.jdt.core.prefs | 14 + ....eclipse.wst.common.project.facet.core.xml | 4 + pom.xml | 134 ++++++ .../DataCalogueMetadataFormatReader.java | 275 +++++++++++++ .../DataCatalogueMetadataDiscovery.java | 79 ++++ .../metadataprofilediscovery/Namespace.java | 19 + .../adapter/DataTypeAdapter.java | 35 ++ .../adapter/TaggingGroupingAdapter.java | 36 ++ .../bean/MetadataProfile.java | 159 +++++++ .../jaxb/DataType.java | 54 +++ .../jaxb/MetadataField.java | 389 ++++++++++++++++++ .../jaxb/MetadataFormat.java | 142 +++++++ .../jaxb/MetadataGrouping.java | 144 +++++++ .../jaxb/MetadataTagging.java | 155 +++++++ .../jaxb/MetadataValidator.java | 81 ++++ .../jaxb/MetadataVocabulary.java | 143 +++++++ .../jaxb/NamespaceCategories.java | 85 ++++ .../jaxb/NamespaceCategory.java | 182 ++++++++ .../jaxb/TaggingGroupingValue.java | 65 +++ .../reader/MetadataFormatDiscovery.java | 197 +++++++++ .../reader/MetadataFormatReader.java | 258 ++++++++++++ .../MetadataProfileNotFoundException.java | 20 + .../reader/NamespaceCategoryReader.java | 167 ++++++++ .../reader/PropertyFileNotFoundException.java | 8 + .../reader/QueryForResourceUtil.java | 58 +++ src/main/resources/Gdcmetadataprofilev3.xsd | 92 +++++ .../NamespacesCatalogueCategories.xsd | 28 ++ ...TestDataCatalogueMetadataFormatReader.java | 70 ++++ .../TestJaxbMetadataMarshUnmarsh.java | 210 ++++++++++ .../TestMetadataFormatDiscovery.java | 43 ++ .../TestMetadataFormatReader.java | 33 ++ 35 files changed, 3442 insertions(+) create mode 100644 .classpath create mode 100644 .gitignore create mode 100644 .project create mode 100644 .settings/org.eclipse.core.resources.prefs create mode 100644 .settings/org.eclipse.jdt.core.prefs create mode 100644 .settings/org.eclipse.wst.common.project.facet.core.xml create mode 100644 pom.xml create mode 100644 src/main/java/org/gcube/common/metadataprofilediscovery/DataCalogueMetadataFormatReader.java create mode 100644 src/main/java/org/gcube/common/metadataprofilediscovery/DataCatalogueMetadataDiscovery.java create mode 100644 src/main/java/org/gcube/common/metadataprofilediscovery/Namespace.java create mode 100644 src/main/java/org/gcube/common/metadataprofilediscovery/adapter/DataTypeAdapter.java create mode 100644 src/main/java/org/gcube/common/metadataprofilediscovery/adapter/TaggingGroupingAdapter.java create mode 100644 src/main/java/org/gcube/common/metadataprofilediscovery/bean/MetadataProfile.java create mode 100644 src/main/java/org/gcube/common/metadataprofilediscovery/jaxb/DataType.java create mode 100644 src/main/java/org/gcube/common/metadataprofilediscovery/jaxb/MetadataField.java create mode 100644 src/main/java/org/gcube/common/metadataprofilediscovery/jaxb/MetadataFormat.java create mode 100644 src/main/java/org/gcube/common/metadataprofilediscovery/jaxb/MetadataGrouping.java create mode 100644 src/main/java/org/gcube/common/metadataprofilediscovery/jaxb/MetadataTagging.java create mode 100644 src/main/java/org/gcube/common/metadataprofilediscovery/jaxb/MetadataValidator.java create mode 100644 src/main/java/org/gcube/common/metadataprofilediscovery/jaxb/MetadataVocabulary.java create mode 100644 src/main/java/org/gcube/common/metadataprofilediscovery/jaxb/NamespaceCategories.java create mode 100644 src/main/java/org/gcube/common/metadataprofilediscovery/jaxb/NamespaceCategory.java create mode 100644 src/main/java/org/gcube/common/metadataprofilediscovery/jaxb/TaggingGroupingValue.java create mode 100644 src/main/java/org/gcube/common/metadataprofilediscovery/reader/MetadataFormatDiscovery.java create mode 100644 src/main/java/org/gcube/common/metadataprofilediscovery/reader/MetadataFormatReader.java create mode 100644 src/main/java/org/gcube/common/metadataprofilediscovery/reader/MetadataProfileNotFoundException.java create mode 100644 src/main/java/org/gcube/common/metadataprofilediscovery/reader/NamespaceCategoryReader.java create mode 100644 src/main/java/org/gcube/common/metadataprofilediscovery/reader/PropertyFileNotFoundException.java create mode 100644 src/main/java/org/gcube/common/metadataprofilediscovery/reader/QueryForResourceUtil.java create mode 100644 src/main/resources/Gdcmetadataprofilev3.xsd create mode 100644 src/main/resources/NamespacesCatalogueCategories.xsd create mode 100644 src/test/java/org/gcube/common/metadataprofilediscovery/TestDataCatalogueMetadataFormatReader.java create mode 100644 src/test/java/org/gcube/common/metadataprofilediscovery/TestJaxbMetadataMarshUnmarsh.java create mode 100644 src/test/java/org/gcube/common/metadataprofilediscovery/TestMetadataFormatDiscovery.java create mode 100644 src/test/java/org/gcube/common/metadataprofilediscovery/TestMetadataFormatReader.java diff --git a/.classpath b/.classpath new file mode 100644 index 0000000..bc0c5c4 --- /dev/null +++ b/.classpath @@ -0,0 +1,28 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..09e3bc9 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/bin/ +/target/ diff --git a/.project b/.project new file mode 100644 index 0000000..66c89c0 --- /dev/null +++ b/.project @@ -0,0 +1,29 @@ + + + metadata-profile-discovery + + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.eclipse.wst.common.project.facet.core.builder + + + + + org.eclipse.m2e.core.maven2Builder + + + + + + org.eclipse.wst.common.project.facet.core.nature + org.eclipse.jdt.core.javanature + org.eclipse.m2e.core.maven2Nature + + diff --git a/.settings/org.eclipse.core.resources.prefs b/.settings/org.eclipse.core.resources.prefs new file mode 100644 index 0000000..f9fe345 --- /dev/null +++ b/.settings/org.eclipse.core.resources.prefs @@ -0,0 +1,4 @@ +eclipse.preferences.version=1 +encoding//src/main/java=UTF-8 +encoding//src/test/java=UTF-8 +encoding/=UTF-8 diff --git a/.settings/org.eclipse.jdt.core.prefs b/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000..e2ceea1 --- /dev/null +++ b/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,14 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.methodParameters=do not generate +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=1.7 +org.eclipse.jdt.core.compiler.debug.lineNumber=generate +org.eclipse.jdt.core.compiler.debug.localVariable=generate +org.eclipse.jdt.core.compiler.debug.sourceFile=generate +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning +org.eclipse.jdt.core.compiler.release=disabled +org.eclipse.jdt.core.compiler.source=1.7 diff --git a/.settings/org.eclipse.wst.common.project.facet.core.xml b/.settings/org.eclipse.wst.common.project.facet.core.xml new file mode 100644 index 0000000..f4ef8aa --- /dev/null +++ b/.settings/org.eclipse.wst.common.project.facet.core.xml @@ -0,0 +1,4 @@ + + + + diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..708eb62 --- /dev/null +++ b/pom.xml @@ -0,0 +1,134 @@ + + + 4.0.0 + + maven-parent + org.gcube.tools + 1.1.0 + + + + org.gcube.common + metadata-profile-discovery + 0.1.0-SNAPSHOT + jar + The Metadata Profile discovery library + The metadata-profile-discovery library is able to build java objects parsing the 'gCube Metadata Profiles' models: + * defined in the contexts (scopes) of the D4Science Infrastructure; + * passed as an input stream; + + + + scm:git:https://code-repo.d4science.org/gCubeSystem/${project.artifactId}.git + scm:git:https://code-repo.d4science.org/gCubeSystem/${project.artifactId}.git + https://code-repo.d4science.org/gCubeSystem/${project.artifactId} + + + + + Francesco Mangiacrapa + francesco.mangiacrapa@isti.cnr.it + CNR Pisa, Istituto di Scienza e Tecnologie dell'Informazione "A. Faedo" + + architect + developer + + + + + + + + org.gcube.distribution + maven-portal-bom + 3.6.0 + pom + import + + + + + + UTF-8 + UTF-8 + + 1.7 + 1.8 + + + + + + org.gcube.resources.discovery + ic-client + provided + + + + org.gcube.core + common-scope-maps + provided + + + + + com.sun.xml.bind + jaxb-core + 2.2.11 + provided + + + + com.sun.xml.bind + jaxb-impl + 2.2.11 + provided + + + + + org.slf4j + slf4j-log4j12 + provided + + + org.slf4j + slf4j-api + provided + + + + + junit + junit + 4.8.1 + test + + + + + ch.qos.logback + logback-classic + 1.0.13 + test + + + + + + + + org.apache.maven.plugins + maven-assembly-plugin + + + make-servicearchive + package + + + + + + diff --git a/src/main/java/org/gcube/common/metadataprofilediscovery/DataCalogueMetadataFormatReader.java b/src/main/java/org/gcube/common/metadataprofilediscovery/DataCalogueMetadataFormatReader.java new file mode 100644 index 0000000..d3379d8 --- /dev/null +++ b/src/main/java/org/gcube/common/metadataprofilediscovery/DataCalogueMetadataFormatReader.java @@ -0,0 +1,275 @@ +package org.gcube.common.metadataprofilediscovery; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.StringReader; +import java.net.URL; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +import javax.xml.XMLConstants; +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.transform.Source; +import javax.xml.transform.dom.DOMSource; +import javax.xml.transform.stream.StreamSource; +import javax.xml.validation.Schema; +import javax.xml.validation.SchemaFactory; +import javax.xml.validation.Validator; + +import org.gcube.common.metadataprofilediscovery.bean.MetadataProfile; +import org.gcube.common.metadataprofilediscovery.jaxb.MetadataFormat; +import org.gcube.common.metadataprofilediscovery.jaxb.NamespaceCategory; +import org.gcube.common.metadataprofilediscovery.reader.MetadataFormatDiscovery; +import org.gcube.common.metadataprofilediscovery.reader.MetadataFormatReader; +import org.gcube.common.metadataprofilediscovery.reader.NamespaceCategoryReader; +import org.gcube.common.scope.api.ScopeProvider; +import org.gcube.common.scope.impl.ScopeBean; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.w3c.dom.Document; +import org.xml.sax.InputSource; +import org.xml.sax.SAXException; + +/** + * The Class DataCalogueMetadataReader. + * + * @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it Jun 8, 2016 + */ +public class DataCalogueMetadataFormatReader implements DataCatalogueMetadataDiscovery { + + private static String SCHEMA_FILENAME = "Gdcmetadataprofilev3.xsd"; + + private MetadataFormatDiscovery medataFormatDiscovery; + private ScopeBean scope; + private Map hashMetadataFormats = null; + private List namespaceCategories = null; + + private String profileSchema = null; + + private static Logger logger = LoggerFactory.getLogger(DataCalogueMetadataFormatReader.class); + + /** + * Instantiates a new data calogue metadata format reader. + * + * @throws Exception the exception + */ + public DataCalogueMetadataFormatReader() throws Exception { + + String scopeString = ScopeProvider.instance.get(); + 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); + readNamespaces(); + readMetadaFormats(); + } + + /** + * 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"); + hashMetadataFormats = new HashMap(medataFormatDiscovery.getMetadataProfiles().size()); + for (MetadataProfile mT : medataFormatDiscovery.getMetadataProfiles()) { + if (mT == null) + continue; + + MetadataFormatReader reader = new MetadataFormatReader(scope, mT.getId()); + hashMetadataFormats.put(mT.getId(), reader.getMetadataFormat()); + logger.debug("MetadataType id: " + mT.getId() + " cached as: " + reader.getMetadataFormat()); + } + } + + /** + * Read namespaces. + */ + private void readNamespaces() { + + try { + if (namespaceCategories == null || namespaceCategories.isEmpty()) { + + if (namespaceCategories == null) + namespaceCategories = new ArrayList(); + + NamespaceCategoryReader rd = new NamespaceCategoryReader(scope); + namespaceCategories.addAll(rd.getNamespaces().getNamespaceCategories()); + } + } catch (Exception e) { + logger.debug("An error occurred during read namespaces for categories: ", e); + } + } + + /** + * Gets the profile schema string. + * + * @return the profile schema string + */ + public static String getProfileSchemaString() { + InputStream inputStream = DataCalogueMetadataFormatReader.getProfileSchemaInputStream(); + return new BufferedReader(new InputStreamReader(inputStream)).lines().collect(Collectors.joining("\n")); + } + + /** + * Gets the profile schema input stream. + * + * @return the profile schema input stream + */ + public static InputStream getProfileSchemaInputStream() { + return DataCalogueMetadataFormatReader.class.getResourceAsStream(SCHEMA_FILENAME); + } + + /** + * Gets the profile schema URL. + * + * @return the profile schema URL + */ + public static URL getProfileSchemaURL() { + return DataCalogueMetadataFormatReader.class.getResource(SCHEMA_FILENAME); + } + + /** + * Validate against XSD. + * + * @param xml the xml + * @param xsd the xsd + * @throws SAXException the SAX exception + * @throws IOException Signals that an I/O exception has occurred. + */ + static void validateAgainstXSD(Source xml, URL xsd) throws SAXException, IOException { + SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); + Schema schema = factory.newSchema(xsd); + Validator validator = schema.newValidator(); + validator.validate(xml); + } + + /** + * Validate profile. + * + * @param xml the xml + * @throws Exception the exception + */ + public static void validateProfile(InputStream xml) throws Exception { + validateAgainstXSD(new StreamSource(xml), getProfileSchemaURL()); + } + + /* + * (non-Javadoc) + * + * @see + * org.gcube.common.metadataprofilediscovery.DataCatalogueMetadataDiscovery# + * getMetadataFormatForMetadataProfile(org.gcube.common.metadataprofilediscovery + * .bean.MetadataProfile) + */ + public MetadataFormat getMetadataFormatForMetadataProfile(MetadataProfile profile) throws Exception { + + if (profile == null) + throw new Exception("Input " + MetadataProfile.class.getSimpleName() + " is null"); + + MetadataFormat format = hashMetadataFormats.get(profile.getId()); + if (format != null) + return format; + + MetadataFormatReader reader = new MetadataFormatReader(scope, profile.getId()); + return reader.getMetadataFormat(); + } + + /* + * (non-Javadoc) + * + * @see + * org.gcube.common.metadataprofilediscovery.DataCatalogueMetadataDiscovery# + * getListOfMetadataProfiles() + */ + public List getListOfMetadataProfiles() throws Exception { + + if (medataFormatDiscovery == null) + readMetadaFormats(); + + return medataFormatDiscovery.getMetadataProfiles(); + } + + /* + * (non-Javadoc) + * + * @see + * org.gcube.common.metadataprofilediscovery.DataCatalogueMetadataDiscovery# + * getListOfNamespaceCategories() + */ + public List getListOfNamespaceCategories() throws Exception { + + if (namespaceCategories == null) + readNamespaces(); + + return namespaceCategories; + } + + /* + * (non-Javadoc) + * + * @see + * org.gcube.common.metadataprofilediscovery.DataCatalogueMetadataDiscovery# + * resetMetadataProfile() + */ + public void resetMetadataProfile() { + + medataFormatDiscovery = null; + hashMetadataFormats = null; + } + + /* + * (non-Javadoc) + * + * @see + * org.gcube.common.metadataprofilediscovery.DataCatalogueMetadataDiscovery# + * resetNamespaceCategories() + */ + public void resetNamespaceCategories() { + + namespaceCategories = null; + } + + /* + * (non-Javadoc) + * + * @see + * org.gcube.common.metadataprofilediscovery.DataCatalogueMetadataDiscovery# + * getProfileSchema() + */ + public String getProfileSchema() { + if (profileSchema == null) { + profileSchema = DataCalogueMetadataFormatReader.getProfileSchemaString(); + } + return profileSchema; + + } + + /* + * (non-Javadoc) + * + * @see + * org.gcube.common.metadataprofilediscovery.DataCatalogueMetadataDiscovery# + * validateProfile(java.lang.String) + */ + public void validateProfile(String xmlProfile) throws ParserConfigurationException, SAXException, IOException { + DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder(); + Document doc = db.parse(new InputSource(new StringReader(xmlProfile))); + DOMSource source = new DOMSource(doc); + validateAgainstXSD(source, getProfileSchemaURL()); + } + +} diff --git a/src/main/java/org/gcube/common/metadataprofilediscovery/DataCatalogueMetadataDiscovery.java b/src/main/java/org/gcube/common/metadataprofilediscovery/DataCatalogueMetadataDiscovery.java new file mode 100644 index 0000000..30e9523 --- /dev/null +++ b/src/main/java/org/gcube/common/metadataprofilediscovery/DataCatalogueMetadataDiscovery.java @@ -0,0 +1,79 @@ +/** + * + */ +package org.gcube.common.metadataprofilediscovery; + +import java.util.List; + +import org.gcube.common.metadataprofilediscovery.bean.MetadataProfile; +import org.gcube.common.metadataprofilediscovery.jaxb.MetadataFormat; +import org.gcube.common.metadataprofilediscovery.jaxb.NamespaceCategory; + + +/** + * The Interface DataCatalogueMetadataDiscovery. + * + * @author Francesco Mangiacrapa at ISTI-CNR (francesco.mangiacrapa@isti.cnr.it) + * + * Sep 30, 2020 + */ +public interface DataCatalogueMetadataDiscovery { + + /** + * Gets the list of metadata types. + * + * @return the list of metadata types + * @throws Exception the exception + */ + 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. + * + * @param type the type + * @return the metadata format for metadata type + * @throws Exception the exception + */ + MetadataFormat getMetadataFormatForMetadataProfile(MetadataProfile type) throws Exception; + + + /** + * Reset metadata profile. + * Forces current list of Metadata Profile at null in order to read it from IS againg + */ + void resetMetadataProfile(); + + /** + * Reset namespace categories. + * Forces current list of Namespace Categories Profile at null in order to read it from IS againg + */ + void resetNamespaceCategories(); + + /** + * Return the XSD of the profile schema. + * + * @return the String representation of the XSD containing the schema for a profile + */ + String getProfileSchema(); + + /** + * Validate the xml provided as argument. + * + * @param xmlProfile the string representation of the XML to validate again the schema + * @throws Exception the exception + */ + void validateProfile(String xmlProfile) throws Exception; + + +} diff --git a/src/main/java/org/gcube/common/metadataprofilediscovery/Namespace.java b/src/main/java/org/gcube/common/metadataprofilediscovery/Namespace.java new file mode 100644 index 0000000..04ef5c6 --- /dev/null +++ b/src/main/java/org/gcube/common/metadataprofilediscovery/Namespace.java @@ -0,0 +1,19 @@ +/** + * + */ +package org.gcube.common.metadataprofilediscovery; + +/** + * The Interface Namespace. + * + * @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it + * Apr 26, 2017 + */ +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/common/metadataprofilediscovery/adapter/DataTypeAdapter.java b/src/main/java/org/gcube/common/metadataprofilediscovery/adapter/DataTypeAdapter.java new file mode 100644 index 0000000..07c0ab7 --- /dev/null +++ b/src/main/java/org/gcube/common/metadataprofilediscovery/adapter/DataTypeAdapter.java @@ -0,0 +1,35 @@ +/** + * + */ + +package org.gcube.common.metadataprofilediscovery.adapter; + +import javax.xml.bind.annotation.adapters.XmlAdapter; + +import org.gcube.common.metadataprofilediscovery.jaxb.DataType; + + +/** + * The Class DataTypeAdapter. + * + * @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it + * Sep 12, 2016 + */ +public class DataTypeAdapter extends XmlAdapter { + + /* (non-Javadoc) + * @see javax.xml.bind.annotation.adapters.XmlAdapter#marshal(java.lang.Object) + */ + public String marshal(DataType dt) { + + return dt.name(); + } + + /* (non-Javadoc) + * @see javax.xml.bind.annotation.adapters.XmlAdapter#unmarshal(java.lang.Object) + */ + public DataType unmarshal(String dt) { + + return DataType.fromValue(dt); + } +} diff --git a/src/main/java/org/gcube/common/metadataprofilediscovery/adapter/TaggingGroupingAdapter.java b/src/main/java/org/gcube/common/metadataprofilediscovery/adapter/TaggingGroupingAdapter.java new file mode 100644 index 0000000..7b7f296 --- /dev/null +++ b/src/main/java/org/gcube/common/metadataprofilediscovery/adapter/TaggingGroupingAdapter.java @@ -0,0 +1,36 @@ +/** + * + */ + +package org.gcube.common.metadataprofilediscovery.adapter; + +import javax.xml.bind.annotation.adapters.XmlAdapter; + +import org.gcube.common.metadataprofilediscovery.jaxb.TaggingGroupingValue; + + + +/** + * The Class TaggingGroupingAdapter. + * + * @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it + * Mar 21, 2017 + */ +public class TaggingGroupingAdapter extends XmlAdapter { + + /* (non-Javadoc) + * @see javax.xml.bind.annotation.adapters.XmlAdapter#marshal(java.lang.Object) + */ + public String marshal(TaggingGroupingValue dt) { + + return dt.value(); + } + + /* (non-Javadoc) + * @see javax.xml.bind.annotation.adapters.XmlAdapter#unmarshal(java.lang.Object) + */ + public TaggingGroupingValue unmarshal(String dt) { + + return TaggingGroupingValue.fromValue(dt); + } +} diff --git a/src/main/java/org/gcube/common/metadataprofilediscovery/bean/MetadataProfile.java b/src/main/java/org/gcube/common/metadataprofilediscovery/bean/MetadataProfile.java new file mode 100644 index 0000000..a00b549 --- /dev/null +++ b/src/main/java/org/gcube/common/metadataprofilediscovery/bean/MetadataProfile.java @@ -0,0 +1,159 @@ +/** + * + */ +package org.gcube.common.metadataprofilediscovery.bean; + +import java.io.Serializable; + + + +/** + * The Class MetadataProfile. + * + * @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it + * May 19, 2017 + */ +public class MetadataProfile implements Serializable{ + + + /** + * + */ + private static final long serialVersionUID = 8829173012445264057L; + private String id; + private String name; + private String description; + private String metadataType; + + /** + * Instantiates a new metadata type. + */ + public MetadataProfile() { + + } + + /** + * Instantiates a new metadata type. + * + * @param id the id + * @param name the name + * @param description the description + * @param metadataType the metadata type + */ + public MetadataProfile(String id, String name, String description, String metadataType) { + + super(); + this.id = id; + this.name = name; + this.description = description; + this.metadataType = metadataType; + } + + + /** + * Gets the id. + * + * @return the id + */ + public String getId() { + + return id; + } + + + /** + * Gets the name. + * + * @return the name + */ + public String getName() { + + return name; + } + + + /** + * Gets the description. + * + * @return the description + */ + public String getDescription() { + + return description; + } + + + /** + * Sets the id. + * + * @param id the id to set + */ + public void setId(String id) { + + this.id = id; + } + + + /** + * Sets the name. + * + * @param name the name to set + */ + public void setName(String name) { + + this.name = name; + } + + + /** + * Sets the description. + * + * @param description the description to set + */ + public void setDescription(String description) { + + this.description = description; + } + + /** + * Gets the metadata type. + * + * @return the metadataType + */ + public String getMetadataType() { + + return metadataType; + } + + + /** + * Sets the metadata type. + * + * @param metadataType the metadataType to set + */ + public void setMetadataType(String metadataType) { + + this.metadataType = metadataType; + } + + /* (non-Javadoc) + * @see java.lang.Object#toString() + */ + @Override + public String toString() { + + StringBuilder builder = new StringBuilder(); + builder.append("MetadataProfile [id="); + builder.append(id); + builder.append(", name="); + builder.append(name); + builder.append(", description="); + builder.append(description); + builder.append(", metadataType="); + builder.append(metadataType); + builder.append("]"); + return builder.toString(); + } + + +} diff --git a/src/main/java/org/gcube/common/metadataprofilediscovery/jaxb/DataType.java b/src/main/java/org/gcube/common/metadataprofilediscovery/jaxb/DataType.java new file mode 100644 index 0000000..3925035 --- /dev/null +++ b/src/main/java/org/gcube/common/metadataprofilediscovery/jaxb/DataType.java @@ -0,0 +1,54 @@ +/** + * + */ +package org.gcube.common.metadataprofilediscovery.jaxb; + +import javax.xml.bind.annotation.XmlEnum; +import javax.xml.bind.annotation.XmlType; + + +/** + * The Enum DataType. + * + * @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it + * Sep 12, 2016 + */ +@XmlType(name = "dataType") +@XmlEnum +public enum DataType { + + String, + Time, + Time_Interval, + Times_ListOf, + Text, + Boolean, + Number, + GeoJSON; + + /** + * Value. + * + * @return the string + */ + public String value() { + return name(); + } + + /** + * From value. + * + * @param v the v + * @return the data type + */ + public static DataType fromValue(String v) { + DataType vv; + try{ + vv = valueOf(v); + }catch(Exception e){ + return DataType.String; + } + + return vv; + } +} diff --git a/src/main/java/org/gcube/common/metadataprofilediscovery/jaxb/MetadataField.java b/src/main/java/org/gcube/common/metadataprofilediscovery/jaxb/MetadataField.java new file mode 100644 index 0000000..b6de026 --- /dev/null +++ b/src/main/java/org/gcube/common/metadataprofilediscovery/jaxb/MetadataField.java @@ -0,0 +1,389 @@ +/** + * + */ +package org.gcube.common.metadataprofilediscovery.jaxb; + +import java.io.Serializable; + +import javax.xml.bind.annotation.XmlAccessType; +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.adapters.XmlJavaTypeAdapter; + +import org.gcube.common.metadataprofilediscovery.Namespace; +import org.gcube.common.metadataprofilediscovery.adapter.DataTypeAdapter; + + + +/** + * The Class MetadataField. + * + * @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it + * Jun 8, 2016 + */ +@XmlRootElement(name="metadatafield") +@XmlAccessorType(XmlAccessType.FIELD) +public class MetadataField implements Serializable{ + + /** + * + */ + private static final String LOCAL_NAME_CATEGORYREF = "categoryref"; + + /** + * + */ + private static final long serialVersionUID = 5935573474465015727L; + + @XmlAttribute(name=LOCAL_NAME_CATEGORYREF) + private String categoryRef = null; //ITS VALUE IS A CATEGORY-ID + @XmlElement(required = true) + private String fieldName; + @XmlElement(required = true) + private Boolean mandatory = false; + + @XmlElement(name = "maxOccurs") + private String maxOccurs; +// private Boolean isBoolean = false; + @XmlJavaTypeAdapter(DataTypeAdapter.class) + private DataType dataType = DataType.String; + private String defaultValue; + private String note; + + //It's the list of eligible values; + @XmlElement(name = "vocabulary") + private MetadataVocabulary vocabulary; + + @XmlElement(name = "validator") + private MetadataValidator validator; + + @XmlElement(name = "tagging") + private MetadataTagging tagging; + + @XmlElement(name = "grouping") + private MetadataGrouping grouping; + + /** + * Instantiates a new metadata field. + */ + public MetadataField() { + } + + /** + * Instantiates a new metadata field. + * + * @param fieldName the field name + * @param mandatory the mandatory + */ + public MetadataField(String fieldName, Boolean mandatory) { + + super(); + this.fieldName = fieldName; + this.mandatory = mandatory; + } + + + /** + * Instantiates a new metadata field. + * + * @param fieldName the field name + * @param mandatory the mandatory + * @param categoryID the category id + */ + public MetadataField(String fieldName, Boolean mandatory, String categoryID) { + + super(); + this.fieldName = fieldName; + this.mandatory = mandatory; + setCategoryRefToCategoryId(categoryID); + } + + /** + * Gets the category ref. If exists its value is a Category-ID + * + * @return the categoryRef + */ + public String getCategoryRef() { + + return categoryRef; + } + + + /** + * Sets the category ref to category id. + * + * @param categoryID the new category ref to category id + */ + public void setCategoryRefToCategoryId(String categoryID) { + + this.categoryRef = categoryID; + } + + /** + * Gets the category field qualified name. + * + * If the Metadata Field belongs to a category, returns the qualified name: {@link MetadataField#categoryRef} {@link Namespace#Separator}} fieldName; + * Otherwise returns the fieldName + * + * @return the category q name + */ + public String getCategoryFieldQName(){ + + return categoryRef==null?fieldName:categoryRef+Namespace.Separator+fieldName; + + } + + + /** + * Gets the field name. + * + * @return the fieldName + */ + public String getFieldName() { + + return fieldName; + } + + + /** + * Gets the mandatory. + * + * @return the mandatory + */ + public Boolean getMandatory() { + + return mandatory; + } + + + /** + * Gets the data type. + * + * @return the dataType + */ + public DataType getDataType() { + + return dataType; + } + + + /** + * Gets the max occurs. + * + * @return the maxOccurs + */ + public String getMaxOccurs() { + + return maxOccurs; + } + + + /** + * @param maxOccurs the maxOccurs to set + */ + public void setMaxOccurs(String maxOccurs) { + + this.maxOccurs = maxOccurs; + } + + /** + * Gets the default value. + * + * @return the defaultValue + */ + public String getDefaultValue() { + + return defaultValue; + } + + + /** + * Gets the note. + * + * @return the note + */ + public String getNote() { + + return note; + } + + + /** + * Gets the vocabulary. + * + * @return the vocabulary + */ + public MetadataVocabulary getVocabulary() { + + return vocabulary; + } + + + /** + * Gets the validator. + * + * @return the validator + */ + public MetadataValidator getValidator() { + + return validator; + } + + + /** + * Gets the tagging. + * + * @return the tagging + */ + public MetadataTagging getTagging() { + + return tagging; + } + + + /** + * Gets the grouping. + * + * @return the grouping + */ + public MetadataGrouping getGrouping() { + + return grouping; + } + + + /** + * Sets the field name. + * + * @param fieldName the fieldName to set + */ + public void setFieldName(String fieldName) { + + this.fieldName = fieldName; + } + + + /** + * Sets the mandatory. + * + * @param mandatory the mandatory to set + */ + public void setMandatory(Boolean mandatory) { + + this.mandatory = mandatory; + } + + + /** + * Sets the data type. + * + * @param dataType the dataType to set + */ + public void setDataType(DataType dataType) { + + this.dataType = dataType; + } + + + /** + * Sets the default value. + * + * @param defaultValue the defaultValue to set + */ + public void setDefaultValue(String defaultValue) { + + this.defaultValue = defaultValue; + } + + + /** + * Sets the note. + * + * @param note the note to set + */ + public void setNote(String note) { + + this.note = note; + } + + + /** + * Sets the vocabulary. + * + * @param vocabulary the vocabulary to set + */ + public void setVocabulary(MetadataVocabulary vocabulary) { + + this.vocabulary = vocabulary; + } + + + /** + * Sets the validator. + * + * @param validator the validator to set + */ + public void setValidator(MetadataValidator validator) { + + this.validator = validator; + } + + + /** + * Sets the tagging. + * + * @param tagging the tagging to set + */ + public void setTagging(MetadataTagging tagging) { + + this.tagging = tagging; + } + + + /** + * Sets the grouping. + * + * @param grouping the grouping to set + */ + public void setGrouping(MetadataGrouping grouping) { + + this.grouping = grouping; + } + + /* (non-Javadoc) + * @see java.lang.Object#toString() + */ + @Override + public String toString() { + + StringBuilder builder = new StringBuilder(); + builder.append("MetadataField [categoryRef="); + builder.append(categoryRef); + builder.append(", fieldName="); + builder.append(fieldName); + builder.append(", mandatory="); + builder.append(mandatory); + builder.append(", maxOccurs="); + builder.append(maxOccurs); + builder.append(", dataType="); + builder.append(dataType); + builder.append(", defaultValue="); + builder.append(defaultValue); + builder.append(", note="); + builder.append(note); + builder.append(", vocabulary="); + builder.append(vocabulary); + builder.append(", validator="); + builder.append(validator); + builder.append(", tagging="); + builder.append(tagging); + builder.append(", grouping="); + builder.append(grouping); + builder.append("]"); + return builder.toString(); + } + + +} diff --git a/src/main/java/org/gcube/common/metadataprofilediscovery/jaxb/MetadataFormat.java b/src/main/java/org/gcube/common/metadataprofilediscovery/jaxb/MetadataFormat.java new file mode 100644 index 0000000..f0573c5 --- /dev/null +++ b/src/main/java/org/gcube/common/metadataprofilediscovery/jaxb/MetadataFormat.java @@ -0,0 +1,142 @@ +/** + * + */ +package org.gcube.common.metadataprofilediscovery.jaxb; + +import java.io.Serializable; +import java.util.ArrayList; +import java.util.List; + +import javax.xml.bind.annotation.XmlAccessType; +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; + + + +/** + * The Class MetadataFormat. + * + * @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it + * Jun 8, 2016 + */ +@XmlRootElement(name="metadataformat") +@XmlAccessorType(XmlAccessType.FIELD) +public class MetadataFormat implements Serializable{ + + /** + * + */ + private static final long serialVersionUID = -5881074882343424963L; + + 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 = "metadatafield") + private List metadataFields; + + @XmlAttribute(name = LOCAL_NAME_METADATA_TYPE, required=true) + private String type = ""; + + @XmlTransient + private String metadataSource = null; + + + /** + * Gets the metadata fields. + * + * @return the metadataFields + */ + public List getMetadataFields() { + + return metadataFields; + } + + + + /** + * Gets the type. + * + * @return the type + */ + public String getType() { + + return type; + } + + + /** + * Sets the type. + * + * @param type the type to set + */ + public void setType(String type) { + + this.type = type; + } + + /** + * Adds the metadata. + * + * @param Field the field + */ + public void addMetadata(MetadataField Field){ + if(this.metadataFields==null) + this.metadataFields = new ArrayList(); + + this.metadataFields.add(Field); + } + + + /** + * Gets the metadata source. + * + * @return the metadata source + */ + public String getMetadataSource() { + + return metadataSource; + } + + + /** + * Sets the metadata source. + * + * @param metadataSource the new metadata source + */ + public void setMetadataSource(String metadataSource) { + + this.metadataSource = metadataSource; + } + + + + /* (non-Javadoc) + * @see java.lang.Object#toString() + */ + @Override + public String toString() { + + StringBuilder builder = new StringBuilder(); + builder.append("MetadataFormat [metadataFields="); + builder.append(metadataFields); + 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/common/metadataprofilediscovery/jaxb/MetadataGrouping.java b/src/main/java/org/gcube/common/metadataprofilediscovery/jaxb/MetadataGrouping.java new file mode 100644 index 0000000..c4e98cc --- /dev/null +++ b/src/main/java/org/gcube/common/metadataprofilediscovery/jaxb/MetadataGrouping.java @@ -0,0 +1,144 @@ +/** + * + */ +package org.gcube.common.metadataprofilediscovery.jaxb; + +import java.io.Serializable; + +import javax.xml.bind.annotation.XmlAccessType; +import javax.xml.bind.annotation.XmlAccessorType; +import javax.xml.bind.annotation.XmlAttribute; +import javax.xml.bind.annotation.XmlRootElement; +import javax.xml.bind.annotation.XmlValue; +import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; + +import org.gcube.common.metadataprofilediscovery.adapter.TaggingGroupingAdapter; + + +/** + * The Class MetadataGrouping. + * + * @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it + * Mar 21, 2017 + */ +@XmlRootElement(name = "grouping") +@XmlAccessorType (XmlAccessType.FIELD) +public class MetadataGrouping implements Serializable{ + + /** + * + */ + private static final long serialVersionUID = 4684835556193102991L; + + @XmlAttribute + private Boolean create = false; + + @XmlAttribute + private Boolean propagateUp = false; + + @XmlJavaTypeAdapter(TaggingGroupingAdapter.class) + @XmlValue + private TaggingGroupingValue groupingValue = null; + + /** + * Instantiates a new metadata grouping. + */ + public MetadataGrouping() { + } + + + /** + * Instantiates a new metadata grouping. + * + * @param create the create + * @param groupingValue the grouping value + */ + public MetadataGrouping(Boolean create, TaggingGroupingValue groupingValue) { + + this.create = create; + this.groupingValue = groupingValue; + } + + /** + * Gets the creates the. + * + * @return the create + */ + public Boolean getCreate() { + + return create; + } + + + /** + * Gets the grouping value. + * + * @return the groupingValue + */ + public TaggingGroupingValue getGroupingValue() { + + return groupingValue; + } + + + /** + * Sets the creates the. + * + * @param create the create to set + */ + public void setCreate(Boolean create) { + + this.create = create; + } + + + /** + * Sets the grouping value. + * + * @param groupingValue the groupingValue to set + */ + public void setGroupingValue(TaggingGroupingValue groupingValue) { + + this.groupingValue = groupingValue; + } + + + + /** + * @return the propagateUp + */ + public Boolean getPropagateUp() { + + return propagateUp; + } + + + + /** + * @param propagateUp the propagateUp to set + */ + public void setPropagateUp(Boolean propagateUp) { + + this.propagateUp = propagateUp; + } + + + /* (non-Javadoc) + * @see java.lang.Object#toString() + */ + @Override + public String toString() { + + StringBuilder builder = new StringBuilder(); + builder.append("MetadataGrouping [create="); + builder.append(create); + builder.append(", propagateUp="); + builder.append(propagateUp); + builder.append(", groupingValue="); + builder.append(groupingValue); + builder.append("]"); + return builder.toString(); + } + + +} diff --git a/src/main/java/org/gcube/common/metadataprofilediscovery/jaxb/MetadataTagging.java b/src/main/java/org/gcube/common/metadataprofilediscovery/jaxb/MetadataTagging.java new file mode 100644 index 0000000..ba38df4 --- /dev/null +++ b/src/main/java/org/gcube/common/metadataprofilediscovery/jaxb/MetadataTagging.java @@ -0,0 +1,155 @@ +/** + * + */ +package org.gcube.common.metadataprofilediscovery.jaxb; + +import java.io.Serializable; + +import javax.xml.bind.annotation.XmlAccessType; +import javax.xml.bind.annotation.XmlAccessorType; +import javax.xml.bind.annotation.XmlAttribute; +import javax.xml.bind.annotation.XmlRootElement; +import javax.xml.bind.annotation.XmlValue; +import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; + +import org.gcube.common.metadataprofilediscovery.adapter.TaggingGroupingAdapter; + + +/** + * The Class MetadataTagging. + * + * @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it + * Mar 21, 2017 + */ +@XmlRootElement(name = "tagging") +@XmlAccessorType (XmlAccessType.FIELD) +public class MetadataTagging implements Serializable{ + + + public static final String DEFAULT_SEPARATOR = "-"; + + /** + * + */ + private static final long serialVersionUID = 641412896487091012L; + + @XmlAttribute + private Boolean create = false; + + @XmlAttribute + private String separator = DEFAULT_SEPARATOR; + + @XmlJavaTypeAdapter(TaggingGroupingAdapter.class) + @XmlValue + private TaggingGroupingValue taggingValue = null; + + + + /** + * Instantiates a new metadata tagging. + */ + public MetadataTagging() { + } + + + /** + * Instantiates a new metadata tagging. + * + * @param create the create + * @param separator the separator + * @param taggingValue the tagging value + */ + public MetadataTagging(Boolean create, String separator, TaggingGroupingValue taggingValue) { + + super(); + this.create = create; + this.separator = separator; + this.taggingValue = taggingValue; + } + + + /** + * Gets the creates the. + * + * @return the create + */ + public Boolean getCreate() { + + return create; + } + + + /** + * Gets the separator. + * + * @return the separator + */ + public String getSeparator() { + + return separator; + } + + + /** + * Gets the tagging value. + * + * @return the taggingValue + */ + public TaggingGroupingValue getTaggingValue() { + + return taggingValue; + } + + + /** + * Sets the creates the. + * + * @param create the create to set + */ + public void setCreate(Boolean create) { + + this.create = create; + } + + + /** + * Sets the separator. + * + * @param separator the separator to set + */ + public void setSeparator(String separator) { + + this.separator = separator; + } + + + /** + * Sets the tagging value. + * + * @param taggingValue the taggingValue to set + */ + public void setTaggingValue(TaggingGroupingValue taggingValue) { + + this.taggingValue = taggingValue; + } + + + /* (non-Javadoc) + * @see java.lang.Object#toString() + */ + @Override + public String toString() { + + StringBuilder builder = new StringBuilder(); + builder.append("MetadataTagging [create="); + builder.append(create); + builder.append(", separator="); + builder.append(separator); + builder.append(", taggingValue="); + builder.append(taggingValue); + builder.append("]"); + return builder.toString(); + } + + +} diff --git a/src/main/java/org/gcube/common/metadataprofilediscovery/jaxb/MetadataValidator.java b/src/main/java/org/gcube/common/metadataprofilediscovery/jaxb/MetadataValidator.java new file mode 100644 index 0000000..53fdeea --- /dev/null +++ b/src/main/java/org/gcube/common/metadataprofilediscovery/jaxb/MetadataValidator.java @@ -0,0 +1,81 @@ +/** + * + */ +package org.gcube.common.metadataprofilediscovery.jaxb; + +import java.io.Serializable; + +import javax.xml.bind.annotation.XmlAccessType; +import javax.xml.bind.annotation.XmlAccessorType; +import javax.xml.bind.annotation.XmlRootElement; + + +/** + * The Class MetadataValidator. + * + * @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it + * Jun 8, 2016 + */ +@XmlRootElement(name = "metadatavalidator") +@XmlAccessorType (XmlAccessType.FIELD) +public class MetadataValidator implements Serializable{ + + private static final long serialVersionUID = -5394122378302593873L; + private String regularExpression; + + /** + * Instantiates a new metadata validator. + */ + public MetadataValidator() { + + } + + /** + * Instantiates a new metadata validator. + * + * @param regularExpression the regular expression + */ + public MetadataValidator(String regularExpression) { + + super(); + this.regularExpression = regularExpression; + } + + + /** + * Gets the regular expression. + * + * @return the regularExpression + */ + public String getRegularExpression() { + + return regularExpression; + } + + + /** + * Sets the regular expression. + * + * @param regularExpression the regularExpression to set + */ + public void setRegularExpression(String regularExpression) { + + this.regularExpression = regularExpression; + } + + + /* (non-Javadoc) + * @see java.lang.Object#toString() + */ + @Override + public String toString() { + + StringBuilder builder = new StringBuilder(); + builder.append("MetadataValidator [regularExpression="); + builder.append(regularExpression); + builder.append("]"); + return builder.toString(); + } + + +} diff --git a/src/main/java/org/gcube/common/metadataprofilediscovery/jaxb/MetadataVocabulary.java b/src/main/java/org/gcube/common/metadataprofilediscovery/jaxb/MetadataVocabulary.java new file mode 100644 index 0000000..22e52ef --- /dev/null +++ b/src/main/java/org/gcube/common/metadataprofilediscovery/jaxb/MetadataVocabulary.java @@ -0,0 +1,143 @@ +/** + * + */ +package org.gcube.common.metadataprofilediscovery.jaxb; + +import java.io.Serializable; +import java.util.List; + +import javax.xml.bind.annotation.XmlAccessType; +import javax.xml.bind.annotation.XmlAccessorType; +import javax.xml.bind.annotation.XmlAttribute; +import javax.xml.bind.annotation.XmlRootElement; + + +/** + * The Class MetadataVocabulary. + * + * @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it + * Sep 12, 2016 + */ +@XmlRootElement(name = "metadatavocabulary") +@XmlAccessorType (XmlAccessType.FIELD) +public class MetadataVocabulary implements Serializable{ + + /** + * + */ + private static final long serialVersionUID = 506451021936766592L; + + private List vocabularyField; + + @XmlAttribute + private Boolean isMultiSelection = false; + + /** + * Instantiates a new metadata vocabulary. + */ + public MetadataVocabulary() { + } + + + /** + * Instantiates a new metadata vocabulary. + * + * @param vocabularyFields the vocabulary fields + */ + public MetadataVocabulary(List vocabularyFields) { + + super(); + this.vocabularyField = vocabularyFields; + } + + /** + * Instantiates a new metadata vocabulary. + * + * @param vocabularyField the vocabulary field + * @param isMultiSelection the is multi selection + */ + public MetadataVocabulary(List vocabularyField, Boolean isMultiSelection) { + + super(); + this.vocabularyField = vocabularyField; + this.isMultiSelection = isMultiSelection; + } + + + /** + * Gets the vocabulary fields. + * + * @return the vocabularyField + */ + public List getVocabularyFields() { + + return vocabularyField; + } + + + + /** + * Sets the vocabulary field. + * + * @param vocabularyField the vocabularyField to set + */ + public void setVocabularyField(List vocabularyField) { + + this.vocabularyField = vocabularyField; + } + + + + /** + * Gets the vocabulary field. + * + * @return the vocabularyField + */ + public List getVocabularyField() { + + return vocabularyField; + } + + + + /** + * Checks if is multi selection. + * + * @return the isMultiSelection + */ + public Boolean isMultiSelection() { + + return isMultiSelection; + } + + + + /** + * Sets the checks if is multi selection. + * + * @param isMultiSelection the isMultiSelection to set + */ + public void setIsMultiSelection(Boolean isMultiSelection) { + + this.isMultiSelection = isMultiSelection; + } + + + /* (non-Javadoc) + * @see java.lang.Object#toString() + */ + @Override + public String toString() { + + StringBuilder builder = new StringBuilder(); + builder.append("MetadataVocabulary [vocabularyField="); + builder.append(vocabularyField); + builder.append(", isMultiSelection="); + builder.append(isMultiSelection); + builder.append("]"); + return builder.toString(); + } + +} + + diff --git a/src/main/java/org/gcube/common/metadataprofilediscovery/jaxb/NamespaceCategories.java b/src/main/java/org/gcube/common/metadataprofilediscovery/jaxb/NamespaceCategories.java new file mode 100644 index 0000000..8f72105 --- /dev/null +++ b/src/main/java/org/gcube/common/metadataprofilediscovery/jaxb/NamespaceCategories.java @@ -0,0 +1,85 @@ +/** + * + */ +package org.gcube.common.metadataprofilediscovery.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 + * May 19, 2017 + */ +@XmlRootElement(name="namespaces") +@XmlAccessorType(XmlAccessType.FIELD) +public class NamespaceCategories { + + @XmlElement(name = "namespace") + private List namespaceCategories; + + /** + * Instantiates a new namespace categories. + */ + public NamespaceCategories() { + + } + + /** + * Instantiates a new namespace categories. + * + * @param namespaceCategories the namespace categories + */ + public NamespaceCategories(List namespaceCategories) { + this.namespaceCategories = namespaceCategories; + } + + + + + /** + * Gets the namespace categories. + * + * @return the namespaceCategories + */ + public List getNamespaceCategories() { + + return namespaceCategories; + } + + + + + /** + * Sets the namespace categories. + * + * @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/common/metadataprofilediscovery/jaxb/NamespaceCategory.java b/src/main/java/org/gcube/common/metadataprofilediscovery/jaxb/NamespaceCategory.java new file mode 100644 index 0000000..a5d6e5f --- /dev/null +++ b/src/main/java/org/gcube/common/metadataprofilediscovery/jaxb/NamespaceCategory.java @@ -0,0 +1,182 @@ +/** + * + */ +package org.gcube.common.metadataprofilediscovery.jaxb; + +import java.io.Serializable; + +import javax.xml.bind.annotation.XmlAccessType; +import javax.xml.bind.annotation.XmlAccessorType; +import javax.xml.bind.annotation.XmlAttribute; +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlID; +import javax.xml.bind.annotation.XmlRootElement; + +import org.gcube.common.metadataprofilediscovery.Namespace; + + + +/** + * The Class NamespaceCategory. + * + * @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it + * May 19, 2017 + */ +@XmlRootElement(name = "namespace") +@XmlAccessorType (XmlAccessType.FIELD) +public class NamespaceCategory implements Namespace, Serializable{ + + + /** The Constant serialVersionUID. */ + private static final long serialVersionUID = -1632300992065668875L; + + @XmlAttribute(required = true) + @XmlID + private String id = null; + + @XmlElement(required = true) + private String name = null; + + @XmlElement(required = true) + private String title = null; + + private String description = ""; + + /** + * Instantiates a new namespace category. + */ + public NamespaceCategory() { + } + + + + /** + * Instantiates a new namespace category. + * + * @param id the id + * @param name the name + * @param title the title + */ + 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; + } + + /** + * Gets the id. + * + * @return the id + */ + public String getId() { + + return id; + } + + + /** + * Gets the title. + * + * @return the title + */ + public String getTitle() { + + return title; + } + + + /** + * Gets the description. + * + * @return the description + */ + public String getDescription() { + + return description; + } + + + /** + * Sets the id. + * + * @param id the id to set + */ + public void setId(String id) { + + this.id = id; + } + + + /** + * 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 getNamespaceCategoryQName(){ + + return id; + } + + + /** + * Sets the title. + * + * @param title the title to set + */ + public void setTitle(String title) { + + this.title = title; + } + + + /** + * Sets the description. + * + * @param description the description to set + */ + public void setDescription(String description) { + + this.description = description; + } + + + + /* (non-Javadoc) + * @see java.lang.Object#toString() + */ + @Override + public String toString() { + + StringBuilder builder = new StringBuilder(); + 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("]"); + return builder.toString(); + } + +} diff --git a/src/main/java/org/gcube/common/metadataprofilediscovery/jaxb/TaggingGroupingValue.java b/src/main/java/org/gcube/common/metadataprofilediscovery/jaxb/TaggingGroupingValue.java new file mode 100644 index 0000000..cec2c2b --- /dev/null +++ b/src/main/java/org/gcube/common/metadataprofilediscovery/jaxb/TaggingGroupingValue.java @@ -0,0 +1,65 @@ +/** + * + */ +package org.gcube.common.metadataprofilediscovery.jaxb; + +import javax.xml.bind.annotation.XmlEnum; + + +/** + * The Enum TaggingGroupingValue. + * + * @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it + * Mar 21, 2017 + */ +@XmlEnum +public enum TaggingGroupingValue { + + onFieldName("onFieldName"), + onValue("onValue"), + onFieldName_onValue("onFieldName_onValue"), + onValue_onFieldName("onValue_onFieldName"); + + private String value; + + /** + * Instantiates a new tagging grouping value. + * + * @param value the value + */ + TaggingGroupingValue(String value) { + this.value = value; + } + + + /** + * Value. + * + * @return the string + */ + public String value() { + return this.value; + } + + + + /** + * From value. + * + * @param value the value + * @return the TaggingGroupingValue matching the input value or default value {@link TaggingGroupingValue#onValue} + */ + public static TaggingGroupingValue fromValue(String value) { + + try{ + for (TaggingGroupingValue tgv : TaggingGroupingValue.values()) { + if(tgv.value.equals(value)) + return tgv; + } + }catch(Exception e){ + return TaggingGroupingValue.onValue; + } + + return TaggingGroupingValue.onValue; + } +} diff --git a/src/main/java/org/gcube/common/metadataprofilediscovery/reader/MetadataFormatDiscovery.java b/src/main/java/org/gcube/common/metadataprofilediscovery/reader/MetadataFormatDiscovery.java new file mode 100644 index 0000000..fe8fee0 --- /dev/null +++ b/src/main/java/org/gcube/common/metadataprofilediscovery/reader/MetadataFormatDiscovery.java @@ -0,0 +1,197 @@ +package org.gcube.common.metadataprofilediscovery.reader; + +import static org.gcube.resources.discovery.icclient.ICFactory.client; + +import java.io.StringReader; +import java.util.ArrayList; +import java.util.List; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.xpath.XPath; +import javax.xml.xpath.XPathConstants; +import javax.xml.xpath.XPathExpression; +import javax.xml.xpath.XPathFactory; + +import org.gcube.common.metadataprofilediscovery.bean.MetadataProfile; +import org.gcube.common.metadataprofilediscovery.jaxb.MetadataFormat; +import org.gcube.common.resources.gcore.utils.XPathHelper; +import org.gcube.common.scope.impl.ScopeBean; +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.Document; +import org.w3c.dom.Node; +import org.xml.sax.InputSource; + + +/** + * The Class MedataFormatDiscovery. + * + * @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it + * Jun 8, 2016 + */ +public class MetadataFormatDiscovery { + + private static Logger logger = LoggerFactory.getLogger(MetadataFormatDiscovery.class); + public static final String DATA_CATALOGUE_METADATA_SECONDARY_TYPE = "DataCatalogueMetadata"; + private String secondaryType; + private ScopeBean scope; + private List metadataProfiles; + + /** + * Instantiates a new medata format reader. + * + * @param scope - the scope to be searched + * @throws Exception the exception + */ + public MetadataFormatDiscovery(ScopeBean scope) throws Exception { + this.scope = scope; + this.secondaryType = DATA_CATALOGUE_METADATA_SECONDARY_TYPE; + this.metadataProfiles = readMetadataProfilesFromInfrastrucure(); + } + + /** + * this method looks up the applicationProfile profile among the ones available in the infrastructure. + * + * @return the applicationProfile profile + * @throws Exception the exception + */ + private List readMetadataProfilesFromInfrastrucure() throws Exception { + logger.trace("read secondary type: "+secondaryType); + + if(this.scope==null) + throw new Exception("Scope is null"); + + String scopeString = this.scope.toString(); + logger.trace("read scope: "+scopeString); + + List list = new ArrayList(); + + try { + +// ScopeProvider.instance.set(scopeString); +// logger.info("scope provider set instance: "+scopeString); + +// ScopeProvider.instance.set(scopeString); + logger.info("Using scope from ScopeProvider: "+scopeString); + + String queryString = QueryForResourceUtil.getGcubeGenericQueryStringForSecondaryType(secondaryType); + logger.trace("queryString: " +queryString); + Query q = new QueryBox(queryString); + + DiscoveryClient client = client(); + List metaProfile = client.submit(q); + + if (metaProfile == null || metaProfile.size() == 0) + throw new MetadataProfileNotFoundException("Resource/s with secondaryType: "+secondaryType+" is/are not registered in the scope: "+scopeString); + else { + + for (String elem : metaProfile) { + try{ + DocumentBuilder docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); + Document doc = docBuilder.parse(new InputSource(new StringReader(elem))); + Node node = doc.getDocumentElement(); + XPathHelper helper = new XPathHelper(node); + MetadataProfile meta = getMetadataProfileFromResource(doc, helper); + list.add(meta); + }catch(Exception e){ + logger.error("Error while trying to fetch resource with secondary type "+secondaryType+" from the infrastructure, scope: "+scopeString,e); + } + } + } + + } catch (Exception e) { + logger.error("Error while trying to fetch applicationProfile "+secondaryType+" from the infrastructure, "+e); + return list; + } + + return list; + + } + + /** + * Gets the metadata type from resource. + * + * @param helper the helper + * @return the metadata type from resource + * @throws MetadataProfileNotFoundException the application profile not found exception + */ + 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()) + throw new MetadataProfileNotFoundException("Resource ID not found for "+helper.toString()); + else{ + metadataProfile.setId(id.get(0)); + } + + List name = helper.evaluate("/Resource/Profile/Name/text()"); + + if(name==null || name.isEmpty()) + throw new MetadataProfileNotFoundException("Resource Name not found for Resource Id: "+metadataProfile.getId()); + else + metadataProfile.setName(name.get(0)); + + logger.info("Building Profile for Resource Name: "+metadataProfile.getName()); + + List description = helper.evaluate("/Resource/Profile/Description/text()"); + + if(description==null || description.isEmpty()) + logger.info("Description not found for Resource ID: "+metadataProfile.getId()+", continuing.. "); + else + metadataProfile.setDescription(description.get(0)); + + XPathFactory xpathFactory = XPathFactory.newInstance(); + XPath xpath = xpathFactory.newXPath(); + XPathExpression expr = xpath.compile("/Resource/Profile/Body/metadataformat/@"+MetadataFormat.LOCAL_NAME_METADATA_TYPE); + String metadataType = (String) expr.evaluate(doc, XPathConstants.STRING); + + if(metadataType==null || metadataType.isEmpty()) + throw new MetadataProfileNotFoundException("Required attribute '"+MetadataFormat.LOCAL_NAME_METADATA_TYPE+"' not found in the element '"+MetadataFormat.LOCAL_NAME_METADATA_FORMAT+"' for Metadata Profile (within Resource) Id: "+metadataProfile.getId()); + else + metadataProfile.setMetadataType(metadataType); + + return metadataProfile; + + } catch (Exception e) { + logger.error("An error occurred in getMetadataProfileFromResource ", e); + return null; + } + + } + + /** + * Gets the secondary type. + * + * @return the secondary type + */ + public String getSecondaryType() { + return secondaryType; + } + + /** + * Gets the scope. + * + * @return the scope + */ + public ScopeBean getScope() { + return scope; + } + + /** + * Gets the metadata types. + * + * @return the metadataTypes + */ + public List getMetadataProfiles() { + + return metadataProfiles; + } +} diff --git a/src/main/java/org/gcube/common/metadataprofilediscovery/reader/MetadataFormatReader.java b/src/main/java/org/gcube/common/metadataprofilediscovery/reader/MetadataFormatReader.java new file mode 100644 index 0000000..2ee2135 --- /dev/null +++ b/src/main/java/org/gcube/common/metadataprofilediscovery/reader/MetadataFormatReader.java @@ -0,0 +1,258 @@ +package org.gcube.common.metadataprofilediscovery.reader; + +import static org.gcube.resources.discovery.icclient.ICFactory.client; + +import java.io.ByteArrayInputStream; +import java.io.InputStream; +import java.io.StringReader; +import java.util.ArrayList; +import java.util.List; + +import javax.xml.bind.JAXBContext; +import javax.xml.bind.Unmarshaller; +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; + +import org.gcube.common.metadataprofilediscovery.jaxb.MetadataField; +import org.gcube.common.metadataprofilediscovery.jaxb.MetadataFormat; +import org.gcube.common.resources.gcore.utils.XPathHelper; +import org.gcube.common.scope.impl.ScopeBean; +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 MetadataFormatReader. + * + * @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it + * May 19, 2017 + */ +public class MetadataFormatReader { + + private static Logger logger = LoggerFactory.getLogger(MetadataFormatReader.class); + private ScopeBean scope; + private MetadataFormat metadataFormat; + private String resourceID; + + + /** + * Instantiates a new medata format reader. + * + * @param scope the scope + * @param resourceID the resource id + * @throws Exception the exception + */ + public MetadataFormatReader(ScopeBean scope, String resourceID) throws Exception { + this.scope = scope; + this.resourceID = resourceID; + this.metadataFormat = getMetadataFormatByResourceID(resourceID); + } + + + + /** + * Gets the metadata format by id. + * + * @param resourceID the resource id + * @return the metadata format by id + * @throws Exception the exception + */ + private MetadataFormat getMetadataFormatByResourceID(String resourceID) throws Exception { + logger.trace("Getting MedataFormat for resourceID: "+resourceID); + + if(this.scope==null) + throw new Exception("Scope is null"); + + String scopeString = this.scope.toString(); + logger.trace("read scope: "+scopeString); + + MetadataFormat mf = new MetadataFormat(); + + 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 (Metadata Format) with 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.."); + mf = getMetadataFormatFromResource(helper); + String metadataSource = getMetadataFormatSourceFromResource(helper); + mf.setMetadataSource(metadataSource); + }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); + return mf; + } finally{ +// ScopeProvider.instance.reset(); + } + + return mf; + } + + + /** + * Gets the metadata format source from resource. + * + * @param helper the helper + * @return the metadata format source from resource + * @throws Exception the exception + */ + private String getMetadataFormatSourceFromResource(XPathHelper helper) throws Exception{ + + try { + + List metadataFormatSource = helper.evaluate("/Resource/Profile/Body/metadataformat"); + + if(metadataFormatSource==null || metadataFormatSource.isEmpty()) + throw new Exception("metadataformat not found in the body, consider to add "); + + return metadataFormatSource.get(0); + + }catch(Exception e){ + logger.error("An error occurred in getMetadataFormatSourceFromResource ", e); + return null; + } + } + + + + /** + * Gets the metadata format from resource. + * + * @param helper the helper + * @return the metadata format from resource + * @throws Exception the exception + */ + private MetadataFormat getMetadataFormatFromResource(XPathHelper helper) throws Exception{ + + try { + + List metadataFormatSource = helper.evaluate("/Resource/Profile/Body/metadataformat"); + + JAXBContext jaxbContext = JAXBContext.newInstance(MetadataFormat.class); + Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller(); + + if(metadataFormatSource==null || metadataFormatSource.size()==0) + throw new Exception("Resource does not contain in the body"); + + InputStream stream = new ByteArrayInputStream(metadataFormatSource.get(0).getBytes()); + MetadataFormat mtdf = (MetadataFormat) jaxbUnmarshaller.unmarshal(stream); + logger.debug("returning metadata format with medata type: "+mtdf.getType()); + + return mtdf; + + }catch(Exception e){ + logger.error("An error occurred in getMetadataFormatSourceFromResource ", e); + return null; + } + } + + + + /** + * Gets the metadata fields from resource. + * + * @param helper the helper + * @return the metadata fields from resource + * @throws Exception the exception + */ + private List getMetadataFieldsFromResource(XPathHelper helper) throws Exception{ + + try { + + List metadatafields = helper.evaluate("/Resource/Profile/Body/metadataformat/metadatafield"); + + if(metadatafields==null || metadatafields.isEmpty()) + throw new Exception("Metadata fields not found"); + + JAXBContext jaxbContext = JAXBContext.newInstance(MetadataField.class); + Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller(); + +// for (String string : metadatafields) { +// logger.debug("mf "+string); +// } + + List mfs = new ArrayList(); + logger.debug("MetadataFields are: "+metadatafields.size()); + + for (String mfield : metadatafields) { + try{ + InputStream stream = new ByteArrayInputStream(mfield.getBytes()); + MetadataField mtdf = (MetadataField) jaxbUnmarshaller.unmarshal(stream); + logger.debug("Unmarshalled: "+mtdf); + mfs.add(mtdf); + }catch(Exception e){ + logger.error("An error occurred during unmarshall performing on "+mfield+", skipping this metadatafield"); + } + } + + return mfs; + + } catch (Exception e) { + logger.error("An error occurred in getMetadataFieldsFromResource ", e); + return null; + } + + } + + + + /** + * Gets the scope. + * + * @return the scope + */ + public ScopeBean getScope() { + + return scope; + } + + + /** + * Gets the metadata format. + * + * @return the metadataFormat + */ + public MetadataFormat getMetadataFormat() { + + return metadataFormat; + } + + + /** + * Gets the resource id. + * + * @return the resourceID + */ + public String getResourceID() { + + return resourceID; + } +} diff --git a/src/main/java/org/gcube/common/metadataprofilediscovery/reader/MetadataProfileNotFoundException.java b/src/main/java/org/gcube/common/metadataprofilediscovery/reader/MetadataProfileNotFoundException.java new file mode 100644 index 0000000..1a7a701 --- /dev/null +++ b/src/main/java/org/gcube/common/metadataprofilediscovery/reader/MetadataProfileNotFoundException.java @@ -0,0 +1,20 @@ +package org.gcube.common.metadataprofilediscovery.reader; + +/** + * The Class ApplicationProfileNotFoundException. + * + * @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it + * Jun 7, 2016 + */ +@SuppressWarnings("serial") +public class MetadataProfileNotFoundException extends Exception { + + /** + * Instantiates a new application profile not found exception. + * + * @param message the message + */ + public MetadataProfileNotFoundException(String message) { + super(message); + } +} \ No newline at end of file diff --git a/src/main/java/org/gcube/common/metadataprofilediscovery/reader/NamespaceCategoryReader.java b/src/main/java/org/gcube/common/metadataprofilediscovery/reader/NamespaceCategoryReader.java new file mode 100644 index 0000000..95303bf --- /dev/null +++ b/src/main/java/org/gcube/common/metadataprofilediscovery/reader/NamespaceCategoryReader.java @@ -0,0 +1,167 @@ +/** + * + */ +package org.gcube.common.metadataprofilediscovery.reader; + +import static org.gcube.resources.discovery.icclient.ICFactory.client; + +import java.io.ByteArrayInputStream; +import java.io.InputStream; +import java.io.StringReader; +import java.util.List; + +import javax.xml.bind.JAXBContext; +import javax.xml.bind.Unmarshaller; +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; + +import org.gcube.common.metadataprofilediscovery.jaxb.NamespaceCategories; +import org.gcube.common.resources.gcore.utils.XPathHelper; +import org.gcube.common.scope.impl.ScopeBean; +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 { + + 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 NamespaceCategories namespaces; + + /** + * Instantiates a new medata format reader. + * + * @param scope the scope + * @throws Exception the exception + */ + public NamespaceCategoryReader(ScopeBean scope) throws Exception { + this.scope = scope; + readNamespaceCategory(); + } + + /** + * Gets the metadata format by id. + * + * @return the metadata format by id + * @throws Exception the exception + */ + private void readNamespaceCategory() throws Exception { + logger.trace("Getting Namespace Category"); + + 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.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); + + if (appProfile == null || appProfile.size() == 0) + 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.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); + 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 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{ + } + } + + + /** + * 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); + } + } + + /** + * 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(", namespaces="); + builder.append(namespaces); + builder.append("]"); + return builder.toString(); + } + +} diff --git a/src/main/java/org/gcube/common/metadataprofilediscovery/reader/PropertyFileNotFoundException.java b/src/main/java/org/gcube/common/metadataprofilediscovery/reader/PropertyFileNotFoundException.java new file mode 100644 index 0000000..2498811 --- /dev/null +++ b/src/main/java/org/gcube/common/metadataprofilediscovery/reader/PropertyFileNotFoundException.java @@ -0,0 +1,8 @@ +package org.gcube.common.metadataprofilediscovery.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/common/metadataprofilediscovery/reader/QueryForResourceUtil.java b/src/main/java/org/gcube/common/metadataprofilediscovery/reader/QueryForResourceUtil.java new file mode 100644 index 0000000..43b67b4 --- /dev/null +++ b/src/main/java/org/gcube/common/metadataprofilediscovery/reader/QueryForResourceUtil.java @@ -0,0 +1,58 @@ +/** + * + */ +package org.gcube.common.metadataprofilediscovery.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"; + } + + /** + * 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"; + } + +} diff --git a/src/main/resources/Gdcmetadataprofilev3.xsd b/src/main/resources/Gdcmetadataprofilev3.xsd new file mode 100644 index 0000000..28789ed --- /dev/null +++ b/src/main/resources/Gdcmetadataprofilev3.xsd @@ -0,0 +1,92 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/main/resources/NamespacesCatalogueCategories.xsd b/src/main/resources/NamespacesCatalogueCategories.xsd new file mode 100644 index 0000000..d6db0b8 --- /dev/null +++ b/src/main/resources/NamespacesCatalogueCategories.xsd @@ -0,0 +1,28 @@ + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/test/java/org/gcube/common/metadataprofilediscovery/TestDataCatalogueMetadataFormatReader.java b/src/test/java/org/gcube/common/metadataprofilediscovery/TestDataCatalogueMetadataFormatReader.java new file mode 100644 index 0000000..ca273fd --- /dev/null +++ b/src/test/java/org/gcube/common/metadataprofilediscovery/TestDataCatalogueMetadataFormatReader.java @@ -0,0 +1,70 @@ +/** + * + */ +package org.gcube.common.metadataprofilediscovery; + +import java.io.InputStream; +import java.util.List; + +import org.gcube.common.metadataprofilediscovery.bean.MetadataProfile; +import org.gcube.common.metadataprofilediscovery.jaxb.MetadataFormat; +import org.gcube.common.metadataprofilediscovery.jaxb.NamespaceCategory; +import org.gcube.common.scope.api.ScopeProvider; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + + +/** + * The Class TestDataCatalogueMetadataFormatReader. + * + * @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it + * Jun 8, 2016 + */ +public class TestDataCatalogueMetadataFormatReader { + + private static final Logger logger = LoggerFactory.getLogger(TestDataCatalogueMetadataFormatReader.class); + + /** + * Test. + */ +// @Test + public void test() { + try { + String scopeString = "/gcube/devsec/devVRE"; + ScopeProvider.instance.set(scopeString); + DataCalogueMetadataFormatReader reader = new DataCalogueMetadataFormatReader(); + int i = 0; + + 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(); + } + } + + + public static String PROFILE_EXAMPLE_FILENAME = "EmptyProfileExample.xml"; + +// @Test + public void validateAgainstProfileSchema() throws Exception { + InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(PROFILE_EXAMPLE_FILENAME); + DataCalogueMetadataFormatReader.validateProfile(inputStream); + } +} + + diff --git a/src/test/java/org/gcube/common/metadataprofilediscovery/TestJaxbMetadataMarshUnmarsh.java b/src/test/java/org/gcube/common/metadataprofilediscovery/TestJaxbMetadataMarshUnmarsh.java new file mode 100644 index 0000000..d5d1858 --- /dev/null +++ b/src/test/java/org/gcube/common/metadataprofilediscovery/TestJaxbMetadataMarshUnmarsh.java @@ -0,0 +1,210 @@ +/** + * + */ +package org.gcube.common.metadataprofilediscovery; + +import java.io.File; +import java.util.ArrayList; + +import javax.xml.bind.JAXBContext; +import javax.xml.bind.JAXBException; +import javax.xml.bind.Marshaller; +import javax.xml.bind.Unmarshaller; + +import org.gcube.common.metadataprofilediscovery.jaxb.MetadataField; +import org.gcube.common.metadataprofilediscovery.jaxb.MetadataFormat; +import org.gcube.common.metadataprofilediscovery.jaxb.NamespaceCategories; +import org.gcube.common.metadataprofilediscovery.jaxb.NamespaceCategory; +import org.junit.Test; + +/** + * The Class TestJaxbMetadataMarshUnmarsh. + * + * @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it + * Jun 7, 2016 + */ +public class TestJaxbMetadataMarshUnmarsh { + + static MetadataField ckanMetadata; + static MetadataFormat metadatas; + static String tmpFileXML = "." + File.separator + "CkanMetadatas.xml"; + static String tmpCategoriesXML = "." + File.separator + "NamespacesCatalogueCategories.xml"; + private ArrayList ckanCategories; + public static int MAX_CATEGORIES = 3; + + /** + * Test. + */ +// @Test + public void test() { + + /*metadatas = new MetadataFormat(); + + ckanCategories = new ArrayList(); + + for (int i=0; i vocabulary = new ArrayList(); + vocabulary.add("field1"); + vocabulary.add("field2"); + vocabulary.add("field3"); + MetadataVocabulary cvc = new MetadataVocabulary(vocabulary); + ckanMetadata.setVocabulary(cvc); + cvc.setIsMultiSelection(true); + ckanMetadata.setNote("shown as suggestions in the insert/update metadata form of CKAN"); + MetadataValidator validator = new MetadataValidator("a regular expression for validating values"); + ckanMetadata.setValidator(validator); + metadatas.addMetadata(ckanMetadata); + + ckanMetadata = new MetadataField(); + ckanMetadata.setFieldName("Accessibility"); + ckanMetadata.setMandatory(true); + ckanMetadata.setDefaultValue("virtual/public"); + + ckanMetadata.setTagging(new MetadataTagging(true, ":", TaggingGroupingValue.onValue_onFieldName)); + + MetadataGrouping grouping = new MetadataGrouping(); + grouping.setCreate(true); + grouping.setGroupingValue(TaggingGroupingValue.onValue); + ckanMetadata.setGrouping(grouping); + List vocabulary2 = new ArrayList(); + vocabulary2.add("virtual/public"); + vocabulary2.add("virtual/private"); + vocabulary2.add("transactional"); + MetadataVocabulary cvc2 = new MetadataVocabulary(vocabulary2); + ckanMetadata.setVocabulary(cvc2); + ckanMetadata.setNote("shown as suggestions in the insert metadata form of CKAN"); + MetadataValidator validator2 = new MetadataValidator("a regular expression for validating values"); + ckanMetadata.setValidator(validator2); + + metadatas.addMetadata(ckanMetadata);*/ + + try { + + //marshalingExample(); + unMarshalingMetadataFields(); + //unMarshalingCategories(); + } + catch (JAXBException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + /** + * Marshaling example. + * + * @throws JAXBException the JAXB exception + */ + private static void marshalingExample() throws JAXBException + { + JAXBContext jaxbContext = JAXBContext.newInstance(MetadataFormat.class); + Marshaller jaxbMarshaller = jaxbContext.createMarshaller(); + + jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); + + //Marshal the employees list in console +// jaxbMarshaller.marshal(employees, System.out); + + //Marshal the employees list in file + jaxbMarshaller.marshal(metadatas, new File(tmpFileXML)); + } + + + /** + * Un marshaling example. + * + * @throws JAXBException the JAXB exception + */ + private static void unMarshalingMetadataFields() throws JAXBException + { + //unMarshalingCategories(); + JAXBContext jaxbContext = JAXBContext.newInstance(MetadataFormat.class); + Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller(); + + //We had written this file in marshalling example + MetadataFormat mtds = (MetadataFormat) jaxbUnmarshaller.unmarshal(new File(tmpFileXML)); + + System.out.println("Metadata Format"); + 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("\n\nMetadata Fields"); + for (MetadataField field : mtds.getMetadataFields()) { + System.out.println(field); + System.out.println("Category Ref: "+field.getCategoryRef()); + System.out.println("Category CategoryField Q Name: "+field.getCategoryFieldQName()); + System.out.println("QName: "+field.getCategoryFieldQName()); + System.out.println("MaxOccurs: "+field.getMaxOccurs()); + +// System.out.println(field.getGrouping()); +// System.out.println(field.getTagging()); + } + } + + + /** + * 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")); + try { + unMarshalingMetadataFields(); + } + catch (JAXBException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } +} diff --git a/src/test/java/org/gcube/common/metadataprofilediscovery/TestMetadataFormatDiscovery.java b/src/test/java/org/gcube/common/metadataprofilediscovery/TestMetadataFormatDiscovery.java new file mode 100644 index 0000000..2f88da4 --- /dev/null +++ b/src/test/java/org/gcube/common/metadataprofilediscovery/TestMetadataFormatDiscovery.java @@ -0,0 +1,43 @@ +/** + * + */ +package org.gcube.common.metadataprofilediscovery; + +import org.gcube.common.metadataprofilediscovery.bean.MetadataProfile; +import org.gcube.common.metadataprofilediscovery.reader.MetadataFormatDiscovery; +import org.gcube.common.scope.api.ScopeProvider; +import org.gcube.common.scope.impl.ScopeBean; +import org.junit.Test; + + +/** + * The Class TestMetadataFormatDiscovery. + * + * @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it + * Jun 8, 2016 + */ +public class TestMetadataFormatDiscovery { + + /** + * Test. + */ +// @Test + public void test() { + + String scopeString = "/gcube/devsec/devVRE"; + final ScopeBean scope = new ScopeBean(scopeString); + MetadataFormatDiscovery reader; + try { + ScopeProvider.instance.set(scopeString); + reader = new MetadataFormatDiscovery(scope); + //System.out.println(reader.getMetadataProfiles()); + + for (MetadataProfile metaProfile : reader.getMetadataProfiles()) { + System.out.println(metaProfile.getId() +", name: "+metaProfile.getName() +", type: "+metaProfile.getMetadataType()); + } + + } catch (Exception e) { + e.printStackTrace(); + } + } +} diff --git a/src/test/java/org/gcube/common/metadataprofilediscovery/TestMetadataFormatReader.java b/src/test/java/org/gcube/common/metadataprofilediscovery/TestMetadataFormatReader.java new file mode 100644 index 0000000..2ced19f --- /dev/null +++ b/src/test/java/org/gcube/common/metadataprofilediscovery/TestMetadataFormatReader.java @@ -0,0 +1,33 @@ +/** + * + */ +package org.gcube.common.metadataprofilediscovery; + +import org.gcube.common.metadataprofilediscovery.reader.MetadataFormatReader; +import org.gcube.common.scope.api.ScopeProvider; +import org.gcube.common.scope.impl.ScopeBean; +import org.junit.Test; + + +/** + * + * @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it + * Jun 8, 2016 + */ +public class TestMetadataFormatReader { + +// @Test + public void test() { + + String scopeString = "/gcube/devsec/devVRE"; + final ScopeBean scope = new ScopeBean(scopeString); + MetadataFormatReader reader; + try { + ScopeProvider.instance.set(scopeString); + reader = new MetadataFormatReader(scope, "0d29d7a9-d779-478c-a13d-d70708dc66c4"); + System.out.println(reader.getMetadataFormat()); + } catch (Exception e) { + e.printStackTrace(); + } + } +}