diff --git a/.classpath b/.classpath index d64ebcb..dc47709 100644 --- a/.classpath +++ b/.classpath @@ -32,7 +32,7 @@ - + diff --git a/.settings/org.eclipse.jdt.core.prefs b/.settings/org.eclipse.jdt.core.prefs index e2ceea1..8b5c4dc 100644 --- a/.settings/org.eclipse.jdt.core.prefs +++ b/.settings/org.eclipse.jdt.core.prefs @@ -3,12 +3,14 @@ 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.compliance=1.8 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.enablePreviewFeatures=disabled org.eclipse.jdt.core.compiler.problem.enumIdentifier=error org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning +org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=ignore org.eclipse.jdt.core.compiler.release=disabled -org.eclipse.jdt.core.compiler.source=1.7 +org.eclipse.jdt.core.compiler.source=1.8 diff --git a/.settings/org.eclipse.wst.common.component b/.settings/org.eclipse.wst.common.component new file mode 100644 index 0000000..bcc91bc --- /dev/null +++ b/.settings/org.eclipse.wst.common.component @@ -0,0 +1 @@ + diff --git a/.settings/org.eclipse.wst.common.project.facet.core.xml b/.settings/org.eclipse.wst.common.project.facet.core.xml index 1b22d70..f4bf050 100644 --- a/.settings/org.eclipse.wst.common.project.facet.core.xml +++ b/.settings/org.eclipse.wst.common.project.facet.core.xml @@ -1,5 +1,5 @@ - + diff --git a/.settings/org.eclipse.wst.validation.prefs b/.settings/org.eclipse.wst.validation.prefs new file mode 100644 index 0000000..04cad8c --- /dev/null +++ b/.settings/org.eclipse.wst.validation.prefs @@ -0,0 +1,2 @@ +disabled=06target +eclipse.preferences.version=1 diff --git a/CHANGELOG.md b/CHANGELOG.md index 04aa045..88c00fc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,8 +4,17 @@ All notable changes to this project will be documented in this file. This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [v1.1.0-SNAPSHOT] - 2022-11-09 + +#### Enhancement + +- [#22890] Extend the MetadataDiscovery logic +- [#23537] Introduce the fieldId in the gCube Metadata Profile +- Moved to maven-portal-bom 3.6.4 +- [#24110] Added dependency required for building with JDK_11 + ## [v1.0.0] - 2020-09-30 #### First release -[#19880] Create the library metadata-profile-discovery \ No newline at end of file +- [#19880] Create the library metadata-profile-discovery \ No newline at end of file diff --git a/pom.xml b/pom.xml index bb0ca56..c72987d 100644 --- a/pom.xml +++ b/pom.xml @@ -12,7 +12,7 @@ org.gcube.common metadata-profile-discovery - 1.0.0 + 1.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: @@ -43,7 +43,7 @@ org.gcube.distribution maven-portal-bom - 3.6.0 + 3.7.0-SNAPSHOT pom import @@ -54,7 +54,7 @@ UTF-8 UTF-8 - 1.7 + 1.8 1.8 @@ -99,6 +99,21 @@ provided + + + + javax.xml.bind + jaxb-api + 2.3.0 + provided + + + + + com.google.guava + guava + + junit @@ -108,12 +123,12 @@ - - - - - - + + + + + + diff --git a/src/main/java/org/gcube/common/metadataprofilediscovery/MetadataProfileReader.java b/src/main/java/org/gcube/common/metadataprofilediscovery/MetadataProfileReader.java index a199d5e..ce30511 100644 --- a/src/main/java/org/gcube/common/metadataprofilediscovery/MetadataProfileReader.java +++ b/src/main/java/org/gcube/common/metadataprofilediscovery/MetadataProfileReader.java @@ -28,10 +28,10 @@ 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.NamespaceCategories; 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; @@ -40,13 +40,12 @@ import org.w3c.dom.Document; import org.xml.sax.InputSource; import org.xml.sax.SAXException; - /** * The Class MetadataProfileReader. * - * @author Francesco Mangiacrapa at ISTI-CNR (francesco.mangiacrapa@isti.cnr.it) + * @author Francesco Mangiacrapa at ISTI-CNR francesco.mangiacrapa@isti.cnr.it * - * Sep 30, 2020 + * Mar 1, 2022 */ public class MetadataProfileReader implements MetadataProfileDiscovery { @@ -63,22 +62,61 @@ public class MetadataProfileReader implements MetadataProfileDiscovery { private String grMetadataProfileSecondaryType; + private String grMetadataProfileResourceName; + private static Logger logger = LoggerFactory.getLogger(MetadataProfileReader.class); - - + /** * Instantiates a new metadata profile reader. * - * @param grMetadataProfileSecondaryType the SecondaryType that must be used to discover the "gCube Metadata Profiles" from Information System + * @param grMetadataProfileSecondaryType the SecondaryType that must be used to + * discover the "gCube Metadata Profiles" + * from Information System * @throws Exception the exception */ public MetadataProfileReader(String grMetadataProfileSecondaryType) throws Exception { - - if(grMetadataProfileSecondaryType==null || grMetadataProfileSecondaryType.isEmpty()) - throw new NullPointerException("Invalid input parameter"); - + + if (grMetadataProfileSecondaryType == null || grMetadataProfileSecondaryType.isEmpty()) + throw new NullPointerException("Invalid input parameter: grMetadataProfileSecondaryType"); + this.grMetadataProfileSecondaryType = grMetadataProfileSecondaryType; - + + readNamespacesAndProfiles(); + } + + /** + * Instantiates a new metadata profile reader. + * + * @param grMetadataProfileSecondaryType the gr metadata profile secondary type + * @param resourceName the resource name + * + * the SecondaryType and the ResourceName + * that must be used to discover the + * "gCube Metadata Profiles" from + * Information System + * @throws Exception the exception + */ + public MetadataProfileReader(String grMetadataProfileSecondaryType, String resourceName) throws Exception { + + if (grMetadataProfileSecondaryType == null || grMetadataProfileSecondaryType.isEmpty()) + throw new NullPointerException("Invalid input parameter: grMetadataProfileSecondaryType"); + + if (resourceName == null || resourceName.isEmpty()) + throw new NullPointerException("Invalid input parameter: resourceName"); + + this.grMetadataProfileSecondaryType = grMetadataProfileSecondaryType; + this.grMetadataProfileResourceName = resourceName; + + readNamespacesAndProfiles(); + } + + /** + * Read namespaces and profiles. + * + * @throws Exception the exception + */ + private void readNamespacesAndProfiles() throws Exception { + String scopeString = ScopeProvider.instance.get(); logger.debug("Read scope " + scopeString + " from ScopeProvider"); @@ -86,20 +124,22 @@ public class MetadataProfileReader implements MetadataProfileDiscovery { throw new Exception("Please set a valid scope into ScopeProvider"); scope = new ScopeBean(scopeString); - + readNamespaces(); readMetadataFormats(); } - + /** * Read metada formats. * * @throws Exception the exception */ private void readMetadataFormats() throws Exception { - - //reading from Generic Resource for Secondary Type passed in input - medataFormatDiscovery = new MetadataFormatDiscovery(scope, grMetadataProfileSecondaryType); + + // reading from Generic Resource for Secondary Type and Resource Name passed in + // input + medataFormatDiscovery = new MetadataFormatDiscovery(scope, grMetadataProfileSecondaryType, + grMetadataProfileResourceName); logger.info("MedataFormatDiscovery has retrieved: " + medataFormatDiscovery.getMetadataProfiles().size() + " metadata type/s"); @@ -113,12 +153,13 @@ public class MetadataProfileReader implements MetadataProfileDiscovery { hashMetadataFormats.put(mT.getId(), reader.getMetadataFormat()); logger.debug("MetadataType id: " + mT.getId() + " cached as: " + reader.getMetadataFormat()); } - + } /** * Read namespaces. - * @throws Exception + * + * @throws Exception the exception */ private void readNamespaces() throws Exception { @@ -128,11 +169,11 @@ public class MetadataProfileReader implements MetadataProfileDiscovery { if (namespaceCategories == null) namespaceCategories = new ArrayList(); - NamespaceCategoryReader rd = new NamespaceCategoryReader(scope); - namespaceCategories.addAll(rd.getNamespaces().getNamespaceCategories()); + NamespaceCategories ncCache = NamespaceCategoriesCache.loadNamespaces(scope.toString()); + namespaceCategories.addAll(ncCache.getNamespaceCategories()); } } catch (Exception e) { - logger.debug("An error occurred during read namespaces for categories: ", e); + logger.warn("An error occurred during read namespaces for categories: ", e); throw e; } } @@ -154,13 +195,13 @@ public class MetadataProfileReader implements MetadataProfileDiscovery { */ public static InputStream getProfileSchemaInputStream() { try { - logger.info("Trying to return the URL resource: "+URL_OF_GCDCMETADATAPROFILEV3_XSD); + logger.info("Trying to return the URL resource: " + URL_OF_GCDCMETADATAPROFILEV3_XSD); return new URL(URL_OF_GCDCMETADATAPROFILEV3_XSD).openStream(); - + } catch (Exception e) { - logger.warn("Error on reading the URL: "+URL_OF_GCDCMETADATAPROFILEV3_XSD); + logger.warn("Error on reading the URL: " + URL_OF_GCDCMETADATAPROFILEV3_XSD); } - logger.info("Returning local resource: "+SCHEMA_FILENAME); + logger.info("Returning local resource: " + SCHEMA_FILENAME); return MetadataProfileReader.class.getResourceAsStream(SCHEMA_FILENAME); } @@ -171,13 +212,13 @@ public class MetadataProfileReader implements MetadataProfileDiscovery { */ public static URL getProfileSchemaURL() { try { - logger.info("Trying to return the URL object: "+URL_OF_GCDCMETADATAPROFILEV3_XSD); + logger.info("Trying to return the URL object: " + URL_OF_GCDCMETADATAPROFILEV3_XSD); return new URL(URL_OF_GCDCMETADATAPROFILEV3_XSD); - + } catch (Exception e) { - logger.warn("Error on reading the URL: "+URL_OF_GCDCMETADATAPROFILEV3_XSD); + logger.warn("Error on reading the URL: " + URL_OF_GCDCMETADATAPROFILEV3_XSD); } - logger.info("Returning local URL of: "+SCHEMA_FILENAME); + logger.info("Returning local URL of: " + SCHEMA_FILENAME); return MetadataProfileReader.class.getResource(SCHEMA_FILENAME); } @@ -206,6 +247,13 @@ public class MetadataProfileReader implements MetadataProfileDiscovery { validateAgainstXSD(new StreamSource(xml), getProfileSchemaURL()); } + /** + * Gets the metadata format for metadata profile. + * + * @param profile the profile + * @return the metadata format for metadata profile + * @throws Exception the exception + */ /* * (non-Javadoc) * @@ -227,6 +275,12 @@ public class MetadataProfileReader implements MetadataProfileDiscovery { return reader.getMetadataFormat(); } + /** + * Gets the list of metadata profiles. + * + * @return the list of metadata profiles + * @throws Exception the exception + */ /* * (non-Javadoc) * @@ -242,6 +296,12 @@ public class MetadataProfileReader implements MetadataProfileDiscovery { return medataFormatDiscovery.getMetadataProfiles(); } + /** + * Gets the list of namespace categories. + * + * @return the list of namespace categories + * @throws Exception the exception + */ /* * (non-Javadoc) * @@ -257,6 +317,9 @@ public class MetadataProfileReader implements MetadataProfileDiscovery { return namespaceCategories; } + /** + * Reset metadata profile. + */ /* * (non-Javadoc) * @@ -270,6 +333,9 @@ public class MetadataProfileReader implements MetadataProfileDiscovery { hashMetadataFormats = null; } + /** + * Reset namespace categories. + */ /* * (non-Javadoc) * @@ -282,6 +348,11 @@ public class MetadataProfileReader implements MetadataProfileDiscovery { namespaceCategories = null; } + /** + * Gets the profile schema. + * + * @return the profile schema + */ /* * (non-Javadoc) * @@ -297,6 +368,15 @@ public class MetadataProfileReader implements MetadataProfileDiscovery { } + /** + * Validate profile. + * + * @param xmlProfile the xml profile + * @throws ParserConfigurationException the parser configuration exception + * @throws SAXException the SAX exception + * @throws IOException Signals that an I/O exception has + * occurred. + */ /* * (non-Javadoc) * @@ -310,23 +390,23 @@ public class MetadataProfileReader implements MetadataProfileDiscovery { DOMSource source = new DOMSource(doc); validateAgainstXSD(source, getProfileSchemaURL()); } - + /** - * Utility method to get a metadata format from a "gCube Metadata Profiles" model (XML-based) + * Utility method to get a metadata format from a "gCube Metadata Profiles" + * model (XML-based). * * @param metadataProfileStream the metadata profile stream * @return the metadata format * @throws JAXBException the JAXB exception */ - public static MetadataFormat toMetadataFormat(InputStream metadataProfileStream) throws JAXBException{ - - if(metadataProfileStream==null) + public static MetadataFormat toMetadataFormat(InputStream metadataProfileStream) throws JAXBException { + + if (metadataProfileStream == null) throw new NullPointerException("Invalid input parameter"); - + JAXBContext jaxbContext = JAXBContext.newInstance(MetadataFormat.class); - Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller(); - return (MetadataFormat) jaxbUnmarshaller.unmarshal(metadataProfileStream); + Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller(); + return (MetadataFormat) jaxbUnmarshaller.unmarshal(metadataProfileStream); } - } diff --git a/src/main/java/org/gcube/common/metadataprofilediscovery/NamespaceCategoriesCache.java b/src/main/java/org/gcube/common/metadataprofilediscovery/NamespaceCategoriesCache.java new file mode 100644 index 0000000..dcbec15 --- /dev/null +++ b/src/main/java/org/gcube/common/metadataprofilediscovery/NamespaceCategoriesCache.java @@ -0,0 +1,145 @@ +/** + * + */ +package org.gcube.common.metadataprofilediscovery; + +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; + +import org.gcube.common.metadataprofilediscovery.jaxb.NamespaceCategories; +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 com.google.common.cache.CacheBuilder; +import com.google.common.cache.CacheLoader; +import com.google.common.cache.LoadingCache; +import com.google.common.cache.RemovalListener; +import com.google.common.cache.RemovalNotification; + +/** + * The Class NamespaceCategoriesCache. + * + * @author Francesco Mangiacrapa at ISTI-CNR francesco.mangiacrapa@isti.cnr.it + * + * Mar 3, 2022 + */ +public class NamespaceCategoriesCache { + + private static Logger LOG = LoggerFactory.getLogger(NamespaceCategoriesCache.class); + private static LoadingCache namespaces; + + static { + + CacheLoader loader = new CacheLoader() { + + @Override + public NamespaceCategories load(String scope) throws Exception { + + LOG.info("Loading the NamespaceCategories cache for scope: {}", scope); + NamespaceCategories namespacesCat = loadNamespaces(scope); + if (namespacesCat != null) + LOG.info("Returning {} for the scope name: {}", NamespaceCategories.class.getSimpleName(), scope); + else { + LOG.info("No NamespaceCategories obj for scope {}", scope); + } + + return namespacesCat; + } + + }; + + RemovalListener removalListener = new RemovalListener() { + + @Override + public void onRemoval(RemovalNotification arg0) { + + LOG.debug("cache expired"); + } + }; + + namespaces = CacheBuilder.newBuilder().maximumSize(300).expireAfterWrite(30, TimeUnit.MINUTES) + .removalListener(removalListener).build(loader); + + } + + /** + * Populate the cache. + * + * @param scope the scope + */ + private static void populateTheCache(ScopeBean scope) { + String origScope = null; + String instScope = scope.toString(); + try { + origScope = ScopeProvider.instance.get(); + // Populating the cache by using the detachedres-library + ScopeProvider.instance.set(scope.toString()); + LOG.info("Trying to populate the Namespaces cache in the scope: "+instScope); + NamespaceCategoryReader rd = new NamespaceCategoryReader(scope); + namespaces.put(instScope, rd.getNamespaces()); + } catch (Exception e) { + // SILENT + } finally { + + if (origScope != null && !origScope.equals(scope.toString())) { + // Setting original scope + ScopeProvider.instance.set(scope.toString()); + } + + } + } + + /** + * Gets the VRE obj for input VRE name. + * + * @param scope the scope + * @return the vre + * @throws ExecutionException the execution exception + */ + public static NamespaceCategories get(String scope) throws ExecutionException { + + try { + return namespaces.get(scope); + } catch (Exception e) { + LOG.info("Error on getting NamespaceCategories obj for scope {}. Is the key {} not found in the cache?", + scope, namespaces); + throw e; + } + } + + /** + * Load VRE obj for VRE name. + * + * @param scope the scope + * @return the vre + */ + protected static NamespaceCategories loadNamespaces(String scope) { + NamespaceCategories namespacesCategories = namespaces.getIfPresent(scope); + + // THIS CHECK SHOULD NOT BE NEEDED + if (namespacesCategories == null) { + LOG.info("loading Catalogue Namespaces for scope: {}",scope); + ScopeBean scopeB = new ScopeBean(scope); + populateTheCache(scopeB); + namespacesCategories = namespaces.getIfPresent(scope); + LOG.info("NamespaceCategories populated correclty with scope: {}",scope); + }else { + LOG.info("NamespaceCategories cache already populated with scope: {}, returning cached NamespaceCategories",scope); + } + + return namespacesCategories; + } + + /** + * Gets the cache. + * + * @return the cache + */ + public LoadingCache getCache() { + return namespaces; + } + +} diff --git a/src/main/java/org/gcube/common/metadataprofilediscovery/jaxb/MetadataField.java b/src/main/java/org/gcube/common/metadataprofilediscovery/jaxb/MetadataField.java index b6de026..43710b5 100644 --- a/src/main/java/org/gcube/common/metadataprofilediscovery/jaxb/MetadataField.java +++ b/src/main/java/org/gcube/common/metadataprofilediscovery/jaxb/MetadataField.java @@ -15,17 +15,16 @@ 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 + * @author Francesco Mangiacrapa at ISTI-CNR francesco.mangiacrapa@isti.cnr.it + * + * Jun 20, 2022 */ -@XmlRootElement(name="metadatafield") +@XmlRootElement(name = "metadatafield") @XmlAccessorType(XmlAccessType.FIELD) -public class MetadataField implements Serializable{ +public class MetadataField implements Serializable { /** * @@ -37,22 +36,27 @@ public class MetadataField implements Serializable{ */ private static final long serialVersionUID = 5935573474465015727L; - @XmlAttribute(name=LOCAL_NAME_CATEGORYREF) - private String categoryRef = null; //ITS VALUE IS A CATEGORY-ID + @XmlAttribute(name = LOCAL_NAME_CATEGORYREF) + private String categoryRef = null; // ITS VALUE IS A CATEGORY-ID + + // optional Field used as fieldName in the (result) Document. If it absent, the + // mandatory fieldName is used + private String fieldId = null; + @XmlElement(required = true) private String fieldName; - @XmlElement(required = true) + @XmlElement(required = true) private Boolean mandatory = false; - @XmlElement(name = "maxOccurs") + @XmlElement(name = "maxOccurs") private String maxOccurs; // private Boolean isBoolean = false; - @XmlJavaTypeAdapter(DataTypeAdapter.class) + @XmlJavaTypeAdapter(DataTypeAdapter.class) private DataType dataType = DataType.String; private String defaultValue; private String note; - //It's the list of eligible values; + // It's the list of eligible values; @XmlElement(name = "vocabulary") private MetadataVocabulary vocabulary; @@ -84,12 +88,11 @@ public class MetadataField implements Serializable{ this.mandatory = mandatory; } - /** * Instantiates a new metadata field. * - * @param fieldName the field name - * @param mandatory the mandatory + * @param fieldName the field name + * @param mandatory the mandatory * @param categoryID the category id */ public MetadataField(String fieldName, Boolean mandatory, String categoryID) { @@ -110,7 +113,6 @@ public class MetadataField implements Serializable{ return categoryRef; } - /** * Sets the category ref to category id. * @@ -124,17 +126,37 @@ public class MetadataField implements Serializable{ /** * Gets the category field qualified name. * - * If the Metadata Field belongs to a category, returns the qualified name: {@link MetadataField#categoryRef} {@link Namespace#Separator}} fieldName; + * 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(){ + public String getCategoryFieldQName() { - return categoryRef==null?fieldName:categoryRef+Namespace.Separator+fieldName; + return categoryRef == null ? fieldName : categoryRef + Namespace.Separator + fieldName; } + /** + * Gets the field id. + * + * @return the field id + */ + public String getFieldId() { + return fieldId; + } + + /** + * Sets the field id. Optional Field used as fieldName in the (result) Document. + * (e.g. JSON Document { fieldId: [data entry value]} If it absent, the + * mandatory fieldName is used + * + * @param fieldId the new field id + */ + public void setFieldId(String fieldId) { + this.fieldId = fieldId; + } /** * Gets the field name. @@ -146,7 +168,6 @@ public class MetadataField implements Serializable{ return fieldName; } - /** * Gets the mandatory. * @@ -157,7 +178,6 @@ public class MetadataField implements Serializable{ return mandatory; } - /** * Gets the data type. * @@ -168,7 +188,6 @@ public class MetadataField implements Serializable{ return dataType; } - /** * Gets the max occurs. * @@ -179,8 +198,9 @@ public class MetadataField implements Serializable{ return maxOccurs; } - /** + * Sets the max occurs. + * * @param maxOccurs the maxOccurs to set */ public void setMaxOccurs(String maxOccurs) { @@ -198,7 +218,6 @@ public class MetadataField implements Serializable{ return defaultValue; } - /** * Gets the note. * @@ -209,7 +228,6 @@ public class MetadataField implements Serializable{ return note; } - /** * Gets the vocabulary. * @@ -220,7 +238,6 @@ public class MetadataField implements Serializable{ return vocabulary; } - /** * Gets the validator. * @@ -231,7 +248,6 @@ public class MetadataField implements Serializable{ return validator; } - /** * Gets the tagging. * @@ -242,7 +258,6 @@ public class MetadataField implements Serializable{ return tagging; } - /** * Gets the grouping. * @@ -253,7 +268,6 @@ public class MetadataField implements Serializable{ return grouping; } - /** * Sets the field name. * @@ -264,7 +278,6 @@ public class MetadataField implements Serializable{ this.fieldName = fieldName; } - /** * Sets the mandatory. * @@ -275,7 +288,6 @@ public class MetadataField implements Serializable{ this.mandatory = mandatory; } - /** * Sets the data type. * @@ -286,7 +298,6 @@ public class MetadataField implements Serializable{ this.dataType = dataType; } - /** * Sets the default value. * @@ -297,7 +308,6 @@ public class MetadataField implements Serializable{ this.defaultValue = defaultValue; } - /** * Sets the note. * @@ -308,7 +318,6 @@ public class MetadataField implements Serializable{ this.note = note; } - /** * Sets the vocabulary. * @@ -319,7 +328,6 @@ public class MetadataField implements Serializable{ this.vocabulary = vocabulary; } - /** * Sets the validator. * @@ -330,7 +338,6 @@ public class MetadataField implements Serializable{ this.validator = validator; } - /** * Sets the tagging. * @@ -341,7 +348,6 @@ public class MetadataField implements Serializable{ this.tagging = tagging; } - /** * Sets the grouping. * @@ -352,7 +358,14 @@ public class MetadataField implements Serializable{ this.grouping = grouping; } - /* (non-Javadoc) + /** + * To string. + * + * @return the string + */ + /* + * (non-Javadoc) + * * @see java.lang.Object#toString() */ @Override @@ -361,6 +374,8 @@ public class MetadataField implements Serializable{ StringBuilder builder = new StringBuilder(); builder.append("MetadataField [categoryRef="); builder.append(categoryRef); + builder.append(", fieldId="); + builder.append(fieldId); builder.append(", fieldName="); builder.append(fieldName); builder.append(", mandatory="); @@ -385,5 +400,4 @@ public class MetadataField implements Serializable{ return builder.toString(); } - } diff --git a/src/main/java/org/gcube/common/metadataprofilediscovery/reader/MetadataFormatDiscovery.java b/src/main/java/org/gcube/common/metadataprofilediscovery/reader/MetadataFormatDiscovery.java index 73114a2..54d7a8c 100644 --- a/src/main/java/org/gcube/common/metadataprofilediscovery/reader/MetadataFormatDiscovery.java +++ b/src/main/java/org/gcube/common/metadataprofilediscovery/reader/MetadataFormatDiscovery.java @@ -40,6 +40,7 @@ public class MetadataFormatDiscovery { private String secondaryType; private ScopeBean scope; private List metadataProfiles; + private String resourceName; /** @@ -49,9 +50,10 @@ public class MetadataFormatDiscovery { * @param secondaryType the secondary type * @throws Exception the exception */ - public MetadataFormatDiscovery(ScopeBean scope, String secondaryType) throws Exception { + public MetadataFormatDiscovery(ScopeBean scope, String secondaryType, String resourceName) throws Exception { this.scope = scope; this.secondaryType = secondaryType; + this.resourceName = resourceName; this.metadataProfiles = readMetadataProfilesFromInfrastrucure(); } @@ -79,8 +81,12 @@ public class MetadataFormatDiscovery { // ScopeProvider.instance.set(scopeString); logger.info("Using scope from ScopeProvider: "+scopeString); - - String queryString = QueryForResourceUtil.getGcubeGenericQueryStringForSecondaryType(secondaryType); + String queryString = null; + if(resourceName!=null) + queryString = QueryForResourceUtil.getGcubeGenericQueryStringForSecondaryTypeAndName(resourceName, secondaryType); + else + queryString = QueryForResourceUtil.getGcubeGenericQueryStringForSecondaryType(secondaryType); + logger.trace("queryString: " +queryString); Query q = new QueryBox(queryString); diff --git a/src/main/java/org/gcube/common/metadataprofilediscovery/reader/NamespaceCategoryReader.java b/src/main/java/org/gcube/common/metadataprofilediscovery/reader/NamespaceCategoryReader.java index 95303bf..5b7c158 100644 --- a/src/main/java/org/gcube/common/metadataprofilediscovery/reader/NamespaceCategoryReader.java +++ b/src/main/java/org/gcube/common/metadataprofilediscovery/reader/NamespaceCategoryReader.java @@ -8,12 +8,21 @@ import static org.gcube.resources.discovery.icclient.ICFactory.client; import java.io.ByteArrayInputStream; import java.io.InputStream; import java.io.StringReader; +import java.io.StringWriter; 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 javax.xml.transform.Transformer; +import javax.xml.transform.TransformerException; +import javax.xml.transform.TransformerFactory; +import javax.xml.transform.dom.DOMSource; +import javax.xml.transform.stream.StreamResult; +import javax.xml.xpath.XPath; +import javax.xml.xpath.XPathConstants; +import javax.xml.xpath.XPathFactory; import org.gcube.common.metadataprofilediscovery.jaxb.NamespaceCategories; import org.gcube.common.resources.gcore.utils.XPathHelper; @@ -23,7 +32,9 @@ 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.w3c.dom.NodeList; import org.xml.sax.InputSource; @@ -35,6 +46,8 @@ import org.xml.sax.InputSource; */ public class NamespaceCategoryReader { + private static final String PATH_RESOURCE_PROFILE_BODY_NAMESPACES = "/Resource/Profile/Body/namespaces"; + private static Logger logger = LoggerFactory.getLogger(NamespaceCategoryReader.class); private static final String GENERIC_RESOURCE_NAMESPACES_NAME = "Namespaces Catalogue Categories"; @@ -89,12 +102,11 @@ public class NamespaceCategoryReader { String theResource = null; try{ theResource = appProfile.get(0); - logger.debug("Resource (Namespaces Catalogue Categories) found"); + logger.debug("Resource "+GENERIC_RESOURCE_NAMESPACES_NAME+" found"); + logger.trace("Resource "+GENERIC_RESOURCE_NAMESPACES_NAME+" is: "+theResource); 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); + Document xmlDocument = docBuilder.parse(new InputSource(new StringReader(theResource))); + readNamespaceCategoryFromResource(xmlDocument); }catch(Exception e){ logger.error("Error while parsing Resource "+theResource+" from the infrastructure, the scope is "+scopeString,e); } @@ -109,6 +121,52 @@ public class NamespaceCategoryReader { } } + /** + * Read namespace category from resource. + * + * @param helper the helper + * @throws Exception the exception + */ + private void readNamespaceCategoryFromResource(Document xmlDocument) throws Exception{ + + try { + + XPath xPath = XPathFactory.newInstance().newXPath(); + String expression = PATH_RESOURCE_PROFILE_BODY_NAMESPACES; + NodeList nodeList = (NodeList) xPath.compile(expression).evaluate(xmlDocument, XPathConstants.NODESET); + + String namespaceSources = getNodeString(nodeList.item(0)); + + JAXBContext jaxbContext = JAXBContext.newInstance(NamespaceCategories.class); + Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller(); + + if(namespaceSources==null || namespaceSources.length()==0){ + throw new Exception("Resource does not contain ... in the body"); + } + + logger.debug("Unmarshalling document path "+PATH_RESOURCE_PROFILE_BODY_NAMESPACES +" to "+NamespaceCategories.class.getSimpleName()); + InputStream stream = new ByteArrayInputStream(namespaceSources.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); + } + } + + private String getNodeString(Node node) { + try { + StringWriter writer = new StringWriter(); + Transformer transformer = TransformerFactory.newInstance().newTransformer(); + transformer.transform(new DOMSource(node), new StreamResult(writer)); + String output = writer.toString(); + return output; + } catch (TransformerException e) { + e.printStackTrace(); + } + return node.getTextContent(); + } /** * Read namespace category from resource. @@ -119,8 +177,13 @@ public class NamespaceCategoryReader { private void readNamespaceCategoryFromResource(XPathHelper helper) throws Exception{ try { + + if(helper==null) { + logger.warn("**************************\n\nXpathHelper is null!!!!\n\n"); + } + - List namespaceSources = helper.evaluate("/Resource/Profile/Body/namespaces"); + List namespaceSources = helper.evaluate(PATH_RESOURCE_PROFILE_BODY_NAMESPACES); JAXBContext jaxbContext = JAXBContext.newInstance(NamespaceCategories.class); Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller(); diff --git a/src/test/java/org/gcube/common/metadataprofilediscovery/TestDataCatalogueMetadataFormatReader.java b/src/test/java/org/gcube/common/metadataprofilediscovery/TestDataCatalogueMetadataFormatReader.java index 6164fd1..bcd89ac 100644 --- a/src/test/java/org/gcube/common/metadataprofilediscovery/TestDataCatalogueMetadataFormatReader.java +++ b/src/test/java/org/gcube/common/metadataprofilediscovery/TestDataCatalogueMetadataFormatReader.java @@ -29,7 +29,7 @@ public class TestDataCatalogueMetadataFormatReader { * Test. * @throws Exception */ - @Test + //@Test public void test() throws Exception { @@ -45,7 +45,7 @@ public class TestDataCatalogueMetadataFormatReader { List categs = reader.getListOfNamespaceCategories(); for (NamespaceCategory namespaceCategory : categs) { - logger.trace("\n\n "+ ++i +".) Category: "+namespaceCategory); + logger.info("\n\n "+ ++i +".) Category: "+namespaceCategory); } i = 0; @@ -55,7 +55,7 @@ public class TestDataCatalogueMetadataFormatReader { continue; MetadataFormat metadataFormat = reader.getMetadataFormatForMetadataProfile(mt); - logger.trace("\n\n "+ ++i +".) Metadata source: "+metadataFormat.getMetadataSource().substring(0, 100)); + logger.info("\n\n "+ ++i +".) Metadata source: "+metadataFormat.getMetadataSource().substring(0, 100)); } } diff --git a/src/test/java/org/gcube/common/metadataprofilediscovery/TestMetadataFormatDiscovery.java b/src/test/java/org/gcube/common/metadataprofilediscovery/TestMetadataFormatDiscovery.java index e58b74a..32b7292 100644 --- a/src/test/java/org/gcube/common/metadataprofilediscovery/TestMetadataFormatDiscovery.java +++ b/src/test/java/org/gcube/common/metadataprofilediscovery/TestMetadataFormatDiscovery.java @@ -4,6 +4,8 @@ package org.gcube.common.metadataprofilediscovery; import org.gcube.common.metadataprofilediscovery.bean.MetadataProfile; +import org.gcube.common.metadataprofilediscovery.jaxb.MetadataField; +import org.gcube.common.metadataprofilediscovery.jaxb.MetadataFormat; import org.gcube.common.metadataprofilediscovery.reader.MetadataFormatDiscovery; import org.gcube.common.scope.api.ScopeProvider; import org.gcube.common.scope.impl.ScopeBean; @@ -25,16 +27,32 @@ public class TestMetadataFormatDiscovery { String scopeString = "/gcube/devsec/devVRE"; String grMetadataProfileSecondaryType = "DataCatalogueMetadata"; + grMetadataProfileSecondaryType = "GeoNaMetadata"; + final ScopeBean scope = new ScopeBean(scopeString); MetadataFormatDiscovery reader; try { ScopeProvider.instance.set(scopeString); - reader = new MetadataFormatDiscovery(scope, grMetadataProfileSecondaryType); + reader = new MetadataFormatDiscovery(scope, grMetadataProfileSecondaryType, null); //System.out.println(reader.getMetadataProfiles()); for (MetadataProfile metaProfile : reader.getMetadataProfiles()) { - System.out.println(metaProfile.getId() +", name: "+metaProfile.getName() +", type: "+metaProfile.getMetadataType()); + System.out.println("\n\n###" + metaProfile.getId() +", name: "+metaProfile.getName() +", type: "+metaProfile.getMetadataType()); + + MetadataProfileReader readerProfile = new MetadataProfileReader(grMetadataProfileSecondaryType, metaProfile.getName()); + MetadataFormat format = readerProfile.getMetadataFormatForMetadataProfile(metaProfile); + int i = 0; + try { + for (MetadataField field : format.getMetadataFields()) { + System.out.println("\t"+ ++i +") "+field); + } + }catch (Exception e) { + System.err.println("\t## Error occurred reading: " + metaProfile.getId() +", name: "+metaProfile.getName() +", type: "+metaProfile.getMetadataType()); + e.printStackTrace(); + } } + + } 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 index 26f29af..d81e312 100644 --- a/src/test/java/org/gcube/common/metadataprofilediscovery/TestMetadataFormatReader.java +++ b/src/test/java/org/gcube/common/metadataprofilediscovery/TestMetadataFormatReader.java @@ -11,11 +11,9 @@ import org.gcube.common.metadataprofilediscovery.reader.MetadataFormatReader; import org.gcube.common.scope.api.ScopeProvider; import org.gcube.common.scope.impl.ScopeBean; - /** * - * @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it - * Jun 8, 2016 + * @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it Jun 8, 2016 */ public class TestMetadataFormatReader { @@ -23,7 +21,7 @@ public class TestMetadataFormatReader { public void test() { String scopeString = "/gcube/devsec/devVRE"; - final ScopeBean scope = new ScopeBean(scopeString); + final ScopeBean scope = new ScopeBean(scopeString); MetadataFormatReader reader; try { ScopeProvider.instance.set(scopeString); @@ -33,16 +31,17 @@ public class TestMetadataFormatReader { e.printStackTrace(); } } - - //@Test + + // @Test public void testReadInputStream() { String fileNameMeatadataProfile = "HarvestedObject.xml"; try { InputStream in = ClassLoader.getSystemResourceAsStream(fileNameMeatadataProfile); - //InputStream in = TestMetadataFormatReader.class.getResourceAsStream(fileNameMeatadataProfile); + // InputStream in = + // TestMetadataFormatReader.class.getResourceAsStream(fileNameMeatadataProfile); MetadataFormat mf = MetadataProfileReader.toMetadataFormat(in); - System.out.println("Source: "+mf.getMetadataSource()); - + System.out.println("Source: " + mf.getMetadataSource()); + for (MetadataField field : mf.getMetadataFields()) { System.out.println(field); } diff --git a/src/test/java/org/gcube/common/metadataprofilediscovery/TestMetadataProfileReader.java b/src/test/java/org/gcube/common/metadataprofilediscovery/TestMetadataProfileReader.java new file mode 100644 index 0000000..d9a3a46 --- /dev/null +++ b/src/test/java/org/gcube/common/metadataprofilediscovery/TestMetadataProfileReader.java @@ -0,0 +1,48 @@ +/** + * + */ +package org.gcube.common.metadataprofilediscovery; + +import org.gcube.common.metadataprofilediscovery.bean.MetadataProfile; +import org.gcube.common.metadataprofilediscovery.jaxb.NamespaceCategory; +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 TestMetadataProfileReader { + + //@Test + public void test() { + + String scopeString = "/gcube/devsec/devVRE"; + final ScopeBean scope = new ScopeBean(scopeString); + + String[] genericResourceNames = new String[] {"Informazioni_di_progetto","Relazione_di_Scavo"}; + + for (int i = 0; i < 2; i++) { + MetadataProfileReader reader; + try { + ScopeProvider.instance.set(scope.toString()); + reader = new MetadataProfileReader("GeoNaMetadata",genericResourceNames[i]); + + int j = 0; + for (MetadataProfile metadataProfile : reader.getListOfMetadataProfiles()) { + System.out.println(j++ + ")" + metadataProfile); + } + + j = 0; + for (NamespaceCategory namespaceCategory : reader.getListOfNamespaceCategories()) { + System.out.println(j++ + ")" + namespaceCategory); + } + } catch (Exception e) { + e.printStackTrace(); + } + } + + } + +}