001: package org.enhydra.snapperAdmin.business;
002:
003: import java.io.File;
004: import java.sql.Connection;
005: import java.sql.PreparedStatement;
006: import java.sql.ResultSet;
007: import java.sql.SQLException;
008: import java.sql.Timestamp;
009: import java.util.HashMap;
010: import java.util.Map;
011: import java.util.TreeSet;
012: import java.util.Vector;
013:
014: import javax.naming.Context;
015: import javax.naming.InitialContext;
016: import javax.sql.DataSource;
017:
018: import org.enhydra.snapper.SnapperManager;
019: import org.enhydra.snapperAdmin.SnapperAdmin;
020:
021: public class BusinessUtil {
022:
023: Vector filtered;
024:
025: int reconnections = 0;
026:
027: //private Vector included;
028:
029: public String title = "";
030:
031: public String update = "";
032:
033: private TreeSet included = null;
034:
035: private boolean checkConn = true;
036:
037: public BusinessUtil() {
038: checkConn = SnapperAdmin.checkConnection();
039: }
040:
041: public Vector getFilteredFiles(String db, String tb, String co)
042: throws SQLException {
043:
044: filtered = new Vector();
045: Connection connection = null;
046: PreparedStatement pstmt = null;
047: ResultSet rs = null;
048: SnapperAdmin.logInfo("Reading filtered files!");
049: try {
050: Context envCtx = (Context) new InitialContext()
051: .lookup("java:comp/env");
052: DataSource ds = (DataSource) envCtx.lookup(db);
053: connection = ds.getConnection();
054:
055: if (checkConn) {
056: pstmt = connection.prepareStatement("Select 1");
057: rs = pstmt.executeQuery();
058: }
059: try {
060: String sql_query = "SELECT DISTINCT " + co + " FROM "
061: + tb + " ORDER BY " + co;
062: pstmt = connection.prepareStatement(sql_query);
063: if (!(SnapperManager.getInstance().getFetchSize() < 1))
064: pstmt.setFetchSize(SnapperManager.getInstance()
065: .getFetchSize());
066: rs = pstmt.executeQuery();
067:
068: if (rs != null)
069: while (rs.next()) {
070: String str = rs.getString(1);
071: str = str.replaceAll("\\\\", "/");
072: filtered.add(str);
073: }
074: } catch (Exception e) {
075: SnapperAdmin.logInfo("Can not read filtered files");
076: }
077:
078: } catch (Exception e) {
079: try {
080: reconnections++;
081: Thread.currentThread().sleep(3000);
082: // System.out.println("Waiting for connection recovery!!!");
083: } catch (InterruptedException e1) {
084: // e1.printStackTrace();
085: }
086: if (reconnections < 2)
087: filtered = getFilteredFiles(db, tb, co);
088: else
089: SnapperAdmin
090: .logInfo("Problem with database connection!!!");
091: } finally {
092: try {
093: if (rs != null)
094: rs.close();
095: } catch (SQLException e) {
096: }
097: try {
098: if (pstmt != null)
099: pstmt.close();
100: } catch (SQLException e) {
101: }
102: try {
103: if (connection != null)
104: connection.close();
105: } catch (SQLException e) {
106: }
107: }
108: SnapperAdmin.logInfo("Reading filtered files finished!");
109: return filtered;
110: }
111:
112: public TreeSet getIncludedFiles(String db, String tb, String co)
113: throws SQLException {
114:
115: included = new TreeSet();
116:
117: Connection connection = null;
118: PreparedStatement pstmt = null;
119: ResultSet rs = null;
120: SnapperAdmin.logInfo("Reading include files!");
121: try {
122: Context envCtx = (Context) new InitialContext()
123: .lookup("java:comp/env");
124: DataSource ds = (DataSource) envCtx.lookup(db);
125: connection = ds.getConnection();
126:
127: if (checkConn) {
128: pstmt = connection.prepareStatement("Select 1");
129: rs = pstmt.executeQuery();
130: }
131: try {
132: String sql_query = "SELECT DISTINCT " + co + " FROM "
133: + tb + " ORDER BY " + co;
134: pstmt = connection.prepareStatement(sql_query);
135: if (!(SnapperManager.getInstance().getFetchSize() < 1))
136: pstmt.setFetchSize(SnapperManager.getInstance()
137: .getFetchSize());
138: Timestamp sts = new Timestamp(System
139: .currentTimeMillis());
140: rs = pstmt.executeQuery();
141: Timestamp sts2 = new Timestamp(System
142: .currentTimeMillis());
143: SnapperAdmin.logInfo("Query execution time: "
144: + (sts2.getTime() - sts.getTime()));
145:
146: sts = new Timestamp(System.currentTimeMillis());
147: if (rs != null)
148: while (rs.next()) {
149: String str = rs.getString(1);
150: included.add(str);
151: }
152: sts2 = new Timestamp(System.currentTimeMillis());
153: SnapperAdmin.logInfo("Tree set input time: "
154: + (sts2.getTime() - sts.getTime()));
155: SnapperAdmin.logInfo("Include list reading finished");
156: SnapperAdmin.logInfo("Number of files in tree set: "
157: + included.size());
158: } catch (Exception e) {
159: SnapperAdmin.logInfo("Can not read include files");
160: }
161: } catch (Exception e) {
162: e.printStackTrace();
163: try {
164: reconnections++;
165: Thread.currentThread().sleep(3000);
166: // System.out.println("Waiting for connection recovery!!!");
167: } catch (InterruptedException e1) {
168: // e1.printStackTrace();
169: }
170: if (reconnections < 2)
171: included = getIncludedFiles(db, tb, co);
172: else
173: SnapperAdmin
174: .logInfo("Problem with database connection!!!");
175: } finally {
176: try {
177: if (rs != null)
178: rs.close();
179: } catch (SQLException e) {
180: }
181: try {
182: if (pstmt != null)
183: pstmt.close();
184: } catch (SQLException e) {
185: }
186: try {
187: if (connection != null)
188: connection.close();
189: } catch (SQLException e) {
190: }
191: }
192: SnapperAdmin.logInfo("Reading include files finished!");
193: return included;
194: }
195:
196: public Map getData(String db, String tb, String filepath,
197: String mod, long indexMod) throws SQLException {
198:
199: Map data = new HashMap();
200:
201: Connection connection = null;
202: PreparedStatement pstmt = null;
203: ResultSet rs = null;
204:
205: SnapperAdmin.logInfo("Reading data files!");
206: try {
207: Context envCtx = (Context) new InitialContext()
208: .lookup("java:comp/env");
209: DataSource ds = (DataSource) envCtx.lookup(db);
210: connection = ds.getConnection();
211:
212: if (checkConn) {
213: pstmt = connection.prepareStatement("Select 1");
214: rs = pstmt.executeQuery();
215: }
216: try {
217: String sql_query = "SELECT DISTINCT " + filepath
218: + " FROM " + tb + " WHERE " + mod
219: + " > ? ORDER BY " + filepath;
220: pstmt = connection.prepareStatement(sql_query);
221: pstmt.setTimestamp(1, new Timestamp(indexMod));
222: if (!(SnapperManager.getInstance().getFetchSize() < 1))
223: pstmt.setFetchSize(SnapperManager.getInstance()
224: .getFetchSize());
225:
226: rs = pstmt.executeQuery();
227:
228: if (rs != null)
229: while (rs.next()) {
230: String path = rs.getString(filepath);
231: File tempFile = new File(path);
232: if (tempFile.exists()) {
233: path = path.replaceAll("\\\\", "/");
234: data.put(path, null);
235: }
236: }
237: } catch (Exception e) {
238: SnapperAdmin.logInfo("Can not read data files");
239: }
240:
241: } catch (Exception e) {
242: try {
243: reconnections++;
244: Thread.currentThread().sleep(3000);
245: // System.out.println("Waiting for connection recovery!!!");
246: } catch (InterruptedException e1) {
247: // e1.printStackTrace();
248: }
249: if (reconnections < 2)
250: data = getData(db, tb, filepath, mod, indexMod);
251: else
252: SnapperAdmin
253: .logInfo("Problem with database connection!!!");
254: } finally {
255: try {
256: if (rs != null)
257: rs.close();
258: } catch (SQLException e) {
259: }
260: try {
261: if (pstmt != null)
262: pstmt.close();
263: } catch (SQLException e) {
264: }
265: try {
266: if (connection != null)
267: connection.close();
268: } catch (SQLException e) {
269: }
270: }
271: SnapperAdmin.logInfo("Reading data files finished!");
272: return data;
273: }
274:
275: public String getMetadata(String path, String db, String tb,
276: String cp, String ck, String cv) throws SQLException {
277:
278: StringBuffer properties = new StringBuffer();
279:
280: Connection connection = null;
281: PreparedStatement pstmt = null;
282: ResultSet rs = null;
283:
284: SnapperAdmin.logInfo("Reading metadata for file: " + path);
285: try {
286: Context envCtx = (Context) new InitialContext()
287: .lookup("java:comp/env");
288: DataSource ds = (DataSource) envCtx.lookup(db);
289: connection = ds.getConnection();
290: if (checkConn) {
291: pstmt = connection.prepareStatement("Select 1");
292: rs = pstmt.executeQuery();
293: }
294: path = path.replaceAll("'", "''");
295: try {
296: String sql_query = "SELECT " + ck + "," + cv + " FROM "
297: + tb + " WHERE " + cp + "= '" + path + "'";
298: pstmt = connection.prepareStatement(sql_query);
299: if (!(SnapperManager.getInstance().getFetchSize() < 1))
300: pstmt.setFetchSize(SnapperManager.getInstance()
301: .getFetchSize());
302: Timestamp sts = new Timestamp(System
303: .currentTimeMillis());
304: rs = pstmt.executeQuery();
305: Timestamp sts2 = new Timestamp(System
306: .currentTimeMillis());
307: SnapperAdmin.logInfo("Query execution time: "
308: + (sts2.getTime() - sts.getTime()));
309:
310: sts = new Timestamp(System.currentTimeMillis());
311: if (rs != null)
312: while (rs.next()) {
313: properties.append(rs.getString(ck) + " = "
314: + rs.getString(cv) + " \n ");
315: if (rs.getString(ck).trim().equals(
316: SnapperManager.getInstance()
317: .getDocumentLogicalName()))
318: title = rs.getString(cv).trim();
319: if (rs.getString(ck).trim().equals(
320: SnapperManager.getInstance()
321: .getDocumentUpdate()))
322: update = rs.getString(cv).trim();
323: }
324: sts2 = new Timestamp(System.currentTimeMillis());
325: SnapperAdmin.logInfo("Tree set input time: "
326: + (sts2.getTime() - sts.getTime()));
327: } catch (Exception e) {
328: SnapperAdmin.logInfo("Can not read metadata for file: "
329: + path);
330: }
331:
332: } catch (Exception e) {
333: try {
334: reconnections++;
335: Thread.currentThread().sleep(3000);
336: //System.out.println("Waiting for connection recovery!!!");
337: } catch (InterruptedException e1) {
338: // e1.printStackTrace();
339: }
340: if (reconnections < 2)
341: properties
342: .append(getMetadata(path, db, tb, cp, ck, cv));
343: else
344: SnapperAdmin
345: .logInfo("Problem with database connection!!!");
346: } finally {
347: try {
348: if (rs != null)
349: rs.close();
350: } catch (SQLException e) {
351: }
352: try {
353: if (pstmt != null)
354: pstmt.close();
355: } catch (SQLException e) {
356: }
357: try {
358: if (connection != null)
359: connection.close();
360: } catch (SQLException e) {
361: }
362: }
363: SnapperAdmin.logInfo("Reading metadata files finished!");
364: return properties.toString();
365: }
366:
367: public TreeSet getMetadataList(String metaDB, String metaTable,
368: String metaFileColumn) throws SQLException {
369:
370: TreeSet result = new TreeSet();
371: Connection connection = null;
372: PreparedStatement pstmt = null;
373: ResultSet rs = null;
374:
375: SnapperAdmin.logInfo("Reading metadatalist!");
376: try {
377: Context envCtx = (Context) new InitialContext()
378: .lookup("java:comp/env");
379: DataSource ds = (DataSource) envCtx.lookup(metaDB);
380: connection = ds.getConnection();
381:
382: if (checkConn) {
383: pstmt = connection.prepareStatement("Select 1");
384: rs = pstmt.executeQuery();
385: }
386: try {
387: String sql_query = "SELECT DISTINCT " + metaFileColumn
388: + " FROM " + metaTable + " ORDER BY "
389: + metaFileColumn;
390: pstmt = connection.prepareStatement(sql_query);
391: if (!(SnapperManager.getInstance().getFetchSize() < 1))
392: pstmt.setFetchSize(SnapperManager.getInstance()
393: .getFetchSize());
394:
395: Timestamp sts = new Timestamp(System
396: .currentTimeMillis());
397: rs = pstmt.executeQuery();
398: Timestamp sts2 = new Timestamp(System
399: .currentTimeMillis());
400: SnapperAdmin.logInfo("Query execution time: "
401: + (sts2.getTime() - sts.getTime()));
402:
403: sts = new Timestamp(System.currentTimeMillis());
404: if (rs != null)
405: while (rs.next()) {
406: String filePath = rs.getString(metaFileColumn);
407: if (!included.contains(filePath))
408: result.add(filePath);
409: }
410: sts2 = new Timestamp(System.currentTimeMillis());
411: SnapperAdmin.logInfo("Tree set input time: "
412: + (sts2.getTime() - sts.getTime()));
413: SnapperAdmin.logInfo("Number of files in tree set: "
414: + result.size());
415: } catch (Exception e) {
416: SnapperAdmin.logInfo("Can not read metadatalist");
417: }
418:
419: } catch (Exception e) {
420: try {
421: reconnections++;
422: Thread.currentThread().sleep(3000);
423: // System.out.println("Waiting for connection recovery!!!");
424: } catch (InterruptedException e1) {
425: // e1.printStackTrace();
426: }
427: if (reconnections < 2)
428: result = getMetadataList(metaDB, metaTable,
429: metaFileColumn);
430: else
431: SnapperAdmin
432: .logInfo("Problem with database connection!!!");
433: } finally {
434: try {
435: if (rs != null)
436: rs.close();
437: } catch (SQLException e) {
438: }
439: try {
440: if (pstmt != null)
441: pstmt.close();
442: } catch (SQLException e) {
443: }
444: try {
445: if (connection != null)
446: connection.close();
447: } catch (SQLException e) {
448: }
449: }
450: SnapperAdmin.logInfo("Reading metadatalist finshed!");
451: return result;
452: }
453:
454: public void gc() {
455: if (filtered != null) {
456: filtered.removeAllElements();
457: filtered = null;
458: }
459: if (included != null) {
460: included.clear();
461: included = null;
462: }
463: }
464:
465: }
|