4184: Create datacatalogue-metadata-discovery library

Task-Url: https://support.d4science.org/issues/4184

Added MetadataFormatDiscovery
Added MetadataFormatReader

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/data-catalogue/gcubedatacatalogue-metadata-discovery@129013 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Francesco Mangiacrapa 2016-06-08 09:36:32 +00:00
parent dac0ef075b
commit fc1993dd43
9 changed files with 169 additions and 349 deletions

View File

@ -7,9 +7,10 @@ import java.io.Serializable;
/** /**
* The Class MetadataType.
* *
* @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it * @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it
* Jun 7, 2016 * Jun 8, 2016
*/ */
public class MetadataType implements Serializable{ public class MetadataType implements Serializable{
@ -23,16 +24,18 @@ public class MetadataType implements Serializable{
private String description; private String description;
/** /**
* * Instantiates a new metadata type.
*/ */
public MetadataType() { public MetadataType() {
} }
/** /**
* @param id * Instantiates a new metadata type.
* @param name *
* @param description * @param id the id
* @param name the name
* @param description the description
*/ */
public MetadataType(String id, String name, String description) { public MetadataType(String id, String name, String description) {
@ -44,6 +47,8 @@ public class MetadataType implements Serializable{
/** /**
* Gets the id.
*
* @return the id * @return the id
*/ */
public String getId() { public String getId() {
@ -53,6 +58,8 @@ public class MetadataType implements Serializable{
/** /**
* Gets the name.
*
* @return the name * @return the name
*/ */
public String getName() { public String getName() {
@ -62,6 +69,8 @@ public class MetadataType implements Serializable{
/** /**
* Gets the description.
*
* @return the description * @return the description
*/ */
public String getDescription() { public String getDescription() {
@ -71,6 +80,8 @@ public class MetadataType implements Serializable{
/** /**
* Sets the id.
*
* @param id the id to set * @param id the id to set
*/ */
public void setId(String id) { public void setId(String id) {
@ -80,6 +91,8 @@ public class MetadataType implements Serializable{
/** /**
* Sets the name.
*
* @param name the name to set * @param name the name to set
*/ */
public void setName(String name) { public void setName(String name) {
@ -89,6 +102,8 @@ public class MetadataType implements Serializable{
/** /**
* Sets the description.
*
* @param description the description to set * @param description the description to set
*/ */
public void setDescription(String description) { public void setDescription(String description) {

View File

@ -9,10 +9,12 @@ import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlRootElement;
/** /**
* The Class MetadataField.
* *
* @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it * @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it
* May 27, 2016 * Jun 8, 2016
*/ */
@XmlRootElement(name="metadatafield") @XmlRootElement(name="metadatafield")
@XmlAccessorType(XmlAccessType.FIELD) @XmlAccessorType(XmlAccessType.FIELD)
@ -32,20 +34,22 @@ public class MetadataField {
private MetadataValidator validator; private MetadataValidator validator;
/** /**
* * Instantiates a new metadata field.
*/ */
public MetadataField() { public MetadataField() {
} }
/** /**
* @param fieldName * Instantiates a new metadata field.
* @param mandatory *
* @param isBoolean * @param fieldName the field name
* @param defaulValue * @param mandatory the mandatory
* @param note * @param isBoolean the is boolean
* @param vocabulary * @param defaulValue the defaul value
* @param validator * @param note the note
* @param vocabulary the vocabulary
* @param validator the validator
*/ */
public MetadataField( public MetadataField(
String fieldName, Boolean mandatory, Boolean isBoolean, String fieldName, Boolean mandatory, Boolean isBoolean,
@ -64,6 +68,8 @@ public class MetadataField {
/** /**
* Gets the field name.
*
* @return the fieldName * @return the fieldName
*/ */
public String getFieldName() { public String getFieldName() {
@ -74,6 +80,8 @@ public class MetadataField {
/** /**
* Gets the mandatory.
*
* @return the mandatory * @return the mandatory
*/ */
public Boolean getMandatory() { public Boolean getMandatory() {
@ -84,6 +92,8 @@ public class MetadataField {
/** /**
* Gets the checks if is boolean.
*
* @return the isBoolean * @return the isBoolean
*/ */
public Boolean getIsBoolean() { public Boolean getIsBoolean() {
@ -94,6 +104,8 @@ public class MetadataField {
/** /**
* Gets the defaul value.
*
* @return the defaulValue * @return the defaulValue
*/ */
public String getDefaulValue() { public String getDefaulValue() {
@ -104,6 +116,8 @@ public class MetadataField {
/** /**
* Gets the note.
*
* @return the note * @return the note
*/ */
public String getNote() { public String getNote() {
@ -114,6 +128,8 @@ public class MetadataField {
/** /**
* Gets the vocabulary.
*
* @return the vocabulary * @return the vocabulary
*/ */
public MetadataVocabulary getVocabulary() { public MetadataVocabulary getVocabulary() {
@ -124,6 +140,8 @@ public class MetadataField {
/** /**
* Gets the validator.
*
* @return the validator * @return the validator
*/ */
public MetadataValidator getValidator() { public MetadataValidator getValidator() {
@ -134,6 +152,8 @@ public class MetadataField {
/** /**
* Sets the field name.
*
* @param fieldName the fieldName to set * @param fieldName the fieldName to set
*/ */
public void setFieldName(String fieldName) { public void setFieldName(String fieldName) {
@ -144,6 +164,8 @@ public class MetadataField {
/** /**
* Sets the mandatory.
*
* @param mandatory the mandatory to set * @param mandatory the mandatory to set
*/ */
public void setMandatory(Boolean mandatory) { public void setMandatory(Boolean mandatory) {
@ -154,6 +176,8 @@ public class MetadataField {
/** /**
* Sets the checks if is boolean.
*
* @param isBoolean the isBoolean to set * @param isBoolean the isBoolean to set
*/ */
public void setIsBoolean(Boolean isBoolean) { public void setIsBoolean(Boolean isBoolean) {
@ -164,6 +188,8 @@ public class MetadataField {
/** /**
* Sets the defaul value.
*
* @param defaulValue the defaulValue to set * @param defaulValue the defaulValue to set
*/ */
public void setDefaulValue(String defaulValue) { public void setDefaulValue(String defaulValue) {
@ -174,6 +200,8 @@ public class MetadataField {
/** /**
* Sets the note.
*
* @param note the note to set * @param note the note to set
*/ */
public void setNote(String note) { public void setNote(String note) {
@ -184,6 +212,8 @@ public class MetadataField {
/** /**
* Sets the vocabulary.
*
* @param vocabulary the vocabulary to set * @param vocabulary the vocabulary to set
*/ */
public void setVocabulary(MetadataVocabulary vocabulary) { public void setVocabulary(MetadataVocabulary vocabulary) {
@ -194,6 +224,8 @@ public class MetadataField {
/** /**
* Sets the validator.
*
* @param validator the validator to set * @param validator the validator to set
*/ */
public void setValidator(MetadataValidator validator) { public void setValidator(MetadataValidator validator) {

View File

@ -12,11 +12,12 @@ import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlRootElement;
/** /**
* The Class MetadataFormat. * The Class MetadataFormat.
* *
* @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it * @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it
* May 27, 2016 * Jun 8, 2016
*/ */
@XmlRootElement(name="metadataformat") @XmlRootElement(name="metadataformat")
@XmlAccessorType(XmlAccessType.FIELD) @XmlAccessorType(XmlAccessType.FIELD)
@ -27,6 +28,8 @@ public class MetadataFormat {
/** /**
* Gets the metadata fields.
*
* @return the metadataFields * @return the metadataFields
*/ */
public List<MetadataField> getMetadataFields() { public List<MetadataField> getMetadataFields() {
@ -36,6 +39,8 @@ public class MetadataFormat {
/** /**
* Sets the metadata fields.
*
* @param metadataFields the metadataFields to set * @param metadataFields the metadataFields to set
*/ */
public void setMetadataFields(List<MetadataField> metadataFields) { public void setMetadataFields(List<MetadataField> metadataFields) {

View File

@ -9,9 +9,10 @@ import javax.xml.bind.annotation.XmlRootElement;
/** /**
* The Class MetadataValidator.
* *
* @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it * @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it
* May 27, 2016 * Jun 8, 2016
*/ */
@XmlRootElement(name = "metadatavalidator") @XmlRootElement(name = "metadatavalidator")
@XmlAccessorType (XmlAccessType.FIELD) @XmlAccessorType (XmlAccessType.FIELD)
@ -21,7 +22,7 @@ public class MetadataValidator {
/** /**
* * Instantiates a new metadata validator.
*/ */
public MetadataValidator() { public MetadataValidator() {
@ -30,7 +31,9 @@ public class MetadataValidator {
/** /**
* @param regularExpression * Instantiates a new metadata validator.
*
* @param regularExpression the regular expression
*/ */
public MetadataValidator(String regularExpression) { public MetadataValidator(String regularExpression) {
@ -40,6 +43,8 @@ public class MetadataValidator {
/** /**
* Gets the regular expression.
*
* @return the regularExpression * @return the regularExpression
*/ */
public String getRegularExpression() { public String getRegularExpression() {
@ -49,6 +54,8 @@ public class MetadataValidator {
/** /**
* Sets the regular expression.
*
* @param regularExpression the regularExpression to set * @param regularExpression the regularExpression to set
*/ */
public void setRegularExpression(String regularExpression) { public void setRegularExpression(String regularExpression) {

View File

@ -10,11 +10,12 @@ import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlRootElement;
/** /**
* The Class MetadataVocabulary. * The Class MetadataVocabulary.
* *
* @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it * @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it
* May 27, 2016 * Jun 8, 2016
*/ */
@XmlRootElement(name = "metadatavocabulary") @XmlRootElement(name = "metadatavocabulary")
@XmlAccessorType (XmlAccessType.FIELD) @XmlAccessorType (XmlAccessType.FIELD)

View File

@ -21,11 +21,12 @@ import org.slf4j.LoggerFactory;
import org.w3c.dom.Node; import org.w3c.dom.Node;
import org.xml.sax.InputSource; import org.xml.sax.InputSource;
/** /**
* The Class MedataFormatReader. * The Class MedataFormatDiscovery.
* *
* @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it * @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it
* @Jul 26, 2013 * Jun 8, 2016
*/ */
public class MedataFormatDiscovery { public class MedataFormatDiscovery {
@ -150,77 +151,11 @@ public class MedataFormatDiscovery {
} }
/** /**
* Gets the metadata type from resource. * Gets the gcube generic query string.
* *
* @param secondaryType the secondary type * @param secondaryType the secondary type
* @return the metadata type from resource * @return the gcube generic query string
*/ */
/*private MetadataType getMetadataTypeFromResource(XPathHelper helper) throws ApplicationProfileNotFoundException{
List<String> metadataformat;
List<GeoexplorerDefaultLayer> listGeoexplorerDefaultLayer = new ArrayList<GeoexplorerDefaultLayer>();
try {
metadataformat = helper.evaluate("/Resource/Profile/Body/metadataformat");
if (metadataformat != null && metadataformat.size() > 0) {
List<String> metadatafields = helper.evaluate("/Resource/Profile/Body/metadataformat/metadatafield/text()");
List<String> 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 <DefaultLayer><UUID> element in <Body>");
} catch (Exception e) {
logger.error("An error occurred in getListDefaultLayerFromNode ", e);
return listGeoexplorerDefaultLayer;
}
return listGeoexplorerDefaultLayer;
}*/
public synchronized String getGcubeGenericQueryString(String secondaryType){ public synchronized String getGcubeGenericQueryString(String secondaryType){
return "for $profile in collection('/db/Profiles/GenericResource')//Resource" + return "for $profile in collection('/db/Profiles/GenericResource')//Resource" +

View File

@ -2,17 +2,22 @@ package org.gcube.datacatalogue.metadatadiscovery.reader;
import static org.gcube.resources.discovery.icclient.ICFactory.client; import static org.gcube.resources.discovery.icclient.ICFactory.client;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.StringReader; import java.io.StringReader;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;
import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.DocumentBuilderFactory;
import org.gcube.common.resources.gcore.utils.XPathHelper; import org.gcube.common.resources.gcore.utils.XPathHelper;
import org.gcube.common.scope.api.ScopeProvider; import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.common.scope.impl.ScopeBean; import org.gcube.common.scope.impl.ScopeBean;
import org.gcube.datacatalogue.metadatadiscovery.bean.MetadataType; import org.gcube.datacatalogue.metadatadiscovery.bean.jaxb.MetadataField;
import org.gcube.datacatalogue.metadatadiscovery.bean.jaxb.MetadataFormat;
import org.gcube.resources.discovery.client.api.DiscoveryClient; import org.gcube.resources.discovery.client.api.DiscoveryClient;
import org.gcube.resources.discovery.client.queries.api.Query; import org.gcube.resources.discovery.client.queries.api.Query;
import org.gcube.resources.discovery.client.queries.impl.QueryBox; import org.gcube.resources.discovery.client.queries.impl.QueryBox;
@ -30,32 +35,34 @@ import org.xml.sax.InputSource;
public class MedataFormatReader { public class MedataFormatReader {
public static Logger logger = LoggerFactory.getLogger(MedataFormatReader.class); 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 ScopeBean scope;
private List<MetadataType> metadataTypes; private MetadataFormat metadataFormat;
private String resourceID; private String resourceID;
/** /**
* Instantiates a new medata format reader. * Instantiates a new medata format reader.
* *
* @param scope - the scope to be searched * @param scope the scope
* @param resourceID the resource id
* @throws Exception the exception * @throws Exception the exception
*/ */
public MedataFormatReader(ScopeBean scope, String resourceID) throws Exception { public MedataFormatReader(ScopeBean scope, String resourceID) throws Exception {
this.scope = scope; this.scope = scope;
this.resourceID = resourceID; this.resourceID = resourceID;
this.metadataTypes = getMedataFormatByID(); this.metadataFormat = getMedataFormatByID(resourceID);
} }
/** /**
* this method looks up the applicationProfile profile among the ones available in the infrastructure. * Gets the medata format by id.
* *
* @return the applicationProfile profile * @param resourceID the resource id
* @return the medata format by id
* @throws Exception the exception * @throws Exception the exception
*/ */
private List<MetadataType> getMedataFormatByID() throws Exception { private MetadataFormat getMedataFormatByID(String resourceID) throws Exception {
logger.trace("read secondary type: "+secondaryType); logger.trace("Get MedataFormat with resourceID: "+resourceID);
if(this.scope==null) if(this.scope==null)
throw new Exception("Scope is null"); throw new Exception("Scope is null");
@ -63,14 +70,14 @@ public class MedataFormatReader {
String scopeString = this.scope.toString(); String scopeString = this.scope.toString();
logger.trace("read scope: "+scopeString); logger.trace("read scope: "+scopeString);
List<MetadataType> list = new ArrayList<MetadataType>(); MetadataFormat mf = new MetadataFormat();
try { try {
ScopeProvider.instance.set(scopeString); ScopeProvider.instance.set(scopeString);
logger.info("scope provider set instance: "+scopeString); logger.info("scope provider set instance: "+scopeString);
String queryString = getGcubeGenericQueryString(secondaryType); String queryString = getQuesryStringForGenericResourceById(resourceID);
logger.trace("queryString: " +queryString); logger.trace("queryString: " +queryString);
Query q = new QueryBox(queryString); Query q = new QueryBox(queryString);
@ -78,70 +85,71 @@ public class MedataFormatReader {
List<String> appProfile = client.submit(q); List<String> appProfile = client.submit(q);
if (appProfile == null || appProfile.size() == 0) if (appProfile == null || appProfile.size() == 0)
throw new ApplicationProfileNotFoundException("Your applicationProfile is not registered in the infrastructure, the scope is "+scopeString); throw new ApplicationProfileNotFoundException("Your resourceID "+resourceID+" is not registered in the infrastructure, the scope is "+scopeString);
else { else {
String theResource = null;
for (String elem : appProfile) { try{
try{ theResource = appProfile.get(0);
DocumentBuilder docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); // logger.trace("Resource with resourceID "+resourceID+" matched "+theResource);
Node node = docBuilder.parse(new InputSource(new StringReader(elem))).getDocumentElement(); logger.trace("Resource with resourceID "+resourceID+" found ");
XPathHelper helper = new XPathHelper(node); DocumentBuilder docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
MetadataType meta = getMetadataTypeFromResource(helper); Node node = docBuilder.parse(new InputSource(new StringReader(theResource))).getDocumentElement();
list.add(meta); XPathHelper helper = new XPathHelper(node);
}catch(Exception e){ List<MetadataField> fields = getMetadataFieldsFromResource(helper);
logger.error("Error while trying to fetch applicationProfile "+secondaryType+" from the infrastructure, ",e); mf.setMetadataFields(fields);
} }catch(Exception e){
logger.error("Error while parsing Resource "+theResource+" from the infrastructure, the scope is "+scopeString,e);
} }
} }
} catch (Exception e) { } catch (Exception e) {
logger.error("Error while trying to fetch applicationProfile "+secondaryType+" from the infrastructure, "+e); logger.error("Error while trying to fetch resourceID "+resourceID+" from the infrastructure, the scope is "+scopeString,e);
return list; return mf;
} finally{ } finally{
ScopeProvider.instance.reset(); ScopeProvider.instance.reset();
} }
return list; return mf;
} }
/** /**
* Gets the metadata type from resource. * Gets the metadata fields from resource.
* *
* @param helper the helper * @param helper the helper
* @return the metadata type from resource * @return the metadata fields from resource
* @throws ApplicationProfileNotFoundException the application profile not found exception * @throws ApplicationProfileNotFoundException the application profile not found exception
*/ */
private MetadataType getMetadataTypeFromResource(XPathHelper helper) throws ApplicationProfileNotFoundException{ private List<MetadataField> getMetadataFieldsFromResource(XPathHelper helper) throws Exception{
try { try {
MetadataType metaType = new MetadataType(); List<String> metadatafields = helper.evaluate("/Resource/Profile/Body/metadataformat/metadatafield");
List<String> id = helper.evaluate("/Resource/ID/text()"); if(metadatafields==null || metadatafields.isEmpty())
throw new Exception("Metadata fields not found");
if(id==null || id.isEmpty()) JAXBContext jaxbContext = JAXBContext.newInstance(MetadataField.class);
throw new ApplicationProfileNotFoundException("Resource ID not found for "+helper.toString()); Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
else{
metaType.setId(id.get(0)); // for (String string : metadatafields) {
// logger.debug("mf "+string);
// }
List<MetadataField> mfs = new ArrayList<MetadataField>();
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");
}
} }
List<String> name = helper.evaluate("/Resource/Profile/Name/text()"); return mfs;
if(name==null || name.isEmpty())
throw new ApplicationProfileNotFoundException("Resource Name not found for "+helper.toString());
else
metaType.setName(name.get(0));
List<String> 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) { } catch (Exception e) {
logger.error("An error occurred in getListDefaultLayerFromNode ", e); logger.error("An error occurred in getListDefaultLayerFromNode ", e);
@ -150,114 +158,42 @@ public class MedataFormatReader {
} }
/** public synchronized String getQuesryStringForGenericResourceById(String resourceId){
* 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<String> metadataformat; return String.format("declare namespace ic = 'http://gcube-system.org/namespaces/informationsystem/registry'; " +
"for $resource in collection('/db/Profiles')//Document/Data/ic:Profile/Resource " +
List<GeoexplorerDefaultLayer> listGeoexplorerDefaultLayer = new ArrayList<GeoexplorerDefaultLayer>(); "where ($resource/ID/text() eq '%s') return $resource", resourceId);
try {
metadataformat = helper.evaluate("/Resource/Profile/Body/metadataformat");
if (metadataformat != null && metadataformat.size() > 0) {
List<String> metadatafields = helper.evaluate("/Resource/Profile/Body/metadataformat/metadatafield/text()");
List<String> 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 <DefaultLayer><UUID> element in <Body>");
} 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 * @return the scope
*/ */
public ScopeBean getScope() { public ScopeBean getScope() {
return scope; return scope;
} }
/**
* Gets the metadata types.
*
* @return the metadataTypes
*/
public List<MetadataType> getMetadataTypes() {
return metadataTypes; /**
* @return the metadataFormat
*/
public MetadataFormat getMetadataFormat() {
return metadataFormat;
} }
/**
* @return the resourceID
*/
public String getResourceID() {
return resourceID;
}
/** /**
* The main method. * The main method.
* *
@ -270,8 +206,8 @@ public class MedataFormatReader {
final ScopeBean scope = new ScopeBean(scopeString); final ScopeBean scope = new ScopeBean(scopeString);
MedataFormatReader reader; MedataFormatReader reader;
try { try {
reader = new MedataFormatReader(scope, ""); reader = new MedataFormatReader(scope, "78355412-b45a-4519-adce-679452583aa2");
System.out.println(reader.getMetadataTypes()); System.out.println(reader.getMetadataFormat());
// String value = "true"; // String value = "true";
// if (value != null){ // if (value != null){

View File

@ -1,111 +0,0 @@
/**
*
*/
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
* May 27, 2016
*/
public class MainJaxb {
static MetadataField ckanMetadata;
static MetadataFormat metadatas;
static String tmpFileXML = "." + File.separator + "CkanMetadatas.xml";
@Test
public static void createMetadata() {
metadatas = new MetadataFormat();
ckanMetadata = new MetadataField();
ckanMetadata.setFieldName("Name");
ckanMetadata.setMandatory(true);
ckanMetadata.setDefaulValue("default value");
List<String> vocabulary = new ArrayList<String>();
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<String> vocabulary2 = new ArrayList<String>();
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);
}
}
}

View File

@ -24,7 +24,7 @@ import org.junit.Test;
* @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it * @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it
* Jun 7, 2016 * Jun 7, 2016
*/ */
public class TestMetadataDiscovery { public class TestJaxbMetadataMarshUnmarsh {
static MetadataField ckanMetadata; static MetadataField ckanMetadata;
static MetadataFormat metadatas; static MetadataFormat metadatas;