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: /**
015: * A class used by Query class to build a query for ejbca log module. Inherits BasicMatch. Main
016: * function is getQueryString which returns a fragment of SQL statment.
017: *
018: * @author TomSelleck
019: * @version $Id: ApprovalMatch.java,v 1.3 2007/05/04 09:06:39 anatom Exp $
020: *
021: * @see org.ejbca.util.query.BasicMatch
022: * @see org.ejbca.util.query.TimeMatch
023: * @see org.ejbca.util.query.ApprovalMatch
024: */
025: public class ApprovalMatch extends BasicMatch {
026: // Public Constants
027:
028: public static final int MATCH_WITH_UNIQUEID = 0;
029: public static final int MATCH_WITH_APPROVALID = 1;
030: public static final int MATCH_WITH_APPROVALTYPE = 2;
031: public static final int MATCH_WITH_ENDENTITYPROFILEID = 3;
032: public static final int MATCH_WITH_CAID = 4;
033: public static final int MATCH_WITH_REQUESTADMINCERTISSUERDN = 5;
034: public static final int MATCH_WITH_REQUESTADMINCERTSERIALNUMBER = 6;
035: public static final int MATCH_WITH_STATUS = 7;
036: public static final int MATCH_WITH_REMAININGAPPROVALS = 8;
037:
038: // Private Constants.
039: static final String[] MATCH_WITH_SQLNAMES = { "id", "approvalid",
040: "approvaltype", "endentityprofileid", "caid",
041: "reqadmincertissuerdn", "reqadmincertsn", "status",
042: "remainingapprovals" }; // Represents the column names in approvals table.
043:
044: // Public methods.
045:
046: /**
047: * Creates a new instance of LogMatch.
048: *
049: * @param matchwith determines which field i logentry table to match with.
050: * @param matchtype determines how to match the field..
051: * @param matchvalue the value to match with.
052: *
053: * @throws NumberFormatException if matchvalue constains illegal numbervalue when matching
054: * number field.
055: */
056: public ApprovalMatch(int matchwith, int matchtype, String matchvalue)
057: throws NumberFormatException {
058: this .matchwith = matchwith;
059: this .matchtype = matchtype;
060: this .matchvalue = matchvalue;
061:
062: // The row below does not do anthing but check that matchvalue contains
063: // a legal number value when matching number field. See @throws clause.
064: if (matchwith != MATCH_WITH_REQUESTADMINCERTISSUERDN
065: && matchwith != MATCH_WITH_REQUESTADMINCERTSERIALNUMBER) {
066: new Integer(matchvalue);
067: }
068: }
069:
070: /**
071: * Returns a SQL statement fragment from the given data.
072: *
073: * @return DOCUMENT ME!
074: */
075: public String getQueryString() {
076: String returnval = "";
077:
078: if (matchtype == BasicMatch.MATCH_TYPE_EQUALS) {
079: // Because some databases (read JavaDB/Derby) does not allow matching of integer with a string expression
080: // like "where status='10'" instead of "where status=10", we have to hav e some special handling here.
081: String stringChar = "'";
082: if ((matchwith >= MATCH_WITH_UNIQUEID && matchwith <= MATCH_WITH_CAID)
083: || (matchwith == MATCH_WITH_STATUS)
084: || (matchwith == MATCH_WITH_REMAININGAPPROVALS)) {
085: stringChar = "";
086: }
087: returnval = MATCH_WITH_SQLNAMES[matchwith] + " = "
088: + stringChar + matchvalue + stringChar;
089: }
090:
091: if (matchtype == BasicMatch.MATCH_TYPE_BEGINSWITH) {
092: returnval = MATCH_WITH_SQLNAMES[matchwith] + " LIKE '"
093: + matchvalue + "%'";
094: }
095:
096: if (matchtype == BasicMatch.MATCH_TYPE_CONTAINS) {
097: returnval = MATCH_WITH_SQLNAMES[matchwith] + " LIKE '%"
098: + matchvalue + "%'";
099: }
100:
101: return returnval;
102: }
103:
104: // getQueryString
105:
106: /**
107: * Checks if query data is ok.
108: *
109: * @return DOCUMENT ME!
110: */
111: public boolean isLegalQuery() {
112: return !(matchvalue.trim().equals(""));
113: }
114:
115: // Private Methods
116: // Private Fields.
117: private int matchwith;
118: private int matchtype;
119: private String matchvalue;
120: }
|