1 | package org.greenstone.gsdl3.util;
|
---|
2 |
|
---|
3 | import javax.swing.*;
|
---|
4 | import java.io.File;
|
---|
5 | import java.sql.Connection;
|
---|
6 | import java.sql.DriverManager;
|
---|
7 | import java.sql.ResultSet;
|
---|
8 | import java.sql.SQLException;
|
---|
9 | import java.sql.SQLWarning;
|
---|
10 | import java.sql.Statement;
|
---|
11 | import java.util.Properties;
|
---|
12 |
|
---|
13 | public class DerbyWrapper
|
---|
14 | {
|
---|
15 | static final String PROTOCOL = "jdbc:derby:";
|
---|
16 | static final String DRIVER = "org.apache.derby.jdbc.EmbeddedDriver";
|
---|
17 | static final String USERSDB = "usersDB";
|
---|
18 | static final String USERS = "users";
|
---|
19 | private Connection conn=null;
|
---|
20 | private Statement state=null;
|
---|
21 | private String protocol_str;
|
---|
22 |
|
---|
23 | public DerbyWrapper(){}
|
---|
24 | public DerbyWrapper(String dbpath){
|
---|
25 | connectDatabase(dbpath,false);
|
---|
26 | }
|
---|
27 |
|
---|
28 | public void connectDatabase(String dbpath, boolean create_database){
|
---|
29 | try{
|
---|
30 | Class.forName(DRIVER).newInstance();
|
---|
31 | //System.out.println("Loaded the embedded driver.");
|
---|
32 | protocol_str = PROTOCOL + dbpath;
|
---|
33 | if (create_database){
|
---|
34 | conn = DriverManager.getConnection(protocol_str + ";create=true");
|
---|
35 | }else{
|
---|
36 | conn = DriverManager.getConnection(protocol_str);
|
---|
37 | }
|
---|
38 | state = conn.createStatement();
|
---|
39 | }catch (Throwable e){
|
---|
40 | System.out.println("exception thrown:");
|
---|
41 | if (e instanceof SQLException){
|
---|
42 | printSQLError((SQLException) e);
|
---|
43 | }else{
|
---|
44 | e.printStackTrace();
|
---|
45 | }
|
---|
46 | }
|
---|
47 | }
|
---|
48 |
|
---|
49 | public void closeDatabase() {
|
---|
50 | state = null;
|
---|
51 | conn = null;
|
---|
52 | boolean gotSQLExc = false;
|
---|
53 | try{
|
---|
54 | //DriverManager.getConnection("jdbc:derby:;shutdown=true");
|
---|
55 | DriverManager.getConnection(protocol_str + ";shutdown=true");
|
---|
56 | }catch (SQLException se){
|
---|
57 | gotSQLExc = true;
|
---|
58 | }
|
---|
59 | if (!gotSQLExc){
|
---|
60 | System.out.println("Database did not shut down normally");
|
---|
61 | }
|
---|
62 | }
|
---|
63 |
|
---|
64 | public void createDatabase()throws SQLException{
|
---|
65 | conn.setAutoCommit(false);
|
---|
66 | state.execute("create table users (username varchar(40) not null, password varchar(40) not null, groups varchar(500), accountstatus varchar(10), comment varchar(100), primary key(username))");
|
---|
67 | //ystem.out.println("table users created successfully!");
|
---|
68 | state.execute("insert into "+USERS+" values ('admin', 'admin', 'administrator,all-collections-editor', 'true', 'change the password for this account as soon as possible')");
|
---|
69 | conn.commit();
|
---|
70 | closeDatabase();
|
---|
71 | }
|
---|
72 |
|
---|
73 | public UserQueryResult listAllUser()throws SQLException{
|
---|
74 | UserQueryResult userQueryResult=new UserQueryResult();
|
---|
75 | String sql_list_all_user="select username, password, groups, accountstatus, comment from " + USERS ;
|
---|
76 |
|
---|
77 | ResultSet rs = state.executeQuery(sql_list_all_user);
|
---|
78 | while (rs.next()){
|
---|
79 | String returned_username= rs.getString("username");
|
---|
80 | String returned_password=rs.getString("password");
|
---|
81 | String returned_groups=rs.getString("groups");
|
---|
82 | String returned_accountstatus=rs.getString("accountstatus");
|
---|
83 | String returned_comment=rs.getString("comment");
|
---|
84 | userQueryResult.addUserTerm(returned_username,returned_password,returned_groups,returned_accountstatus,returned_comment);
|
---|
85 | }
|
---|
86 | if (userQueryResult.getSize()==0){
|
---|
87 | System.out.println("couldn't find any users");
|
---|
88 | return null;
|
---|
89 | }else{
|
---|
90 | return userQueryResult;
|
---|
91 | }
|
---|
92 | }
|
---|
93 |
|
---|
94 | public String addUser(String username, String password, String groups, String accountstatus, String comment)throws SQLException{
|
---|
95 | conn.setAutoCommit(false);
|
---|
96 | String sql_insert_user="insert into " + USERS + " values ('"+username +"', '"+password+"', '"+groups+"', '"+accountstatus+"', '"+comment+"')";
|
---|
97 | try{
|
---|
98 | state.execute(sql_insert_user);
|
---|
99 | conn.commit();
|
---|
100 | }catch(Throwable e){
|
---|
101 | System.out.println("exception thrown:");
|
---|
102 | if (e instanceof SQLException){
|
---|
103 | printSQLError((SQLException) e);
|
---|
104 | }else{
|
---|
105 | e.printStackTrace();
|
---|
106 | }
|
---|
107 | closeDatabase();
|
---|
108 | System.out.println("Error:"+e.getMessage());
|
---|
109 | return "Error:"+e.getMessage();
|
---|
110 | }
|
---|
111 | return "succeed";
|
---|
112 | }
|
---|
113 |
|
---|
114 | public String deleteUser(String del_username)throws SQLException{
|
---|
115 | conn.setAutoCommit(false);
|
---|
116 | String sql_delete_user="delete from " + USERS + " where username='"+del_username+"'";
|
---|
117 | try{
|
---|
118 | state.execute(sql_delete_user);
|
---|
119 | conn.commit();
|
---|
120 | }catch(Throwable e){
|
---|
121 | System.out.println("exception thrown:");
|
---|
122 | if (e instanceof SQLException){
|
---|
123 | printSQLError((SQLException) e);
|
---|
124 | }else{
|
---|
125 | e.printStackTrace();
|
---|
126 | }
|
---|
127 | closeDatabase();
|
---|
128 | return "Error:"+e.getMessage();
|
---|
129 | }
|
---|
130 | return "succeed";
|
---|
131 | }
|
---|
132 |
|
---|
133 | public boolean deleteAllUser() throws SQLException{
|
---|
134 | conn.setAutoCommit(false);
|
---|
135 | try{
|
---|
136 | state.execute("delete from " + USERS);
|
---|
137 | conn.commit();
|
---|
138 | }catch(Throwable e){
|
---|
139 | System.out.println("exception thrown:");
|
---|
140 | if (e instanceof SQLException){
|
---|
141 | printSQLError((SQLException) e);
|
---|
142 | }else{
|
---|
143 | e.printStackTrace();
|
---|
144 | }
|
---|
145 | closeDatabase();
|
---|
146 | return false;
|
---|
147 | }
|
---|
148 | closeDatabase();
|
---|
149 | return true;
|
---|
150 | }
|
---|
151 |
|
---|
152 | public UserQueryResult findUser(String username, String password)throws SQLException{
|
---|
153 | UserQueryResult userQueryResult=new UserQueryResult();
|
---|
154 |
|
---|
155 | conn.setAutoCommit(false);
|
---|
156 | String sql_find_user="SELECT username, password, groups, accountstatus, comment FROM "+USERS;
|
---|
157 | String append_sql="";
|
---|
158 |
|
---|
159 | if (username!=null){
|
---|
160 | append_sql=" WHERE username = '" + username+ "'";
|
---|
161 | }
|
---|
162 | if (password!=null){
|
---|
163 | if (append_sql.equals("")){
|
---|
164 | append_sql=" WHERE password = '"+password+"'";
|
---|
165 | }else{
|
---|
166 | append_sql+=" and password = '"+password+"'";
|
---|
167 | }
|
---|
168 | }
|
---|
169 | if (!append_sql.equals("")){
|
---|
170 | sql_find_user+=append_sql;
|
---|
171 | }
|
---|
172 | ResultSet rs = state.executeQuery(sql_find_user);
|
---|
173 | while (rs.next()){
|
---|
174 | String returned_username= rs.getString("username");
|
---|
175 | System.out.println("returned_username :" + returned_username);
|
---|
176 | String returned_password=rs.getString("password");
|
---|
177 | System.out.println("returned_password :" + returned_password);
|
---|
178 | String returned_groups=rs.getString("groups");
|
---|
179 | System.out.println("returned_groups :" + returned_groups);
|
---|
180 | String returned_accountstatus=rs.getString("accountstatus");
|
---|
181 | System.out.println("returned_accountstatus :" + returned_accountstatus);
|
---|
182 | String returned_comment=rs.getString("comment");
|
---|
183 | System.out.println("returned_comment :" + returned_comment);
|
---|
184 | userQueryResult.addUserTerm(returned_username,returned_password,returned_groups,returned_accountstatus,returned_comment);
|
---|
185 | System.out.println(userQueryResult.toString());
|
---|
186 | }
|
---|
187 | conn.commit();
|
---|
188 | if (userQueryResult.getSize()==0){
|
---|
189 | System.out.println("couldn't find the user");
|
---|
190 | return null;
|
---|
191 | }else{
|
---|
192 | return userQueryResult;
|
---|
193 | }
|
---|
194 | }
|
---|
195 |
|
---|
196 | public String modifyUserInfo(String username, String new_password,String groups, String accountstatus, String comment)throws SQLException{
|
---|
197 | conn.setAutoCommit(false);
|
---|
198 | String sql_modify_user_info="update " + USERS +" set ";
|
---|
199 | if (new_password!=null && !new_password.equals("")){
|
---|
200 | sql_modify_user_info += "password='" + new_password +"', ";
|
---|
201 | }
|
---|
202 |
|
---|
203 | sql_modify_user_info += "groups='" + groups + "'" + ", accountstatus='" + accountstatus + "'" + ", comment='" + comment + "'" + " where username='" + username+"'";
|
---|
204 | try{
|
---|
205 | System.out.println(sql_modify_user_info);
|
---|
206 | state.execute(sql_modify_user_info);
|
---|
207 | conn.commit();
|
---|
208 | }catch(Throwable e){
|
---|
209 | System.out.println("exception thrown:");
|
---|
210 | if (e instanceof SQLException){
|
---|
211 | printSQLError((SQLException) e);
|
---|
212 | }else{
|
---|
213 | e.printStackTrace();
|
---|
214 | }
|
---|
215 | closeDatabase();
|
---|
216 | return "Error:"+e.getMessage();
|
---|
217 | }
|
---|
218 | return "succeed";
|
---|
219 | }
|
---|
220 |
|
---|
221 | public void db2txt() throws SQLException{
|
---|
222 | UserQueryResult userQueryResult=new UserQueryResult();
|
---|
223 | String sql_list_all_user="select username, password, groups, accountstatus, comment from " + USERS ;
|
---|
224 | ResultSet rs = state.executeQuery(sql_list_all_user);
|
---|
225 |
|
---|
226 | while (rs.next()){
|
---|
227 | String returned_username= rs.getString("username");
|
---|
228 | System.out.println("[" + returned_username+"]");
|
---|
229 | String returned_comment=rs.getString("comment");
|
---|
230 | System.out.println("<comment>" + returned_comment);
|
---|
231 | String returned_accountstatus=rs.getString("accountstatus");
|
---|
232 | System.out.println("<enabled>" + returned_accountstatus);
|
---|
233 | String returned_groups=rs.getString("groups");
|
---|
234 | System.out.println("<groups>" + returned_groups);
|
---|
235 | String returned_password=rs.getString("password");
|
---|
236 | System.out.println("<password>" + returned_password);
|
---|
237 | System.out.println("<username>" + returned_username);
|
---|
238 | System.out.println("");
|
---|
239 | System.out.println("----------------------------------------------------------------------");
|
---|
240 | }
|
---|
241 | conn.commit();
|
---|
242 | closeDatabase();
|
---|
243 | }
|
---|
244 |
|
---|
245 | static void printSQLError(SQLException e)
|
---|
246 | {
|
---|
247 | while (e != null)
|
---|
248 | {
|
---|
249 | System.out.println(e.toString());
|
---|
250 | e = e.getNextException();
|
---|
251 | }
|
---|
252 | }
|
---|
253 | }
|
---|