[16869] | 1 | /*
|
---|
| 2 | * txt2usersDB.java
|
---|
| 3 | * Copyright (C) 2008 New Zealand Digital Library, http://www.nzdl.org
|
---|
| 4 | *
|
---|
| 5 | * This program is free software; you can redistribute it and/or modify
|
---|
| 6 | * it under the terms of the GNU General Public License as published by
|
---|
| 7 | * the Free Software Foundation; either version 2 of the License, or
|
---|
| 8 | * (at your option) any later version.
|
---|
| 9 | *
|
---|
| 10 | * This program is distributed in the hope that it will be useful,
|
---|
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 13 | * GNU General Public License for more details.
|
---|
| 14 | *
|
---|
| 15 | * You should have received a copy of the GNU General Public License
|
---|
| 16 | * along with this program; if not, write to the Free Software
|
---|
| 17 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
| 18 | */
|
---|
[14334] | 19 | package org.greenstone.gsdl3.util;
|
---|
| 20 |
|
---|
| 21 | import java.io.BufferedReader;
|
---|
| 22 | import java.io.IOException;
|
---|
| 23 | import java.io.FileReader;
|
---|
| 24 | import java.sql.SQLException;
|
---|
| 25 |
|
---|
| 26 | public class txt2usersDB {
|
---|
| 27 |
|
---|
| 28 | public static void main(String[] args) throws SQLException{
|
---|
| 29 |
|
---|
| 30 | if (args.length!=2){
|
---|
| 31 | System.out.println("Usage: java org.greenstone.gsdl3.txt2usersDB full_path_of_the_text_file full_path_of_the_usersDB");
|
---|
| 32 | System.exit(0);
|
---|
| 33 | }
|
---|
| 34 | try {
|
---|
| 35 | BufferedReader in = new BufferedReader(new FileReader(args[0]));
|
---|
| 36 | String str;
|
---|
| 37 | DerbyWrapper dw=new DerbyWrapper();
|
---|
| 38 | dw.connectDatabase(args[1],false);
|
---|
[14336] | 39 | boolean delete_rows = dw.deleteAllUser();
|
---|
[25308] | 40 | dw.closeDatabase();
|
---|
[14336] | 41 | if (!delete_rows){
|
---|
| 42 | System.out.println("Couldn't delete rows of the users table");
|
---|
| 43 | System.exit(0);
|
---|
| 44 | }
|
---|
[14334] | 45 | String username=null;
|
---|
| 46 | String password=null;
|
---|
| 47 | String groups=null;
|
---|
| 48 | String accountstatus=null;
|
---|
| 49 | String comment=null;
|
---|
[25308] | 50 | String email=null;
|
---|
[14334] | 51 | while ((str = in.readLine()) != null) {
|
---|
| 52 | //ystem.out.println(str);
|
---|
[25308] | 53 |
|
---|
| 54 | if(str.indexOf(" = ") != -1) { // works with DerbyWrapper.db2txt() and usersDB2txt.java. Fields listed as: USERNAME = admin
|
---|
| 55 | String field=str.substring(0,str.indexOf(" = "));
|
---|
| 56 | if (field.equalsIgnoreCase("email")){
|
---|
| 57 | email=str.substring(str.indexOf(" = ")+3,str.length());
|
---|
| 58 | }
|
---|
| 59 | if (field.equalsIgnoreCase("comment")){
|
---|
| 60 | comment=str.substring(str.indexOf(" = ")+3,str.length());
|
---|
| 61 | }
|
---|
| 62 | if (field.equalsIgnoreCase("status")){
|
---|
| 63 | accountstatus=str.substring(str.indexOf(" = ")+3,str.length());
|
---|
| 64 | }
|
---|
| 65 | if (field.equalsIgnoreCase("groups")){
|
---|
| 66 | groups=str.substring(str.indexOf(" = ")+3,str.length());
|
---|
| 67 | }
|
---|
| 68 | if (field.equalsIgnoreCase("password")){
|
---|
| 69 | //password=dw.rot13(str.substring(str.indexOf(">")+1,str.length()));
|
---|
| 70 | password=str.substring(str.indexOf(" = ")+3,str.length());
|
---|
| 71 | }
|
---|
| 72 | if (field.equalsIgnoreCase("username")){
|
---|
| 73 | username=str.substring(str.indexOf(" = ")+3,str.length());
|
---|
| 74 | }
|
---|
| 75 | }
|
---|
| 76 | else if (str.startsWith("<")){ // fields listed as: <username>admin
|
---|
[14334] | 77 | String field=str.substring(1,str.indexOf(">"));
|
---|
[25308] | 78 | if (field.equals("email")){
|
---|
| 79 | email=str.substring(str.indexOf(">")+1,str.length());
|
---|
| 80 | }
|
---|
[14334] | 81 | if (field.equals("comment")){
|
---|
| 82 | comment=str.substring(str.indexOf(">")+1,str.length());
|
---|
| 83 | }
|
---|
[25308] | 84 | if (field.equals("enabled") || field.equals("status")){
|
---|
[14334] | 85 | accountstatus=str.substring(str.indexOf(">")+1,str.length());
|
---|
| 86 | }
|
---|
| 87 | if (field.equals("groups")){
|
---|
| 88 | groups=str.substring(str.indexOf(">")+1,str.length());
|
---|
| 89 | }
|
---|
| 90 | if (field.equals("password")){
|
---|
[25308] | 91 | //password=dw.rot13(str.substring(str.indexOf(">")+1,str.length()));
|
---|
| 92 | password=str.substring(str.indexOf(">")+1,str.length());
|
---|
[14334] | 93 | }
|
---|
| 94 | if (field.equals("username")){
|
---|
| 95 | username=str.substring(str.indexOf(">")+1,str.length());
|
---|
| 96 | }
|
---|
| 97 | }
|
---|
[25308] | 98 | else if (str.equals("----------------------------------------------------------------------")
|
---|
| 99 | || str.equals("-------------------------------------")) {
|
---|
| 100 |
|
---|
| 101 | if ((username!=null) && (password!=null) && (groups!=null) && (accountstatus!=null) && (comment!=null) && (email!=null)) {
|
---|
[14336] | 102 | dw.connectDatabase(args[1],false);
|
---|
[25308] | 103 | dw.addUser(username, password, groups, accountstatus, comment, email);
|
---|
[14334] | 104 | username=null;
|
---|
| 105 | password=null;
|
---|
| 106 | groups=null;
|
---|
| 107 | accountstatus=null;
|
---|
| 108 | comment=null;
|
---|
[25308] | 109 | email=null;
|
---|
| 110 | //dw.connectDatabase(args[1],false); // should this be closeDatabase()????
|
---|
| 111 | dw.closeDatabase();
|
---|
[14334] | 112 | }
|
---|
| 113 | }
|
---|
[25308] | 114 | else { // encrypted passwords can span multiple lines for some reason
|
---|
| 115 | // assume that is the case here
|
---|
| 116 | if(password != null) {
|
---|
| 117 | password = password + "\n" + str;
|
---|
| 118 | }
|
---|
| 119 | }
|
---|
[14334] | 120 | }
|
---|
| 121 | in.close();
|
---|
| 122 | } catch (IOException e) {
|
---|
| 123 | }
|
---|
| 124 | }
|
---|
| 125 | }
|
---|