diff --git a/src/main/java/org/gcube/datacatalogue/metadatadiscovery/bean/jaxb/MetadataFormat.java b/src/main/java/org/gcube/datacatalogue/metadatadiscovery/bean/jaxb/MetadataFormat.java index f0d6b12..de5d9e0 100644 --- a/src/main/java/org/gcube/datacatalogue/metadatadiscovery/bean/jaxb/MetadataFormat.java +++ b/src/main/java/org/gcube/datacatalogue/metadatadiscovery/bean/jaxb/MetadataFormat.java @@ -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 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(); } diff --git a/src/main/java/org/gcube/datacatalogue/metadatadiscovery/bean/jaxb/TaggingGroupingValue.java b/src/main/java/org/gcube/datacatalogue/metadatadiscovery/bean/jaxb/TaggingGroupingValue.java index 0b68576..e665694 100644 --- a/src/main/java/org/gcube/datacatalogue/metadatadiscovery/bean/jaxb/TaggingGroupingValue.java +++ b/src/main/java/org/gcube/datacatalogue/metadatadiscovery/bean/jaxb/TaggingGroupingValue.java @@ -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; } diff --git a/src/main/java/org/gcube/datacatalogue/metadatadiscovery/reader/MedataFormatReader.java b/src/main/java/org/gcube/datacatalogue/metadatadiscovery/reader/MedataFormatReader.java index 6dcb994..afaf527 100644 --- a/src/main/java/org/gcube/datacatalogue/metadatadiscovery/reader/MedataFormatReader.java +++ b/src/main/java/org/gcube/datacatalogue/metadatadiscovery/reader/MedataFormatReader.java @@ -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 fields = getMetadataFieldsFromResource(helper); - mf.setMetadataFields(fields); +// List 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 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 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. * diff --git a/src/test/java/org/gcube/datacatalogue/metadatadiscovery/TestDataCatalogueMetadataFormatReader.java b/src/test/java/org/gcube/datacatalogue/metadatadiscovery/TestDataCatalogueMetadataFormatReader.java index ac9f6df..df37697 100644 --- a/src/test/java/org/gcube/datacatalogue/metadatadiscovery/TestDataCatalogueMetadataFormatReader.java +++ b/src/test/java/org/gcube/datacatalogue/metadatadiscovery/TestDataCatalogueMetadataFormatReader.java @@ -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) { diff --git a/src/test/java/org/gcube/datacatalogue/metadatadiscovery/TestJaxbMetadataMarshUnmarsh.java b/src/test/java/org/gcube/datacatalogue/metadatadiscovery/TestJaxbMetadataMarshUnmarsh.java index b988dba..cf24451 100644 --- a/src/test/java/org/gcube/datacatalogue/metadatadiscovery/TestJaxbMetadataMarshUnmarsh.java +++ b/src/test/java/org/gcube/datacatalogue/metadatadiscovery/TestJaxbMetadataMarshUnmarsh.java @@ -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()) {