From dac0ef075b6f188e5a229482edd584cd2843cb21 Mon Sep 17 00:00:00 2001 From: Francesco Mangiacrapa Date: Tue, 7 Jun 2016 16:29:19 +0000 Subject: [PATCH] Added beans and reader git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/data-catalogue/gcubedatacatalogue-metadata-discovery@129012 82a268e6-3cf1-43bd-a215-b396298e98cf --- pom.xml | 15 + .../metadatadiscovery/bean/MetadataType.java | 118 +++++++ .../{ => bean/jaxb}/MetadataField.java | 21 +- .../{ => bean/jaxb}/MetadataFormat.java | 5 +- .../{ => bean/jaxb}/MetadataValidator.java | 2 +- .../{ => bean/jaxb}/MetadataVocabulary.java | 2 +- .../ApplicationProfileNotFoundException.java | 20 ++ .../reader/MedataFormatDiscovery.java | 288 +++++++++++++++++ .../reader/MedataFormatReader.java | 289 ++++++++++++++++++ .../metadatadiscovery/MainJaxb.java | 9 +- .../TestMetadataDiscovery.java | 108 +++++++ 11 files changed, 866 insertions(+), 11 deletions(-) create mode 100644 src/main/java/org/gcube/datacatalogue/metadatadiscovery/bean/MetadataType.java rename src/main/java/org/gcube/datacatalogue/metadatadiscovery/{ => bean/jaxb}/MetadataField.java (95%) rename src/main/java/org/gcube/datacatalogue/metadatadiscovery/{ => bean/jaxb}/MetadataFormat.java (96%) rename src/main/java/org/gcube/datacatalogue/metadatadiscovery/{ => bean/jaxb}/MetadataValidator.java (95%) rename src/main/java/org/gcube/datacatalogue/metadatadiscovery/{ => bean/jaxb}/MetadataVocabulary.java (96%) create mode 100644 src/main/java/org/gcube/datacatalogue/metadatadiscovery/reader/ApplicationProfileNotFoundException.java create mode 100644 src/main/java/org/gcube/datacatalogue/metadatadiscovery/reader/MedataFormatDiscovery.java create mode 100644 src/main/java/org/gcube/datacatalogue/metadatadiscovery/reader/MedataFormatReader.java rename src/{main => test}/java/org/gcube/datacatalogue/metadatadiscovery/MainJaxb.java (89%) create mode 100644 src/test/java/org/gcube/datacatalogue/metadatadiscovery/TestMetadataDiscovery.java diff --git a/pom.xml b/pom.xml index 6e187dd..7a6569d 100644 --- a/pom.xml +++ b/pom.xml @@ -58,6 +58,21 @@ + + + org.gcube.resources.discovery + ic-client + provided + + + + org.gcube.core + common-scope-maps + provided + [1.0.0-SNAPSHOT, 2.0.0-SNAPSHOT) + + + com.sun.xml.bind jaxb-core diff --git a/src/main/java/org/gcube/datacatalogue/metadatadiscovery/bean/MetadataType.java b/src/main/java/org/gcube/datacatalogue/metadatadiscovery/bean/MetadataType.java new file mode 100644 index 0000000..2c17a0c --- /dev/null +++ b/src/main/java/org/gcube/datacatalogue/metadatadiscovery/bean/MetadataType.java @@ -0,0 +1,118 @@ +/** + * + */ +package org.gcube.datacatalogue.metadatadiscovery.bean; + +import java.io.Serializable; + + +/** + * + * @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it + * Jun 7, 2016 + */ +public class MetadataType implements Serializable{ + + /** + * + */ + private static final long serialVersionUID = -975360133066572549L; + + private String id; + private String name; + private String description; + + /** + * + */ + public MetadataType() { + + } + + /** + * @param id + * @param name + * @param description + */ + public MetadataType(String id, String name, String description) { + + super(); + this.id = id; + this.name = name; + this.description = description; + } + + + /** + * @return the id + */ + public String getId() { + + return id; + } + + + /** + * @return the name + */ + public String getName() { + + return name; + } + + + /** + * @return the description + */ + public String getDescription() { + + return description; + } + + + /** + * @param id the id to set + */ + public void setId(String id) { + + this.id = id; + } + + + /** + * @param name the name to set + */ + public void setName(String name) { + + this.name = name; + } + + + /** + * @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("MetadataType [id="); + builder.append(id); + builder.append(", name="); + builder.append(name); + builder.append(", description="); + builder.append(description); + builder.append("]"); + return builder.toString(); + } + + + +} diff --git a/src/main/java/org/gcube/datacatalogue/metadatadiscovery/MetadataField.java b/src/main/java/org/gcube/datacatalogue/metadatadiscovery/bean/jaxb/MetadataField.java similarity index 95% rename from src/main/java/org/gcube/datacatalogue/metadatadiscovery/MetadataField.java rename to src/main/java/org/gcube/datacatalogue/metadatadiscovery/bean/jaxb/MetadataField.java index 62fb0e3..12de43d 100644 --- a/src/main/java/org/gcube/datacatalogue/metadatadiscovery/MetadataField.java +++ b/src/main/java/org/gcube/datacatalogue/metadatadiscovery/bean/jaxb/MetadataField.java @@ -1,7 +1,7 @@ /** * */ -package org.gcube.datacatalogue.metadatadiscovery; +package org.gcube.datacatalogue.metadatadiscovery.bean.jaxb; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; @@ -62,6 +62,7 @@ public class MetadataField { } + /** * @return the fieldName */ @@ -71,6 +72,7 @@ public class MetadataField { } + /** * @return the mandatory */ @@ -80,15 +82,17 @@ public class MetadataField { } + /** * @return the isBoolean */ - public Boolean isBoolean() { + public Boolean getIsBoolean() { return isBoolean; } + /** * @return the defaulValue */ @@ -98,6 +102,7 @@ public class MetadataField { } + /** * @return the note */ @@ -107,6 +112,7 @@ public class MetadataField { } + /** * @return the vocabulary */ @@ -116,6 +122,7 @@ public class MetadataField { } + /** * @return the validator */ @@ -125,6 +132,7 @@ public class MetadataField { } + /** * @param fieldName the fieldName to set */ @@ -134,6 +142,7 @@ public class MetadataField { } + /** * @param mandatory the mandatory to set */ @@ -143,6 +152,7 @@ public class MetadataField { } + /** * @param isBoolean the isBoolean to set */ @@ -152,6 +162,7 @@ public class MetadataField { } + /** * @param defaulValue the defaulValue to set */ @@ -161,6 +172,7 @@ public class MetadataField { } + /** * @param note the note to set */ @@ -170,6 +182,7 @@ public class MetadataField { } + /** * @param vocabulary the vocabulary to set */ @@ -179,6 +192,7 @@ public class MetadataField { } + /** * @param validator the validator to set */ @@ -195,7 +209,7 @@ public class MetadataField { public String toString() { StringBuilder builder = new StringBuilder(); - builder.append("CkanMetadata [fieldName="); + builder.append("MetadataField [fieldName="); builder.append(fieldName); builder.append(", mandatory="); builder.append(mandatory); @@ -213,5 +227,4 @@ public class MetadataField { return builder.toString(); } - } diff --git a/src/main/java/org/gcube/datacatalogue/metadatadiscovery/MetadataFormat.java b/src/main/java/org/gcube/datacatalogue/metadatadiscovery/bean/jaxb/MetadataFormat.java similarity index 96% rename from src/main/java/org/gcube/datacatalogue/metadatadiscovery/MetadataFormat.java rename to src/main/java/org/gcube/datacatalogue/metadatadiscovery/bean/jaxb/MetadataFormat.java index fec1e9f..1f45dea 100644 --- a/src/main/java/org/gcube/datacatalogue/metadatadiscovery/MetadataFormat.java +++ b/src/main/java/org/gcube/datacatalogue/metadatadiscovery/bean/jaxb/MetadataFormat.java @@ -1,7 +1,7 @@ /** * */ -package org.gcube.datacatalogue.metadatadiscovery; +package org.gcube.datacatalogue.metadatadiscovery.bean.jaxb; import java.util.ArrayList; import java.util.List; @@ -78,7 +78,4 @@ public class MetadataFormat { builder.append("]"); return builder.toString(); } - - - } diff --git a/src/main/java/org/gcube/datacatalogue/metadatadiscovery/MetadataValidator.java b/src/main/java/org/gcube/datacatalogue/metadatadiscovery/bean/jaxb/MetadataValidator.java similarity index 95% rename from src/main/java/org/gcube/datacatalogue/metadatadiscovery/MetadataValidator.java rename to src/main/java/org/gcube/datacatalogue/metadatadiscovery/bean/jaxb/MetadataValidator.java index 28f32cf..e248a12 100644 --- a/src/main/java/org/gcube/datacatalogue/metadatadiscovery/MetadataValidator.java +++ b/src/main/java/org/gcube/datacatalogue/metadatadiscovery/bean/jaxb/MetadataValidator.java @@ -1,7 +1,7 @@ /** * */ -package org.gcube.datacatalogue.metadatadiscovery; +package org.gcube.datacatalogue.metadatadiscovery.bean.jaxb; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; diff --git a/src/main/java/org/gcube/datacatalogue/metadatadiscovery/MetadataVocabulary.java b/src/main/java/org/gcube/datacatalogue/metadatadiscovery/bean/jaxb/MetadataVocabulary.java similarity index 96% rename from src/main/java/org/gcube/datacatalogue/metadatadiscovery/MetadataVocabulary.java rename to src/main/java/org/gcube/datacatalogue/metadatadiscovery/bean/jaxb/MetadataVocabulary.java index f390330..a2b7673 100644 --- a/src/main/java/org/gcube/datacatalogue/metadatadiscovery/MetadataVocabulary.java +++ b/src/main/java/org/gcube/datacatalogue/metadatadiscovery/bean/jaxb/MetadataVocabulary.java @@ -1,7 +1,7 @@ /** * */ -package org.gcube.datacatalogue.metadatadiscovery; +package org.gcube.datacatalogue.metadatadiscovery.bean.jaxb; import java.util.List; diff --git a/src/main/java/org/gcube/datacatalogue/metadatadiscovery/reader/ApplicationProfileNotFoundException.java b/src/main/java/org/gcube/datacatalogue/metadatadiscovery/reader/ApplicationProfileNotFoundException.java new file mode 100644 index 0000000..f3eb097 --- /dev/null +++ b/src/main/java/org/gcube/datacatalogue/metadatadiscovery/reader/ApplicationProfileNotFoundException.java @@ -0,0 +1,20 @@ +package org.gcube.datacatalogue.metadatadiscovery.reader; + +/** + * The Class ApplicationProfileNotFoundException. + * + * @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it + * Jun 7, 2016 + */ +@SuppressWarnings("serial") +public class ApplicationProfileNotFoundException extends Exception { + + /** + * Instantiates a new application profile not found exception. + * + * @param message the message + */ + public ApplicationProfileNotFoundException(String message) { + super(message); + } +} \ No newline at end of file diff --git a/src/main/java/org/gcube/datacatalogue/metadatadiscovery/reader/MedataFormatDiscovery.java b/src/main/java/org/gcube/datacatalogue/metadatadiscovery/reader/MedataFormatDiscovery.java new file mode 100644 index 0000000..5e5282b --- /dev/null +++ b/src/main/java/org/gcube/datacatalogue/metadatadiscovery/reader/MedataFormatDiscovery.java @@ -0,0 +1,288 @@ +package org.gcube.datacatalogue.metadatadiscovery.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 org.gcube.common.resources.gcore.utils.XPathHelper; +import org.gcube.common.scope.api.ScopeProvider; +import org.gcube.common.scope.impl.ScopeBean; +import org.gcube.datacatalogue.metadatadiscovery.bean.MetadataType; +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 MedataFormatReader. + * + * @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it + * @Jul 26, 2013 + */ +public class MedataFormatDiscovery { + + public static Logger logger = LoggerFactory.getLogger(MedataFormatDiscovery.class); + protected static final String DATA_CATALOGUE_METADATA_SECONDARY_TYPE = "DataCatalogueMetadata"; + private String secondaryType; + private ScopeBean scope; + private List metadataTypes; + + /** + * Instantiates a new medata format reader. + * + * @param scope - the scope to be searched + * @throws Exception the exception + */ + public MedataFormatDiscovery(ScopeBean scope) throws Exception { + this.scope = scope; + this.secondaryType = DATA_CATALOGUE_METADATA_SECONDARY_TYPE; + this.metadataTypes = readProfileFromInfrastrucure(); + } + + /** + * this method looks up the applicationProfile profile among the ones available in the infrastructure. + * + * @return the applicationProfile profile + * @throws Exception the exception + */ + private List readProfileFromInfrastrucure() 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); + + String queryString = getGcubeGenericQueryString(secondaryType); + 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 ApplicationProfileNotFoundException("Your applicationProfile is not registered in the infrastructure, the scope is "+scopeString); + else { + + for (String elem : appProfile) { + try{ + DocumentBuilder docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); + Node node = docBuilder.parse(new InputSource(new StringReader(elem))).getDocumentElement(); + XPathHelper helper = new XPathHelper(node); + MetadataType meta = getMetadataTypeFromResource(helper); + list.add(meta); + }catch(Exception e){ + logger.error("Error while trying to fetch applicationProfile "+secondaryType+" from the infrastructure, ",e); + } + } + } + + } catch (Exception e) { + logger.error("Error while trying to fetch applicationProfile "+secondaryType+" from the infrastructure, "+e); + return list; + } finally{ + ScopeProvider.instance.reset(); + } + + return list; + + } + + /** + * Gets the metadata type from resource. + * + * @param helper the helper + * @return the metadata type from resource + * @throws ApplicationProfileNotFoundException the application profile not found exception + */ + private MetadataType getMetadataTypeFromResource(XPathHelper helper) throws ApplicationProfileNotFoundException{ + + + try { + + MetadataType metaType = new MetadataType(); + + List id = helper.evaluate("/Resource/ID/text()"); + + if(id==null || id.isEmpty()) + throw new ApplicationProfileNotFoundException("Resource ID not found for "+helper.toString()); + else{ + metaType.setId(id.get(0)); + } + + List name = helper.evaluate("/Resource/Profile/Name/text()"); + + if(name==null || name.isEmpty()) + throw new ApplicationProfileNotFoundException("Resource Name not found for "+helper.toString()); + else + metaType.setName(name.get(0)); + + List description = helper.evaluate("/Resource/Profile/Description/text()"); + + if(description==null || description.isEmpty()) + throw new ApplicationProfileNotFoundException("Resource Name not found for "+helper.toString()); + else + metaType.setDescription(description.get(0)); + + return metaType; + + } catch (Exception e) { + logger.error("An error occurred in getListDefaultLayerFromNode ", e); + return null; + } + + } + + /** + * Gets the metadata type from resource. + * + * @param secondaryType the secondary type + * @return the metadata type from resource + */ + /*private MetadataType getMetadataTypeFromResource(XPathHelper helper) throws ApplicationProfileNotFoundException{ + + List metadataformat; + + List listGeoexplorerDefaultLayer = new ArrayList(); + + try { + + metadataformat = helper.evaluate("/Resource/Profile/Body/metadataformat"); + + if (metadataformat != null && metadataformat.size() > 0) { + + List metadatafields = helper.evaluate("/Resource/Profile/Body/metadataformat/metadatafield/text()"); + + List currValue = null; + + for (String metafield : metadatafields) { + logger.trace("found metafield : "+metafield); + GeoexplorerDefaultLayer geoDefLayer = new GeoexplorerDefaultLayer(); + geoDefLayer.setUUID(uuid); + geoDefLayer.setScope(scope.toString()); + +// currValue = helper.evaluate("/Resource/Profile/Body/DefaultLayers/DefaultLayer[UUID='"+uuid+"']/@isBaseLayer"); + currValue = helper.evaluate("/Resource/Profile/Body/DefaultLayers/DefaultLayer[UUID='"+uuid+"']/IsBaseLayer/text()"); + + logger.trace("is base layer?" +currValue); + if (currValue != null && currValue.size() > 0){ + boolean isBase = Boolean.parseBoolean(currValue.get(0)); + geoDefLayer.setBaseLayer(isBase); +// logger.trace("isBaseLayer : "+isBase); + } + + currValue = helper.evaluate("/Resource/Profile/Body/DefaultLayers/DefaultLayer[UUID='"+uuid+"']/Description/text()"); + + String descr = ""; + if (currValue != null && currValue.size() > 0) { + descr = currValue.get(0); + geoDefLayer.setDescription(descr); +// logger.trace("description: "+descr+" for UUID: "+uuid); + } + + currValue = helper.evaluate("/Resource/Profile/Body/DefaultLayers/DefaultLayer[UUID='"+uuid+"']/Name/text()"); + descr = ""; + if (currValue != null && currValue.size() > 0) { + descr = currValue.get(0); + geoDefLayer.setName(descr); +// logger.trace("name: "+descr+" for UUID: "+uuid); + } + + logger.trace("Filled object: "+geoDefLayer); + listGeoexplorerDefaultLayer.add(geoDefLayer); + } + + } + + else throw new ApplicationProfileNotFoundException("Your applicationProfile with scope "+scope.toString()+" is wrong, consider adding element in "); + + } catch (Exception e) { + logger.error("An error occurred in getListDefaultLayerFromNode ", e); + return listGeoexplorerDefaultLayer; + } + + return listGeoexplorerDefaultLayer; + }*/ + + + public synchronized String getGcubeGenericQueryString(String secondaryType){ + + return "for $profile in collection('/db/Profiles/GenericResource')//Resource" + + " where $profile/Profile/SecondaryType/string() eq '"+secondaryType+"'" + + " return $profile"; + } + + /** + * Gets the secondary type. + * + * @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 getMetadataTypes() { + + return metadataTypes; + } + + + /** + * The main method. + * + * @param args the arguments + * @throws InterruptedException the interrupted exception + */ + public static void main(String[] args) throws InterruptedException { + + String scopeString = "/gcube/devsec/devVRE"; + final ScopeBean scope = new ScopeBean(scopeString); + MedataFormatDiscovery reader; + try { + reader = new MedataFormatDiscovery(scope); + System.out.println(reader.getMetadataTypes()); + +// String value = "true"; +// if (value != null){ +// boolean isBase = Boolean.parseBoolean(value); +// System.out.println(isBase); +//// logger.trace("isBaseLayer : "+isBase); +// } + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + } + +} diff --git a/src/main/java/org/gcube/datacatalogue/metadatadiscovery/reader/MedataFormatReader.java b/src/main/java/org/gcube/datacatalogue/metadatadiscovery/reader/MedataFormatReader.java new file mode 100644 index 0000000..04e3780 --- /dev/null +++ b/src/main/java/org/gcube/datacatalogue/metadatadiscovery/reader/MedataFormatReader.java @@ -0,0 +1,289 @@ +package org.gcube.datacatalogue.metadatadiscovery.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 org.gcube.common.resources.gcore.utils.XPathHelper; +import org.gcube.common.scope.api.ScopeProvider; +import org.gcube.common.scope.impl.ScopeBean; +import org.gcube.datacatalogue.metadatadiscovery.bean.MetadataType; +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 MedataFormatReader. + * + * @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it + * @Jul 26, 2013 + */ +public class MedataFormatReader { + + public static Logger logger = LoggerFactory.getLogger(MedataFormatReader.class); + protected static final String DATA_CATALOGUE_METADATA_SECONDARY_TYPE = "DataCatalogueMetadata"; + private String secondaryType; + private ScopeBean scope; + private List metadataTypes; + private String resourceID; + + /** + * Instantiates a new medata format reader. + * + * @param scope - the scope to be searched + * @throws Exception the exception + */ + public MedataFormatReader(ScopeBean scope, String resourceID) throws Exception { + this.scope = scope; + this.resourceID = resourceID; + this.metadataTypes = getMedataFormatByID(); + } + + /** + * this method looks up the applicationProfile profile among the ones available in the infrastructure. + * + * @return the applicationProfile profile + * @throws Exception the exception + */ + private List getMedataFormatByID() 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); + + String queryString = getGcubeGenericQueryString(secondaryType); + 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 ApplicationProfileNotFoundException("Your applicationProfile is not registered in the infrastructure, the scope is "+scopeString); + else { + + for (String elem : appProfile) { + try{ + DocumentBuilder docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); + Node node = docBuilder.parse(new InputSource(new StringReader(elem))).getDocumentElement(); + XPathHelper helper = new XPathHelper(node); + MetadataType meta = getMetadataTypeFromResource(helper); + list.add(meta); + }catch(Exception e){ + logger.error("Error while trying to fetch applicationProfile "+secondaryType+" from the infrastructure, ",e); + } + } + } + + } catch (Exception e) { + logger.error("Error while trying to fetch applicationProfile "+secondaryType+" from the infrastructure, "+e); + return list; + } finally{ + ScopeProvider.instance.reset(); + } + + return list; + + } + + /** + * Gets the metadata type from resource. + * + * @param helper the helper + * @return the metadata type from resource + * @throws ApplicationProfileNotFoundException the application profile not found exception + */ + private MetadataType getMetadataTypeFromResource(XPathHelper helper) throws ApplicationProfileNotFoundException{ + + + try { + + MetadataType metaType = new MetadataType(); + + List id = helper.evaluate("/Resource/ID/text()"); + + if(id==null || id.isEmpty()) + throw new ApplicationProfileNotFoundException("Resource ID not found for "+helper.toString()); + else{ + metaType.setId(id.get(0)); + } + + List name = helper.evaluate("/Resource/Profile/Name/text()"); + + if(name==null || name.isEmpty()) + throw new ApplicationProfileNotFoundException("Resource Name not found for "+helper.toString()); + else + metaType.setName(name.get(0)); + + List description = helper.evaluate("/Resource/Profile/Description/text()"); + + if(description==null || description.isEmpty()) + throw new ApplicationProfileNotFoundException("Resource Name not found for "+helper.toString()); + else + metaType.setDescription(description.get(0)); + + return metaType; + + } catch (Exception e) { + logger.error("An error occurred in getListDefaultLayerFromNode ", e); + return null; + } + + } + + /** + * Gets the metadata type from resource. + * + * @param secondaryType the secondary type + * @return the metadata type from resource + */ + /*private MetadataType getMetadataTypeFromResource(XPathHelper helper) throws ApplicationProfileNotFoundException{ + + List metadataformat; + + List listGeoexplorerDefaultLayer = new ArrayList(); + + try { + + metadataformat = helper.evaluate("/Resource/Profile/Body/metadataformat"); + + if (metadataformat != null && metadataformat.size() > 0) { + + List metadatafields = helper.evaluate("/Resource/Profile/Body/metadataformat/metadatafield/text()"); + + List currValue = null; + + for (String metafield : metadatafields) { + logger.trace("found metafield : "+metafield); + GeoexplorerDefaultLayer geoDefLayer = new GeoexplorerDefaultLayer(); + geoDefLayer.setUUID(uuid); + geoDefLayer.setScope(scope.toString()); + +// currValue = helper.evaluate("/Resource/Profile/Body/DefaultLayers/DefaultLayer[UUID='"+uuid+"']/@isBaseLayer"); + currValue = helper.evaluate("/Resource/Profile/Body/DefaultLayers/DefaultLayer[UUID='"+uuid+"']/IsBaseLayer/text()"); + + logger.trace("is base layer?" +currValue); + if (currValue != null && currValue.size() > 0){ + boolean isBase = Boolean.parseBoolean(currValue.get(0)); + geoDefLayer.setBaseLayer(isBase); +// logger.trace("isBaseLayer : "+isBase); + } + + currValue = helper.evaluate("/Resource/Profile/Body/DefaultLayers/DefaultLayer[UUID='"+uuid+"']/Description/text()"); + + String descr = ""; + if (currValue != null && currValue.size() > 0) { + descr = currValue.get(0); + geoDefLayer.setDescription(descr); +// logger.trace("description: "+descr+" for UUID: "+uuid); + } + + currValue = helper.evaluate("/Resource/Profile/Body/DefaultLayers/DefaultLayer[UUID='"+uuid+"']/Name/text()"); + descr = ""; + if (currValue != null && currValue.size() > 0) { + descr = currValue.get(0); + geoDefLayer.setName(descr); +// logger.trace("name: "+descr+" for UUID: "+uuid); + } + + logger.trace("Filled object: "+geoDefLayer); + listGeoexplorerDefaultLayer.add(geoDefLayer); + } + + } + + else throw new ApplicationProfileNotFoundException("Your applicationProfile with scope "+scope.toString()+" is wrong, consider adding element in "); + + } catch (Exception e) { + logger.error("An error occurred in getListDefaultLayerFromNode ", e); + return listGeoexplorerDefaultLayer; + } + + return listGeoexplorerDefaultLayer; + }*/ + + + public synchronized String getGcubeGenericQueryString(String secondaryType){ + + return "for $profile in collection('/db/Profiles/GenericResource')//Resource" + + " where $profile/Profile/SecondaryType/string() eq '"+secondaryType+"'" + + " return $profile"; + } + + /** + * Gets the secondary type. + * + * @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 getMetadataTypes() { + + return metadataTypes; + } + + + /** + * The main method. + * + * @param args the arguments + * @throws InterruptedException the interrupted exception + */ + public static void main(String[] args) throws InterruptedException { + + String scopeString = "/gcube/devsec/devVRE"; + final ScopeBean scope = new ScopeBean(scopeString); + MedataFormatReader reader; + try { + reader = new MedataFormatReader(scope, ""); + System.out.println(reader.getMetadataTypes()); + +// String value = "true"; +// if (value != null){ +// boolean isBase = Boolean.parseBoolean(value); +// System.out.println(isBase); +//// logger.trace("isBaseLayer : "+isBase); +// } + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + } + +} diff --git a/src/main/java/org/gcube/datacatalogue/metadatadiscovery/MainJaxb.java b/src/test/java/org/gcube/datacatalogue/metadatadiscovery/MainJaxb.java similarity index 89% rename from src/main/java/org/gcube/datacatalogue/metadatadiscovery/MainJaxb.java rename to src/test/java/org/gcube/datacatalogue/metadatadiscovery/MainJaxb.java index b0933ae..c6a8b69 100644 --- a/src/main/java/org/gcube/datacatalogue/metadatadiscovery/MainJaxb.java +++ b/src/test/java/org/gcube/datacatalogue/metadatadiscovery/MainJaxb.java @@ -12,6 +12,12 @@ import javax.xml.bind.JAXBException; import javax.xml.bind.Marshaller; import javax.xml.bind.Unmarshaller; +import org.gcube.datacatalogue.metadatadiscovery.bean.jaxb.MetadataField; +import org.gcube.datacatalogue.metadatadiscovery.bean.jaxb.MetadataFormat; +import org.gcube.datacatalogue.metadatadiscovery.bean.jaxb.MetadataValidator; +import org.gcube.datacatalogue.metadatadiscovery.bean.jaxb.MetadataVocabulary; +import org.junit.Test; + /** * @@ -24,7 +30,8 @@ public class MainJaxb { static MetadataFormat metadatas; static String tmpFileXML = "." + File.separator + "CkanMetadatas.xml"; - public static void main(String[] args) { + @Test + public static void createMetadata() { metadatas = new MetadataFormat(); diff --git a/src/test/java/org/gcube/datacatalogue/metadatadiscovery/TestMetadataDiscovery.java b/src/test/java/org/gcube/datacatalogue/metadatadiscovery/TestMetadataDiscovery.java new file mode 100644 index 0000000..43063e4 --- /dev/null +++ b/src/test/java/org/gcube/datacatalogue/metadatadiscovery/TestMetadataDiscovery.java @@ -0,0 +1,108 @@ +/** + * + */ +package org.gcube.datacatalogue.metadatadiscovery; + +import java.io.File; +import java.util.ArrayList; +import java.util.List; + +import javax.xml.bind.JAXBContext; +import javax.xml.bind.JAXBException; +import javax.xml.bind.Marshaller; +import javax.xml.bind.Unmarshaller; + +import org.gcube.datacatalogue.metadatadiscovery.bean.jaxb.MetadataField; +import org.gcube.datacatalogue.metadatadiscovery.bean.jaxb.MetadataFormat; +import org.gcube.datacatalogue.metadatadiscovery.bean.jaxb.MetadataValidator; +import org.gcube.datacatalogue.metadatadiscovery.bean.jaxb.MetadataVocabulary; +import org.junit.Test; + + +/** + * + * @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it + * Jun 7, 2016 + */ +public class TestMetadataDiscovery { + + static MetadataField ckanMetadata; + static MetadataFormat metadatas; + static String tmpFileXML = "." + File.separator + "CkanMetadatas.xml"; + + @Test + public void test() { + + metadatas = new MetadataFormat(); + + ckanMetadata = new MetadataField(); + ckanMetadata.setFieldName("Name"); + ckanMetadata.setMandatory(true); + ckanMetadata.setDefaulValue("default value"); + List vocabulary = new ArrayList(); + vocabulary.add("field1"); + vocabulary.add("field2"); + vocabulary.add("field3"); + MetadataVocabulary cvc = new MetadataVocabulary(vocabulary); + ckanMetadata.setVocabulary(cvc); + 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.setDefaulValue("virtual/public"); + 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(); + unMarshalingExample(); + } + catch (JAXBException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + 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)); + } + + + private static void unMarshalingExample() throws JAXBException + { + 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)); + + for(MetadataField mtd : mtds.getMetadataFields()) + { + System.out.println(mtd); + } + } +}