Mercurial > hg > openmind
changeset 8:478fd6f26ea8
(none)
author | jurzua |
---|---|
date | Tue, 24 Feb 2015 10:45:45 +0000 |
parents | ac2fd7a4378d |
children | 2db1752315bd |
files | src/main/java/org/mpi/openmind/scripts/DBUtils.java src/main/java/org/mpi/openmind/scripts/NormalizeOW.java src/main/java/org/mpi/openmind/scripts/RenameAttributes.java |
diffstat | 3 files changed, 140 insertions(+), 2 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/org/mpi/openmind/scripts/DBUtils.java Tue Feb 24 10:45:45 2015 +0000 @@ -0,0 +1,69 @@ +package org.mpi.openmind.scripts; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; + +import javax.naming.spi.DirStateFactory.Result; + + +public class DBUtils { + + private static String DB_URL = "jdbc:mysql://localhost/openmind?characterEncoding=UTF-8"; + + public static Connection getConn(String mysqlUser, String mysqlPwd){ + Connection conn = null; + try { + + Class.forName("com.mysql.jdbc.Driver").newInstance(); + conn = DriverManager.getConnection(DB_URL, mysqlUser, mysqlPwd); + + return conn; + } catch (ClassNotFoundException ex) { + System.err.println(ex.getMessage()); + } catch (IllegalAccessException ex) { + System.err.println(ex.getMessage()); + } catch (InstantiationException ex) { + System.err.println(ex.getMessage()); + } catch (SQLException ex) { + System.err.println(ex.getMessage()); + } + return conn; + } + + public static ResultSet getDefinition(Connection conn, String oc) throws SQLException{ + String query = "select * from node where node_type = 'ENTITY' and object_class = 'TBox' and own_value = '" + oc + "'"; + Statement stmt = conn.createStatement(); + return stmt.executeQuery(query); + } + + public static Long getDefId(Connection conn, String oc) throws SQLException{ + ResultSet rs = getDefinition(conn, oc); + if(rs != null && rs.next()) + return rs.getLong("id"); + return null; + } + + public static ResultSet getDefAtts(Connection conn, String oc, String attName) throws SQLException{ + Long defId = getDefId(conn, oc); + String query = + "select * from node where " + + "node_type = 'ATTRIBUTE' and " + + "object_class = 'TBox' and " + + "source_id = '" + defId + "' and " + + "own_value = '" + attName + "'"; + + Statement stmt = conn.createStatement(); + return stmt.executeQuery(query); + } + + public static void getAtt(String oc, String attName){ + + String query = + "select * from node where node_type = 'ATTRIBUTE' and source_obj_class = 'WITNESS' and object_class = 'notes'"; + + } + +}
--- a/src/main/java/org/mpi/openmind/scripts/NormalizeOW.java Thu Oct 30 12:50:26 2014 +0000 +++ b/src/main/java/org/mpi/openmind/scripts/NormalizeOW.java Tue Feb 24 10:45:45 2015 +0000 @@ -23,7 +23,7 @@ Class.forName("com.mysql.jdbc.Driver").newInstance(); String url = "jdbc:mysql://localhost/openmind?characterEncoding=UTF-8"; - conn = DriverManager.getConnection(url, "ismi", "ismipw"); + conn = DriverManager.getConnection(url, "root", "admin"); Map<Long, String> selectedMap = select(conn, type); @@ -93,13 +93,15 @@ } public static void main(String[] args) { + NormalizeOW.execute("all"); + /* String arg = args[0]; if(StringUtils.isNotEmpty(arg)){ if(arg.equals("ATTRIBUTE") || arg.equals("ENTITY") || arg.equals("all")){ NormalizeOW.execute(arg); System.exit(0); } - } + }*/ } }
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/org/mpi/openmind/scripts/RenameAttributes.java Tue Feb 24 10:45:45 2015 +0000 @@ -0,0 +1,67 @@ +package org.mpi.openmind.scripts; + +import java.sql.Connection; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; + +public class RenameAttributes { + + public static void execute(String oc, String oldAttName, String newAttName, String mysqlUser, String mysqlPwd){ + System.out.println("RenameAttributes " + oc + " [from: " + oldAttName + " to " + newAttName + "]"); + + try { + Connection conn = DBUtils.getConn(mysqlUser, mysqlPwd); + renameAtts(conn, oc, oldAttName, newAttName); + renameDefAtt(conn, oc, oldAttName, newAttName); + conn.close(); + } catch (SQLException e) { + e.printStackTrace(); + } + + } + + public static void renameAtts(Connection conn, String oc, String oldAttName, String newAttName){ + System.out.println("Rename all Atts"); + try { + Statement st = conn.createStatement(); + String st0 = "UPDATE node " + + "SET object_class='" + newAttName + "' " + + "WHERE node_type = 'ATTRIBUTE' and source_obj_class = '" + oc + "' and object_class = '" + oldAttName + "'"; + System.out.println(st0); + int counter = st.executeUpdate(st0); + System.out.println(counter + " rows modfied!"); + } catch (SQLException e) { + e.printStackTrace(); + } + } + + public static void renameDefAtt(Connection conn, String oc, String oldAttName, String newAttName) throws SQLException{ + System.out.println("Rename Definition Atts"); + ResultSet rs = DBUtils.getDefAtts(conn, oc, oldAttName); + + while(rs.next()){ + Long attRowId = rs.getLong("row_id"); + renameDefAtt(conn, attRowId, newAttName); + } + } + + public static void renameDefAtt(Connection conn, Long attRowId, String newAttName) throws SQLException{ + Statement st = conn.createStatement(); + String st0 = "UPDATE node " + + "SET own_value='" + newAttName + "' " + + "WHERE row_id = '" + attRowId + "'"; + System.out.println(st0); + int counter = st.executeUpdate(st0); + System.out.println(counter + " rows modfied!"); + } + + + public static void main(String[] args){ + execute("WITNESS", "notes", "notes_old", "root", "admin"); + execute("TEXT", "notes", "notes_old", "root", "admin"); + execute("CODEX", "notes", "notes_old", "root", "admin"); + execute("PERSON", "notes", "notes_old", "root", "admin"); + } + +}