This commit is contained in:
Lucio Lelii 2009-01-08 20:23:47 +00:00
parent 5ffa9dbefe
commit c3fd4dbe0e
1 changed files with 250 additions and 207 deletions

View File

@ -1,20 +1,31 @@
package org.gcube.vremanagement.vremodeler.impl.util;
import java.io.StringWriter;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.Enumeration;
import java.util.Hashtable;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.gcube.common.core.utils.logging.GCUBELog;
import org.gcube.vremanagement.vremodeler.db.DBInterface;
import org.gcube.vremanagement.vremodeler.impl.ModelerService;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
/**
*
* @author lucio
* @author Lucio Lelii
*
*/
public class XMLUtil {
@ -25,95 +36,97 @@ public class XMLUtil {
/**
* it creates the XML output for VDLGenerator
*
* @param res
* @return String
* @param res the result set of the query on database
* @return String the XML
*/
public static String PrepareCollectionXML(ResultSet res, ArrayList<String> relatedCollection){
StringBuilder temp= new StringBuilder();
String selectedString="false";
temp.append("<ResultSet>");
Document doc= null;
try {
doc= DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
} catch (ParserConfigurationException e1) {
logger.error("Error creating XML Message");
e1.printStackTrace();
}
Element root= doc.createElement("ResultSet");
try{
while (res.next()){
selectedString= relatedCollection.contains(res.getString("ID")) ? "true" : "false";
temp.append("<Collection><ID>");
temp.append(res.getString(1));
temp.append("</ID><Name>");
temp.append(res.getString(2));
temp.append("</Name><Info><Description>");
temp.append(res.getString(3));
temp.append("</Description><NumberOfMembers>");
temp.append(res.getString(4));
temp.append("</NumberOfMembers><CreationTime>");
temp.append(res.getString(5));
temp.append("</CreationTime><LastUpdateTime>");
temp.append(res.getString(6));
temp.append("</LastUpdateTime></Info><Selected>");
temp.append(selectedString);
temp.append("</Selected></Collection>");
Element collection = addElements(doc, "Collection",
new Element[]{createTextElement(doc, "ID", res.getString(1)),
createTextElement(doc, "Name", res.getString(2)),
addElements(doc, "Info",
new Element[]{
createTextElement(doc, "Description",res.getString(3)),
createTextElement(doc, "NumberOfMembers",res.getString(4)),
createTextElement(doc, "CreationTime",res.getString(5)),
createTextElement(doc, "LastUpdateTime",res.getString(6))}),
createTextElement(doc, "Selected",relatedCollection.contains(res.getString(1)) ? "true" : "false") });
root.appendChild(collection);
}
}catch(SQLException e){
logger.error("database error "+e.getMessage());
e.printStackTrace();
}
temp.append("</ResultSet>");
doc.appendChild(root);
//logger.debug("VDLMODEL: request collection XML: "+temp);
return temp.toString();
return docToString(doc);
}
@SuppressWarnings("unchecked")
public static String PrepareGHNsXML(ResultSet res, ArrayList<String> relatedGHNs){
StringBuilder temp = new StringBuilder(),
runningInstancesString ;
Document doc= null;
try {
doc= DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
} catch (ParserConfigurationException e1) {
logger.error("Error creating XML Message");
e1.printStackTrace();
}
Element root= doc.createElement("ResultSet");
ResultSet riRes;
String selectedString="false";
temp.append("<ResultSet>");
try{
while (res.next()){
selectedString= relatedGHNs.contains(res.getString("ID")) ? "true" : "false";
riRes= DBInterface.queryDB("select RI.id, RI.name from RUNNINGINSTANCE as RI, GHNRELATEDRI as REL where REL.GHNID='"+res.getString("ID")+"' and REL.RIID=RI.ID");
runningInstancesString= new StringBuilder();
Element risElement=doc.createElement("RunningInstances");
while (riRes.next()){
runningInstancesString.append("<RunningInstance id=\"");
runningInstancesString.append(riRes.getString(1));
runningInstancesString.append("\" name=\"");
runningInstancesString.append(riRes.getString(2));
runningInstancesString.append("\" />");
Element riElement= createTextElement(doc, "RunningInstances", null, new Couple<String, String>("id", riRes.getString(1)), new Couple<String, String>("name", riRes.getString(2)));
risElement.appendChild(riElement);
}
temp.append("<GHN><ID>");
temp.append(res.getString(1));
temp.append("</ID><Host>");
temp.append(res.getString(2));
temp.append("</Host><Info><Security>");
temp.append(res.getString(3));
temp.append("</Security><UpTime>");
temp.append(res.getString(4));
temp.append("</UpTime><Memory><VirtualAvailable>");
temp.append(res.getString(5));
temp.append("</VirtualAvailable><VirtualSize>");
temp.append(res.getString(6));
temp.append("</VirtualSize><LocalAvailableSpace>");
temp.append(res.getString(7));
temp.append("</LocalAvailableSpace></Memory><Site><Location>");
temp.append(res.getString(8));
temp.append("</Location><Country>");
temp.append(res.getString(9));
temp.append("</Country><Domain>");
temp.append(res.getString(10));
temp.append("</Domain></Site><RunningInstances>");
temp.append(runningInstancesString);
temp.append("</RunningInstances><Libraries></Libraries></Info><Selected>");
temp.append(selectedString);
temp.append("</Selected></GHN>");
Element ghnEl = addElements(doc, "GHN", new Element[]{
createTextElement(doc, "ID",res.getString(1)),
createTextElement(doc, "Host",res.getString(2)),
addElements(doc, "Info", new Element[]{
createTextElement(doc, "Security", res.getString(3)),
createTextElement(doc, "UpTime", res.getString(4)),
addElements(doc, "Memory", new Element[]{
createTextElement(doc, "VirtualAvailable", res.getString(5)),
createTextElement(doc, "VirtualSize", res.getString(6)),
createTextElement(doc, "LocalAvailableSpace", res.getString(7))}),
addElements(doc, "Site", new Element[]{
createTextElement(doc, "Location", res.getString(8)),
createTextElement(doc, "Country", res.getString(9)),
createTextElement(doc, "Domain", res.getString(10))}),
createTextElement(doc, "Libraries", null),
createTextElement(doc, "Selected", relatedGHNs.contains(res.getString("ID")) ? "true" : "false")
})
});
root.appendChild(ghnEl);
}
}catch(SQLException e){
logger.error("database error "+e.getMessage());
}
temp.append("</ResultSet>");
doc.appendChild(root);
//logger.debug("VDLMODEL: request collection XML: "+temp);
return temp.toString();
return docToString(doc);
}
/**
@ -123,89 +136,92 @@ public class XMLUtil {
*/
public static String PrepareMCFormatXML(ResultSet resNative, ResultSet resDerivable, ResultSet allMF, Hashtable<String, ArrayList<String>> selectedMetadata){
Hashtable<String, ArrayList<String>> MDFHash= new Hashtable<String, ArrayList<String>>();
StringBuilder temp;
StringBuilder keyTemp=null;
StringBuilder totalMFList=new StringBuilder();
totalMFList.append("<MFL>");
ArrayList<String> tempArray;
try{
Document doc= null;
try {
doc= DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
} catch (ParserConfigurationException e1) {
logger.error("Error creating XML Message");
e1.printStackTrace();
}
Hashtable<Element, ArrayList<Element>> MDFHash= new Hashtable<Element, ArrayList<Element>>();
Element mflEl= doc.createElement("MFL");
ArrayList<Element> tempArray;
try{
while (allMF.next())
{
temp= new StringBuilder();
temp.append("<Element><MFID>");
temp.append(allMF.getString(1));
temp.append("</MFID><MFName>");
temp.append(allMF.getString(2));
temp.append("</MFName><URI>");
temp.append(allMF.getString(3));
temp.append("</URI><Language>");
temp.append(allMF.getString(4));
temp.append("</Language></Element>");
totalMFList.append(temp);
Element elementEl=addElements(doc, "Element", new Element[]{
createTextElement(doc, "MFID", allMF.getString(1)),
createTextElement(doc, "MFName", allMF.getString(2)),
createTextElement(doc, "URI", allMF.getString(3)),
createTextElement(doc, "Language", allMF.getString(4))
});
mflEl.appendChild(elementEl);
}
totalMFList.append("</MFL>");
//setting the native MDF
while(resNative.next()){
keyTemp= new StringBuilder();
keyTemp.append("<CollectionID>");
keyTemp.append(resNative.getString(1));
keyTemp.append("</CollectionID><Name>");
keyTemp.append(resNative.getString(2));
keyTemp.append("</Name><Description>");
keyTemp.append(resNative.getString(3));
keyTemp.append("</Description>");
temp= new StringBuilder();
temp.append("<MetadataFormat><Id>");
temp.append(resNative.getString(4));
if(selectedMetadata.isEmpty())
temp.append("</Id><Selectable>True</Selectable><Selected>True</Selected></MetadataFormat>");
Element tmpKeyEl= addElements(doc, "Collection", new Element[]{
createTextElement(doc,"CollectionID" , resNative.getString(1)),
createTextElement(doc,"Name" , resNative.getString(2)),
createTextElement(doc,"Description" , resNative.getString(3))
});
Element tmpEl;
if(selectedMetadata.isEmpty() || (selectedMetadata.containsKey(resNative.getString(1))) && (selectedMetadata.get(resNative.getString(1)).contains(resNative.getString(4))) )
tmpEl= addElements(doc, "MetadataFormat", new Element[]{
createTextElement(doc, "Id", resNative.getString(4)),
createTextElement(doc, "Selectable", "True"),
createTextElement(doc, "Selected", "True"),
});
else{
if((selectedMetadata.containsKey(resNative.getString(1))) && (selectedMetadata.get(resNative.getString(1)).contains(resNative.getString(4))))
temp.append("</Id><Selectable>True</Selectable><Selected>True</Selected></MetadataFormat>");
else
temp.append("</Id><Selectable>True</Selectable><Selected>False</Selected></MetadataFormat>");
tmpEl= addElements(doc, "MetadataFormat", new Element[]{
createTextElement(doc, "Id", resNative.getString(4)),
createTextElement(doc, "Selectable", "True"),
createTextElement(doc, "Selected", "False"),
});
}
//logger.debug("MDFProva: "+keyTemp+" "+temp);
if(MDFHash.contains(keyTemp.toString())) MDFHash.get(keyTemp.toString()).add(temp.toString());
if(MDFHash.contains(tmpKeyEl)) MDFHash.get(tmpKeyEl).add(tmpEl);
else{
tempArray=new ArrayList<String>();
tempArray.add(temp.toString());
MDFHash.put(keyTemp.toString(), tempArray );
tempArray=new ArrayList<Element>();
tempArray.add(tmpEl);
MDFHash.put(tmpKeyEl, tempArray );
}
//logger.debug("MDFProva: Native "+MDFHash.get(keyTemp).size());
}
//setting the derivable MDF
while(resDerivable.next()){
keyTemp= new StringBuilder();
keyTemp.append("<CollectionID>");
keyTemp.append(resDerivable.getString(1));
keyTemp.append("</CollectionID><Name>");
keyTemp.append(resDerivable.getString(2));
keyTemp.append("</Name><Description>");
keyTemp.append(resDerivable.getString(3));
keyTemp.append("</Description>");
temp= new StringBuilder();
temp.append("<MetadataFormat><Id>");
temp.append(resDerivable.getString(4));
if(selectedMetadata.isEmpty())
temp.append("</Id><Selectable>True</Selectable><Selected>False</Selected></MetadataFormat>");
Element tmpKeyEl= addElements(doc, "Collection", new Element[]{
createTextElement(doc,"CollectionID" , resDerivable.getString(1)),
createTextElement(doc,"Name" , resDerivable.getString(2)),
createTextElement(doc,"Description" , resDerivable.getString(3))
});
Element tmpEl;
if(selectedMetadata.isEmpty() || !(selectedMetadata.containsKey(resDerivable.getString(1))) && (selectedMetadata.get(resDerivable.getString(1)).contains(resDerivable.getString(4))))
tmpEl= addElements(doc, "MetadataFormat", new Element[]{
createTextElement(doc, "Id", resDerivable.getString(4)),
createTextElement(doc, "Selectable", "True"),
createTextElement(doc, "Selected", "False"),
});
else{
if((selectedMetadata.containsKey(resDerivable.getString(1))) && (selectedMetadata.get(resDerivable.getString(1)).contains(resDerivable.getString(4))))
temp.append("</Id><Selectable>True</Selectable><Selected>True</Selected></MetadataFormat>");
else
temp.append("</Id><Selectable>True</Selectable><Selected>False</Selected></MetadataFormat>");
tmpEl= addElements(doc, "MetadataFormat", new Element[]{
createTextElement(doc, "Id", resDerivable.getString(4)),
createTextElement(doc, "Selectable", "False"),
createTextElement(doc, "Selected", "False"),
});
}
//logger.debug("MDFProva: Derivable "+keyTemp+" "+temp);
if(MDFHash.contains(keyTemp.toString())) MDFHash.get(keyTemp.toString()).add(temp.toString());
if(MDFHash.contains(tmpKeyEl)) MDFHash.get(tmpKeyEl).add(tmpEl);
else{
tempArray=new ArrayList<String>();
tempArray.add(temp.toString());
MDFHash.put(keyTemp.toString(), tempArray );
tempArray=new ArrayList<Element>();
tempArray.add(tmpEl);
MDFHash.put(tmpKeyEl, tempArray );
}
}
@ -215,44 +231,31 @@ public class XMLUtil {
e.printStackTrace();
}
StringBuilder resultString= new StringBuilder();
resultString.append("<ResultSet>");
resultString.append(totalMFList);
resultString.append("<CollectionList>");
String key;
Enumeration<String> k=MDFHash.keys();
Element collectionListEl= doc.createElement("CollectionList");
Element key;
Enumeration<Element> k=MDFHash.keys();
while (k.hasMoreElements()){
key= k.nextElement();
ArrayList<String> tempMD=MDFHash.get(key);
ArrayList<Element> tempMD=MDFHash.get(key);
//logger.debug("ArrayProva"+tempMD.size());
resultString.append("<Collection>");
resultString.append(key);
resultString.append("<MetadataFormatList>");;
for (String obj: tempMD){
resultString.append(obj);
//logger.debug("ArrayProva "+obj);
}
resultString.append("</MetadataFormatList></Collection>");
collectionListEl.appendChild(addElements(doc, "Collection", new Element[]{
key, addElements(doc, "MetadataFormatList", tempMD.toArray(new Element[0]))
}));
}
resultString.append("</CollectionList></ResultSet>");
doc.appendChild(addElements(doc, "ResultSet", new Element[]{mflEl, collectionListEl}));
//logger.debug("VDLMODEL: request MCFormat XML "+resultString);
return resultString.toString();
return docToString(doc);
}
/**
*
* @param resNative
* @param resDerivable
* @return
*
* @return the XML representing the quality string
*/
public static String PrepareQualityXML(){
StringBuilder qualityString=new StringBuilder();
qualityString.append("<ResultSet>");
qualityString.append("<Quality><DisplayName>Response Time</DisplayName><Description>This quality parameter indicates the importance of response time (i.e., the delay from a function request to the reception of the response) ");
qualityString.append("in selecting the resource (both in number and type) needed to satisfy the Digital Library definition criteria. E.g., the specification of a 100% response time will result in a maximization of the resources ");
qualityString.append("allocated as to minimise the response time. </Description><Multiple value=\"true\"/><CanBeEmpty value=\"false\"/><AllowedValues><Value default=\"true\" selected=\"false\">*</Value></AllowedValues></Quality>");
qualityString.append("<Quality><DisplayName>Robustness</DisplayName><Description>This quality parameter indicates the importance of robustness (i.e., the resilience to faults) in selecting the resource (both in number and type)");
@ -275,43 +278,44 @@ public class XMLUtil {
/**
*
* @param res
* @param selectedFunct
* @param selectedCS
* @return
*/
@SuppressWarnings("unchecked")
public static String PrepareFunctionalityXML(ResultSet res, ArrayList<Integer> selectedFunct, ArrayList<String> selectedCS){
StringBuilder toReturn=new StringBuilder();
toReturn.append("<Resultset>");
Document doc= null;
try {
doc= DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
} catch (ParserConfigurationException e1) {
logger.error("Error creating XML Message");
e1.printStackTrace();
}
Element root= doc.createElement("ResultSet");
try {
while(res.next()){
StringBuilder temp= new StringBuilder();
temp.append("<Functionality id=\"");
temp.append(res.getInt(1));
temp.append("\" name=\"");
temp.append(res.getString(2));
temp.append("\" ");
if (res.getInt(1)==10) temp.append(" enableCS=\"true\" ");
if(selectedFunct.contains(res.getInt(1))) temp.append("selected=\"true\">");
else {
temp.append("selected=\"false\">");
temp.append("<FunctionalityDescription>");
temp.append(res.getString(3));
temp.append("</FunctionalityDescription>");
}
Element functionalityEl= createTextElement(doc, "Functionality", null, new Couple<String, String>("id", res.getInt(1)+""),
new Couple<String, String>("name", res.getString(2)),
new Couple<String, String>("enableCS", res.getInt(1)==10 ? "true" : "false"),
new Couple<String, String>("selected", selectedFunct.contains(res.getInt(1)) ? "true" : "false")
);
functionalityEl.appendChild(createTextElement(doc, "FunctionalityDescription", res.getString(3)));
ResultSet subFunctRes=DBInterface.queryDB("select * from FUNCTIONALITY where father='"+res.getInt(1)+"';");
while(subFunctRes.next()){
temp.append("<child>");
temp.append("<Functionality id=\"");
temp.append(subFunctRes.getInt(1));
temp.append("\" name=\"");
temp.append(subFunctRes.getString(2));
temp.append("\" ");
if(selectedFunct.contains(subFunctRes.getInt(1))) temp.append("selected=\"true\">");
else temp.append("selected=\"false\">");
temp.append("<FunctionalityDescription>");
temp.append(subFunctRes.getString(3));
temp.append("</FunctionalityDescription>");
temp.append("</Functionality></child>");
Element subFunctEl=createTextElement(doc,"Functionality" , null, new Couple<String, String>("id", subFunctRes.getInt(1)+""),
new Couple<String, String>("name", subFunctRes.getString(2)),
new Couple<String, String>("enableCS", subFunctRes.getInt(1)==10 ? "true" : "false"),
new Couple<String, String>("selected", selectedFunct.contains(subFunctRes.getInt(1)) ? "true" : "false"));
subFunctEl.appendChild(createTextElement(doc, "FunctionalityDescription", subFunctRes.getString(3)));
functionalityEl.appendChild(addElements(doc, "child", new Element[]{subFunctEl}));
}
temp.append("</Functionality>");
toReturn.append(temp);
root.appendChild(functionalityEl);
}
} catch (SQLException e) {
logger.error("VDLModelService: DB error preparing XML for Functionality");
@ -321,31 +325,15 @@ public class XMLUtil {
try {
ResultSet csRes=DBInterface.queryDB("select * from cs;");
if (csRes.next()){
StringBuilder temp= new StringBuilder();
temp.append("<CS id=\"-1\" name=\"Workflows\" ");
//if(selectedFunct.contains(res.getInt(1))) temp+="selected=\"true\">";
//else
temp.append("selected=\"false\">");
temp.append("<CSDescription>This class of functions contains compound functions defined by aggregating existing functions in structured workflows as to deliver advanced features.</CSDescription>");
Element csEl= createTextElement(doc, "CS", null, new Couple<String, String>("id", "-1"), new Couple<String, String>("name", "Workflows"), new Couple<String, String>("selected", "false"));
csEl.appendChild(createTextElement(doc,"CSDescription" , "This class of functions contains compound functions defined by aggregating existing functions in structured workflows as to deliver advanced features."));
do{
temp.append("<child><CS id=\"");
temp.append(csRes.getString(1));
temp.append("\" name=\"");
temp.append(csRes.getString(2));
temp.append("\" ");
if(selectedCS.contains(csRes.getString(1))) temp.append("selected=\"true\">");
else temp.append("selected=\"false\">");
temp.append("<CSDescription>");
temp.append(csRes.getString(3));
temp.append("</CSDescription>");
temp.append("</CS></child>");
Element subCsEl= createTextElement(doc, "CS", null, new Couple<String, String>("id", csRes.getString(1)), new Couple<String, String>("name", csRes.getString(2)), new Couple<String, String>("selected", selectedCS.contains(csRes.getString(1)) ? "true" : "false"));
subCsEl.appendChild(createTextElement(doc,"CSDescription" , csRes.getString(3)));
csEl.appendChild(addElements(doc, "child", new Element[]{subCsEl}));
}while (csRes.next());
temp.append("</CS>");
toReturn.append(temp);
root.appendChild(csEl);
}
@ -353,9 +341,8 @@ public class XMLUtil {
logger.error("VDLModelService: DB error retreiving CS");
e.printStackTrace();
}
toReturn.append("</Resultset>");
return toReturn.toString();
doc.appendChild(root);
return docToString(doc);
}
@ -408,5 +395,61 @@ public class XMLUtil {
return toReturn.toString();
}
/**
* transform a Document into string
*
* @param doc Document
* @return a String
*/
public static String docToString(Document doc){
String temp= null;
try{
DOMSource domSource = new DOMSource(doc);
TransformerFactory tf = TransformerFactory.newInstance();
Transformer serializer = tf.newTransformer();
StringWriter sw= new StringWriter();
StreamResult sr= new StreamResult(sw);
serializer.transform(domSource, sr);
temp=sr.getWriter().toString();
}catch(Exception e){
logger.error("transformation to String Error");
e.printStackTrace();
}
return temp;
}
/**
*
* @param doc the root Document
* @param root the element tag name
* @param elements elements to add
* @return the result element
*/
public static Element addElements(Document doc, String root, Element[] elements){
Element returnEl= doc.createElement(root);
for (Element el: elements){
returnEl.appendChild(el);
}
return returnEl;
}
/**
*
* @param doc the root Document
* @param Tag the element tag name
* @param text the text to add at the element
* @return the result element
*/
public static Element createTextElement(Document doc, String Tag, String text, Couple<String, String> ... attributes ){
Element returnEl= doc.createElement("UpTime");
if (attributes!=null){
for (Couple<String, String> attribute : attributes){
returnEl.setAttribute(attribute.first, attribute.second);
}
}
if (text!=null) returnEl.appendChild(doc.createTextNode(text));
return returnEl;
}
}