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

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

started to work on multi-valued predicates; started to rework the filter algorithm

  • Property svn:keywords set to Author Date Id Revision
File size: 5.6 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
34 // query -> QueryPredicates referring to it
35 private static Map documentTitleQueryPredicates = new TreeMap();
36 private static Map documentContentQueryPredicates = new TreeMap();
37
38
39 /**
40 * @param key
41 * @param value
42 * @return
43 * @throws ParseException
44 * @throws DatabaseException
45 * @throws SQLException
46 */
47 public static Predicate createPredicate(String key, String value)
48 throws SQLException, DatabaseException {
49 System.out.println("creating predicate for key " + key + " and value " + value);
50 if (!Predicate.isSingleValued(key)) {
51 return null;
52 }
53 Predicate result = null;
54 if (value == null || value.length() == 0) {
55 return null;
56 }
57
58 if (Predicate.isMultiValued(key)) {
59 List list = new ArrayList();
60 list.add(value);
61 return createPredicate(key, list);
62 } else if (key.startsWith("document")) {
63 result = createQueryPredicate(key, value);
64 } else if (key.endsWith("_name")) {
65 result = createSubstringMatchPredicate(key, value);
66 } else {
67 return null;
68 }
69 result.setID(result.saveToDatabase());
70 return result;
71 }
72
73 public static Predicate createPredicate(String key, List values) throws SQLException, DatabaseException {
74 if (!Predicate.isMultiValued(key)) {
75 return null;
76 }
77 Predicate result = createIdEqualsPredicate(key, values);
78 result.setID(result.saveToDatabase());
79 return result;
80 }
81
82 /**
83 * @param key
84 * @param value
85 * @return
86 */
87 private static SubstringMatchPredicate createSubstringMatchPredicate(String key, String value) {
88 SubstringMatchPredicate predicate = null;
89 if (key.equals("collection_name")) {
90 if (collectionNameMatchPredicates.containsKey(value)) {
91 predicate = (SubstringMatchPredicate) collectionNameMatchPredicates.get(value);
92 } else {
93 predicate = new SubstringMatchPredicate(key, value);
94 collectionNameMatchPredicates.put(value, predicate);
95 }
96 } // TODO other fields than collection_name
97 return predicate;
98 }
99
100 /**
101 * @param key
102 * @param values
103 * @return
104 */
105 private static IdEqualsPredicate createIdEqualsPredicate(String key, List values) {
106 IdEqualsPredicate predicate = null;
107
108 Map keyToPredicates;
109 if (!idEqualsPredicates.containsKey(key)) {
110 keyToPredicates = new HashMap();
111 idEqualsPredicates.put(key, keyToPredicates);
112 }
113 keyToPredicates = (Map) idEqualsPredicates.get(key);
114
115 for (Iterator iter = values.iterator(); iter.hasNext();) {
116 String value = (String) iter.next();
117 if (keyToPredicates.containsKey(value)) {
118 predicate = (IdEqualsPredicate) keyToPredicates.get(value);
119 } else {
120 predicate = new IdEqualsPredicate(key, value);
121 keyToPredicates.put(value, predicate);
122 }
123 }
124 return predicate;
125 }
126
127 /**
128 * @param field
129 * @param query
130 * @return
131 */
132 private static QueryPredicate createQueryPredicate(String field,
133 String query) {
134 QueryPredicate predicate = new QueryPredicate(field, query);
135 if (field.equals("document_content")) {
136 documentContentQueryPredicates.put(query, predicate);
137 } else if (field.equals("document_title")) {
138 documentTitleQueryPredicates.put(query, predicate);
139 }
140 return predicate;
141 }
142
143 public static Collection getAllQueryPredicates() {
144 Set result = new TreeSet();
145 result.addAll(documentTitleQueryPredicates.values());
146 result.addAll(documentContentQueryPredicates.values());
147 return Collections.unmodifiableSet(result);
148 }
149
150 public static Collection getQueryPredicates(String field) {
151 Set result = new TreeSet();
152 if (field.equals("document_title")) {
153 result.addAll(documentTitleQueryPredicates.values());
154 } else if (field.equals("document_content")) {
155 result.addAll(documentContentQueryPredicates.values());
156 }
157 return Collections.unmodifiableSet(result);
158 }
159
160 public static Collection getAllSubstringMatchPredicates() {
161 return Collections.unmodifiableCollection(collectionNameMatchPredicates.values());
162 }
163
164 /**
165 * @param string
166 * @param string2
167 * @return
168 */
169 public static Predicate getIdEqualsPredicate(String key, String value) {
170 return (Predicate) ((Map)idEqualsPredicates.get(key)).get(value);
171 }
172
173}
Note: See TracBrowser for help on using the repository browser.