Mercurial > hg > openmind
changeset 11:d71f28dac165
FixRelationNonCurrent now fixes relations with non-current sources.
author | casties |
---|---|
date | Thu, 09 Apr 2015 20:03:51 +0000 |
parents | dc458969f479 |
children | 3c28ecafafc1 |
files | pom.xml src/main/java/org/mpi/openmind/scripts/FixRelationNonCurrent.java |
diffstat | 2 files changed, 78 insertions(+), 28 deletions(-) [+] |
line wrap: on
line diff
--- a/pom.xml Thu Apr 09 17:31:55 2015 +0000 +++ b/pom.xml Thu Apr 09 20:03:51 2015 +0000 @@ -204,6 +204,7 @@ <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-source-plugin</artifactId> + <version>2.4</version> <executions> <execution> <id>attach-sources</id>
--- a/src/main/java/org/mpi/openmind/scripts/FixRelationNonCurrent.java Thu Apr 09 17:31:55 2015 +0000 +++ b/src/main/java/org/mpi/openmind/scripts/FixRelationNonCurrent.java Thu Apr 09 20:03:51 2015 +0000 @@ -1,6 +1,3 @@ -/** - * - */ package org.mpi.openmind.scripts; import java.sql.Connection; @@ -11,15 +8,29 @@ import java.util.List; /** + * Fixes relations with sources or targets that do not point to the current + * versions of the respective entities. + * * @author casties * */ public class FixRelationNonCurrent { - public static void show(String dbUser, String dbPw) { + public static int show(String dbUser, String dbPw) { Connection conn = DBUtils.getConn(dbUser, dbPw); - List<Long> relIds = getRelIds(conn); - for (Long id : relIds) { + List<Long> sourceRelIds = getRelIds("source", conn); + for (Long id : sourceRelIds) { + System.out.println("RELATION "+id+" source is not CURRENT_VERSION"); + int ncv = getCurrentVersionTime(id, conn).size(); + if (ncv == 1) { + System.out.println(" RELATION "+id+" can be fixed"); + } else { + System.out.println(" ERROR: RELATION " + id + " has " + ncv + " CURRENT_VERSIONs"); + } + } + System.out.println(); + List<Long> targetRelIds = getRelIds("target", conn); + for (Long id : targetRelIds) { System.out.println("RELATION "+id+" target is not CURRENT_VERSION"); int ncv = getCurrentVersionTime(id, conn).size(); if (ncv == 1) { @@ -28,31 +39,62 @@ System.out.println(" ERROR: RELATION "+id+" has " + ncv + " CURRENT_VERSIONs"); } } - System.out.println(relIds.size() + " RELATION targets are not CURRENT_VERSION"); + System.out.println(); + System.out.println(sourceRelIds.size() + " RELATION sources are not CURRENT_VERSION"); + System.out.println(targetRelIds.size() + " RELATION targets are not CURRENT_VERSION"); + try { + conn.close(); + } catch (SQLException e) { + } + if (sourceRelIds.size() > 0 || targetRelIds.size() > 0) { + return 1; + } + return 0; } - public static void repair(String dbUser, String dbPw) { + public static int repair(String dbUser, String dbPw) { Connection conn = DBUtils.getConn(dbUser, dbPw); - List<Long> relIds = getRelIds(conn); - int fixed = 0; - for (Long id : relIds) { - System.out.println("RELATION "+id+" target is not CURRENT_VERSION"); - if (updateCurrentRelationTarget(id, conn)) { + List<Long> relSourceIds = getRelIds("source", conn); + int fixedSs = 0; + for (Long id : relSourceIds) { + System.out.println("RELATION "+id+" source is not CURRENT_VERSION"); + if (updateCurrentRelation(id, "source", conn)) { System.out.println(" RELATION "+id+" target updated to current version"); - fixed += 1; + fixedSs += 1; } else { System.out.println(" ERROR: RELATION "+id+" was not fixed!"); } } - System.out.println(relIds.size() + " RELATION targets are not CURRENT_VERSION"); - System.out.println(fixed + " RELATION targets were fixed."); + List<Long> relTargetIds = getRelIds("target", conn); + int fixedTs = 0; + for (Long id : relTargetIds) { + System.out.println("RELATION "+id+" target is not CURRENT_VERSION"); + if (updateCurrentRelation(id, "target", conn)) { + System.out.println(" RELATION "+id+" target updated to current version"); + fixedTs += 1; + } else { + System.out.println(" ERROR: RELATION "+id+" was not fixed!"); + } + } + System.out.println(relSourceIds.size() + " RELATION sources are not CURRENT_VERSION"); + System.out.println(relTargetIds.size() + " RELATION targets are not CURRENT_VERSION"); + System.out.println(fixedSs + " RELATION sources were fixed."); + System.out.println(fixedTs + " RELATION targets were fixed."); + try { + conn.close(); + } catch (SQLException e) { + } + if (relSourceIds.size() > fixedSs || relTargetIds.size() > fixedTs) { + return 1; + } + return 0; } - public static List<Long> getRelIds(Connection conn) { + public static List<Long> getRelIds(String type, Connection conn) { List<Long> relIds = new ArrayList<Long>(); String qTargetRels = "select rel.id from openmind.node rel, openmind.node ent " - + "where rel.target_id = ent.id " - + "and rel.target_modif = ent.modification_time " + + "where rel." + type + "_id = ent.id " + + "and rel." + type + "_modif = ent.modification_time " + "and rel.node_type = 'RELATION' " + "and rel.system_status = 'CURRENT_VERSION' " + "and ent.system_status != 'CURRENT_VERSION' "; @@ -64,7 +106,7 @@ long id = rTargetRels.getLong(1); relIds.add(id); } - + sTargetRels.close(); } catch (SQLException e) { e.printStackTrace(); } @@ -83,15 +125,16 @@ long mtime = results.getLong(1); times.add(mtime); } + statement.close(); } catch (SQLException e) { e.printStackTrace(); } return times; } - public static boolean updateCurrentRelationTarget(Long id, Connection conn) { + public static boolean updateCurrentRelation(Long id, String type, Connection conn) { // get target_id of relation - String qTargetId = "select target_id from openmind.node " + String qTargetId = "select " + type + "_id from openmind.node " + "where id = " + id.toString() + " " + "and system_status = 'CURRENT_VERSION' "; try { @@ -104,16 +147,18 @@ if (target_mtimes.size() == 1) { // update target_mtime of relation String qUpdate = "update openmind.node " - + "set target_modif = " + target_mtimes.get(0).toString() + " " + + "set " + type + "_modif = " + target_mtimes.get(0).toString() + " " + "where id = " + id.toString() + " " + "and system_status = 'CURRENT_VERSION' "; Statement sUpdate = conn.createStatement(); int rUpdate = sUpdate.executeUpdate(qUpdate); + sUpdate.close(); return true; } else { - System.out.println("ERROR: relation target " + target_id + " has " + target_mtimes.size() + " CURRENT_VERSIONs"); + System.out.println("ERROR: relation " + type + target_id + " has " + target_mtimes.size() + " CURRENT_VERSIONs"); } } + sTargetId.close(); } catch (SQLException e) { e.printStackTrace(); } @@ -124,17 +169,21 @@ * @param args */ public static void main(String[] args) { - if (args.length == 3) { + int rc = 0; + if (args.length > 1 && args.length < 4) { String user = args[1]; - String pw = args[2]; + String pw = (args.length == 3) ? args[2] : null; if (args[0].equalsIgnoreCase("fix")) { - repair(user, pw); + rc = repair(user, pw); } else { - show(user, pw); + rc = show(user, pw); } } else { System.out.println("Parameter/s not found! Should be: mode(SHOW/FIX), mysql_user, mysql_password"); + System.out.println(" got: "+args.toString() + "("+args.length+")"); + System.exit(1); } + System.exit(rc); } }