1 13 14 19 package org.ejbca.util.query; 20 21 import java.util.Date ; 22 import java.util.Iterator ; 23 import java.util.Vector ; 24 25 import org.apache.log4j.Logger; 26 import org.ejbca.util.StringTools; 27 28 29 30 38 public class Query implements java.io.Serializable { 39 40 private static final long serialVersionUID = -1L; 41 42 private static Logger log = Logger.getLogger(Query.class); 43 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 54 60 public Query(int type) { 61 matches = new Vector (); 62 connectors = new Vector (); 63 this.type = type; 64 } 65 66 73 public void add(Date startdate, Date enddate) { 74 matches.addElement(new TimeMatch(type, startdate, enddate)); 75 } 76 77 86 public void add(int matchwith, Date startdate, Date enddate) { 87 matches.addElement(new TimeMatch(type, matchwith, startdate, enddate)); 88 } 89 90 98 public void add(Date startdate, Date enddate, int connector) { 99 matches.addElement(new TimeMatch(type, startdate, enddate)); 100 connectors.addElement(new Integer (connector)); 101 } 102 103 113 public void add(int matchwith, Date startdate, Date enddate, int connector) { 114 matches.addElement(new TimeMatch(type, matchwith, startdate, enddate)); 115 connectors.addElement(new Integer (connector)); 116 } 117 118 128 public void add(int matchwith, int matchtype, String matchvalue) 129 throws NumberFormatException { 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 157 public void add(int matchwith, int matchtype, String matchvalue, int connector) 158 throws NumberFormatException { 159 add(matchwith,matchtype,matchvalue); 160 connectors.addElement(new Integer (connector)); 161 162 163 } 164 165 172 public void add(int connector) { 173 connectors.addElement(new Integer (connector)); 174 } 175 176 181 public String getQueryString() { 182 String 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 ) connectors.elementAt(i)).intValue()]; 187 } 188 189 returnval += ((BasicMatch) matches.elementAt(matches.size() - 1)).getQueryString(); 190 191 return returnval; 192 } 193 194 200 public boolean isLegalQuery() { 201 boolean returnval = true; 202 Iterator 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 226 public boolean hasIllegalSqlChars() { 227 log.debug("hasIllegalSqlChars: "+hasIllegalSqlChars); 228 return hasIllegalSqlChars; 229 } 230 231 static final String [] CONNECTOR_SQL_NAMES = { " AND ", " OR ", " AND NOT ", " OR NOT " }; 233 234 private Vector matches = null; private Vector connectors = null; protected int type = 0; 238 private boolean hasIllegalSqlChars = false; 239 } 240 | Popular Tags |