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

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

substring match for host/collection works

  • Property svn:keywords set to Author Date Id Revision
File size: 7.2 KB
Line 
1/*
2 * Created on Nov 1, 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.SQLException;
12import java.util.*;
13import java.util.Map;
14import java.util.TreeMap;
15
16import org.greenstone.gsdlas.database.DatabaseException;
17
18
19/**
20 * @author schweer
21 *
22 * TODO To change the template for this generated type comment go to Window -
23 * Preferences - Java - Code Style - Code Templates
24 */
25public abstract class PredicateFactory {
26
27 private static int id = 0;
28
29 private static Map idEqualsPredicates = new TreeMap();
30
31 // substring for collection_name -> SubstringMatchPredicates referring to it
32 private static Map collectionNameMatchPredicates = new TreeMap();
33 private static Map hostNameMatchPredicates = new TreeMap();
34
35 // query -> QueryPredicates referring to it
36 private static Map documentTitleQueryPredicates = new TreeMap();
37 private static Map documentContentQueryPredicates = new TreeMap();
38
39
40 /**
41 * @param key
42 * @param value
43 * @return
44 * @throws ParseException
45 * @throws DatabaseException
46 * @throws SQLException
47 */
48 public static Predicate createPredicate(String key, String value)
49 throws SQLException, DatabaseException {
50 System.out.println("creating predicate for key " + key + " and value " + value);
51 if (!Predicate.isSingleValued(key)) {
52 return null;
53 }
54 Predicate result = null;
55 if (value == null || value.length() == 0) {
56 return null;
57 }
58
59 if (Predicate.isMultiValued(key)) {
60 List list = new ArrayList();
61 list.add(value);
62 return (Predicate) createPredicates(key, list).get(0);
63 } else if (key.startsWith("document")) {
64 result = createQueryPredicate(key, value);
65 } else if (key.endsWith("_query")) {
66 result = createSubstringMatchPredicate(key, value);
67 } else {
68 return null;
69 }
70 result.setID(result.saveToDatabase());
71 return result;
72 }
73
74 public static List createPredicates(String key, List values) throws SQLException, DatabaseException {
75 if (!Predicate.isMultiValued(key)) {
76 return null;
77 }
78 List results = createIdEqualsPredicates(key, values);
79 for (Iterator iter = results.iterator(); iter.hasNext();) {
80 IdEqualsPredicate pred = (IdEqualsPredicate) iter.next();
81
82 pred.setID(pred.saveToDatabase());
83 }
84 return results;
85 }
86
87 /**
88 * @param key
89 * @param value
90 * @return
91 */
92 private static SubstringMatchPredicate createSubstringMatchPredicate(String key, String value) {
93 SubstringMatchPredicate predicate = null;
94 if (key.equals("collection_query")) {
95 if (collectionNameMatchPredicates.containsKey(value)) {
96 predicate = (SubstringMatchPredicate) collectionNameMatchPredicates.get(value);
97 } else {
98 predicate = new SubstringMatchPredicate("collectionID", value);
99 collectionNameMatchPredicates.put(value, predicate);
100 }
101 } else if (key.equals("host_query")) {
102 if (hostNameMatchPredicates.containsKey(value)) {
103 predicate = (SubstringMatchPredicate) hostNameMatchPredicates.get(value);
104 } else {
105 predicate = new SubstringMatchPredicate("hostID", value);
106 hostNameMatchPredicates.put(value, predicate);
107 }
108 }
109 // TODO other fields?
110 return predicate;
111 }
112
113 /**
114 * @param key
115 * @param values
116 * @return
117 */
118 private static List createIdEqualsPredicates(String key, List values) {
119 List result = new Vector();
120
121 Map keyToPredicates;
122 if (!idEqualsPredicates.containsKey(key)) {
123 keyToPredicates = new HashMap();
124 idEqualsPredicates.put(key, keyToPredicates);
125 }
126 keyToPredicates = (Map) idEqualsPredicates.get(key);
127
128 for (Iterator iter = values.iterator(); iter.hasNext();) {
129 String value = (String) iter.next();
130 if (value == null || value.length() == 0) {
131 continue;
132 }
133
134 if (key.equals("hostID") && !(value.startsWith("http://"))) {
135 value = "http://" + value + ":8080/soap/servlet/rpcrouter";
136 }
137
138 IdEqualsPredicate predicate = null;
139 if (keyToPredicates.containsKey(value)) {
140 predicate = (IdEqualsPredicate) keyToPredicates.get(value);
141 } else {
142 predicate = new IdEqualsPredicate(key, value);
143 keyToPredicates.put(value, predicate);
144 }
145 result.add(predicate);
146 }
147 return result;
148 }
149
150 /**
151 * @param field
152 * @param query
153 * @return
154 */
155 private static QueryPredicate createQueryPredicate(String field,
156 String query) {
157 QueryPredicate predicate = null;
158 if (field.equals("document_content")) {
159 if (!documentContentQueryPredicates.containsKey(query)) {
160 predicate = new QueryPredicate(field, query);
161 documentContentQueryPredicates.put(query, predicate);
162 }
163 return (QueryPredicate) documentContentQueryPredicates.get(query);
164 } else if (field.equals("document_title")) {
165 if (!documentTitleQueryPredicates.containsKey(query)) {
166 predicate = new QueryPredicate(field, query);
167 documentTitleQueryPredicates.put(query, predicate);
168 }
169 return (QueryPredicate) documentTitleQueryPredicates.get(query);
170 }
171 return predicate;
172 }
173
174 public static Collection getAllQueryPredicates() {
175 Set result = new TreeSet();
176 result.addAll(documentTitleQueryPredicates.values());
177 result.addAll(documentContentQueryPredicates.values());
178 return Collections.unmodifiableSet(result);
179 }
180
181 public static Collection getQueryPredicates(String field) {
182 Set result = new TreeSet();
183 if (field.equals("document_title")) {
184 result.addAll(documentTitleQueryPredicates.values());
185 } else if (field.equals("document_content")) {
186 result.addAll(documentContentQueryPredicates.values());
187 }
188 return Collections.unmodifiableSet(result);
189 }
190
191 public static Collection getAllSubstringMatchPredicates() {
192 Collection result = new Vector();
193 result.addAll(hostNameMatchPredicates.values());
194 result.addAll(collectionNameMatchPredicates.values());
195 return Collections.unmodifiableCollection(result);
196 }
197
198 /**
199 * @param string
200 * @param string2
201 * @return
202 */
203 public static IdEqualsPredicate getIdEqualsPredicate(String key, String value) {
204 IdEqualsPredicate result = null;
205 if (idEqualsPredicates.containsKey(key)) {
206 result = (IdEqualsPredicate) ((Map)idEqualsPredicates.get(key)).get(value);
207 }
208 return result;
209 }
210
211}
Note: See TracBrowser for help on using the repository browser.