001: /*************************************************************************
002: * *
003: * EJBCA: The OpenSource Certificate Authority *
004: * *
005: * This software is free software; you can redistribute it and/or *
006: * modify it under the terms of the GNU Lesser General Public *
007: * License as published by the Free Software Foundation; either *
008: * version 2.1 of the License, or any later version. *
009: * *
010: * See terms of license at gnu.org. *
011: * *
012: *************************************************************************/package org.ejbca.util.query;
013:
014: import java.util.Date;
015:
016: /**
017: * A class used by Query class to build a query for ejbca log or ra modules. Inherits BasicMatch.
018: * Main function is getQueryString which returns a fragment of SQL statment.
019: *
020: * @author TomSelleck
021: * @version $Id: TimeMatch.java,v 1.3 2008/03/14 08:00:58 anatom Exp $
022: *
023: * @see org.ejbca.util.query.BasicMatch
024: * @see org.ejbca.util.query.UserMatch
025: * @see org.ejbca.util.query.LogMatch
026: */
027: public class TimeMatch extends BasicMatch {
028: // Public Constants
029: /** UserMatch Specific Constant */
030: public static final int MATCH_WITH_TIMECREATED = 0;
031: /** UserMatch Specific Constant */
032: public static final int MATCH_WITH_TIMEMODIFIED = 1;
033:
034: /** ApprovalMatch Specific Constant */
035: public static final int MATCH_WITH_REQUESTORAPPROVALTIME = 0;
036: /** ApprovalMatch Specific Constant */
037: public static final int MATCH_WITH_EXPIRETIME = 1;
038:
039: // Private Constants.
040: private static final String[] MATCH_WITH_SQLNAMES = { "time",
041: "time", "timeCreated", "timeModified", "requestDate",
042: "expireDate" }; // Represents the column names in log/ra tables.
043:
044: // Public methods.
045:
046: /**
047: * Creates a new instance of TimeMatch. Construtor should only be used in ra user queries.
048: *
049: * @param type uses Query class constants to determine if it's a log query or ra query.
050: * @param matchwith should be one of MATCH_WITH contants to determine with field to search.
051: * Only used in ra user queries.
052: * @param startdate gives a startdate for the query, null if not needed.
053: * @param startdate gives a enddate for the query, null if not needed.
054: */
055: public TimeMatch(int type, int matchwith, Date startdate,
056: Date enddate) {
057: this .type = type;
058: this .matchwith = matchwith;
059: this .startdate = startdate;
060: this .enddate = enddate;
061: }
062:
063: /**
064: * Creates a new instance of TimeMatch. Constructor should only be used in log queries.
065: *
066: * @param type uses Query class constants to determine if it's a log query or ra query.
067: * @param startdate gives a startdate for the query, null if not needed.
068: * @param startdate gives a enddate for the query, null if not needed.
069: */
070: public TimeMatch(int type, Date startdate, Date enddate) {
071: this .type = type;
072: this .matchwith = 0;
073: this .startdate = startdate;
074: this .enddate = enddate;
075: }
076:
077: /**
078: * DOCUMENT ME!
079: *
080: * @return DOCUMENT ME!
081: */
082: public String getQueryString() {
083: String returnval = "( ";
084:
085: if (startdate != null) {
086: returnval += (MATCH_WITH_SQLNAMES[(type * 2) + matchwith]
087: + " >= " + startdate.getTime() + " ");
088:
089: if (enddate != null) {
090: returnval += " AND ";
091: }
092: }
093:
094: if (enddate != null) {
095: returnval += (MATCH_WITH_SQLNAMES[(type * 2) + matchwith]
096: + " <= " + enddate.getTime() + " ");
097: }
098:
099: returnval += " )";
100:
101: return returnval;
102: }
103:
104: // getQueryString
105:
106: /**
107: * DOCUMENT ME!
108: *
109: * @return DOCUMENT ME!
110: */
111: public boolean isLegalQuery() {
112: return !((startdate == null) && (enddate == null));
113: }
114:
115: // Private Fields.
116: private int matchwith;
117: private int type;
118: private Date startdate;
119: private Date enddate;
120: }
|