From 8c9328e2997bdf863e9f4d041c51b892ac8d92ff Mon Sep 17 00:00:00 2001 From: Lucio Lelii Date: Fri, 12 Dec 2008 18:49:02 +0000 Subject: [PATCH] git-svn-id: http://svn.research-infrastructures.eu/public/d4science/gcube/trunk/vre-management/VREModeler@7769 82a268e6-3cf1-43bd-a215-b396298e98cf --- .../vremodeler/db/IStoDBUtil.java | 114 ++++++++++++------ 1 file changed, 79 insertions(+), 35 deletions(-) diff --git a/src/org/gcube/vremanagement/vremodeler/db/IStoDBUtil.java b/src/org/gcube/vremanagement/vremodeler/db/IStoDBUtil.java index f985afa..6128733 100644 --- a/src/org/gcube/vremanagement/vremodeler/db/IStoDBUtil.java +++ b/src/org/gcube/vremanagement/vremodeler/db/IStoDBUtil.java @@ -1,12 +1,21 @@ package org.gcube.vremanagement.vremodeler.db; import java.io.StringReader; +import java.io.StringWriter; +import java.net.URI; import java.rmi.RemoteException; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.transform.OutputKeys; +import javax.xml.transform.Transformer; +import javax.xml.transform.TransformerFactory; +import javax.xml.transform.dom.DOMSource; +import javax.xml.transform.stream.StreamResult; import javax.xml.xpath.XPath; +import javax.xml.xpath.XPathConstants; import javax.xml.xpath.XPathExpression; import javax.xml.xpath.XPathFactory; @@ -43,6 +52,8 @@ import org.gcube.vremanagement.vremodeler.impl.util.MBrokerServiceHandler; import org.gcube.vremanagement.vremodeler.impl.util.MFRelationDerivate; import org.gcube.vremanagement.vremodeler.impl.util.MFRelationNative; import org.gcube.vremanagement.vremodeler.impl.util.MetadataFormat; +import org.w3c.dom.Document; +import org.w3c.dom.NodeList; import org.xml.sax.InputSource; import com.sun.org.apache.bcel.internal.generic.NEW; @@ -247,7 +258,7 @@ public class IStoDBUtil { List row; List metadataFormatList= new ArrayList(); List nativeMetadataFormatList= new ArrayList(); - + List derivateMetadataFormatList= new ArrayList(); for (GCUBEMCollection col: collectionList ){ row= new ArrayList(4); @@ -269,9 +280,9 @@ public class IStoDBUtil { try { ServiceContext.getContext().setScope(scope); - getMFDerivate(metadataFormatList, col); + derivateMetadataFormatList.addAll(getMFDerivate(metadataFormatList, col)); } catch (Exception e) { - System.out.println("Error in getMFDerivate"); + logger.error("Error in getMFDerivate"+e); e.printStackTrace(); } @@ -284,6 +295,7 @@ public class IStoDBUtil { DBInterface.InsertInto("mdcollection", values); DBInterface.insertIntoListable("mdformat", metadataFormatList ); DBInterface.insertIntoListable("nativemdf", nativeMetadataFormatList); + DBInterface.insertIntoListable("derivablemdf", derivateMetadataFormatList ); } @@ -291,13 +303,15 @@ public class IStoDBUtil { throw new GCUBEFault(e); } } - + + + private static List getMFDerivate(List mdfList, GCUBEMCollection col ) throws Exception{ - MBrokerServiceHandler, MetadataFormat> sHandler= new MBrokerServiceHandler,MetadataFormat >(){ + MBrokerServiceHandler sHandler= new MBrokerServiceHandler(){ @Override - protected List makeCall(MetadataBrokerPortType mbportType) throws Exception { + protected String[] makeCall(MetadataBrokerPortType mbportType) throws Exception { FindPossibleTransformationPrograms fptp= new FindPossibleTransformationPrograms(); MetadataBrokerFormat mbInFormat= new MetadataBrokerFormat(); mbInFormat.setType("collection"); @@ -308,44 +322,74 @@ public class IStoDBUtil { MetadataBrokerFormat mbOutFormat= new MetadataBrokerFormat(); mbOutFormat.setType("collection"); fptp.setOutputFormat(mbOutFormat); - - List genericRes= new ArrayList(); - GCUBEGenericResource gen; - if (mbportType==null) System.out.println("the MetadataBroker portType is null"); - FindPossibleTransformationProgramsResponse tpRes= mbportType.findPossibleTransformationPrograms(fptp); - if (tpRes==null){ - System.out.println("the result is null"); - return new ArrayList(); - } - String[] tpArray = tpRes.getTransformationProgram(); - if (tpArray== null){ - System.out.println("the array is null"); - return new ArrayList(); - } - for (String res:tpArray){ - gen= GHNContext.getImplementation(GCUBEGenericResource.class); - gen.load(new StringReader(res)); - genericRes.add(gen); - } - return genericRes; + return tpRes.getTransformationProgram(); } }; + sHandler.setHandled(new GCUBEServiceClientImpl()); sHandler.setParameter(mdfList.get(mdfList.size()-1)); sHandler.run(); - List resList=sHandler.getReturnValue(); + String[] resList=sHandler.getReturnValue(); - XPathFactory factory = XPathFactory.newInstance(); - XPath xpath= factory.newXPath(); - XPathExpression schemaExpr=xpath.compile("/TransformationProgram/Output[@Name='TPOutput']/Schema/text()"); - XPathExpression languageExpr=xpath.compile("/TransformationProgram/Output[@Name='TPOutput']/Language[@isVariable]/text()"); - for(GCUBEGenericResource res: resList){ - System.out.println(schemaExpr.evaluate(new InputSource(new StringReader(res.getBody())))); - System.out.println(languageExpr.evaluate(new InputSource(new StringReader(res.getBody())))); + XPath xpath= XPathFactory.newInstance().newXPath(); + String schemaExpr="//TransformationProgram/Output[@name='TPOutput']/Schema/text()"; + String languageExpr="//TransformationProgram/Output[@name='TPOutput']/Language[@isVariable='false']/text()"; + String typeExpr="//TransformationProgram/Output[@name='TPOutput']/Type/text()"; + String tpIdExpr="/Resource/ID/text()"; + + Document doc; + + Transformer transformer = TransformerFactory.newInstance().newTransformer(); + transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION,"yes"); + + List derivatedList= new ArrayList(); + + NodeList set; + StreamResult sr; + String tpId; + for(String res: resList){ + doc= DocumentBuilderFactory.newInstance().newDocumentBuilder().parse( new InputSource(new StringReader(res))); + + set = (NodeList) xpath.evaluate(schemaExpr,doc, XPathConstants.NODESET); + sr = new StreamResult(new StringWriter()); + try {transformer.transform(new DOMSource(set.item(0)),sr);}catch(Exception ignore) {continue;} + if (sr.getWriter().toString().compareTo("collection")!=0){ + + set = (NodeList) xpath.evaluate(languageExpr,doc, XPathConstants.NODESET); + sr = new StreamResult(new StringWriter()); + try {transformer.transform(new DOMSource(set.item(0)),sr);}catch(Exception ignore) {continue;} + String[] schemas= sr.getWriter().toString().split("="); + + set = (NodeList) xpath.evaluate(tpIdExpr,doc, XPathConstants.NODESET); + sr = new StreamResult(new StringWriter()); + try {transformer.transform(new DOMSource(set.item(0)),sr);}catch(Exception ignore) {continue;} + tpId= sr.getWriter().toString(); + + MetadataFormat mfTemp; + + set = (NodeList) xpath.evaluate(typeExpr,doc, XPathConstants.NODESET); + sr = new StreamResult(new StringWriter()); + try {transformer.transform(new DOMSource(set.item(0)),sr);}catch(Exception ignore) {continue;} + if (sr.getWriter().toString().compareTo("")==0) + mfTemp=new MetadataFormat(uuidMFGEN.nextUUID(), schemas[0], new URI(schemas[1]), "any"); + else + mfTemp=new MetadataFormat(uuidMFGEN.nextUUID(), schemas[0], new URI(schemas[1]), sr.getWriter().toString()); + + MFRelationDerivate mfRelTemp; + if (mdfList.contains(mfTemp)) + mfRelTemp =new MFRelationDerivate(col.getID(), mdfList.get(mdfList.indexOf(mfTemp)).getId(), tpId ); + else { + mfRelTemp =new MFRelationDerivate(col.getID(), mfTemp.getId(), tpId ); + mdfList.add(mfTemp); + } + + derivatedList.add(mfRelTemp); + + } } - return null; + return derivatedList; }