001: /*
002: * Lucane - a collaborative platform
003: * Copyright (C) 2004 Vincent Fiack <vfiack@mail15.com>
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: */
019: package org.lucane.server.tools;
020:
021: import java.sql.Connection;
022: import java.sql.PreparedStatement;
023: import java.sql.ResultSet;
024: import java.util.ArrayList;
025: import java.util.Iterator;
026: import java.util.logging.Level;
027:
028: import org.lucane.common.Logging;
029: import org.lucane.server.Server;
030: import org.lucane.server.ServerConfig;
031: import org.lucane.server.database.DatabaseAbstractionLayer;
032:
033: public class UpdateUsers {
034: public static void main(String[] args) throws Exception {
035: Logging.getLogger().setLevel(Level.OFF);
036: ServerConfig config = new ServerConfig("etc/server-config.xml");
037: if (!config.getStoreBackend().equals("database")) {
038: System.err.println("not using database backend, sorry.");
039: System.exit(1);
040: }
041:
042: if (args.length != 0) {
043: System.out.println("usage: UpdateUsers");
044: System.exit(1);
045: }
046:
047: DatabaseAbstractionLayer dbLayer = DatabaseAbstractionLayer
048: .createLayer(config);
049: Connection c = dbLayer.getConnection();
050: PreparedStatement select = c
051: .prepareStatement("SELECT * FROM users");
052: ResultSet rs = select.executeQuery();
053: if (rs.getMetaData().getColumnName(5)
054: .equalsIgnoreCase("pubkey"))
055: migrateUsers(dbLayer, c, rs);
056: else
057: System.err.println("User store is older than 0.7.2 !");
058:
059: rs.close();
060: select.close();
061: c.close();
062: }
063:
064: private static void migrateUsers(DatabaseAbstractionLayer dbLayer,
065: Connection c, ResultSet rs) throws Exception {
066: //fetch old values
067: System.out.print("fetching current users... ");
068: ArrayList users = new ArrayList();
069: while (rs.next()) {
070: String login = rs.getString(1);
071: String md5pass = rs.getString(2);
072: String locked = rs.getString(3);
073: String startupPlugin = rs.getString(4);
074: String description = rs.getString(7);
075: String[] user = { login, md5pass, locked, startupPlugin,
076: description };
077: users.add(user);
078: }
079: System.out.println("(" + users.size() + " users).");
080:
081: //drop table
082: System.out.print("dropping old table... ");
083: PreparedStatement drop = c.prepareStatement("DROP TABLE users");
084: drop.execute();
085: drop.close();
086: System.out.println("ok");
087:
088: //create new table
089: System.out.print("creating new table... ");
090: String wd = System.getProperty("user.dir").replace('\\', '/');
091: String url = "jar:file:///" + wd + "/lib/lucane-server-"
092: + Server.VERSION + ".jar!/" + "db-sqlstore.xml";
093: dbLayer.getTableCreator().createFromXml(url, "users");
094: System.out.println("ok");
095:
096: //copy saved users
097: System.out.print("restoring users... ");
098: PreparedStatement insert = c
099: .prepareStatement("INSERT INTO USERS"
100: + " VALUES(?, ?, ?, ?, ?, ?, ?, ?);");
101: Iterator i = users.iterator();
102: while (i.hasNext()) {
103: String[] user = (String[]) i.next();
104: insert.setString(1, user[0]);
105: insert.setString(2, user[1]);
106: insert.setString(3, user[0]);
107: insert.setString(4, "");
108: insert.setString(5, "en");
109: insert.setInt(6, Integer.parseInt(user[2]));
110: insert.setString(7, user[3]);
111: insert.setString(8, user[4]);
112: insert.execute();
113: }
114: insert.close();
115: System.out.println("ok !");
116: }
117: }
|