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

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

some todo and licensing comments

  • Property svn:keywords set to Author Date Id Revision
File size: 4.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 "host",
45 "collection" };
46
47 public void addSubscription(int subscriptionID) {
48 subscriptions.add(new Integer(subscriptionID));
49 }
50
51 /* (non-Javadoc)
52 * @see org.greenstone.gsdlas.profiles.Predicate#getSubscriptions()
53 */
54 public Set getSubscriptionIDs() {
55 return Collections.unmodifiableSet(subscriptions);
56 }
57
58 public int getID() {
59 return id;
60 }
61
62 public void setID(int i) {
63 this.id = i;
64 }
65
66 /* (non-Javadoc)
67 * @see org.greenstone.gsdlas.profiles.Predicate#getKey()
68 */
69 public String getField() {
70 return field;
71 }
72
73 /* (non-Javadoc)
74 * @see org.greenstone.gsdlas.profiles.Predicate#getValue()
75 */
76 public String getValue() {
77 return value;
78 }
79
80 public String toString() {
81 return field + "=" + value;
82 }
83
84 public boolean equals(Object other) {
85 if (other == null || !(other instanceof Predicate)) return false;
86 return id == ((Predicate)other).getID();
87 }
88
89 public int compareTo(Object other) {
90 Predicate otherPredicate = (Predicate) other;
91 return new Integer(id).compareTo(new Integer(otherPredicate.getID()));
92 }
93
94 public abstract boolean isSatisfied(Map event);
95
96 /**
97 * @param key
98 * @return
99 */
100 public static boolean isSingleValued(String key) {
101 return ArrayHelper.contains(singleValueFields, key);
102 }
103
104 /**
105 * @param key
106 * @return
107 */
108 public static boolean isMultiValued(String key) {
109 return ArrayHelper.contains(singleValueFields, key);
110 }
111
112 /**
113 * @return
114 * @throws DatabaseException
115 * @throws SQLException
116 */
117 int saveToDatabase() throws SQLException, DatabaseException {
118 // TODO handle multi-valued predicate (in subclass?)
119 String query = "SELECT id FROM predicates " +
120 "WHERE type='single' AND field = '" + field +
121 "' AND singleValue = '" + value + "';";
122 Statement statement = DatabaseManager.getInstance().getDatabaseConnection().createStatement();
123 System.out.println(query);
124 ResultSet result = statement.executeQuery(query);
125 if (result.next()) { // predicate already exists in database
126 System.out.println("predicate " + id + " already exists");
127 return result.getInt("id");
128 }
129 // predicate has been newly created
130 String insert = "INSERT INTO predicates (type, field, singleValue) " +
131 "VALUES ('single', '" + field + "', '" + value + "');";
132 System.out.println(insert);
133 statement.execute(insert);
134 result = statement.executeQuery(query);
135 if (result.next()) {
136 return result.getInt("id");
137 } else {
138 throw new DatabaseException("could not save predicate");
139 }
140 }
141
142}
Note: See TracBrowser for help on using the repository browser.