/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package nl.biggrid.plier.tools; import java.io.File; import java.io.Serializable; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.util.ArrayList; import java.util.List; import java.util.Properties; import org.hibernate.HibernateException; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import nu.xom.*; import nl.biggrid.plier.opm.*; /** * * @author Souley */ public class PersistenceManager { static final String OPM_NS = "http://openprovenance.org/model/v1.1.a"; static final String CHAR_ENCODING = "UTF-8"; static private PersistenceManager instance = null; private SessionFactory sessionFactory = null; static public PersistenceManager instance() { if (null == instance) { instance = new PersistenceManager(); } return instance; } public void init(String propFile) { sessionFactory = new Configuration().configure(new File(propFile)).buildSessionFactory(); } public void init() { sessionFactory = new Configuration().configure().buildSessionFactory(); } public void init(String propFile, Properties properties) { sessionFactory = new Configuration().configure(propFile) .addProperties(properties) .addResource("opm.hbm.xml") .buildSessionFactory(); } public void shutdown() { } public void persist(final Object o) { Session session = sessionFactory.openSession(); Transaction tx = null; try { tx = session.beginTransaction(); session.save(o); tx.commit(); } catch (HibernateException he) { if (tx != null) { tx.rollback(); } he.printStackTrace(); } finally { session.close(); } } public Object get(final Class type, final Serializable oId) { Object res = null; Session session = sessionFactory.openSession(); Transaction tx = null; try { tx = session.beginTransaction(); res = session.get(type, oId); tx.commit(); } catch (HibernateException he) { if (tx != null) { tx.rollback(); } he.printStackTrace(); } finally { session.close(); } return res; } public void delete(final Object o) { Session session = sessionFactory.openSession(); Transaction tx = null; try { tx = session.beginTransaction(); session.delete(o); tx.commit(); } catch (HibernateException he) { if (tx != null) { tx.rollback(); } he.printStackTrace(); } finally { session.close(); } } public void update(final Object o) { Session session = sessionFactory.openSession(); Transaction tx = null; try { tx = session.beginTransaction(); session.update(o); tx.commit(); } catch (HibernateException he) { if (tx != null) { tx.rollback(); } he.printStackTrace(); } finally { session.close(); } } public List getAll() { List experiments = new ArrayList(); Session session = sessionFactory.openSession(); Transaction tx = null; try { tx = session.beginTransaction(); Query query = session.createQuery("from OPMGraph"); List results = query.list(); for (Object o : results) { experiments.add((OPMGraph)o); } tx.commit(); } catch (HibernateException he) { if (tx != null) { tx.rollback(); } he.printStackTrace(); } finally { session.close(); } return experiments; } public List executeQuery(String query) { List results = null; Session session = sessionFactory.openSession(); Transaction tx = null; try { tx = session.beginTransaction(); results = session.createQuery(query).list(); tx.commit(); } catch (HibernateException he) { if (tx != null) { tx.rollback(); } he.printStackTrace(); } finally { session.close(); } return results; } public List executeSQL(String query) { List results = null; Session session = sessionFactory.openSession(); Transaction tx = null; try { tx = session.beginTransaction(); results = session.createSQLQuery(query).list(); tx.commit(); } catch (HibernateException he) { if (tx != null) { tx.rollback(); } he.printStackTrace(); } finally { session.close(); } return results; } public List executeSQL(String query, Class type, String gid) { List results = null; Session session = sessionFactory.openSession(); Transaction tx = null; try { tx = session.beginTransaction(); results = session.createSQLQuery(query).addEntity(type).setString(0, gid).list(); tx.commit(); } finally { session.close(); } return results; } static public String toXML(final OPMGraph graph) { Element root = (Element) new Element("opm:OPMGraph", OPM_NS); root.addAttribute(new Attribute("id", graph.getId())); Element accounts = (Element) new Element("opm:Accounts", OPM_NS); root.appendChild(accounts); for (Account acc : graph.getAccounts().getAccount()) { addAccount(accounts, acc); } Element agents = (Element) new Element("opm:Agents", OPM_NS); root.appendChild(agents); for (Agent agt : graph.getAgents().getAgent()) { addAgent(agents, agt); } Element artifacts = (Element) new Element("opm:Artifacts", OPM_NS); root.appendChild(artifacts); for (Artifact art : graph.getArtifacts().getArtifact()) { addArtifact(artifacts, art); } Element processes = (Element) new Element("opm:Processes", OPM_NS); root.appendChild(processes); for (nl.biggrid.plier.opm.Process process : graph.getProcesses().getProcess()) { addProcess(processes, process); } Element dependencies = (Element) new Element("opm:CausalDependencies", OPM_NS); root.appendChild(dependencies); for (CausalDependency dep : graph.getCausalDependencies().getDependency()) { addDependency(dependencies, dep); } Element annotations = (Element) new Element("opm:Annotations", OPM_NS); root.appendChild(annotations); for (Annotation anno : graph.getAnnotations().getAnnotation()) { addAnnotation(annotations, anno); } return textify(new Document(root)); } static public void addAccount(final Element parent, final Account account) { Element accElt = (Element) new Element("opm:Account", OPM_NS); accElt.addAttribute(new Attribute("id", account.getId())); for (EmbeddedAnnotation ann : account.getAnnotation()) { addAnnotation(accElt, (Annotation)ann); } parent.appendChild(accElt); } static public void addAccountElement(final Element parent, final Account account) { Element accElt = (Element) new Element("account"); accElt.appendChild(account.getId()); parent.appendChild(accElt); } static public void addAgent(final Element parent, final Agent agent) { Element agtElt = (Element) new Element("opm:Agent", OPM_NS); agtElt.addAttribute(new Attribute("id", agent.getId())); for (Account acc : agent.getAccount()) { addAccountElement(agtElt, acc); } for (EmbeddedAnnotation ann : agent.getAnnotation()) { addAnnotation(agtElt, (Annotation)ann); } parent.appendChild(agtElt); } static public void addProperty(final Element parent, final Property prop) { Element propElt = (Element) new Element("property"); propElt.addAttribute(new Attribute("key", prop.getUri())); propElt.addAttribute(new Attribute("value", prop.getValue())); parent.appendChild(propElt); } static public void addAnnotation(final Element parent, final Annotation anno) { Element annElt = (Element) new Element("opm:Annotation", OPM_NS); annElt.addAttribute(new Attribute("id", anno.getId())); Element esElt = (Element) new Element("externalSubject"); esElt.appendChild(anno.getExternalSubject()); annElt.appendChild(esElt); Element lsElt = (Element) new Element("localSubject"); lsElt.appendChild(anno.getLocalSubject()); annElt.appendChild(lsElt); for (Property prop : anno.getPropertyList()) { addProperty(annElt, prop); } //Element accounts = (Element) new Element("opm:Accounts"); //annElt.appendChild(accounts); for (Account acc : anno.getAccount()) { addAccountElement(annElt, acc); } parent.appendChild(annElt); } static public void addArtifact(final Element parent, final Artifact artifact) { Element artElt = (Element) new Element("opm:Artifact", OPM_NS); artElt.addAttribute(new Attribute("id", artifact.getId())); Element valElt = (Element) new Element("opm:label", OPM_NS); valElt.addAttribute(new Attribute("value", artifact.getValue())); artElt.appendChild(valElt); for (Account acc : artifact.getAccount()) { addAccountElement(artElt, acc); } for (EmbeddedAnnotation ann : artifact.getAnnotation()) { addAnnotation(artElt, (Annotation)ann); } parent.appendChild(artElt); } static public void addProcess(final Element parent, final nl.biggrid.plier.opm.Process process) { Element proElt = (Element) new Element("opm:Process", OPM_NS); proElt.addAttribute(new Attribute("id", process.getId())); for (Account acc : process.getAccount()) { addAccountElement(proElt, acc); } for (EmbeddedAnnotation ann : process.getAnnotation()) { addAnnotation(proElt, (Annotation)ann); } parent.appendChild(proElt); } static public void addDependency(final Element parent, final CausalDependency dep) { if (dep instanceof Used) { addUsed(parent, (Used) dep); } else if (dep instanceof WasControlledBy) { addWasControlledBy(parent, (WasControlledBy) dep); } else if (dep instanceof WasGeneratedBy) { addWasGeneratedBy(parent, (WasGeneratedBy) dep); } else if (dep instanceof WasDerivedFrom) { addWasDerivedFrom(parent, (WasDerivedFrom) dep); } else if (dep instanceof WasTriggeredBy) { addWasTriggeredBy(parent, (WasTriggeredBy) dep); } } static public void addUsed(final Element parent, final Used used) { Element uElt = (Element) new Element("opm:Used", OPM_NS); Element effElt = (Element) new Element("opm:effect", OPM_NS); effElt.addAttribute(new Attribute("ref", used.getEffect().getId())); Element cozElt = (Element) new Element("opm:cause", OPM_NS); cozElt.addAttribute(new Attribute("ref", used.getCause().getId())); Element rolElt = (Element) new Element("opm:role", OPM_NS); rolElt.addAttribute(new Attribute("value", used.getRole())); uElt.appendChild(effElt); uElt.appendChild(cozElt); uElt.appendChild(rolElt); for (Account acc : used.getAccount()) { addAccountElement(uElt, acc); } parent.appendChild(uElt); } static public void addWasControlledBy(final Element parent, final WasControlledBy wcb) { Element wcbElt = (Element) new Element("opm:WasControlledBy", OPM_NS); Element effElt = (Element) new Element("opm:effect", OPM_NS); effElt.addAttribute(new Attribute("ref", wcb.getEffect().getId())); Element cozElt = (Element) new Element("opm:cause", OPM_NS); cozElt.addAttribute(new Attribute("ref", wcb.getCause().getId())); Element rolElt = (Element) new Element("opm:role", OPM_NS); rolElt.addAttribute(new Attribute("value", wcb.getRole())); wcbElt.appendChild(effElt); wcbElt.appendChild(cozElt); wcbElt.appendChild(rolElt); for (Account acc : wcb.getAccount()) { addAccountElement(wcbElt, acc); } parent.appendChild(wcbElt); } static public void addWasGeneratedBy(final Element parent, final WasGeneratedBy wgb) { Element wgbElt = (Element) new Element("opm:WasGeneratedBy", OPM_NS); Element effElt = (Element) new Element("opm:effect", OPM_NS); effElt.addAttribute(new Attribute("ref", wgb.getEffect().getId())); Element cozElt = (Element) new Element("opm:cause", OPM_NS); cozElt.addAttribute(new Attribute("ref", wgb.getCause().getId())); Element rolElt = (Element) new Element("opm:role", OPM_NS); rolElt.addAttribute(new Attribute("value", wgb.getRole())); wgbElt.appendChild(effElt); wgbElt.appendChild(cozElt); for (Account acc : wgb.getAccount()) { addAccountElement(wgbElt, acc); } wgbElt.appendChild(rolElt); parent.appendChild(wgbElt); } static public void addWasDerivedFrom(final Element parent, final WasDerivedFrom wdf) { Element wdfElt = (Element) new Element("opm:WasDerivedFrom", OPM_NS); Element effElt = (Element) new Element("opm:effect", OPM_NS); effElt.addAttribute(new Attribute("ref", wdf.getEffect().getId())); Element cozElt = (Element) new Element("opm:cause", OPM_NS); cozElt.addAttribute(new Attribute("ref", wdf.getCause().getId())); wdfElt.appendChild(effElt); wdfElt.appendChild(cozElt); for (Account acc : wdf.getAccount()) { addAccountElement(wdfElt, acc); } parent.appendChild(wdfElt); } static public void addWasTriggeredBy(final Element parent, final WasTriggeredBy wtb) { Element wtbElt = (Element) new Element("opm:WasTriggeredBy", OPM_NS); Element effElt = (Element) new Element("opm:effect", OPM_NS); effElt.addAttribute(new Attribute("ref", wtb.getEffect().getId())); Element cozElt = (Element) new Element("opm:cause", OPM_NS); cozElt.addAttribute(new Attribute("ref", wtb.getCause().getId())); wtbElt.appendChild(effElt); wtbElt.appendChild(cozElt); for (Account acc : wtb.getAccount()) { addAccountElement(wtbElt, acc); } parent.appendChild(wtbElt); } static public String textify(final Document doc) { String xml = ""; ByteArrayOutputStream baos = new ByteArrayOutputStream(); try { Serializer serializer = new Serializer(baos, CHAR_ENCODING); serializer.setIndent(4); serializer.write(doc); xml = baos.toString(CHAR_ENCODING); } catch (UnsupportedEncodingException uce) { System.err.println(uce); } catch (IOException ex) { System.err.println(ex); } return xml; } static public OPMGraph fromXML(final String xmlGraph) { OPMGraph opmGraph = new OPMGraph(); Builder builder = new Builder(); try { Document doc = builder.build(xmlGraph, null); Element root = doc.getRootElement(); if ("opm:OPMGraph".equalsIgnoreCase(root.getQualifiedName())) { opmGraph.setId(root.getAttributeValue("id")); } Elements children = root.getChildElements(); for (int i=0; i