1 | /*
|
---|
2 | * Created on Nov 1, 2004
|
---|
3 | * Copyright (C) 2004, 2005 Andrea Schweer
|
---|
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.SQLException;
|
---|
12 | import java.util.*;
|
---|
13 | import java.util.Map;
|
---|
14 | import java.util.TreeMap;
|
---|
15 |
|
---|
16 | import org.greenstone.gsdlas.Constants;
|
---|
17 | import org.greenstone.gsdlas.database.DatabaseException;
|
---|
18 |
|
---|
19 |
|
---|
20 | /**
|
---|
21 | * @author schweer
|
---|
22 | *
|
---|
23 | * TODO To change the template for this generated type comment go to Window -
|
---|
24 | * Preferences - Java - Code Style - Code Templates
|
---|
25 | */
|
---|
26 | public abstract class PredicateFactory {
|
---|
27 |
|
---|
28 | private static int id = 0;
|
---|
29 |
|
---|
30 | private static Map equalsPredicates = new TreeMap();
|
---|
31 |
|
---|
32 | // substring for collection_name -> SubstringMatchPredicates referring to it
|
---|
33 | private static Map collectionNameMatchPredicates = new TreeMap();
|
---|
34 | private static Map hostNameMatchPredicates = new TreeMap();
|
---|
35 |
|
---|
36 | // query -> QueryPredicates referring to it
|
---|
37 | private static Map documentTitleQueryPredicates = new TreeMap();
|
---|
38 | private static Map documentContentQueryPredicates = new TreeMap();
|
---|
39 |
|
---|
40 |
|
---|
41 | /**
|
---|
42 | * @param key
|
---|
43 | * @param value
|
---|
44 | * @return
|
---|
45 | * @throws ParseException
|
---|
46 | * @throws DatabaseException
|
---|
47 | * @throws SQLException
|
---|
48 | */
|
---|
49 | public static Predicate createPredicate(String key, String value)
|
---|
50 | throws SQLException, DatabaseException {
|
---|
51 | System.out.println("creating predicate for key " + key + " and value " + value);
|
---|
52 | if (!Predicate.isSingleValued(key)) {
|
---|
53 | return null;
|
---|
54 | }
|
---|
55 | Predicate result = null;
|
---|
56 | if (value == null || value.length() == 0) {
|
---|
57 | return null;
|
---|
58 | }
|
---|
59 |
|
---|
60 | if (Predicate.isMultiValued(key)) {
|
---|
61 | List list = new ArrayList();
|
---|
62 | list.add(value);
|
---|
63 | return (Predicate) createPredicates(key, list).get(0);
|
---|
64 | } else if (key.startsWith("document")) {
|
---|
65 | result = createQueryPredicate(key, value);
|
---|
66 | } else if (key.endsWith("_query")) {
|
---|
67 | result = createSubstringMatchPredicate(key, value);
|
---|
68 | } else {
|
---|
69 | return null;
|
---|
70 | }
|
---|
71 | result.setID(result.saveToDatabase());
|
---|
72 | return result;
|
---|
73 | }
|
---|
74 |
|
---|
75 | public static List createPredicates(String key, List values) throws SQLException, DatabaseException {
|
---|
76 | if (!Predicate.isMultiValued(key)) {
|
---|
77 | return null;
|
---|
78 | }
|
---|
79 | List results = createEqualsPredicates(key, values);
|
---|
80 | for (Iterator iter = results.iterator(); iter.hasNext();) {
|
---|
81 | EqualsPredicate pred = (EqualsPredicate) iter.next();
|
---|
82 |
|
---|
83 | pred.setID(pred.saveToDatabase());
|
---|
84 | }
|
---|
85 | return results;
|
---|
86 | }
|
---|
87 |
|
---|
88 | /**
|
---|
89 | * @param key
|
---|
90 | * @param value
|
---|
91 | * @return
|
---|
92 | */
|
---|
93 | private static SubstringMatchPredicate createSubstringMatchPredicate(String key, String value) {
|
---|
94 | SubstringMatchPredicate predicate = null;
|
---|
95 | if (key.equals(Constants.COLLECTION_QUERY_FIELD)) {
|
---|
96 | if (collectionNameMatchPredicates.containsKey(value)) {
|
---|
97 | predicate = (SubstringMatchPredicate) collectionNameMatchPredicates.get(value);
|
---|
98 | } else {
|
---|
99 | predicate = new SubstringMatchPredicate(Constants.COLLECTION_ID_FIELD, value);
|
---|
100 | collectionNameMatchPredicates.put(value, predicate);
|
---|
101 | }
|
---|
102 | } else if (key.equals(Constants.HOST_QUERY_FIELD)) {
|
---|
103 | if (hostNameMatchPredicates.containsKey(value)) {
|
---|
104 | predicate = (SubstringMatchPredicate) hostNameMatchPredicates.get(value);
|
---|
105 | } else {
|
---|
106 | predicate = new SubstringMatchPredicate(Constants.HOST_ID_FIELD, value);
|
---|
107 | hostNameMatchPredicates.put(value, predicate);
|
---|
108 | }
|
---|
109 | }
|
---|
110 | // TODO other fields?
|
---|
111 | return predicate;
|
---|
112 | }
|
---|
113 |
|
---|
114 | /**
|
---|
115 | * @param key
|
---|
116 | * @param values
|
---|
117 | * @return
|
---|
118 | */
|
---|
119 | private static List createEqualsPredicates(String key, List values) {
|
---|
120 | List result = new Vector();
|
---|
121 |
|
---|
122 | Map keyToPredicates;
|
---|
123 | if (!equalsPredicates.containsKey(key)) {
|
---|
124 | keyToPredicates = new HashMap();
|
---|
125 | equalsPredicates.put(key, keyToPredicates);
|
---|
126 | }
|
---|
127 | keyToPredicates = (Map) equalsPredicates.get(key);
|
---|
128 |
|
---|
129 | for (Iterator iter = values.iterator(); iter.hasNext();) {
|
---|
130 | String value = (String) iter.next();
|
---|
131 | if (value == null || value.length() == 0) {
|
---|
132 | continue;
|
---|
133 | }
|
---|
134 |
|
---|
135 | if (key.equals(Constants.HOST_ID_FIELD) && !(value.startsWith("http://"))) {
|
---|
136 | value = "http://" + value + ":8080/soap/servlet/rpcrouter";
|
---|
137 | }
|
---|
138 |
|
---|
139 | EqualsPredicate predicate = null;
|
---|
140 | if (keyToPredicates.containsKey(value)) {
|
---|
141 | predicate = (EqualsPredicate) keyToPredicates.get(value);
|
---|
142 | } else {
|
---|
143 | predicate = new EqualsPredicate(key, value);
|
---|
144 | keyToPredicates.put(value, predicate);
|
---|
145 | }
|
---|
146 | result.add(predicate);
|
---|
147 | }
|
---|
148 | return result;
|
---|
149 | }
|
---|
150 |
|
---|
151 | /**
|
---|
152 | * @param field
|
---|
153 | * @param query
|
---|
154 | * @return
|
---|
155 | */
|
---|
156 | private static QueryPredicate createQueryPredicate(String field,
|
---|
157 | String query) {
|
---|
158 | QueryPredicate predicate = null;
|
---|
159 | if (field.equals(Constants.DOCUMENT_CONTENT_FIELD)) {
|
---|
160 | if (!documentContentQueryPredicates.containsKey(query)) {
|
---|
161 | predicate = new QueryPredicate(field, query);
|
---|
162 | documentContentQueryPredicates.put(query, predicate);
|
---|
163 | }
|
---|
164 | return (QueryPredicate) documentContentQueryPredicates.get(query);
|
---|
165 | } else if (field.equals(Constants.DOCUMENT_TITLE_FIELD)) {
|
---|
166 | if (!documentTitleQueryPredicates.containsKey(query)) {
|
---|
167 | predicate = new QueryPredicate(field, query);
|
---|
168 | documentTitleQueryPredicates.put(query, predicate);
|
---|
169 | }
|
---|
170 | return (QueryPredicate) documentTitleQueryPredicates.get(query);
|
---|
171 | }
|
---|
172 | return predicate;
|
---|
173 | }
|
---|
174 |
|
---|
175 | public static Collection getAllQueryPredicates() {
|
---|
176 | Set result = new TreeSet();
|
---|
177 | result.addAll(documentTitleQueryPredicates.values());
|
---|
178 | result.addAll(documentContentQueryPredicates.values());
|
---|
179 | return Collections.unmodifiableSet(result);
|
---|
180 | }
|
---|
181 |
|
---|
182 | public static Collection getQueryPredicates(String field) {
|
---|
183 | Set result = new TreeSet();
|
---|
184 | if (field.equals(Constants.DOCUMENT_TITLE_FIELD)) {
|
---|
185 | result.addAll(documentTitleQueryPredicates.values());
|
---|
186 | } else if (field.equals(Constants.DOCUMENT_CONTENT_FIELD)) {
|
---|
187 | result.addAll(documentContentQueryPredicates.values());
|
---|
188 | }
|
---|
189 | return Collections.unmodifiableSet(result);
|
---|
190 | }
|
---|
191 |
|
---|
192 | public static Collection getAllSubstringMatchPredicates() {
|
---|
193 | Collection result = new Vector();
|
---|
194 | result.addAll(hostNameMatchPredicates.values());
|
---|
195 | result.addAll(collectionNameMatchPredicates.values());
|
---|
196 | return Collections.unmodifiableCollection(result);
|
---|
197 | }
|
---|
198 |
|
---|
199 | /**
|
---|
200 | * @param string
|
---|
201 | * @param string2
|
---|
202 | * @return
|
---|
203 | */
|
---|
204 | public static EqualsPredicate getEqualsPredicate(String key, String value) {
|
---|
205 | EqualsPredicate result = null;
|
---|
206 | if (equalsPredicates.containsKey(key)) {
|
---|
207 | result = (EqualsPredicate) ((Map)equalsPredicates.get(key)).get(value);
|
---|
208 | }
|
---|
209 | return result;
|
---|
210 | }
|
---|
211 |
|
---|
212 | /**
|
---|
213 | * @param predicate
|
---|
214 | */
|
---|
215 | public static void deletePredicate(Predicate predicate) {
|
---|
216 | String field = predicate.getField();
|
---|
217 | String value = predicate.getValue();
|
---|
218 |
|
---|
219 | if (predicate instanceof EqualsPredicate) {
|
---|
220 | Map map = (Map) equalsPredicates.get(field);
|
---|
221 | if (map.containsKey(value)) {
|
---|
222 | map.remove(value);
|
---|
223 | }
|
---|
224 | } else if (predicate instanceof QueryPredicate) {
|
---|
225 | if (field.equals(Constants.DOCUMENT_TITLE_FIELD)) {
|
---|
226 | documentTitleQueryPredicates.remove(value);
|
---|
227 | } else if (field.equals(Constants.DOCUMENT_CONTENT_FIELD)) {
|
---|
228 | documentContentQueryPredicates.remove(value);
|
---|
229 | } else {
|
---|
230 | System.err.println("problem while removing: unknown field " + field);
|
---|
231 | }
|
---|
232 | } else if (predicate instanceof SubstringMatchPredicate) {
|
---|
233 | if (field.equals(Constants.HOST_ID_FIELD)) {
|
---|
234 | hostNameMatchPredicates.remove(value);
|
---|
235 | } else if (field.equals(Constants.COLLECTION_ID_FIELD)) {
|
---|
236 | collectionNameMatchPredicates.remove(value);
|
---|
237 | } else {
|
---|
238 | System.err.println("problem while removing: unknown field " + field);
|
---|
239 | }
|
---|
240 | } else {
|
---|
241 | System.err.println("problem while removing: unknown class " + predicate.getClass().getName());
|
---|
242 | }
|
---|
243 | }
|
---|
244 |
|
---|
245 | } |
---|