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
This commit is contained in:
parent
5bbf34ed13
commit
dac0ef075b
15
pom.xml
15
pom.xml
|
@ -58,6 +58,21 @@
|
|||
|
||||
<dependencies>
|
||||
|
||||
<!-- FWS -->
|
||||
<dependency>
|
||||
<groupId>org.gcube.resources.discovery</groupId>
|
||||
<artifactId>ic-client</artifactId>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.gcube.core</groupId>
|
||||
<artifactId>common-scope-maps</artifactId>
|
||||
<scope>provided</scope>
|
||||
<version>[1.0.0-SNAPSHOT, 2.0.0-SNAPSHOT)</version>
|
||||
</dependency>
|
||||
<!-- END FWS -->
|
||||
|
||||
<dependency>
|
||||
<groupId>com.sun.xml.bind</groupId>
|
||||
<artifactId>jaxb-core</artifactId>
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
|
@ -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();
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -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();
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
|
@ -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;
|
|
@ -1,7 +1,7 @@
|
|||
/**
|
||||
*
|
||||
*/
|
||||
package org.gcube.datacatalogue.metadatadiscovery;
|
||||
package org.gcube.datacatalogue.metadatadiscovery.bean.jaxb;
|
||||
|
||||
import java.util.List;
|
||||
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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<MetadataType> 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<MetadataType> 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<MetadataType> list = new ArrayList<MetadataType>();
|
||||
|
||||
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<String> client = client();
|
||||
List<String> 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<String> 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<String> 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<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) {
|
||||
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<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){
|
||||
|
||||
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<MetadataType> 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();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -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<MetadataType> 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<MetadataType> 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<MetadataType> list = new ArrayList<MetadataType>();
|
||||
|
||||
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<String> client = client();
|
||||
List<String> 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<String> 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<String> 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<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) {
|
||||
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<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){
|
||||
|
||||
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<MetadataType> 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();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -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();
|
||||
|
|
@ -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<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);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue