[HepData-svn] r1335 - trunk/hepdata-migration/src/main/java/cedar/hepdata/migration

blackhole at projects.hepforge.org blackhole at projects.hepforge.org
Wed Jan 6 15:12:03 GMT 2010


Author: whalley
Date: Wed Jan  6 15:12:02 2010
New Revision: 1335

Log:
adding java code for deletion of db records

Added:
   trunk/hepdata-migration/src/main/java/cedar/hepdata/migration/DeleteFromDb.java
   trunk/hepdata-migration/src/main/java/cedar/hepdata/migration/DeleteFromDbHepId.java
   trunk/hepdata-migration/src/main/java/cedar/hepdata/migration/GetFromDb.java

Added: trunk/hepdata-migration/src/main/java/cedar/hepdata/migration/DeleteFromDb.java
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ trunk/hepdata-migration/src/main/java/cedar/hepdata/migration/DeleteFromDb.java	Wed Jan  6 15:12:02 2010	(r1335)
@@ -0,0 +1,72 @@
+package cedar.hepdata.migration;
+
+import cedar.hepdata.model.*;
+import cedar.hepdata.xml.*;
+import cedar.hepdata.db.*;
+
+import org.hibernate.Query;
+
+import java.io.File;
+import java.util.*;
+
+import org.apache.log4j.*;
+
+
+// USAGE
+//
+// To run this class, use:
+//   java cedar.hepdata.migration.DeleteFromDb irn [irn ...]
+//
+// There will be the usual issues with Java classpaths. I recommend
+// running
+//   mvn dependency:build-classpath -Dmaven.dep.cpFile=cp.txt
+// and then using the java command with the -cp option, cf.
+//   java -cp $(echo -n "target/test-classes:target/classes:" && cat cp.txt) ...
+
+
+public class DeleteFromDb {
+    static Logger log = Logger.getLogger(DeleteFromDb.class);
+
+
+    static public void main(String[] args) {
+        log.info("Deleting Papers (via irns) from database");
+
+
+        // Get irns
+        String[] spiresIds = args;
+        if (spiresIds.length == 0) {
+            System.exit(1);
+        }
+
+        DbUtils.getSession();
+	DbUtils.beginTransaction();
+
+        for (String spiresId : spiresIds) {
+            Query q = DbUtils.getSession().createQuery("select distinct p from Paper p where p._spiresId=:spiresId");
+            q.setString("spiresId",spiresId);
+	    q.uniqueResult();
+	    List results = q.list();
+	    if(results != null){
+	        for (Object result : results){
+	            Paper p = (Paper) result;
+                    log.info(p);
+                    try {
+                        log.info("Deleting from db");
+	                DbUtils.getSession().delete(p);
+                        log.info("Deleted from db");
+                    } catch (Exception e) {
+                        log.error("Problem with deleting from database:");
+                        log.error(e);
+                        if (log.isDebugEnabled()) e.printStackTrace();
+                    }  
+                }  
+            } else{
+	        log.error("Query failed");
+	    }
+        }
+        DbUtils.endTransaction();
+        DbUtils.endSession();
+    }
+
+
+}

Added: trunk/hepdata-migration/src/main/java/cedar/hepdata/migration/DeleteFromDbHepId.java
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ trunk/hepdata-migration/src/main/java/cedar/hepdata/migration/DeleteFromDbHepId.java	Wed Jan  6 15:12:02 2010	(r1335)
@@ -0,0 +1,72 @@
+package cedar.hepdata.migration;
+
+import cedar.hepdata.model.*;
+import cedar.hepdata.xml.*;
+import cedar.hepdata.db.*;
+
+import org.hibernate.Query;
+
+import java.io.File;
+import java.util.*;
+
+import org.apache.log4j.*;
+
+
+// USAGE
+//
+// To run this class, use:
+//   java cedar.hepdata.migration.DeleteFromDbHepId HepdataId [HepdataId ...]
+//
+// There will be the usual issues with Java classpaths. I recommend
+// running
+//   mvn dependency:build-classpath -Dmaven.dep.cpFile=cp.txt
+// and then using the java command with the -cp option, cf.
+//   java -cp $(echo -n "target/test-classes:target/classes:" && cat cp.txt) ...
+
+
+public class DeleteFromDbHepId {
+    static Logger log = Logger.getLogger(DeleteFromDbHepId.class);
+
+
+    static public void main(String[] args) {
+        log.info("Deleting Papers (via hepdataIds) from database");
+
+
+        // Get hepdataIds
+        String[] hepdataIds = args;
+        if (hepdataIds.length == 0) {
+            System.exit(1);
+        }
+
+        DbUtils.getSession();
+	DbUtils.beginTransaction();
+
+        for (String hepdataId : hepdataIds) {
+            Query q = DbUtils.getSession().createQuery("select distinct p from Paper p where p._hepdataId=:hepdataId");
+            q.setString("hepdataId",hepdataId);
+	    q.uniqueResult();
+	    List results = q.list();
+	    if(results != null){
+	        for (Object result : results){
+	            Paper p = (Paper) result;
+                    log.info(p);
+                    try {
+                        log.info("Deleting from db");
+	                DbUtils.getSession().delete(p);
+                        log.info("Deleted from db");
+                    } catch (Exception e) {
+                        log.error("Problem with deleting from database:");
+                        log.error(e);
+                        if (log.isDebugEnabled()) e.printStackTrace();
+                    }  
+                }  
+            } else{
+	        log.error("Query failed");
+	    }
+        }
+        DbUtils.endTransaction();
+        DbUtils.endSession();
+    }
+
+
+}

