KickJava   Java API By Example, From Geeks To Geeks.

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


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  * Query.java
16  *
17  * Created on den 23 juli 2002, 01:24
18  */

19 package org.ejbca.util.query;
20
21 import java.util.Date JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.Vector JavaDoc;
24
25 import org.apache.log4j.Logger;
26 import org.ejbca.util.StringTools;
27
28
29
30 /**
31  * A class used to produce advanced querys from the log and user data tables. It's main function is
32  * getQueryString which returns a string which should be placed in the 'WHERE' clause of a SQL
33  * statement.
34  *
35  * @author tomselleck
36  * @version $Id: Query.java,v 1.4.6.1 2007/02/02 09:02:49 anatom Exp $
37  */

38 public class Query implements java.io.Serializable JavaDoc {
39     
40     private static final long serialVersionUID = -1L;
41     
42     private static Logger log = Logger.getLogger(Query.class);
43     // Public Constants.
44
public static final int TYPE_LOGQUERY = 0;
45     public static final int TYPE_USERQUERY = 1;
46     public static final int TYPE_APPROVALQUERY = 2;
47     public static final int CONNECTOR_AND = 0;
48     public static final int CONNECTOR_OR = 1;
49     public static final int CONNECTOR_ANDNOT = 2;
50     public static final int CONNECTOR_ORNOT = 3;
51
52     // Public methods.
53

54     /**
55      * Creates a new instance of Query
56      *
57      * @param type is the typ of query to produce. Should be one of the 'TYPE' constants of this
58      * class.
59      */

60     public Query(int type) {
61         matches = new Vector JavaDoc();
62         connectors = new Vector JavaDoc();
63         this.type = type;
64     }
65
66     /**
67      * Adds a time restraint to the query. Both parameter cannot be null This method should only be
68      * used in ra user queries.
69      *
70      * @param startdate gives the start date of the query or null if it no startdate.
71      * @param enddate gives the end date of the query or null if it no startdate.
72      */

73     public void add(Date JavaDoc startdate, Date JavaDoc enddate) {
74         matches.addElement(new TimeMatch(type, startdate, enddate));
75     }
76
77     /**
78      * Adds a time restraint to the query. Both start and enddate parameters cannot be null This
79      * method should only be used in ra user queries.
80      *
81      * @param matchwith should indicate which field to match with, on of the TimeMatch.MATCH_WITH
82      * constants.
83      * @param startdate gives the start date of the query or null if it no startdate.
84      * @param enddate gives the end date of the query or null if it no startdate.
85      */

86     public void add(int matchwith, Date JavaDoc startdate, Date JavaDoc enddate) {
87         matches.addElement(new TimeMatch(type, matchwith, startdate, enddate));
88     }
89
90     /**
91      * Adds a time restraint and a connector to the query. Both parameter cannot be null. This
92      * method should only be used in log queries.
93      *
94      * @param startdate gives the start date of the query or null if it no startdate.
95      * @param enddate gives the end date of the query or null if it no startdate.
96      * @param connector should be one of the 'CONNECTOR' constants.
97      */

98     public void add(Date JavaDoc startdate, Date JavaDoc enddate, int connector) {
99         matches.addElement(new TimeMatch(type, startdate, enddate));
100         connectors.addElement(new Integer JavaDoc(connector));
101     }
102
103     /**
104      * Adds a time restraint and a connector to the query. Both start and enddate parameters cannot
105      * be null. This method should only be used in ra user queries.
106      *
107      * @param matchwith should indicate which field to match with, on of the TimeMatch.MATCH_WITH
108      * constants.
109      * @param startdate gives the start date of the query or null if it no startdate.
110      * @param enddate gives the end date of the query or null if it no startdate.
111      * @param connector should be one of the 'CONNECTOR' constants.
112      */

113     public void add(int matchwith, Date JavaDoc startdate, Date JavaDoc enddate, int connector) {
114         matches.addElement(new TimeMatch(type, matchwith, startdate, enddate));
115         connectors.addElement(new Integer JavaDoc(connector));
116     }
117
118     /**
119      * Adds a match ot type UserMatch or LogMatch to the query.
120      *
121      * @param matchwith should be one of the the UserMatch.MATCH_WITH or LogMatch.MATCH_WITH
122      * connstants depending on query type.
123      * @param matchtype should be one of BasicMatch.MATCH_TYPE constants.
124      * @param matchvalue should be a string representation to match against.
125      *
126      * @throws NumberFormatException if there is an illegal character in matchvalue string.
127      */

128     public void add(int matchwith, int matchtype, String JavaDoc matchvalue)
129         throws NumberFormatException JavaDoc {
130         switch (this.type) {
131         case TYPE_LOGQUERY:
132             matches.addElement(new LogMatch(matchwith, matchtype, matchvalue));
133             break;
134         case TYPE_USERQUERY:
135             matches.addElement(new UserMatch(matchwith, matchtype, matchvalue));
136             break;
137         case TYPE_APPROVALQUERY:
138             matches.addElement(new ApprovalMatch(matchwith, matchtype, matchvalue));
139             break;
140         }
141         if (StringTools.hasSqlStripChars(matchvalue)) {
142             hasIllegalSqlChars = true;
143         }
144     }
145
146     /**
147      * Adds a match ot type UserMatch or LogMatch ant a connector to the query.
148      *
149      * @param matchwith should be one of the the UserMatch.MATCH_WITH or LogMatch.MATCH_WITH
150      * connstants depending on query type.
151      * @param matchtype should be one of BasicMatch.MATCH_TYPE constants.
152      * @param matchvalue should be a string representation to match against.
153      * @param connector should be one of the 'CONNECTOR' constants.
154      *
155      * @throws NumberFormatException if there is an illegal character in matchvalue string.
156      */

157     public void add(int matchwith, int matchtype, String JavaDoc matchvalue, int connector)
158         throws NumberFormatException JavaDoc {
159         add(matchwith,matchtype,matchvalue);
160         connectors.addElement(new Integer JavaDoc(connector));
161
162  
163     }
164
165     /**
166      * Adds a connector to the query.
167      *
168      * @param connector should be one of the 'CONNECTOR' constants.
169      *
170      * @throws NumberFormatException if there is an illegal character in matchvalue string.
171      */

172     public void add(int connector) {
173         connectors.addElement(new Integer JavaDoc(connector));
174     }
175
176     /**
177      * Gives the string to be used in the 'WHERE' clause int the SQL-statement.
178      *
179      * @return the string to be used in the 'WHERE'-clause.
180      */

181     public String JavaDoc getQueryString() {
182         String JavaDoc returnval = "";
183
184         for (int i = 0; i < (matches.size() - 1); i++) {
185             returnval += ((BasicMatch) matches.elementAt(i)).getQueryString();
186             returnval += CONNECTOR_SQL_NAMES[((Integer JavaDoc) connectors.elementAt(i)).intValue()];
187         }
188
189         returnval += ((BasicMatch) matches.elementAt(matches.size() - 1)).getQueryString();
190
191         return returnval;
192     }
193
194     /**
195      * Checks if the present query is legal by checking if every match is legal and that the number
196      * of connectors is one less than matches.
197      *
198      * @return true if the query is legal, false otherwise
199      */

200     public boolean isLegalQuery() {
201         boolean returnval = true;
202         Iterator JavaDoc i = matches.iterator();
203
204         while (i.hasNext()) {
205             BasicMatch match = (BasicMatch) i.next();
206             returnval = returnval && match.isLegalQuery();
207             if (!returnval) {
208                 log.error("Query is illegal: "+match.getQueryString());
209             }
210         }
211
212         returnval = returnval && ((matches.size() - 1) == connectors.size());
213
214         returnval = returnval && (matches.size() > 0);
215
216         return returnval;
217     }
218
219     /**
220      * Checks if the present query contains illegal SQL string charcters as set by add(String) methods.
221      * The add(String) methods checks against StringTools.hasStripChars.
222      *
223      * @return true if the query is legal, false otherwise
224      * @see org.ejbca.util.StringTools#hasStripChars
225      */

226     public boolean hasIllegalSqlChars() {
227         log.debug("hasIllegalSqlChars: "+hasIllegalSqlChars);
228         return hasIllegalSqlChars;
229     }
230
231     // Private Constants.
232
static final String JavaDoc[] CONNECTOR_SQL_NAMES = { " AND ", " OR ", " AND NOT ", " OR NOT " };
233
234     // Private fields.
235
private Vector JavaDoc matches = null; // Should only contain BasicMatch objects.
236
private Vector JavaDoc connectors = null; // Should only containg CONNECTOR constants.
237
protected int type = 0;
238     private boolean hasIllegalSqlChars = false;
239 }
240
Popular Tags