KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ejbca > util > query > TimeMatch


1 /*************************************************************************
2  * *
3  * EJBCA: The OpenSource Certificate Authority *
4  * *
5  * This software is free software; you can redistribute it and/or *
6  * modify it under the terms of the GNU Lesser General Public *
7  * License as published by the Free Software Foundation; either *
8  * version 2.1 of the License, or any later version. *
9  * *
10  * See terms of license at gnu.org. *
11  * *
12  *************************************************************************/

13  
14 /*
15  * TimeMatch.java
16  *
17  * Created on den 20 juli 2002, 23:20
18  */

19 package org.ejbca.util.query;
20
21 import java.util.Date JavaDoc;
22
23
24 /**
25  * A class used by Query class to build a query for ejbca log or ra modules. Inherits BasicMatch.
26  * Main function is getQueryString which returns a fragment of SQL statment.
27  *
28  * @author TomSelleck
29  * @version $Id: TimeMatch.java,v 1.2 2006/07/29 11:26:37 herrvendil Exp $
30  *
31  * @see org.ejbca.util.query.BasicMatch
32  * @see org.ejbca.util.query.UserMatch
33  * @see org.ejbca.util.query.LogMatch
34  */

35 public class TimeMatch extends BasicMatch {
36     // Public Constants
37
/** UserMatch Specific Constant */
38     public static final int MATCH_WITH_TIMECREATED = 0;
39     /** UserMatch Specific Constant */
40     public static final int MATCH_WITH_TIMEMODIFIED = 1;
41     
42     /** ApprovalMatch Specific Constant */
43     public static final int MATCH_WITH_REQUESTORAPPROVALTIME = 0;
44     /** ApprovalMatch Specific Constant */
45     public static final int MATCH_WITH_EXPIRETIME = 1;
46
47     // Private Constants.
48
private static final String JavaDoc[] MATCH_WITH_SQLNAMES = {
49         "time", "time", "timeCreated", "timeModified","requestdate","expiredate"
50     }; // Represents the column names in log/ra tables.
51

52     // Public methods.
53

54     /**
55      * Creates a new instance of TimeMatch. Construtor should only be used in ra user queries.
56      *
57      * @param type uses Query class constants to determine if it's a log query or ra query.
58      * @param matchwith should be one of MATCH_WITH contants to determine with field to search.
59      * Only used in ra user queries.
60      * @param startdate gives a startdate for the query, null if not needed.
61      * @param startdate gives a enddate for the query, null if not needed.
62      */

63     public TimeMatch(int type, int matchwith, Date JavaDoc startdate, Date JavaDoc enddate) {
64         this.type = type;
65         this.matchwith = matchwith;
66         this.startdate = startdate;
67         this.enddate = enddate;
68     }
69
70     /**
71      * Creates a new instance of TimeMatch. Constructor should only be used in log queries.
72      *
73      * @param type uses Query class constants to determine if it's a log query or ra query.
74      * @param startdate gives a startdate for the query, null if not needed.
75      * @param startdate gives a enddate for the query, null if not needed.
76      */

77     public TimeMatch(int type, Date JavaDoc startdate, Date JavaDoc enddate) {
78         this.type = type;
79         this.matchwith = 0;
80         this.startdate = startdate;
81         this.enddate = enddate;
82     }
83
84     /**
85      * DOCUMENT ME!
86      *
87      * @return DOCUMENT ME!
88      */

89     public String JavaDoc getQueryString() {
90         String JavaDoc returnval = "( ";
91
92         if (startdate != null) {
93             returnval += (MATCH_WITH_SQLNAMES[(type * 2) + matchwith] + " >= " +
94             startdate.getTime() + " ");
95
96             if (enddate != null) {
97                 returnval += " AND ";
98             }
99         }
100
101         if (enddate != null) {
102             returnval += (MATCH_WITH_SQLNAMES[(type * 2) + matchwith] + " <= " + enddate.getTime() +
103             " ");
104         }
105
106         returnval += " )";
107
108         return returnval;
109     }
110
111     // getQueryString
112

113     /**
114      * DOCUMENT ME!
115      *
116      * @return DOCUMENT ME!
117      */

118     public boolean isLegalQuery() {
119         return !((startdate == null) && (enddate == null));
120     }
121
122     // Private Fields.
123
private int matchwith;
124     private int type;
125     private Date JavaDoc startdate;
126     private Date JavaDoc enddate;
127 }
128
Popular Tags