fixes on unmarshalling

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/data-catalogue/gcubedatacatalogue-metadata-discovery@146303 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Francesco Mangiacrapa 2017-03-23 16:48:38 +00:00
parent 65b1fae2b2
commit 6f4b3c0397
5 changed files with 49 additions and 21 deletions

View File

@ -9,6 +9,7 @@ import java.util.List;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;
@ -43,7 +44,7 @@ public class MetadataFormat implements Serializable{
@XmlElement(name = "metadatafield")
private List<MetadataField> metadataFields;
@XmlElement(name = "metadatatype", required=true)
@XmlAttribute(name = "metadatatype", required=true)
private String metadataType = "";
@XmlTransient
@ -82,8 +83,6 @@ public class MetadataFormat implements Serializable{
}
/**
* Gets the metadata categories.
*
@ -94,8 +93,6 @@ public class MetadataFormat implements Serializable{
return metadataCategories;
}
/**
* Sets the metadata categories.
*
@ -180,8 +177,6 @@ public class MetadataFormat implements Serializable{
builder.append(metadataFields);
builder.append(", metadataType=");
builder.append(metadataType);
builder.append(", metadataSource=");
builder.append(metadataSource);
builder.append("]");
return builder.toString();
}

View File

@ -53,7 +53,6 @@ public enum TaggingGroupingValue {
try{
for (TaggingGroupingValue tgv : TaggingGroupingValue.values()) {
System.out.println("Comparing input value: "+value +" with "+tgv.value );
if(tgv.value.equals(value))
return tgv;
}

View File

@ -49,19 +49,20 @@ public class MedataFormatReader {
public MedataFormatReader(ScopeBean scope, String resourceID) throws Exception {
this.scope = scope;
this.resourceID = resourceID;
this.metadataFormat = getMedataFormatByID(resourceID);
this.metadataFormat = getMetadataFormatByID(resourceID);
}
/**
* Gets the medata format by id.
* Gets the metadata format by id.
*
* @param resourceID the resource id
* @return the medata format by id
* @return the metadata format by id
* @throws Exception the exception
*/
private MetadataFormat getMedataFormatByID(String resourceID) throws Exception {
logger.trace("Get MedataFormat with resourceID: "+resourceID);
private MetadataFormat getMetadataFormatByID(String resourceID) throws Exception {
logger.trace("Getting MedataFormat with resourceID: "+resourceID);
if(this.scope==null)
throw new Exception("Scope is null");
@ -73,9 +74,6 @@ public class MedataFormatReader {
try {
// ScopeProvider.instance.set(scopeString);
// logger.info("scope provider set instance: "+scopeString);
logger.info("Using scope from ScopeProvider: "+scopeString);
String queryString = getQuesryStringForGenericResourceById(resourceID);
@ -92,15 +90,16 @@ public class MedataFormatReader {
try{
theResource = appProfile.get(0);
// logger.trace("Resource with resourceID "+resourceID+" matched "+theResource);
logger.trace("Resource with resourceID "+resourceID+" found ");
logger.debug("Resource (Metadata Format) with resourceID "+resourceID+" found");
DocumentBuilder docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Node node = docBuilder.parse(new InputSource(new StringReader(theResource))).getDocumentElement();
XPathHelper helper = new XPathHelper(node);
List<MetadataField> fields = getMetadataFieldsFromResource(helper);
mf.setMetadataFields(fields);
// List<MetadataField> fields = getMetadataFieldsFromResource(helper);
// mf.setMetadataFields(fields);
logger.debug("Unmarshalling it..");
mf = getMetadataFormatFromResource(helper);
String metadataSource = getMetadataFormatSourceFromResource(helper);
mf.setMetadataSource(metadataSource);
}catch(Exception e){
logger.error("Error while parsing Resource "+theResource+" from the infrastructure, the scope is "+scopeString,e);
}
@ -141,6 +140,38 @@ public class MedataFormatReader {
}
}
/**
* Gets the metadata format from resource.
*
* @param helper the helper
* @return the metadata format from resource
* @throws Exception the exception
*/
private MetadataFormat getMetadataFormatFromResource(XPathHelper helper) throws Exception{
try {
List<String> metadataFormatSource = helper.evaluate("/Resource/Profile/Body/metadataformat");
JAXBContext jaxbContext = JAXBContext.newInstance(MetadataFormat.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
if(metadataFormatSource==null || metadataFormatSource.size()==0)
throw new Exception("Resource does not contain <metadataformat> in the body");
InputStream stream = new ByteArrayInputStream(metadataFormatSource.get(0).getBytes());
MetadataFormat mtdf = (MetadataFormat) jaxbUnmarshaller.unmarshal(stream);
logger.debug("returning metadata format with medata type: "+mtdf.getMetadataType());
return mtdf;
}catch(Exception e){
logger.error("An error occurred in getMetadataFormatSourceFromResource ", e);
return null;
}
}
/**
* Gets the metadata fields from resource.
*

View File

@ -35,7 +35,7 @@ public class TestDataCatalogueMetadataFormatReader {
int i = 0;
for (MetadataType mt : reader.getListOfMetadataTypes()) {
MetadataFormat metadataFormat = reader.getMetadataFormatForMetadataType(mt);
logger.trace("\n\n "+ ++i +".) Metadata Format: "+metadataFormat.toString());
//logger.trace("\n\n "+ ++i +".) Metadata Format: "+metadataFormat.toString());
}
} catch (Exception e) {

View File

@ -134,6 +134,9 @@ public class TestJaxbMetadataMarshUnmarsh {
//We had written this file in marshalling example
MetadataFormat mtds = (MetadataFormat) jaxbUnmarshaller.unmarshal(new File(tmpFileXML));
System.out.println("Metadata Format");
System.out.println(mtds);
System.out.println("Metadata Categories");
if(mtds.getMetadataCategories()!=null){
for (MetadataCategory cat : mtds.getMetadataCategories()) {