/* * Created on Nov 2, 2004 * Copyright (C) Andrea Schweer, 2004 * * This file is part of the Greenstone Alerting Service. * Refer to the COPYING file in the base directory of this package * for licensing information. */ package org.greenstone.gsdlas.profiles; import java.sql.*; import java.sql.SQLException; import java.sql.Statement; import java.util.*; import java.util.Set; import java.util.TreeSet; import org.greenstone.gsdlas.database.DatabaseException; import org.greenstone.gsdlas.database.DatabaseManager; import org.greenstone.gsdlas.util.ArrayHelper; /** * @author schweer * * TODO To change the template for this generated type comment go to * Window - Preferences - Java - Code Style - Code Templates */ public abstract class Predicate implements Comparable { protected Set subscriptions = new TreeSet(); protected String field; protected String value; protected int id; public static final String[] singleValueFields = new String[] { "document_title", "document_content", "metadata_has_field", "host_query", "collection_query" }; public static final String[] multiValueFields = new String[] { "type", "hostID", "collectionID", "documentID" }; public void addSubscription(int subscriptionID) { subscriptions.add(new Integer(subscriptionID)); } /* (non-Javadoc) * @see org.greenstone.gsdlas.profiles.Predicate#getSubscriptions() */ public Set getSubscriptionIDs() { return Collections.unmodifiableSet(subscriptions); } public int getID() { return id; } public void setID(int i) { this.id = i; } /* (non-Javadoc) * @see org.greenstone.gsdlas.profiles.Predicate#getKey() */ public String getField() { return field; } /* (non-Javadoc) * @see org.greenstone.gsdlas.profiles.Predicate#getValue() */ public String getValue() { return value; } public String toString() { return field + "=" + value; } public boolean equals(Object other) { if (other == null || !(other instanceof Predicate)) return false; return id == ((Predicate)other).getID(); } public int compareTo(Object other) { Predicate otherPredicate = (Predicate) other; return new Integer(id).compareTo(new Integer(otherPredicate.getID())); } public abstract boolean isSatisfied(Map event); /** * @param key * @return */ public static boolean isSingleValued(String key) { return ArrayHelper.contains(singleValueFields, key); } /** * @param key * @return */ public static boolean isMultiValued(String key) { return ArrayHelper.contains(multiValueFields, key); } /** * @return * @throws DatabaseException * @throws SQLException */ int saveToDatabase() throws SQLException, DatabaseException { String query = "SELECT id FROM predicates " + "WHERE field = '" + field + "' AND value = '" + value + "';"; Statement statement = DatabaseManager.getInstance().getDatabaseConnection().createStatement(); System.out.println(query); ResultSet result = statement.executeQuery(query); if (result.next()) { // predicate already exists in database System.out.println("predicate " + id + " already exists"); return result.getInt("id"); } // predicate has been newly created String insert = "INSERT INTO predicates (field, value) " + "VALUES ('" + field + "', '" + value + "');"; System.out.println(insert); statement.execute(insert); result = statement.executeQuery(query); if (result.next()) { return result.getInt("id"); } else { throw new DatabaseException("could not save predicate"); } } /** * @return */ public List getValueList() { throw new UnsupportedOperationException("you can only call this method for multi-valued predicates"); } /** * @param key * @return */ public static boolean isFieldName(String key) { return ArrayHelper.contains(singleValueFields, key) || ArrayHelper.contains(multiValueFields, key); } }