Added: trunk/hepdata-migration/src/main/java/cedar/hepdata/migration/GetFromDb.java
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ trunk/hepdata-migration/src/main/java/cedar/hepdata/migration/GetFromDb.java	Wed Jan  6 15:12:02 2010	(r1335)
@@ -0,0 +1,104 @@
+package cedar.hepdata.migration;
+
+import cedar.hepdata.model.*;
+import cedar.hepdata.xml.*;
+import cedar.hepdata.db.*;
+
+import org.hibernate.Query;
+
+import java.io.File;
+import java.util.*;
+
+import java.io.Writer;
+import java.io.StringWriter;
+import java.io.FileWriter;
+import java.io.PrintWriter;
+import java.io.FileOutputStream;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.io.File;
+import java.io.InputStream;
+import java.net.URL;
+
+import org.apache.log4j.*;
+
+import org.exolab.castor.xml.*;
+import org.exolab.castor.mapping.Mapping;
+
+// USAGE
+//
+// To run this class, use:
+//   java cedar.hepdata.migration.GetFromDb irn [irn ...]
+//
+// There will be the usual issues with Java classpaths. I recommend
+// running
+//   mvn dependency:build-classpath -Dmaven.dep.cpFile=cp.txt
+// and then using the java command with the -cp option, cf.
+//   java -cp $(echo -n "target/test-classes:target/classes:" && cat cp.txt) ...
+
+
+public class GetFromDb {
+    static Logger log = Logger.getLogger(AddToDb.class);
+
+    static public void main(String[] args) {
+        log.info("Fetching paper as HepML for Database");
+
+
+        // Get irn
+        String[] irns = args;
+        if (irns.length == 0) {
+            System.exit(1);
+        }
+        for (String irn : irns) {
+            System.out.println(irn);
+            DbUtils.getSession();
+            DbUtils.beginTransaction();
+//            Query q = DbUtils.getSession().createQuery("select distinct ds from  Dataset ds where ds._id=1"); 
+//            Query q = DbUtils.getSession().createQuery("select distinct ds from  Paper p join fetch p._datasets ds where  p._spiresId=:irn"); 
+            Query q = DbUtils.getSession().createQuery("select distinct p from  Paper p where p._spiresId=:irn"); 
+            q.setString( "irn",irn);
+            System.out.println(q);
+            List results = q.list();
+            Paper p = (Paper) q.uniqueResult();
+            Data d = Data.makeExample();
+//            Dataset ds = (Dataset) q.uniqueResult();
+            log.info(d.toString());
+//            if (results != null) {
+//                log.info("Num papers: " + results.size());
+//                for (Object result : results) {
+//                    Paper p = (Paper) result;
+                      try{ 
+//                           Writer writer = new FileWriter("test.xml");
+                           StringWriter sw = new StringWriter();
+                           Mapping mapping = new Mapping();
+                           URL mapURL = XMLUtils.class.getResource("/mapping.xml");                           
+                            log.info(mapURL.toString());
+                            mapping.loadMapping(mapURL);                            
+//                            mapping.loadMapping("mapping.xml");                            
+//                            Marshaller.marshal(p,sw);
+////                           log.info(mapping.toString());
+                            Marshaller m = new Marshaller(sw);
+                            m.setMapping(mapping);
+                            m.marshal(p);
+                            log.info(sw);
+//                           log.info(XMLUtils.makeHepMLString(p)); 
+                      } catch(Exception eget){
+                           log.info(eget.toString());
+//                           log.info(p.toString());                    
+                             log.error(eget);
+                              if (log.isDebugEnabled()) eget.printStackTrace();
+
+                           log.info("failed to get hepML" + eget);
+                      }
+//                }
+//            } else {
+//                log.error("Query failed");
+//            }
+        }
+
+        DbUtils.endTransaction();
+        DbUtils.endSession();
+    }
+
+
+}


More information about the HepData-svn mailing list