package com.uas.account.init;

import java.net.UnknownHostException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import javax.sql.DataSource;

import com.alibaba.druid.pool.DruidDataSource;
import com.mongodb.DB;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.MongoClient;

public class GetIMPassword {

	private static DataSource getDataSource() {
		DruidDataSource ds = new DruidDataSource();
		ds.setDriverClassName("oracle.jdbc.driver.OracleDriver");
		ds.setUsername("platformmanager");
		ds.setPassword("select!#%*(");
		ds.setUrl("jdbc:oracle:thin:@//10.10.100.215:1521/orcl");
		return ds;
	}

	public static void main(String[] args) throws UnknownHostException {
		MongoClient client = new MongoClient("10.10.101.22");
		DB db = client.getDB("tigase");
		DBCursor cursor = db.getCollection("tig_users").find();
		Connection connection = null;
		PreparedStatement statement = null;
		DataSource dataSource = getDataSource();
		try {
			connection = dataSource.getConnection();
			statement = connection.prepareStatement("update ac$users set password=? where dialectUID=? and appId='im'");
			while (cursor.hasNext()) {
				DBObject object = cursor.next();
				if (object.get("password") != null) {
					String userId = object.get("user_id").toString();
					if (userId.contains("@")) {
						userId = userId.substring(0, userId.indexOf("@"));
					}
					String password = object.get("password").toString();
					statement.setString(1, password);
					statement.setString(2, userId);
					statement.addBatch();
				}
			}
			statement.executeBatch();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			cursor.close();
			client.close();
			try {
				statement.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
			try {
				connection.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

}
