[16869] | 1 | /*
|
---|
| 2 | * DerbyWrapper.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 | */
|
---|
[14289] | 19 | package org.greenstone.gsdl3.util;
|
---|
| 20 |
|
---|
| 21 | import java.sql.Connection;
|
---|
| 22 | import java.sql.DriverManager;
|
---|
| 23 | import java.sql.ResultSet;
|
---|
| 24 | import java.sql.SQLException;
|
---|
| 25 | import java.sql.Statement;
|
---|
[25092] | 26 | import java.util.ArrayList;
|
---|
| 27 | import java.util.HashMap;
|
---|
[14289] | 28 |
|
---|
[27316] | 29 | import org.greenstone.gsdl3.service.Authentication;
|
---|
| 30 |
|
---|
[14289] | 31 | public class DerbyWrapper
|
---|
| 32 | {
|
---|
[24983] | 33 | static final String PROTOCOL = "jdbc:derby:";
|
---|
| 34 | static final String DRIVER = "org.apache.derby.jdbc.EmbeddedDriver";
|
---|
| 35 | static final String USERSDB = "usersDB";
|
---|
| 36 | static final String USERS = "users";
|
---|
[25092] | 37 | static final String ROLES = "roles";
|
---|
[24983] | 38 | private Connection conn = null;
|
---|
| 39 | private Statement state = null;
|
---|
| 40 | private String protocol_str;
|
---|
[14289] | 41 |
|
---|
[24983] | 42 | public DerbyWrapper()
|
---|
| 43 | {
|
---|
| 44 | }
|
---|
[14289] | 45 |
|
---|
[24983] | 46 | public DerbyWrapper(String dbpath)
|
---|
| 47 | {
|
---|
| 48 | connectDatabase(dbpath, false);
|
---|
[14289] | 49 | }
|
---|
| 50 |
|
---|
[24983] | 51 | public void connectDatabase(String dbpath, boolean create_database)
|
---|
| 52 | {
|
---|
| 53 | try
|
---|
| 54 | {
|
---|
| 55 | Class.forName(DRIVER).newInstance();
|
---|
| 56 | //System.out.println("Loaded the embedded driver.");
|
---|
| 57 | protocol_str = PROTOCOL + dbpath;
|
---|
| 58 | if (create_database)
|
---|
| 59 | {
|
---|
| 60 | conn = DriverManager.getConnection(protocol_str + ";create=true");
|
---|
| 61 | }
|
---|
| 62 | else
|
---|
| 63 | {
|
---|
| 64 | conn = DriverManager.getConnection(protocol_str);
|
---|
| 65 | }
|
---|
| 66 | state = conn.createStatement();
|
---|
| 67 | }
|
---|
| 68 | catch (Throwable e)
|
---|
| 69 | {
|
---|
| 70 | System.out.println("exception thrown:");
|
---|
| 71 | if (e instanceof SQLException)
|
---|
| 72 | {
|
---|
| 73 | printSQLError((SQLException) e);
|
---|
| 74 | }
|
---|
| 75 | else
|
---|
| 76 | {
|
---|
| 77 | e.printStackTrace();
|
---|
| 78 | }
|
---|
| 79 | }
|
---|
[14289] | 80 | }
|
---|
[24983] | 81 |
|
---|
| 82 | public void closeDatabase()
|
---|
| 83 | {
|
---|
[27316] | 84 | //state = null;
|
---|
[26911] | 85 | //conn = null;
|
---|
[24983] | 86 | boolean gotSQLExc = false;
|
---|
| 87 | try
|
---|
[27316] | 88 | {
|
---|
| 89 | // shutdown the database
|
---|
| 90 | DriverManager.getConnection(protocol_str + ";shutdown=true");
|
---|
| 91 |
|
---|
| 92 | }
|
---|
[24983] | 93 | catch (SQLException se)
|
---|
| 94 | {
|
---|
[27316] | 95 | // this is good (i.e. what Derby is designed to do on a successful shutdown)
|
---|
| 96 | gotSQLExc = true;
|
---|
[24983] | 97 | }
|
---|
[27316] | 98 | catch (Exception e)
|
---|
| 99 | {
|
---|
| 100 | e.printStackTrace();
|
---|
[26911] | 101 | }
|
---|
| 102 |
|
---|
[24983] | 103 | if (!gotSQLExc)
|
---|
| 104 | {
|
---|
[27316] | 105 | System.err.println("Warning: Derby Database did not shut down normally");
|
---|
[24983] | 106 | }
|
---|
[14289] | 107 | }
|
---|
| 108 |
|
---|
[27316] | 109 | public static void shutdownDatabaseServer()
|
---|
| 110 | {
|
---|
| 111 | boolean gotSQLExc = false;
|
---|
[26911] | 112 |
|
---|
[27316] | 113 | try
|
---|
| 114 | {
|
---|
| 115 | // shutdown the whole server
|
---|
| 116 | DriverManager.getConnection(PROTOCOL + ";shutdown=true");
|
---|
[26911] | 117 |
|
---|
[27316] | 118 | }
|
---|
| 119 | catch (SQLException se)
|
---|
| 120 | {
|
---|
| 121 | // this is good (i.e. what Derby is designed to do on a successful shutdown)
|
---|
| 122 | gotSQLExc = true;
|
---|
| 123 | //System.out.println("Shutdown returned: " + se);
|
---|
| 124 | }
|
---|
| 125 | catch (Exception e)
|
---|
| 126 | {
|
---|
[26911] | 127 |
|
---|
[27316] | 128 | e.printStackTrace();
|
---|
| 129 | }
|
---|
| 130 | if (!gotSQLExc)
|
---|
| 131 | {
|
---|
| 132 | System.err.println("Warning: Derby did not shut down normally");
|
---|
| 133 | }
|
---|
[26911] | 134 |
|
---|
[27316] | 135 | }
|
---|
| 136 |
|
---|
[25092] | 137 | public void createDatabase()
|
---|
[24983] | 138 | {
|
---|
[25092] | 139 | try
|
---|
| 140 | {
|
---|
| 141 | conn.setAutoCommit(false);
|
---|
[25261] | 142 | state.execute("create table users (username varchar(40) not null, password varchar(40) not null, accountstatus varchar(10), comment varchar(100), email varchar(40), primary key(username))");
|
---|
[25092] | 143 | state.execute("create table roles (username varchar(40) not null, role varchar(40) not null, primary key (username, role))");
|
---|
[25261] | 144 | state.execute("insert into " + USERS + " values ('admin', '" + Authentication.hashPassword("admin") + "', 'true', 'change the password for this account as soon as possible', '')");
|
---|
[25092] | 145 | state.execute("insert into " + ROLES + " values ('admin', 'administrator')");
|
---|
| 146 | state.execute("insert into " + ROLES + " values ('admin', 'all-collections-editor')");
|
---|
| 147 | conn.commit();
|
---|
| 148 | }
|
---|
[25125] | 149 | catch (Exception ex)
|
---|
[25092] | 150 | {
|
---|
| 151 | ex.printStackTrace();
|
---|
| 152 | }
|
---|
[24983] | 153 | }
|
---|
[14289] | 154 |
|
---|
[24983] | 155 | public UserQueryResult listAllUser() throws SQLException
|
---|
| 156 | {
|
---|
| 157 | UserQueryResult userQueryResult = new UserQueryResult();
|
---|
[25261] | 158 | String sql_list_all_user = "SELECT username, password, accountstatus, email, comment FROM " + USERS;
|
---|
[14289] | 159 |
|
---|
[25125] | 160 | ArrayList<HashMap<String, String>> users = new ArrayList<HashMap<String, String>>();
|
---|
[24983] | 161 | ResultSet rs = state.executeQuery(sql_list_all_user);
|
---|
| 162 | while (rs.next())
|
---|
| 163 | {
|
---|
[25092] | 164 | HashMap<String, String> user = new HashMap<String, String>();
|
---|
| 165 | user.put("username", rs.getString("username"));
|
---|
| 166 | user.put("password", rs.getString("password"));
|
---|
| 167 | user.put("as", rs.getString("accountstatus"));
|
---|
| 168 | user.put("comment", rs.getString("comment"));
|
---|
[25261] | 169 | user.put("email", rs.getString("email"));
|
---|
[25125] | 170 |
|
---|
[25092] | 171 | users.add(user);
|
---|
[24983] | 172 | }
|
---|
[25125] | 173 |
|
---|
| 174 | for (HashMap<String, String> user : users)
|
---|
[25092] | 175 | {
|
---|
| 176 | ResultSet gs = state.executeQuery("SELECT role FROM roles WHERE username = '" + user.get("username") + "'");
|
---|
| 177 | String group = "";
|
---|
[25125] | 178 | while (gs.next())
|
---|
[25092] | 179 | {
|
---|
[25125] | 180 | if (!group.equals(""))
|
---|
[25092] | 181 | {
|
---|
| 182 | group += ",";
|
---|
| 183 | }
|
---|
| 184 | group += gs.getString("role");
|
---|
| 185 | }
|
---|
[25261] | 186 | userQueryResult.addUserTerm(user.get("username"), user.get("password"), group, user.get("as"), user.get("comment"), user.get("email"));
|
---|
[25092] | 187 | }
|
---|
[25125] | 188 |
|
---|
[24983] | 189 | if (userQueryResult.getSize() == 0)
|
---|
| 190 | {
|
---|
| 191 | System.out.println("couldn't find any users");
|
---|
| 192 | return null;
|
---|
| 193 | }
|
---|
| 194 | else
|
---|
| 195 | {
|
---|
| 196 | return userQueryResult;
|
---|
| 197 | }
|
---|
[14289] | 198 | }
|
---|
[24983] | 199 |
|
---|
[25261] | 200 | public boolean addUser(String username, String password, String groups, String accountstatus, String comment, String email)
|
---|
[24983] | 201 | {
|
---|
| 202 | try
|
---|
| 203 | {
|
---|
[25092] | 204 | conn.setAutoCommit(false);
|
---|
[25261] | 205 | String sql_insert_user = "insert into " + USERS + " values ('" + username + "', '" + password + "', '" + accountstatus + "', '" + comment + "', '" + email + "')";
|
---|
[24983] | 206 | state.execute(sql_insert_user);
|
---|
[25125] | 207 |
|
---|
[25092] | 208 | String[] groupArray = groups.split(",");
|
---|
[25125] | 209 | for (String g : groupArray)
|
---|
[25092] | 210 | {
|
---|
| 211 | String sql_insert_group = "insert into " + ROLES + " values ('" + username + "', '" + g + "')";
|
---|
| 212 | state.execute(sql_insert_group);
|
---|
| 213 | }
|
---|
[25125] | 214 |
|
---|
[24983] | 215 | conn.commit();
|
---|
| 216 | }
|
---|
| 217 | catch (Throwable e)
|
---|
| 218 | {
|
---|
| 219 | System.out.println("exception thrown:");
|
---|
| 220 | if (e instanceof SQLException)
|
---|
| 221 | {
|
---|
| 222 | printSQLError((SQLException) e);
|
---|
| 223 | }
|
---|
| 224 | else
|
---|
| 225 | {
|
---|
| 226 | e.printStackTrace();
|
---|
| 227 | }
|
---|
| 228 | closeDatabase();
|
---|
| 229 | System.out.println("Error:" + e.getMessage());
|
---|
[25261] | 230 | return false;
|
---|
[24983] | 231 | }
|
---|
[25125] | 232 |
|
---|
[25261] | 233 | return true;
|
---|
[14289] | 234 | }
|
---|
| 235 |
|
---|
[25261] | 236 | public boolean deleteUser(String del_username)
|
---|
[24983] | 237 | {
|
---|
| 238 | try
|
---|
| 239 | {
|
---|
[25092] | 240 | conn.setAutoCommit(false);
|
---|
| 241 | String sql_delete_user = "delete from " + USERS + " where username='" + del_username + "'";
|
---|
| 242 | String sql_delete_groups = "delete from " + ROLES + " where username='" + del_username + "'";
|
---|
[24983] | 243 | state.execute(sql_delete_user);
|
---|
[25092] | 244 | state.execute(sql_delete_groups);
|
---|
[24983] | 245 | conn.commit();
|
---|
| 246 | }
|
---|
| 247 | catch (Throwable e)
|
---|
| 248 | {
|
---|
| 249 | System.out.println("exception thrown:");
|
---|
| 250 | if (e instanceof SQLException)
|
---|
| 251 | {
|
---|
| 252 | printSQLError((SQLException) e);
|
---|
| 253 | }
|
---|
| 254 | else
|
---|
| 255 | {
|
---|
| 256 | e.printStackTrace();
|
---|
| 257 | }
|
---|
| 258 | closeDatabase();
|
---|
[25261] | 259 | return false;
|
---|
[24983] | 260 | }
|
---|
[25261] | 261 | return true;
|
---|
[14289] | 262 | }
|
---|
[24983] | 263 |
|
---|
| 264 | public boolean deleteAllUser() throws SQLException
|
---|
| 265 | {
|
---|
| 266 | conn.setAutoCommit(false);
|
---|
| 267 | try
|
---|
| 268 | {
|
---|
| 269 | state.execute("delete from " + USERS);
|
---|
[25092] | 270 | state.execute("delete from " + ROLES);
|
---|
[24983] | 271 | conn.commit();
|
---|
| 272 | }
|
---|
| 273 | catch (Throwable e)
|
---|
| 274 | {
|
---|
| 275 | System.out.println("exception thrown:");
|
---|
| 276 | if (e instanceof SQLException)
|
---|
| 277 | {
|
---|
| 278 | printSQLError((SQLException) e);
|
---|
| 279 | }
|
---|
| 280 | else
|
---|
| 281 | {
|
---|
| 282 | e.printStackTrace();
|
---|
| 283 | }
|
---|
| 284 | closeDatabase();
|
---|
| 285 | return false;
|
---|
| 286 | }
|
---|
| 287 | return true;
|
---|
[14289] | 288 | }
|
---|
[14335] | 289 |
|
---|
[25261] | 290 | public UserQueryResult findUser(String username, String password)
|
---|
[24983] | 291 | {
|
---|
| 292 | UserQueryResult userQueryResult = new UserQueryResult();
|
---|
| 293 |
|
---|
[25261] | 294 | try
|
---|
| 295 | {
|
---|
| 296 | conn.setAutoCommit(false);
|
---|
| 297 | }
|
---|
| 298 | catch (Exception ex)
|
---|
| 299 | {
|
---|
| 300 | ex.printStackTrace();
|
---|
| 301 | return null;
|
---|
| 302 | }
|
---|
| 303 |
|
---|
| 304 | String sql_find_user = "SELECT username, password, accountstatus, comment, email FROM " + USERS;
|
---|
[24983] | 305 | String append_sql = "";
|
---|
| 306 |
|
---|
| 307 | if (username != null)
|
---|
| 308 | {
|
---|
| 309 | append_sql = " WHERE username = '" + username + "'";
|
---|
| 310 | }
|
---|
| 311 | if (password != null)
|
---|
| 312 | {
|
---|
| 313 | if (append_sql.equals(""))
|
---|
| 314 | {
|
---|
| 315 | append_sql = " WHERE password = '" + password + "'";
|
---|
| 316 | }
|
---|
| 317 | else
|
---|
| 318 | {
|
---|
| 319 | append_sql += " and password = '" + password + "'";
|
---|
| 320 | }
|
---|
| 321 | }
|
---|
| 322 | if (!append_sql.equals(""))
|
---|
| 323 | {
|
---|
| 324 | sql_find_user += append_sql;
|
---|
| 325 | }
|
---|
[25125] | 326 |
|
---|
[25261] | 327 | try
|
---|
[24983] | 328 | {
|
---|
[25261] | 329 | ArrayList<HashMap<String, String>> users = new ArrayList<HashMap<String, String>>();
|
---|
| 330 | ResultSet rs = state.executeQuery(sql_find_user);
|
---|
| 331 | while (rs.next())
|
---|
| 332 | {
|
---|
| 333 | HashMap<String, String> user = new HashMap<String, String>();
|
---|
| 334 | user.put("username", rs.getString("username"));
|
---|
| 335 | user.put("password", rs.getString("password"));
|
---|
| 336 | user.put("as", rs.getString("accountstatus"));
|
---|
| 337 | user.put("comment", rs.getString("comment"));
|
---|
| 338 | user.put("email", rs.getString("email"));
|
---|
[25125] | 339 |
|
---|
[25261] | 340 | users.add(user);
|
---|
| 341 | }
|
---|
| 342 | conn.commit();
|
---|
[25125] | 343 |
|
---|
[25261] | 344 | for (HashMap<String, String> user : users)
|
---|
| 345 | {
|
---|
| 346 | ResultSet gs = state.executeQuery("SELECT role FROM " + ROLES + " WHERE username = '" + user.get("username") + "'");
|
---|
[25125] | 347 |
|
---|
[25261] | 348 | String group = "";
|
---|
| 349 | while (gs.next())
|
---|
[25092] | 350 | {
|
---|
[25261] | 351 | if (!group.equals(""))
|
---|
| 352 | {
|
---|
| 353 | group += ",";
|
---|
| 354 | }
|
---|
| 355 | group += gs.getString("role");
|
---|
[25092] | 356 | }
|
---|
[25261] | 357 |
|
---|
| 358 | userQueryResult.addUserTerm(user.get("username"), user.get("password"), group, user.get("as"), user.get("comment"), user.get("email"));
|
---|
[25092] | 359 | }
|
---|
| 360 | }
|
---|
[25261] | 361 | catch (Exception ex)
|
---|
| 362 | {
|
---|
| 363 | ex.printStackTrace();
|
---|
| 364 | return null;
|
---|
| 365 | }
|
---|
[25125] | 366 |
|
---|
[24983] | 367 | if (userQueryResult.getSize() > 0)
|
---|
| 368 | {
|
---|
| 369 | return userQueryResult;
|
---|
| 370 | }
|
---|
| 371 | else
|
---|
| 372 | {
|
---|
| 373 | return null;
|
---|
| 374 | }
|
---|
[14335] | 375 | }
|
---|
| 376 |
|
---|
[27316] | 377 | // findUser(null) will return all users, which is why a UserQueryResult
|
---|
| 378 | // (a vector of UserTermInfo) is returned
|
---|
[25125] | 379 | public UserQueryResult findUser(String username) throws SQLException
|
---|
| 380 | {
|
---|
| 381 | UserQueryResult userQueryResult = new UserQueryResult();
|
---|
| 382 |
|
---|
| 383 | conn.setAutoCommit(false);
|
---|
[25261] | 384 | String sql_find_user = "SELECT username, password, accountstatus, comment, email FROM " + USERS;
|
---|
[25125] | 385 | String append_sql = "";
|
---|
| 386 |
|
---|
| 387 | if (username != null)
|
---|
| 388 | {
|
---|
| 389 | append_sql = " WHERE username = '" + username + "'";
|
---|
| 390 | }
|
---|
| 391 | if (!append_sql.equals(""))
|
---|
| 392 | {
|
---|
| 393 | sql_find_user += append_sql;
|
---|
| 394 | }
|
---|
| 395 |
|
---|
| 396 | ArrayList<HashMap<String, String>> users = new ArrayList<HashMap<String, String>>();
|
---|
| 397 | ResultSet rs = state.executeQuery(sql_find_user);
|
---|
| 398 | while (rs.next())
|
---|
| 399 | {
|
---|
| 400 | HashMap<String, String> user = new HashMap<String, String>();
|
---|
| 401 | user.put("username", rs.getString("username"));
|
---|
| 402 | user.put("password", rs.getString("password"));
|
---|
| 403 | user.put("as", rs.getString("accountstatus"));
|
---|
| 404 | user.put("comment", rs.getString("comment"));
|
---|
[25261] | 405 | user.put("email", rs.getString("email"));
|
---|
[25125] | 406 |
|
---|
| 407 | users.add(user);
|
---|
| 408 | }
|
---|
| 409 | conn.commit();
|
---|
| 410 |
|
---|
| 411 | for (HashMap<String, String> user : users)
|
---|
| 412 | {
|
---|
| 413 | ResultSet gs = state.executeQuery("SELECT role FROM " + ROLES + " WHERE username = '" + user.get("username") + "'");
|
---|
| 414 |
|
---|
| 415 | String group = "";
|
---|
| 416 | while (gs.next())
|
---|
| 417 | {
|
---|
| 418 | if (!group.equals(""))
|
---|
| 419 | {
|
---|
| 420 | group += ",";
|
---|
| 421 | }
|
---|
| 422 | group += gs.getString("role");
|
---|
| 423 | }
|
---|
| 424 |
|
---|
[25261] | 425 | userQueryResult.addUserTerm(user.get("username"), user.get("password"), group, user.get("as"), user.get("comment"), user.get("email"));
|
---|
[25125] | 426 | }
|
---|
| 427 |
|
---|
| 428 | if (userQueryResult.getSize() > 0)
|
---|
| 429 | {
|
---|
| 430 | return userQueryResult;
|
---|
| 431 | }
|
---|
| 432 | else
|
---|
| 433 | {
|
---|
| 434 | System.out.println("couldn't find the user");
|
---|
| 435 | return null;
|
---|
| 436 | }
|
---|
| 437 | }
|
---|
| 438 |
|
---|
[25261] | 439 | public String modifyUserInfo(String username, String new_password, String groups, String accountstatus, String comment, String email)
|
---|
[24983] | 440 | {
|
---|
| 441 | try
|
---|
| 442 | {
|
---|
[25092] | 443 | conn.setAutoCommit(false);
|
---|
| 444 | String sql_modify_user_info = "update " + USERS + " set ";
|
---|
[27316] | 445 |
|
---|
[25261] | 446 | boolean needComma = false;
|
---|
[25092] | 447 | if (new_password != null && !new_password.equals(""))
|
---|
| 448 | {
|
---|
| 449 | sql_modify_user_info += "password='" + new_password + "'";
|
---|
[25261] | 450 | needComma = true;
|
---|
[25092] | 451 | }
|
---|
[25125] | 452 |
|
---|
[25092] | 453 | if (accountstatus != null && comment != null)
|
---|
| 454 | {
|
---|
[25261] | 455 | sql_modify_user_info += (needComma ? "," : "") + " accountstatus='" + accountstatus + "'" + ", comment='" + comment + "'";
|
---|
| 456 | needComma = true;
|
---|
[25092] | 457 | }
|
---|
[27316] | 458 |
|
---|
| 459 | if (email != null)
|
---|
[25261] | 460 | {
|
---|
| 461 | sql_modify_user_info += (needComma ? "," : "") + " email='" + email + "'";
|
---|
| 462 | }
|
---|
[27316] | 463 |
|
---|
[25092] | 464 | sql_modify_user_info += " where username='" + username + "'";
|
---|
[24983] | 465 | state.execute(sql_modify_user_info);
|
---|
[25125] | 466 |
|
---|
[25092] | 467 | String sql_delete_groups = "delete from " + ROLES + " where username='" + username + "'";
|
---|
| 468 | state.execute(sql_delete_groups);
|
---|
[25125] | 469 |
|
---|
[25092] | 470 | String[] groupsArray = groups.split(",");
|
---|
[25125] | 471 | for (String g : groupsArray)
|
---|
[25092] | 472 | {
|
---|
| 473 | String sql_insert_group = "insert into " + ROLES + " values ('" + username + "', '" + g + "')";
|
---|
| 474 | state.execute(sql_insert_group);
|
---|
| 475 | }
|
---|
[25125] | 476 |
|
---|
[24983] | 477 | conn.commit();
|
---|
| 478 | }
|
---|
| 479 | catch (Throwable e)
|
---|
| 480 | {
|
---|
| 481 | System.out.println("exception thrown:");
|
---|
| 482 | if (e instanceof SQLException)
|
---|
| 483 | {
|
---|
| 484 | printSQLError((SQLException) e);
|
---|
| 485 | }
|
---|
| 486 | else
|
---|
| 487 | {
|
---|
| 488 | e.printStackTrace();
|
---|
| 489 | }
|
---|
| 490 | closeDatabase();
|
---|
| 491 | return "Error:" + e.getMessage();
|
---|
| 492 | }
|
---|
| 493 | return "succeed";
|
---|
[14289] | 494 | }
|
---|
[24983] | 495 |
|
---|
[25261] | 496 | public void db2txt()
|
---|
[24983] | 497 | {
|
---|
[27316] | 498 | System.err.println(db2txtString());
|
---|
[25308] | 499 | }
|
---|
| 500 |
|
---|
| 501 | public String db2txtString()
|
---|
| 502 | {
|
---|
[27316] | 503 | //String db2txt = "Error in converting db2txt string.";
|
---|
| 504 | String db2txt = "";
|
---|
[25261] | 505 | try
|
---|
| 506 | {
|
---|
[27316] | 507 | conn.setAutoCommit(false); // An exception at this line can happen when the GS3 tomcat server is already running
|
---|
| 508 | // and GS3 is already accessing the usersDB when this function independently tries to
|
---|
| 509 | // connect to it (via usersDB2txt.java's main(). For an explanation of the possible
|
---|
| 510 | // reasons, see http://db.apache.org/derby/papers/DerbyTut/embedded_intro.html
|
---|
| 511 | // section "Embedded Derby supports multiple users in one JVM".
|
---|
[25261] | 512 | String sql_list_all_user = "select username, password, accountstatus, comment, email from " + USERS;
|
---|
| 513 | ResultSet rs = state.executeQuery(sql_list_all_user);
|
---|
[24983] | 514 |
|
---|
[25261] | 515 | ArrayList<HashMap<String, String>> infoMap = new ArrayList<HashMap<String, String>>();
|
---|
| 516 |
|
---|
| 517 | while (rs.next())
|
---|
[25092] | 518 | {
|
---|
[25261] | 519 | HashMap<String, String> userMap = new HashMap<String, String>();
|
---|
| 520 | userMap.put("username", rs.getString("username"));
|
---|
| 521 | userMap.put("password", rs.getString("password"));
|
---|
| 522 | userMap.put("status", rs.getString("accountstatus"));
|
---|
| 523 | userMap.put("comment", rs.getString("comment"));
|
---|
| 524 | userMap.put("email", rs.getString("email"));
|
---|
| 525 | infoMap.add(userMap);
|
---|
| 526 | }
|
---|
| 527 | conn.commit();
|
---|
| 528 |
|
---|
[25337] | 529 | StringBuffer buffer = new StringBuffer();//("-------------------------------------");
|
---|
[25261] | 530 | for (HashMap<String, String> user : infoMap)
|
---|
| 531 | {
|
---|
| 532 | ResultSet groupsSet = state.executeQuery("SELECT role FROM " + ROLES + " WHERE username = '" + user.get("username") + "'");
|
---|
| 533 | String returnedGroups = "";
|
---|
| 534 | while (groupsSet.next())
|
---|
[25092] | 535 | {
|
---|
[25261] | 536 | if (!returnedGroups.equals(""))
|
---|
| 537 | {
|
---|
| 538 | returnedGroups += ",";
|
---|
| 539 | }
|
---|
| 540 | returnedGroups += groupsSet.getString("role");
|
---|
[25092] | 541 | }
|
---|
[25261] | 542 | conn.commit();
|
---|
[27316] | 543 |
|
---|
[25308] | 544 | buffer.append("USERNAME = " + user.get("username"));
|
---|
| 545 | buffer.append("\nPASSWORD = " + user.get("password"));
|
---|
| 546 | buffer.append("\nGROUPS = " + returnedGroups);
|
---|
| 547 | buffer.append("\nSTATUS = " + user.get("status"));
|
---|
| 548 | buffer.append("\nCOMMENT = " + user.get("comment"));
|
---|
| 549 | buffer.append("\nEMAIL = " + user.get("email"));
|
---|
[27316] | 550 | buffer.append("\n-------------------------------------\n");
|
---|
[25092] | 551 | }
|
---|
[25337] | 552 | db2txt = buffer.toString();
|
---|
[25261] | 553 |
|
---|
| 554 | conn.commit();
|
---|
| 555 | closeDatabase();
|
---|
[24983] | 556 | }
|
---|
[25261] | 557 | catch (Exception ex)
|
---|
| 558 | {
|
---|
| 559 | ex.printStackTrace();
|
---|
| 560 | }
|
---|
[27316] | 561 | finally
|
---|
| 562 | {
|
---|
| 563 | return db2txt;
|
---|
| 564 | }
|
---|
[14289] | 565 | }
|
---|
| 566 |
|
---|
[24983] | 567 | static void printSQLError(SQLException e)
|
---|
| 568 | {
|
---|
| 569 | while (e != null)
|
---|
| 570 | {
|
---|
| 571 | System.out.println(e.toString());
|
---|
| 572 | e = e.getNextException();
|
---|
| 573 | }
|
---|
[14289] | 574 | }
|
---|
| 575 | }
|
---|