view src/main/java/org/mpi/openmind/scripts/NormalizeOW.java @ 8:478fd6f26ea8

(none)
author jurzua
date Tue, 24 Feb 2015 10:45:45 +0000
parents 615d27dce9b3
children c009ce2e60be
line wrap: on
line source

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 java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.mpi.openmind.repository.utils.ArabicNormalizerUtils;
import org.mpi.openmind.repository.utils.NormalizerUtils;

public class NormalizeOW {
	public static void execute(String type) {
		try {
			System.out.println("Normalizing own values for all: " + type +"S.");
			System.out.println("INFO: only for the CURRENT_VERSION of the mentioned nodes will be affected.");
			Connection conn;

			Class.forName("com.mysql.jdbc.Driver").newInstance();
			String url = "jdbc:mysql://localhost/openmind?characterEncoding=UTF-8";
			conn = DriverManager.getConnection(url, "root", "admin");
			
			Map<Long, String> selectedMap = select(conn, type);
			
			System.out.println("Amount of nodes=" + selectedMap.size());
			change(conn, selectedMap);
			System.out.println("End");
			
			conn.close();
		} 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());
		}

	}
	
	public static void change(Connection conn, Map<Long, String> map){
		String s = new String();
		for(Long id : map.keySet()){
			try {
		    	Statement st = conn.createStatement();
		    	String normalizedOW = NormalizerUtils.normalize(map.get(id));
		    	String normalizedArabicOW = ArabicNormalizerUtils.normalize(map.get(id));
				st.executeUpdate("UPDATE node SET normalized_own_value='" + normalizedOW + "' WHERE row_id='"+ id +"'");
				s = "UPDATE node SET normalized_arabic_own_value='" + normalizedArabicOW + "' WHERE row_id='"+ id +"'";
				//System.out.println(s);
				st.executeUpdate(s);
			} catch (SQLException e) {
				System.err.println(s);
				e.printStackTrace();
			}
		}
	}
	
	@SuppressWarnings("finally")
	public static Map<Long, String> select(Connection conn, String type){
		Map<Long, String> map = new HashMap<Long, String>();
		String query = "select row_id, own_value " +
				"from node " +
				"where system_status = 'CURRENT_VERSION'";
				
		if(type.equals("ATTRIBUTE") || type.equals("ENTITY")){
			query += " AND node_type = '"+ type +"'";
		}
				
		try
	    {
	      Statement st = conn.createStatement();
	      ResultSet rs = st.executeQuery(query);
	      while (rs.next())
	      {
	        String id = rs.getString("row_id");
	        String ow = rs.getString("own_value");
	        map.put(new Long(id), ow);
	      }
	    }
	    catch (SQLException ex){
	    	ex.printStackTrace();
	      //System.err.println(ex.getMessage());
	    }finally{
	    	return map;
	    }
	}

	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);
			}
		}*/
	}

}