Source code: com/cybertivity/powerjournal/database/JournalManager.java
1 package com.cybertivity.powerjournal.database;
2 import java.sql.*;
3 import java.util.ArrayList;
4 import java.util.HashMap;
5 import java.util.Iterator;
6 import java.util.Set;
7 import org.hsqldb.jdbcDriver;
8 import org.hsqldb.util.*;
9 import com.cybertivity.powerjournal.*;
10
11 /**
12 * Title: PowerJournal
13 * Description: $Id: JournalManager.java,v 1.4 2001/12/06 04:51:49 arrowood Exp $
14 * Copyright: Copyright (c) 2001
15 * Company: <A HREF="http://www.cybertivity.com">Cybertivity</A>
16 *
17 * @author <A HREF="mailto:chris.arrowood@cybertivity.com">Chris Arrowood</A>
18 * @created November 30, 2001
19 * @version 1.0
20 */
21
22 public class JournalManager extends DBManager {
23
24 /**
25 */
26 public final static String TABLE_NAME = "JOURNALS";
27 /**
28 */
29 public final static String DB_NAME = "pj_data";
30 private static JournalManager instance = null;
31 /**
32 */
33 protected String dbType = null;
34
35
36 public JournalManager(String dbTypeArg, String urlArg, String driverArg, String userArg, String passwordArg) throws DBException {
37 super(urlArg, driverArg, userArg, passwordArg);
38 dbType = dbTypeArg;
39 createDatabase(dbType, DB_NAME);
40 if (!tableExists(TABLE_NAME)) {
41 createTable();
42 }
43 }
44
45
46 public static JournalManager getInstance(String dbTypeArg, String urlArg, String driverArg, String userArg, String passwordArg, boolean forceNew) throws DBException {
47 if (forceNew && instance != null) {
48 instance = null;
49 }
50 if (instance == null) {
51 instance = new JournalManager(dbTypeArg, urlArg, driverArg, userArg, passwordArg);
52 }
53 return instance;
54 }
55
56
57 public static JournalManager getInstance(String dbTypeArg, String urlArg, String driverArg, String userArg, String passwordArg) throws DBException {
58 return getInstance(dbTypeArg, urlArg, driverArg, userArg, passwordArg, false);
59 }
60
61
62
63 public HashMap exportAllJournalEntries(EntryManager entryManager) throws DBException {
64 HashMap journals = new HashMap();
65 ArrayList journalNames = getAllJournalNames();
66 Iterator iter = journalNames.iterator();
67 while (iter.hasNext()) {
68 String journalName = (String) iter.next();
69 if (journalName != null) {
70 entryManager.changeJournal(journalName);
71 journals.put(journalName, entryManager.getEntries());
72 }
73 }
74 return journals;
75 }
76
77
78 public HashMap exportAllJournalsConfig() throws DBException {
79 HashMap journals = new HashMap();
80 ArrayList journalNames = getAllJournalNames();
81 Iterator iter = journalNames.iterator();
82 while (iter.hasNext()) {
83 String journalName = (String) iter.next();
84 journals.put(journalName, getJournalHashedPassword(journalName));
85 }
86 return journals;
87 }
88
89
90 public boolean journalExists(String journalName) throws DBException {
91 if (tableExists(journalName)) {
92 return true;
93 } else {
94 return false;
95 }
96 }
97
98
99 public boolean journalConfigExists(String journalName) throws DBException {
100 boolean found = false;
101 ArrayList journals = getAllJournalNames();
102 for (int i = 0; i < journals.size(); i++) {
103 String thisJournalName = (String) journals.get(i);
104 if (thisJournalName.equalsIgnoreCase(journalName)) {
105 found = true;
106 break;
107 }
108 }
109 return found;
110 }
111
112
113 public static boolean isValidJournalName(String name) {
114 boolean isvalid = true;
115 //only Aa-Zz and space
116 if (!UtilLib.isAlphaNumeric(name, true)) {
117 isvalid = false;
118 }
119 //must begin with a letter
120 String test = name.substring(0, 1);
121 if (!UtilLib.isAlpha(test, false)) {
122 isvalid = false;
123 }
124 if (name.length()<3) {
125 isvalid = false;
126 }
127
128 return isvalid;
129 }
130
131
132 public void createJournals(HashMap config) throws DBException {
133 Set keys = config.keySet();
134 Iterator iter = keys.iterator();
135 while (iter.hasNext()) {
136 String journalName = (String) iter.next();
137 String journalHashedPassword = (String) config.get(journalName);
138 //prevent dupes
139 if (!journalConfigExists(journalName)) {
140 createJournal(journalName, journalHashedPassword, false);
141 }
142 }
143 }
144
145
146 public static String makeUrl(String dbAddress, String dbPort, String dbType, String powerJournalDir, boolean specifyDatabase) {
147 String url = "";
148 if (dbType.equals(ConfigManager.VALUE_DB_VERSION_HSQL)) {
149 url = "jdbc:hsqldb:" + powerJournalDir + "/";
150 } else if (dbType.equals(ConfigManager.VALUE_DB_VERSION_MYSQL)) {
151 url = "jdbc:mysql://" + dbAddress + ":" + dbPort + "/";
152 }
153 if (specifyDatabase) {
154 url += DB_NAME;
155 }
156 return url;
157 }
158
159
160 public static String makeUrl(String dbAddress, String dbPort, String dbType, String powerJournalDir) {
161 return makeUrl(dbAddress, dbPort, dbType, powerJournalDir, true);
162 }
163
164
165 public String getUrl() {
166 return url;
167 }
168
169
170 public ArrayList getAllJournalNames() throws DBException {
171 String query = "SELECT journal FROM " + TABLE_NAME + " ";
172 ArrayList journals = new ArrayList();
173 try {
174 conn = getConnection();
175 statement = conn.createStatement();
176 resultSet = statement.executeQuery(query);
177 while (resultSet.next()) {
178 journals.add(resultSet.getString("journal"));
179 }
180 statement.close();
181 } catch (SQLException e) {
182 throw new DBException("SQLException: " + e.getMessage());
183 }
184 return journals;
185 }
186
187
188 public void deleteJournal(String journalName) throws DBException {
189 String query = "DELETE FROM " + TABLE_NAME + " WHERE journal = '" + journalName + "' ";
190 executeSQL(query);
191 query = "DROP TABLE " + journalName + " ";
192 executeSQL(query);
193 }
194
195
196 public int getJournalCount() {
197 try {
198 ArrayList journals = getAllJournalNames();
199 return journals.size();
200 } catch (DBException ex) {
201 return 0;
202 }
203 }
204
205
206 public Integer createJournal(String journalName, String passwordArg) throws DBException {
207 return createJournal(journalName, passwordArg, true);
208 }
209
210
211
212 public Integer createJournal(String journalName, String passwordArg, boolean hashPassword) throws DBException {
213 Integer ID = null;
214 journalName = journalName.trim();
215 journalName = journalName.replace(' ', '_');
216 try {
217 if (hashPassword) {
218 passwordArg = PasswordLib.hashPassword(passwordArg.toLowerCase());
219 }
220 conn = getConnection();
221 // String sql = "insert into " + TABLE_NAME + " values ( ?, ?, ?); CALL IDENTITY()";
222 String sql = "insert into " + TABLE_NAME + " values ( ?, ?, ?);";
223 PreparedStatement p = conn.prepareStatement(sql);
224 p.clearParameters();
225 p.setNull(1, Types.INTEGER);
226 p.setString(2, journalName);
227 p.setString(3, passwordArg);
228 p.executeUpdate();
229 statement.close();
230 //get new journal id
231 ID = getJournalID(journalName);
232 } catch (SQLException ex) {
233 throw new DBException(ex.getMessage());
234 } catch (java.security.NoSuchAlgorithmException ex) {
235 throw new DBException(ex.getMessage());
236 }
237 return ID;
238 }
239
240
241 private Integer getJournalID(String journalName) throws DBException {
242 Integer ID = null;
243 try {
244 String query = "SELECT id FROM " + TABLE_NAME + " where journal = '" + journalName + "'";
245 ArrayList journals = new ArrayList();
246 conn = getConnection();
247 statement = conn.createStatement();
248 resultSet = statement.executeQuery(query);
249 resultSet.setFetchSize(1);
250 resultSet.next();
251 String idString = resultSet.getString("id");
252 try {
253 ID = new Integer(Integer.parseInt(idString));
254 } catch (Exception ex) {
255 //already handled by returning null
256 }
257
258 statement.close();
259 } catch (SQLException ex) {
260 throw new DBException(ex.getMessage());
261 }
262 return ID;
263 }
264
265
266 public void changePassword(String journalName, String plainTextPassword) throws DBException {
267 try {
268 String hashedPassword = PasswordLib.hashPassword(plainTextPassword.toLowerCase());
269 String query = "UPDATE " + TABLE_NAME + " SET password = '" + hashedPassword + "' where journal = '" + journalName + "'";
270 executeSQL(query);
271 } catch (java.security.NoSuchAlgorithmException ex) {
272 throw new DBException(ex.getMessage());
273 }
274 }
275
276
277 private String getJournalHashedPassword(String journalName) throws DBException {
278 String passwordString = null;
279 try {
280 String query = "SELECT password FROM " + TABLE_NAME + " where journal = '" + journalName + "'";
281 ArrayList journals = new ArrayList();
282 conn = getConnection();
283 statement = conn.createStatement();
284 resultSet = statement.executeQuery(query);
285 resultSet.setFetchSize(1);
286 resultSet.next();
287 passwordString = resultSet.getString("password");
288 statement.close();
289 } catch (SQLException ex) {
290 throw new DBException(ex.getMessage());
291 }
292 return passwordString;
293 }
294
295
296 private void createTable() throws DBException {
297 if (dbType.equals(ConfigManager.VALUE_DB_VERSION_HSQL)) {
298 executeSQL("CREATE TABLE " + TABLE_NAME + " (id INT NOT NULL IDENTITY, journal LONGVARCHAR, password LONGVARCHAR) ");
299 } else if (dbType.equals(ConfigManager.VALUE_DB_VERSION_MYSQL)) {
300 executeSQL("CREATE TABLE " + TABLE_NAME + " (id INT UNSIGNED NOT NULL AUTO_INCREMENT, journal TEXT, password TEXT, PRIMARY KEY(id), UNIQUE(id), INDEX(id)) ");
301 }
302 }
303
304
305 public String getHashedPassword(String journalName) throws DBException {
306 String hashedPassword = null;
307 try {
308 String query = "SELECT password FROM " + TABLE_NAME + " where journal = '" + journalName + "'";
309 ArrayList journals = new ArrayList();
310 conn = getConnection();
311 statement = conn.createStatement();
312 resultSet = statement.executeQuery(query);
313 resultSet.setFetchSize(1);
314 resultSet.next();
315 hashedPassword = resultSet.getString("password");
316 statement.close();
317 } catch (SQLException ex) {
318 throw new DBException(ex.getMessage());
319 }
320 return hashedPassword;
321 }
322
323
324 public int getEntryCount() throws DBException {
325 return getRowCount(TABLE_NAME);
326 }
327
328 }