source: main/trunk/greenstone3/src/java/org/greenstone/gsdl3/util/txt2usersDB.java@ 25308

Last change on this file since 25308 was 25308, checked in by ak19, 12 years ago
  1. Updated txt2usersDB.java to compile again and to also work with usersDB2txt.java such that the output of running one becomes the exact input required by the other and vice-versa. So running usersDB2txt and then txt2usersDB on the txt file generated by the former, will generate a DB file that can be fed back into usersDB2txt which will once more produce an identical text output. 2. Created the ChangePwdUsersDB.java file which build.xml's new ant config-admin target calls to reset the admin password.
File size: 4.4 KB
Line 
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 */
19package org.greenstone.gsdl3.util;
20
21import java.io.BufferedReader;
22import java.io.IOException;
23import java.io.FileReader;
24import java.sql.SQLException;
25
26public 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);
39 boolean delete_rows = dw.deleteAllUser();
40 dw.closeDatabase();
41 if (!delete_rows){
42 System.out.println("Couldn't delete rows of the users table");
43 System.exit(0);
44 }
45 String username=null;
46 String password=null;
47 String groups=null;
48 String accountstatus=null;
49 String comment=null;
50 String email=null;
51 while ((str = in.readLine()) != null) {
52 //ystem.out.println(str);
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
77 String field=str.substring(1,str.indexOf(">"));
78 if (field.equals("email")){
79 email=str.substring(str.indexOf(">")+1,str.length());
80 }
81 if (field.equals("comment")){
82 comment=str.substring(str.indexOf(">")+1,str.length());
83 }
84 if (field.equals("enabled") || field.equals("status")){
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")){
91 //password=dw.rot13(str.substring(str.indexOf(">")+1,str.length()));
92 password=str.substring(str.indexOf(">")+1,str.length());
93 }
94 if (field.equals("username")){
95 username=str.substring(str.indexOf(">")+1,str.length());
96 }
97 }
98 else if (str.equals("----------------------------------------------------------------------")
99 || str.equals("-------------------------------------")) {
100
101 if ((username!=null) && (password!=null) && (groups!=null) && (accountstatus!=null) && (comment!=null) && (email!=null)) {
102 dw.connectDatabase(args[1],false);
103 dw.addUser(username, password, groups, accountstatus, comment, email);
104 username=null;
105 password=null;
106 groups=null;
107 accountstatus=null;
108 comment=null;
109 email=null;
110 //dw.connectDatabase(args[1],false); // should this be closeDatabase()????
111 dw.closeDatabase();
112 }
113 }
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 }
120 }
121 in.close();
122 } catch (IOException e) {
123 }
124 }
125}
Note: See TracBrowser for help on using the repository browser.