Skip to content

Commit

Permalink
Version 1.1 which adds the ability to clean up orphaned archival obje…
Browse files Browse the repository at this point in the history
…ct records
  • Loading branch information
Nathan Stevens committed Apr 22, 2015
1 parent 5e0838c commit 6937a6c
Show file tree
Hide file tree
Showing 4 changed files with 152 additions and 4 deletions.
5 changes: 3 additions & 2 deletions src/aspacedbutil/ASpaceDBUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public Connection getConnection() throws SQLException {
* @throws java.sql.SQLException
*/
public static void main(String[] args) throws SQLException {
System.out.println("ASpaceDBUtil v1.0 (10-15-2014)");
System.out.println("ASpaceDBUtil v1.1 (04-21-2015)");

String dbmsURL = "";
String username = "";
Expand Down Expand Up @@ -81,7 +81,8 @@ public static void main(String[] args) throws SQLException {
System.out.println("\n");

DeDupLocations.runTask(connection);
CleanClassifcations.runTask(connection);
CleanClassifications.runTask(connection);
CleanOrphanedArchivalObjects.runTask(connection);
} catch (SQLException ex) {
System.out.println("A database error occured\n" + ex.getMessage());
} finally {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
*
* @author nathan
*/
public class CleanClassifcations {
public class CleanClassifications {
/**
* Method to called to do the actual cleaning up of classifications records
* @param conn
Expand Down
147 changes: 147 additions & 0 deletions src/aspacedbutil/CleanOrphanedArchivalObjects.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
/*
* A util class to remove classfications records not linked to any term
* or resources
*/
package aspacedbutil;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;

/**
*
* @author nathan
*/
public class CleanOrphanedArchivalObjects {

/**
* Method to called to do the actual cleaning up of classifications records
*
* @param conn
* @throws SQLException
*/
public static void runTask(Connection conn) throws SQLException {
System.out.println("Deleting Orphaned Archival Objects ...\n");

// create a hashmap to store the archival objects for making finding the duplicate archival object
// much faster
HashMap<String, String[]> archivalObjectsMap = new HashMap<>();

// an array list which stores the archival objects id orphaned archival objects
ArrayList<String> orphanedList = new ArrayList<>();

// find any archival objects which do not have parent which is attached to
// to the root resource record
String findArchivalObjectsSQL = "SELECT id, title, root_record_id, parent_id FROM archival_object";

Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(findArchivalObjectsSQL);

while (rs.next()) {
String[] info = new String[3];

info[0] = rs.getString("id");
info[1] = rs.getString("title");
info[2] = rs.getString("parent_id");

// store this in the hash map
archivalObjectsMap.put(info[0], info);
}

stmt.closeOnCompletion();

// now find the duplicates using the hashmap
int total = 0;
for (String id : archivalObjectsMap.keySet()) {
String[] info = archivalObjectsMap.get(id);

if (info[2] != null && !info[2].equals("0")) {
System.out.println("Child Archival Object :: " + id + "\t" + info[1] + "\t" + info[2]);

if (hasTopParentId(archivalObjectsMap, info[2], 0)) {
System.out.println("Has valid top Archival Object");
} else {
total++;
orphanedList.add(id);
System.out.println("Orphaned Archival Object");
}
}
}

System.out.println("Total Number of Orphaned Archival Object: " + total);

// call method to actual delete the records
if(!orphanedList.isEmpty()) {
deleteOrphaneRecords(conn, orphanedList);
}
}

/**
* Method to check to see if this archival object is not circular in nature
*
* @param conn
* @param parentId
* @param level
* @return
* @throws SQLException
*/
private static boolean hasTopParentId(HashMap<String, String[]> archivalObjectsMap, String parentId, int level) throws SQLException {
// need to check level is not more than 12 which will indicate we in loop
level++;

if (level >= 10) {
System.out.println("Circular parent child relationship for " + parentId);
return false;
}

if (archivalObjectsMap.containsKey(parentId)) {
String[] info = archivalObjectsMap.get(parentId);
String newParentId = info[2];

if (newParentId == null || newParentId.equals("0")) {
return true;
} else {
return hasTopParentId(archivalObjectsMap, newParentId, level);
}
} else {
return false;
}
}

/**
* This is where the orphaned archival objects are actual deleted
*
* @param conn
* @param orphanedList
*/
private static void deleteOrphaneRecords(Connection conn, ArrayList<String> orphanedList) throws SQLException {
String ids = "";
for (String id : orphanedList) {
ids += id + ",";
}

// need to remove the last , from the string
ids = "(" + ids.substring(0, ids.length() - 1) + ")";

Statement stmt = conn.createStatement();
String deleteSQL = "DELETE FROM archival_object WHERE id IN " + ids;
System.out.println(deleteSQL);

int rows = -1;

// disable foreign key checks so we can delete records without worrying
// about chld parent links
stmt.executeUpdate("SET FOREIGN_KEY_CHECKS=0");

if (!ASpaceDBUtil.TEST) {
rows = stmt.executeUpdate(deleteSQL);
}

stmt.executeUpdate("SET FOREIGN_KEY_CHECKS=1");

System.out.println("Number of archival objects deleted: " + rows);
}
}
2 changes: 1 addition & 1 deletion src/aspacedbutil/DeDupLocations.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ private static void updateContainersAndDeleteLocations(Statement stmt, HashMap<S

System.out.println("Number of containers updated: " + rows);

// now we must delete the deplicate rows from the locations table
// now we must delete the duplicate rows from the locations table
String idsToDelete = ids.replace("(" + firstId + ",", "(");
String deleteSQL = "DELETE FROM location WHERE id IN " + idsToDelete;
System.out.println(deleteSQL);
Expand Down

0 comments on commit 6937a6c

Please sign in to comment.