source: trunk/greenstone3-extensions/gsdl-as/src/org/greenstone/gsdlas/profiles/Predicate.java@ 8867

Last change on this file since 8867 was 8867, checked in by schweer, 19 years ago

fixed BTS 12: restore subscriptions/predicates from database

  • Property svn:keywords set to Author Date Id Revision
File size: 5.0 KB
Line 
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 */
9package org.greenstone.gsdlas.profiles;
10
11import java.sql.*;
12import java.sql.SQLException;
13import java.sql.Statement;
14import java.util.*;
15import java.util.Set;
16import java.util.TreeSet;
17
18import org.greenstone.gsdlas.database.DatabaseException;
19import org.greenstone.gsdlas.database.DatabaseManager;
20import 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 */
28public 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 "hostID",
45 "collectionID",
46 "documentID" };
47
48 public void addSubscription(int subscriptionID) {
49 subscriptions.add(new Integer(subscriptionID));
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(multiValueFields, key);
111 }
112
113 /**
114 * @return
115 * @throws DatabaseException
116 * @throws SQLException
117 */
118 int saveToDatabase() throws SQLException, DatabaseException {
119 String query = "SELECT id FROM predicates " +
120 "WHERE field = '" + field +
121 "' AND value = '" + value +
122 "' AND type = '" + this.getClass().getName() + "';";
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 int id = result.getInt("id");
128 System.out.println("predicate already exists with id " + id);
129 return id;
130 }
131 // predicate has been newly created
132 String insert = "INSERT INTO predicates (field, type, value) " +
133 "VALUES ('" + field + "', '" + this.getClass().getName() + "', '" + value + "');";
134 System.out.println(insert);
135 statement.execute(insert);
136 result = statement.executeQuery(query);
137 if (result.next()) {
138 return result.getInt("id");
139 } else {
140 throw new DatabaseException("could not save predicate");
141 }
142 }
143
144 /**
145 * @param key
146 * @return
147 */
148 public static boolean isFieldName(String key) {
149 return ArrayHelper.contains(singleValueFields, key) || ArrayHelper.contains(multiValueFields, key);
150 }
151
152 /**
153 * @param subID
154 * @throws DatabaseException
155 * @throws SQLException
156 */
157 public void removeSubscription(Integer subID) throws SQLException, DatabaseException {
158 subscriptions.remove(subID);
159
160 Statement statement = DatabaseManager.getInstance().getDatabaseConnection().createStatement();
161 String sqlString = "DELETE FROM subs_to_predicates " +
162 "WHERE subscription = " + subID +
163 " AND predicate = " + id;
164 statement.executeUpdate(sqlString);
165
166 if (subscriptions.isEmpty()) {
167 PredicateFactory.deletePredicate(this);
168 sqlString = "DELETE FROM predicates WHERE id = " + id;
169 statement.executeUpdate(sqlString);
170 }
171 }
172
173}
Note: See TracBrowser for help on using the repository browser.