1 | /*
|
---|
2 | * Created on Nov 2, 2004
|
---|
3 | * Copyright (C) Andrea Schweer, 2004
|
---|
4 | *
|
---|
5 | * This file is part of the Greenstone Alerting Service.
|
---|
6 | * Refer to the COPYING file in the base directory of this package
|
---|
7 | * for licensing information.
|
---|
8 | */
|
---|
9 | package org.greenstone.gsdlas.profiles;
|
---|
10 |
|
---|
11 | import java.sql.*;
|
---|
12 | import java.sql.SQLException;
|
---|
13 | import java.sql.Statement;
|
---|
14 | import java.util.*;
|
---|
15 | import java.util.Set;
|
---|
16 | import java.util.TreeSet;
|
---|
17 |
|
---|
18 | import org.greenstone.gsdlas.database.DatabaseException;
|
---|
19 | import org.greenstone.gsdlas.database.DatabaseManager;
|
---|
20 | import org.greenstone.gsdlas.util.ArrayHelper;
|
---|
21 |
|
---|
22 | /**
|
---|
23 | * @author schweer
|
---|
24 | *
|
---|
25 | * TODO To change the template for this generated type comment go to
|
---|
26 | * Window - Preferences - Java - Code Style - Code Templates
|
---|
27 | */
|
---|
28 | public abstract class Predicate implements Comparable {
|
---|
29 |
|
---|
30 | protected Set subscriptions = new TreeSet();
|
---|
31 | protected String field;
|
---|
32 | protected String value;
|
---|
33 | protected int id;
|
---|
34 |
|
---|
35 |
|
---|
36 | public static final String[] singleValueFields = new String[] {
|
---|
37 | "document_title",
|
---|
38 | "document_content",
|
---|
39 | "metadata_has_field",
|
---|
40 | "host_query",
|
---|
41 | "collection_query" };
|
---|
42 | public static final String[] multiValueFields = new String[] {
|
---|
43 | "type",
|
---|
44 | "host",
|
---|
45 | "collection" };
|
---|
46 |
|
---|
47 | public void addSubscription(int subscriptionID) {
|
---|
48 | subscriptions.add(new Integer(subscriptionID));
|
---|
49 | // TODO save to db
|
---|
50 | }
|
---|
51 |
|
---|
52 | /* (non-Javadoc)
|
---|
53 | * @see org.greenstone.gsdlas.profiles.Predicate#getSubscriptions()
|
---|
54 | */
|
---|
55 | public Set getSubscriptionIDs() {
|
---|
56 | return Collections.unmodifiableSet(subscriptions);
|
---|
57 | }
|
---|
58 |
|
---|
59 | public int getID() {
|
---|
60 | return id;
|
---|
61 | }
|
---|
62 |
|
---|
63 | public void setID(int i) {
|
---|
64 | this.id = i;
|
---|
65 | }
|
---|
66 |
|
---|
67 | /* (non-Javadoc)
|
---|
68 | * @see org.greenstone.gsdlas.profiles.Predicate#getKey()
|
---|
69 | */
|
---|
70 | public String getField() {
|
---|
71 | return field;
|
---|
72 | }
|
---|
73 |
|
---|
74 | /* (non-Javadoc)
|
---|
75 | * @see org.greenstone.gsdlas.profiles.Predicate#getValue()
|
---|
76 | */
|
---|
77 | public String getValue() {
|
---|
78 | return value;
|
---|
79 | }
|
---|
80 |
|
---|
81 | public String toString() {
|
---|
82 | return field + "=" + value;
|
---|
83 | }
|
---|
84 |
|
---|
85 | public boolean equals(Object other) {
|
---|
86 | if (other == null || !(other instanceof Predicate)) return false;
|
---|
87 | return id == ((Predicate)other).getID();
|
---|
88 | }
|
---|
89 |
|
---|
90 | public int compareTo(Object other) {
|
---|
91 | Predicate otherPredicate = (Predicate) other;
|
---|
92 | return new Integer(id).compareTo(new Integer(otherPredicate.getID()));
|
---|
93 | }
|
---|
94 |
|
---|
95 | public abstract boolean isSatisfied(Map event);
|
---|
96 |
|
---|
97 | /**
|
---|
98 | * @param key
|
---|
99 | * @return
|
---|
100 | */
|
---|
101 | public static boolean isSingleValued(String key) {
|
---|
102 | return ArrayHelper.contains(singleValueFields, key);
|
---|
103 | }
|
---|
104 |
|
---|
105 | /**
|
---|
106 | * @param key
|
---|
107 | * @return
|
---|
108 | */
|
---|
109 | public static boolean isMultiValued(String key) {
|
---|
110 | return ArrayHelper.contains(singleValueFields, key);
|
---|
111 | }
|
---|
112 |
|
---|
113 | /**
|
---|
114 | * @return
|
---|
115 | * @throws DatabaseException
|
---|
116 | * @throws SQLException
|
---|
117 | */
|
---|
118 | int saveToDatabase() throws SQLException, DatabaseException {
|
---|
119 | // TODO handle multi-valued predicate (in subclass?)
|
---|
120 | String query = "SELECT id FROM predicates " +
|
---|
121 | "WHERE type='single' AND field = '" + field +
|
---|
122 | "' AND singleValue = '" + value + "';";
|
---|
123 | Statement statement = DatabaseManager.getInstance().getDatabaseConnection().createStatement();
|
---|
124 | System.out.println(query);
|
---|
125 | ResultSet result = statement.executeQuery(query);
|
---|
126 | if (result.next()) { // predicate already exists in database
|
---|
127 | System.out.println("predicate " + id + " already exists");
|
---|
128 | return result.getInt("id");
|
---|
129 | }
|
---|
130 | // predicate has been newly created
|
---|
131 | String insert = "INSERT INTO predicates (type, field, singleValue) " +
|
---|
132 | "VALUES ('single', '" + field + "', '" + value + "');";
|
---|
133 | System.out.println(insert);
|
---|
134 | statement.execute(insert);
|
---|
135 | result = statement.executeQuery(query);
|
---|
136 | if (result.next()) {
|
---|
137 | return result.getInt("id");
|
---|
138 | } else {
|
---|
139 | throw new DatabaseException("could not save predicate");
|
---|
140 | }
|
---|
141 | }
|
---|
142 |
|
---|
143 | }
|
---|