001: /*
002: * $Id: AbstractBaseStorage.java,v 1.1 2006/03/06 11:30:53 azzazzel Exp $
003: *
004: * Copyright 2006 Commsen International
005: *
006: * Licensed under the Common Public License, Version 1.0 (the "License");
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.opensource.org/licenses/cpl1.0.txt
011: *
012: * THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
013: * EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT LIMITATION, ANY WARRANTIES OR CONDITIONS
014: * OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
015: *
016: */
017: package com.commsen.stopwatch.storages;
018:
019: import java.sql.Connection;
020: import java.sql.DriverManager;
021: import java.sql.PreparedStatement;
022: import java.sql.ResultSet;
023: import java.sql.SQLException;
024: import java.sql.Statement;
025: import java.sql.Timestamp;
026: import java.util.ArrayList;
027: import java.util.Arrays;
028: import java.util.Calendar;
029: import java.util.Date;
030:
031: import org.apache.log4j.Logger;
032:
033: import com.commsen.stopwatch.Report;
034: import com.commsen.stopwatch.StopwatchStorage;
035: import com.commsen.stopwatch.StopwatchStorageException;
036: import com.commsen.stopwatch.reports.DefaultStopwatchReport;
037:
038: /**
039: * Abstract class representig database storage. It has predefined methods to
040: * crete table(s) as well as insert, update and delete records.
041: * The default implementation is limited to the very basic measurment data - time and count.
042: * If any other info is to be stored, the extending class should overwrite appropriate
043: * <code>getXxxxQuery()</code> method(s)
044: *
045: * @author Milen Dyankov
046: *
047: */
048: public abstract class AbstractDatabaseStorage implements
049: StopwatchStorage {
050:
051: /**
052: * Called to obtain the JDBC driver to use
053: *
054: * @return the fully qualified class name
055: */
056: protected abstract String getDriver();
057:
058: /**
059: * Called to obtain the connection string
060: *
061: * @return the connection string
062: */
063: protected abstract String getConnectionString();
064:
065: /**
066: * Called to obtain the database user name
067: *
068: * @return database user name
069: */
070: protected abstract String getUser();
071:
072: /**
073: * Called to obtain the database password
074: *
075: * @return database password
076: */
077: protected abstract String getPassword();
078:
079: /**
080: * Provides the table name
081: *
082: * @return table name
083: */
084: protected abstract String getTableName();
085:
086: /**
087: * Provides SQL query which will be executed to check if table exist
088: * <p>
089: * Default is:
090: * <code>select 1 from {@link #getTableName()}</code>
091: *
092: * @return SQL query to check if table exist
093: */
094: protected String getCheckTableQuery() {
095: return "select 1 from " + getTableName();
096: }
097:
098: /**
099: * Called to obtain the query to be executed when table needs to be created.
100: * <p>
101: * Default is:
102: * <code><pre>
103: * create table {@link #getTableName()} (
104: * _id INT GENERATED BY DEFAULT AS IDENTITY,
105: * _group VARCHAR,
106: * _label VARCHAR,
107: * _start TIMESTAMP,
108: * _end TIMESTAMP
109: * )
110: * </pre></code>
111: *
112: * @return SQL query to create table
113: */
114: protected String getCreateTableQuery() {
115: return " create table " + getTableName() + " ("
116: + " _id INT GENERATED BY DEFAULT AS IDENTITY,"
117: + " _group VARCHAR," + " _label VARCHAR,"
118: + " _start TIMESTAMP," + " _end TIMESTAMP" + ")";
119: }
120:
121: /**
122: * Called to obtain the query to be executed when table needs to be truncated
123: * <p>
124: * Default is:
125: * <code>delete from {@link #getTableName()}</code>
126: *
127: * @return SQL query to truncate table
128: */
129: protected String getTruncTableQuery() {
130: return "delete from " + getTableName();
131: }
132:
133: /**
134: * Called to obtain the columns to be returned. The columns returned by this method are
135: * used in all report queries.
136: *
137: * @return part of SQL query
138: */
139: protected abstract String getReturnColumns();
140:
141: /**
142: * The group by clause used in all report queries
143: * Default is: <code>_group, _label</code>
144: *
145: * @return part of SQL query
146: */
147: protected String getGroupBy() {
148: return "_group, _label";
149: }
150:
151: /**
152: * The order by clause used in all report queries
153: * Default is: <code>_group, _label</code>
154: *
155: * @return part of SQL query
156: */
157: protected String getOrderBy() {
158: return "_group, _label";
159: }
160:
161: /**
162: * Called to obtain the query to be executed when report about all groups and labels is to be generated
163: * <p>
164: * Default is:
165: * <code><pre>
166: * select {@link #getReturnColumns()}
167: * from {@link #getTableName()}
168: * where _end is not null
169: * group by {@link #getGroupBy()}
170: * order by {@link #getOrderBy()}
171: * )
172: * </pre></code>
173: *
174: * @return SQL query used to generate report
175: */
176: protected String getAllReportQuery() {
177: return " select _group, _label, " + getReturnColumns()
178: + " from " + getTableName() + " main "
179: + " where _end is not null " + " group by "
180: + getGroupBy() + " order by " + getOrderBy();
181: }
182:
183: /**
184: * Called to obtain the query to be executed when summary report about all groups is to be generated
185: * <p>
186: * Default is:
187: * <code><pre>
188: * select {@link #getReturnColumns()}
189: * from {@link #getTableName()}
190: * where _end is not null
191: * group by _group
192: * order by _group
193: * )
194: * </pre></code>
195: *
196: * @return SQL query used to generate report
197: */
198: protected String getAllByGroupReportQuery() {
199: return " select _group, '', " + getReturnColumns() + " from "
200: + getTableName() + " main "
201: + " where _end is not null " + " group by _group"
202: + " order by _group";
203: }
204:
205: /**
206: * Called to obtain the query to be executed when summary report about all labels is to be generated
207: * <p>
208: * Default is:
209: * <code><pre>
210: * select {@link #getReturnColumns()}
211: * from {@link #getTableName()}
212: * where _end is not null
213: * group by _label
214: * order by _label
215: * )
216: * </pre></code>
217: *
218: * @return SQL query used to generate report
219: */
220: protected String getAllByLabelReportQuery() {
221: return " select '', _label, " + getReturnColumns() + " from "
222: + getTableName() + " main "
223: + " where _end is not null " + " group by _label"
224: + " order by _label";
225: }
226:
227: /**
228: * Called to obtain the query to be executed when report about given group and label is to be generated
229: * <p>
230: * Default is:
231: * <code><pre>
232: * select {@link #getReturnColumns()}
233: * from {@link #getTableName()}
234: * where _end is not null and _group = ? and _label = ?
235: * group by {@link #getGroupBy()}
236: * order by {@link #getOrderBy()}
237: * )
238: * </pre></code>
239: *
240: * @return SQL query used to generate report
241: */
242: protected String getSingleReportQuery() {
243: return " select _group, _label, "
244: + getReturnColumns()
245: + " from "
246: + getTableName()
247: + " main "
248: + " where _end is not null and _group = ? and _label = ? "
249: + " group by " + getGroupBy() + " order by "
250: + getOrderBy();
251: }
252:
253: /**
254: * Called to obtain the query to be executed when report about given group and label is to be generated
255: * <p>
256: * Default is:
257: * <code><pre>
258: * select {@link #getReturnColumns()}
259: * from {@link #getTableName()}
260: * where _end is not null and _label = ?
261: * group by {@link #getGroupBy()}
262: * order by {@link #getOrderBy()}
263: * )
264: * </pre></code>
265: *
266: * @return SQL query used to generate report
267: */
268: protected String getLabelReportQuery() {
269: return " select _group, _label, " + getReturnColumns()
270: + " from " + getTableName() + " main "
271: + " where _end is not null and _label = ? "
272: + " group by " + getGroupBy() + " order by "
273: + getOrderBy();
274: }
275:
276: /**
277: * Called to obtain the query to be executed when report about given group and label is to be generated
278: * <p>
279: * Default is:
280: * <code><pre>
281: * select {@link #getReturnColumns()}
282: * from {@link #getTableName()}
283: * where _end is not null and _group = ?
284: * group by {@link #getGroupBy()}
285: * order by {@link #getOrderBy()}
286: * )
287: * </pre></code>
288: *
289: * @return SQL query used to generate report
290: */
291: protected String getGroupReportQuery() {
292: return " select _group, _label, " + getReturnColumns()
293: + " from " + getTableName() + " main "
294: + " where _end is not null and _group = ? "
295: + " group by " + getGroupBy() + " order by "
296: + getOrderBy();
297: }
298:
299: public String getInsertQuery() {
300: return "insert into " + getTableName()
301: + " (_group, _label, _start) values (?, ?, ?)";
302: }
303:
304: protected String getUpdateQuery() {
305: return "update " + getTableName()
306: + " set _end = ? where _id = ? and _end IS NULL";
307: }
308:
309: protected String getDeleteQuery() {
310: return "delete from " + getTableName() + " where _id = ?";
311: }
312:
313: protected abstract String getLastIdentityQuery();
314:
315: protected String getLoadQuery() {
316: return "select count(1) from "
317: + getTableName()
318: + " where (_start < ? and _end > ?) or (_start > ? and _start < ?) or (_end > ? and _end < ?)";
319: }
320:
321: protected String getGroupLoadQuery() {
322: return "select count(1) from "
323: + getTableName()
324: + " where _group=? and ((_start < ? and _end > ?) or (_start > ? and _start < ?) or (_end > ? and _end < ?))";
325: }
326:
327: protected String getLabelLoadQuery() {
328: return "select count(1) from "
329: + getTableName()
330: + " where _label=? and ((_start < ? and _end > ?) or (_start > ? and _start < ?) or (_end > ? and _end < ?))";
331: }
332:
333: protected String getGroupLabelLoadQuery() {
334: return "select count(1) from "
335: + getTableName()
336: + " where _group=? and _label=? and ((_start < ? and _end > ?) or (_start > ? and _start < ?) or (_end > ? and _end < ?))";
337: }
338:
339: /**
340: * Prepared statement initialized in {@link #open()} method with query returned by {@link #getInsertQuery()}
341: */
342: protected PreparedStatement insertPreparedStatement;
343:
344: /**
345: * Prepared statement initialized in {@link #open()} method with query returned by {@link #getUpdateQuery()}
346: */
347: protected PreparedStatement updatePreparedStatement;
348:
349: /**
350: * Prepared statement initialized in {@link #open()} method with query returned by {@link #getDeleteQuery()}
351: */
352: protected PreparedStatement deletePreparedStatement;
353:
354: /**
355: * Prepared statement initialized in {@link #open()} method with query returned by {@link #getLastIdentityQuery()}
356: */
357: protected PreparedStatement lastIdentityStatement;
358:
359: /**
360: * Prepared statement initialized in {@link #open()} method with query returned by {@link #getAllReportQuery()}
361: */
362: protected PreparedStatement allReportStatement;
363:
364: /**
365: * Prepared statement initialized in {@link #open()} method with query returned by {@link #getAllByGroupReportQuery()}
366: */
367: protected PreparedStatement allByGroupReportStatement;
368:
369: /**
370: * Prepared statement initialized in {@link #open()} method with query returned by {@link #getAllByLabelReportQuery()}
371: */
372: protected PreparedStatement allByLabelReportStatement;
373:
374: /**
375: * Prepared statement initialized in {@link #open()} method with query returned by {@link #getGroupReportQuery()}
376: */
377: protected PreparedStatement groupReportStatement;
378:
379: /**
380: * Prepared statement initialized in {@link #open()} method with query returned by {@link #getLabelReportQuery()}
381: */
382: protected PreparedStatement labelReportStatement;
383:
384: /**
385: * Prepared statement initialized in {@link #open()} method with query returned by {@link #getSingleReportQuery()}
386: */
387: protected PreparedStatement singleReportStatement;
388:
389: /**
390: *
391: */
392: protected PreparedStatement groupLabelLoadStatement;
393:
394: /**
395: *
396: */
397: protected PreparedStatement groupLoadStatement;
398:
399: /**
400: *
401: */
402: protected PreparedStatement labelLoadStatement;
403:
404: /**
405: *
406: */
407: protected PreparedStatement loadStatement;
408:
409: /**
410: * Detabase connection used to execute all update queries
411: */
412: Connection updateConnection;
413:
414: /**
415: * Detabase connection used to execute all select queries
416: */
417: Connection selectConnection;
418:
419: /**
420: *
421: * @see com.commsen.stopwatch.StopwatchStorage#open()
422: */
423: public void open() throws StopwatchStorageException {
424: try {
425: Class.forName(getDriver());
426: } catch (ClassNotFoundException e) {
427: throw new StopwatchStorageException("failed to load "
428: + getDriver() + " driver.", e);
429: }
430:
431: try {
432: if (isDebug())
433: getLogger().debug("Connecting to database ... ");
434:
435: updateConnection = DriverManager.getConnection(
436: getConnectionString(), getUser(), getPassword());
437: selectConnection = DriverManager.getConnection(
438: getConnectionString(), getUser(), getPassword());
439:
440: if (isDebug())
441: getLogger().debug(
442: "connection sucsessful. Checking tables ... ");
443:
444: } catch (SQLException e) {
445: throw new StopwatchStorageException(
446: "database connection error", e);
447: }
448:
449: // check if table exists
450: try {
451: Statement statement = selectConnection.createStatement();
452: statement.execute(getCheckTableQuery());
453: statement.close();
454: if (getTruncTableQuery() != null
455: && !"".equals(getTruncTableQuery().trim())) {
456: try {
457: statement = updateConnection.createStatement();
458: statement.executeUpdate(getTruncTableQuery());
459: statement.close();
460: } catch (SQLException e) {
461: throw new StopwatchStorageException(
462: "Can not truncate table", e);
463: }
464: }
465: if (isDebug())
466: getLogger()
467: .debug(
468: "table(s) exists. Engine will now attempt to create prepared statements ... ");
469: } catch (SQLException e) {
470: try {
471: Statement statement = updateConnection
472: .createStatement();
473: statement.execute(getCreateTableQuery());
474: statement.close();
475: if (isDebug())
476: getLogger()
477: .debug(
478: "table(s) created. Engine will now attempt to create prepared statements ... ");
479: } catch (SQLException e1) {
480: throw new StopwatchStorageException(
481: "Can not create table(s)", e1);
482: }
483: }
484:
485: try {
486: insertPreparedStatement = updateConnection
487: .prepareStatement(getInsertQuery());
488: updatePreparedStatement = updateConnection
489: .prepareStatement(getUpdateQuery());
490: deletePreparedStatement = updateConnection
491: .prepareStatement(getDeleteQuery());
492: lastIdentityStatement = updateConnection
493: .prepareStatement(getLastIdentityQuery());
494:
495: allReportStatement = selectConnection
496: .prepareStatement(getAllReportQuery());
497: allByGroupReportStatement = selectConnection
498: .prepareStatement(getAllByGroupReportQuery());
499: allByLabelReportStatement = selectConnection
500: .prepareStatement(getAllByLabelReportQuery());
501: groupReportStatement = selectConnection
502: .prepareStatement(getGroupReportQuery());
503: labelReportStatement = selectConnection
504: .prepareStatement(getLabelReportQuery());
505: singleReportStatement = selectConnection
506: .prepareStatement(getSingleReportQuery());
507:
508: loadStatement = selectConnection
509: .prepareStatement(getLoadQuery());
510: groupLoadStatement = selectConnection
511: .prepareStatement(getGroupLoadQuery());
512: labelLoadStatement = selectConnection
513: .prepareStatement(getLabelLoadQuery());
514: groupLabelLoadStatement = selectConnection
515: .prepareStatement(getGroupLabelLoadQuery());
516:
517: if (isDebug())
518: getLogger().debug("Prepared statements created!");
519: } catch (SQLException e) {
520: throw new StopwatchStorageException(
521: "can not create prepared statements", e);
522: }
523: }
524:
525: /**
526: *
527: * @see com.commsen.stopwatch.StopwatchStorage#freeze()
528: */
529: public void freeze() throws StopwatchStorageException {
530: // by default do nothing on freeze
531: }
532:
533: /**
534: *
535: * @see com.commsen.stopwatch.StopwatchStorage#unfreeze()
536: */
537: public void unfreeze() throws StopwatchStorageException {
538: // by default do nothing on unfreeze
539: }
540:
541: /**
542: *
543: * @see com.commsen.stopwatch.StopwatchStorage#close()
544: */
545: public void close() throws StopwatchStorageException {
546: try {
547: updateConnection.close();
548: updateConnection = null;
549: selectConnection.close();
550: selectConnection = null;
551: if (isDebug())
552: getLogger().debug("Database shut down !!!");
553: } catch (SQLException e) {
554: throw new StopwatchStorageException("database error", e);
555: }
556: }
557:
558: /**
559: *
560: * @see com.commsen.stopwatch.StopwatchStorage#newRecord(java.lang.Object[])
561: */
562: public long newRecord(Object[] parameters)
563: throws StopwatchStorageException {
564:
565: if (insertPreparedStatement == null)
566: return -1;
567: try {
568: synchronized (insertPreparedStatement.getConnection()) {
569: insertPreparedStatement.setString(1,
570: (String) parameters[0]);
571: insertPreparedStatement.setString(2,
572: (String) parameters[1]);
573: insertPreparedStatement.setTimestamp(3, new Timestamp(
574: ((Long) parameters[2]).longValue()));
575: insertPreparedStatement.executeUpdate();
576: ResultSet resultSet = lastIdentityStatement
577: .executeQuery();
578: resultSet.next();
579: long result = resultSet.getLong(1);
580: resultSet.close();
581: return result;
582: }
583: } catch (SQLException e) {
584: throw new StopwatchStorageException("database error", e);
585: }
586: }
587:
588: /**
589: *
590: * @see com.commsen.stopwatch.StopwatchStorage#newRecord(java.lang.Object[])
591: */
592: public long newCompleteRecord(Object[] startParameters,
593: Object[] endParameters) throws StopwatchStorageException {
594: long id = newRecord(startParameters);
595: completeRecord(id, endParameters);
596: return id;
597: }
598:
599: /**
600: *
601: * @see com.commsen.stopwatch.StopwatchStorage#removeRecord(long)
602: */
603: public boolean removeRecord(long id)
604: throws StopwatchStorageException {
605: if (id < 0)
606: return false;
607: try {
608: synchronized (deletePreparedStatement.getConnection()) {
609: deletePreparedStatement.setLong(1, id);
610: deletePreparedStatement.executeUpdate();
611: return true;
612: }
613: } catch (SQLException e) {
614: throw new StopwatchStorageException("database error", e);
615: }
616: }
617:
618: /**
619: *
620: * @see com.commsen.stopwatch.StopwatchStorage#completeRecord(long, Object[])
621: */
622: public boolean completeRecord(long id, Object[] parameters)
623: throws StopwatchStorageException {
624: if (id < 0)
625: return false;
626: try {
627: synchronized (updatePreparedStatement.getConnection()) {
628: updatePreparedStatement.setTimestamp(1, new Timestamp(
629: ((Long) parameters[0]).longValue()));
630: updatePreparedStatement.setLong(2, id);
631: updatePreparedStatement.executeUpdate();
632: return true;
633: }
634: } catch (SQLException e) {
635: throw new StopwatchStorageException("database error", e);
636: }
637:
638: }
639:
640: /**
641: * This method simply calls {@link #prepareReports(PreparedStatement, Object[])} passing
642: * {@link #allReportStatement} as statement and no parameters (<code>null</code> value).
643: *
644: * @see StopwatchStorage#getReports()
645: */
646: public Report[] getReports() {
647: try {
648: return prepareReports(allReportStatement, null);
649: } catch (SQLException e) {
650: getLogger().error("database error!", e);
651: }
652: return null;
653: }
654:
655: /**
656: * This method simply calls {@link #prepareReports(PreparedStatement, Object[])} passing
657: * {@link #allByGroupReportStatement} as statement and no parameters (<code>null</code> value).
658: *
659: * @see StopwatchStorage#getReports()
660: */
661: public Report[] getAllByGroupReports() {
662: try {
663: return prepareReports(allByGroupReportStatement, null);
664: } catch (SQLException e) {
665: getLogger().error("database error!", e);
666: }
667: return null;
668: }
669:
670: /**
671: * This method simply calls {@link #prepareReports(PreparedStatement, Object[])} passing
672: * {@link #allByLabelReportStatement} as statement and no parameters (<code>null</code> value).
673: *
674: * @see StopwatchStorage#getReports()
675: */
676: public Report[] getAllByLabelReports() {
677: try {
678: return prepareReports(allByLabelReportStatement, null);
679: } catch (SQLException e) {
680: getLogger().error("database error!", e);
681: }
682: return null;
683: }
684:
685: /**
686: * This method simply calls {@link #prepareReports(PreparedStatement, Object[])} passing
687: * {@link #singleReportStatement} as statement and <code>group</code> and <code>label</code> as parameters.
688: *
689: * @see StopwatchStorage#getReport(java.lang.String, java.lang.String)
690: */
691: public Report getReport(String group, String label) {
692: Report[] reports = null;
693: try {
694: reports = prepareReports(singleReportStatement,
695: new Object[] { group, label });
696: } catch (SQLException e) {
697: getLogger().error("database error!", e);
698: }
699:
700: if (reports != null && reports.length > 0) {
701: return reports[0];
702: }
703:
704: return null;
705: }
706:
707: /**
708: * This method simply calls {@link #prepareReports(PreparedStatement, Object[])} passing
709: * {@link #groupReportStatement} as statement and <code>group</code> as parameter.
710: *
711: * @see StopwatchStorage#getGroupReports(java.lang.String)
712: */
713: public Report[] getGroupReports(String group) {
714: try {
715: return prepareReports(groupReportStatement,
716: new Object[] { group });
717: } catch (SQLException e) {
718: getLogger().error("database error!", e);
719: }
720: return null;
721: }
722:
723: /**
724: * This method simply calls {@link #prepareReports(PreparedStatement, Object[])} passing
725: * {@link #labelReportStatement} as statement and <code>label</code> as parameter.
726: *
727: * @see StopwatchStorage#getLabelReports(java.lang.String)
728: */
729: public Report[] getLabelReports(String label) {
730: try {
731: return prepareReports(labelReportStatement,
732: new Object[] { label });
733: } catch (SQLException e) {
734: getLogger().error("database error!", e);
735: }
736: return null;
737: }
738:
739: /**
740: * This method simply executes given <code>statement</code> with given <code>params</code>.
741: * All report queries call this method to obtain array of reports. This method should be overwriten
742: * by extending classes that need to provide more measurment information then basic time and count.
743: *
744: * @param statement the prepared statement to execute
745: * @param params the params to passed to the prepared statement
746: * @return array of {@link Report}
747: * @throws SQLException on database connection error or other errors
748: */
749: protected Report[] prepareReports(PreparedStatement statement,
750: Object[] params) throws SQLException {
751:
752: if (statement == null)
753: return new Report[0];
754: ArrayList list = new ArrayList();
755: synchronized (statement.getConnection()) {
756:
757: if (params != null && params.length > 0) {
758: for (int i = 0; i < params.length; i++) {
759: statement.setObject(i + 1, params[i]);
760: }
761: }
762:
763: ResultSet resultSet = statement.executeQuery();
764: while (resultSet.next()) {
765: list.add(new DefaultStopwatchReport(resultSet
766: .getString(1), resultSet.getString(2),
767: resultSet.getLong(3), resultSet.getDouble(4),
768: resultSet.getDouble(5), resultSet.getDouble(6),
769: resultSet.getDouble(7)));
770: }
771: }
772: return (Report[]) list.toArray(new Report[list.size()]);
773: }
774:
775: /**
776: * @see com.commsen.stopwatch.StopwatchStorage#getLoad(int, int)
777: */
778: public long[] getLoad(String group, String label, int field,
779: int value) {
780:
781: ArrayList paramsArray = new ArrayList();
782: PreparedStatement statement;
783: if (group == null && label == null) {
784: statement = loadStatement;
785: } else if (group == null) {
786: statement = labelLoadStatement;
787: paramsArray.add(label);
788: } else if (label == null) {
789: statement = groupLoadStatement;
790: paramsArray.add(group);
791: } else {
792: statement = groupLabelLoadStatement;
793: paramsArray.add(group);
794: paramsArray.add(label);
795:
796: }
797:
798: long[] result = new long[value];
799: Arrays.fill(result, 0);
800: Calendar calendarEnd = Calendar.getInstance();
801: Calendar calendarStart = Calendar.getInstance();
802: calendarStart.add(field, -1);
803: try {
804: for (int i = 0; i < value; i++) {
805: ArrayList params = new ArrayList(paramsArray);
806: params.add(calendarStart.getTime());
807: params.add(calendarEnd.getTime());
808: params.add(calendarStart.getTime());
809: params.add(calendarEnd.getTime());
810: params.add(calendarStart.getTime());
811: params.add(calendarEnd.getTime());
812: synchronized (statement.getConnection()) {
813: for (int index = 0; index < params.size(); index++) {
814: Object o = params.get(index);
815: if (o instanceof Date) {
816: statement
817: .setTimestamp(index + 1,
818: new Timestamp(((Date) o)
819: .getTime()));
820: } else {
821: statement.setObject(index + 1, o);
822: }
823: }
824: ResultSet rs = statement.executeQuery();
825:
826: if (rs.next() == true)
827: result[value - i - 1] = rs.getLong(1);
828: }
829:
830: calendarEnd.add(field, -1);
831: calendarStart.add(field, -1);
832:
833: }
834: } catch (SQLException e) {
835: getLogger().error("database error!", e);
836: }
837:
838: return result;
839: }
840:
841: /**
842: * Returns the logger for this class
843: * @return the logger for this class
844: */
845: protected abstract Logger getLogger();
846:
847: /**
848: * Checks if debug log level is enabled in both Stopwatch and Log4j.
849: * @return <code>true</code> if debug log level is enabled, <code>false</code> otherwise.
850: */
851: protected boolean isDebug() {
852: return isDebugEnabled() && getLogger().isDebugEnabled();
853: }
854:
855: /**
856: * @return Returns the debugEnabled.
857: * @see com.commsen.stopwatch.StopwatchEngine#setDebugEnabled(boolean)
858: */
859: public abstract boolean isDebugEnabled();
860:
861: }
|