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

blackhole at projects.hepforge.org blackhole at projects.hepforge.org
Tue Nov 19 12:04:51 GMT 2013


Author: whalley
Date: Tue Nov 19 12:04:51 2013
New Revision: 1678

Log:
missing code

Added:
   trunk/hepdata-migration/src/main/java/cedar/hepdata/migration/DeleteFromDbDurhamId.java

Added: trunk/hepdata-migration/src/main/java/cedar/hepdata/migration/DeleteFromDbDurhamId.java
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ trunk/hepdata-migration/src/main/java/cedar/hepdata/migration/DeleteFromDbDurhamId.java	Tue Nov 19 12:04:51 2013	(r1678)
@@ -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 DurhamId [DurhamId ...]
+//
+// 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 DeleteFromDbDurhamId {
+    static Logger log = Logger.getLogger(DeleteFromDbDurhamId.class);
+
+
+    static public void main(String[] args) {
+        log.info("Deleting Papers (via durhamIds) from database");
+
+
+        // Get hepdataIds
+        String[] redIds = args;
+        if (redIds.length == 0) {
+            System.exit(1);
+        }
+
+        DbUtils.getSession();
+	DbUtils.beginTransaction();
+
+        for (String redId : redIds) {
+            Query q = DbUtils.getSession().createQuery("select distinct p from Paper p where p._redId=:redId");
+            q.setString("redId",redId);
+	    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();
+    }
+
+
+}


More information about the HepData-svn mailing list