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

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

putting the alerting service servlet under version control

  • Property svn:keywords set to Author Date Id Revision
File size: 4.9 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.util.*;
12import java.util.Map;
13import java.util.TreeMap;
14
15
16/**
17 * @author schweer
18 *
19 * TODO To change the template for this generated type comment go to Window -
20 * Preferences - Java - Code Style - Code Templates
21 */
22public abstract class PredicateFactory {
23
24 private static int id = 0;
25
26 // documentID -> IdEqualsPredicate referring to it
27 private static Map documentIdEqualsPredicates = new TreeMap();
28 // collectionID -> IdEqualsPredicate referring to it
29 private static Map collectionIdEqualsPredicates = new TreeMap();
30
31 // substring for collection_name -> SubstringMatchPredicate 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 */
45 public static Predicate createPredicate(String key, String value)
46 throws ParseException {
47 Predicate result = null;
48 if (key.equals("type")) {
49 return result;
50 } else if (key.endsWith("ID")) {
51 result = createIdEqualsPredicate(key, value);
52 } else if (key.startsWith("document")) {
53 result = createQueryPredicate(key, value);
54 } else if (key.endsWith("_name")) {
55 result = createSubstringMatchPredicate(key, value);
56 } else {
57 throw new ParseException("value " + value + " for field " + key
58 + " does not lead to a parseable predicate");
59 }
60 result.setID(id++);
61 return result;
62 }
63
64 /**
65 * @param key
66 * @param value
67 * @return
68 */
69 private static SubstringMatchPredicate createSubstringMatchPredicate(String key, String value) {
70 SubstringMatchPredicate predicate = null;
71 if (key.equals("collection_name")) {
72 if (collectionNameMatchPredicates.containsKey(value)) {
73 predicate = (SubstringMatchPredicate) collectionNameMatchPredicates.get(value);
74 } else {
75 predicate = new SubstringMatchPredicate(key, value);
76 collectionNameMatchPredicates.put(value, predicate);
77 }
78 } // TODO other fields than collection_name
79 return predicate;
80 }
81
82 /**
83 * @param key
84 * @param value
85 * @return
86 */
87 private static IdEqualsPredicate createIdEqualsPredicate(String key, String value) {
88 IdEqualsPredicate predicate = null;
89 if (key.equals("documentID")) {
90 if (documentIdEqualsPredicates.containsKey(value)) {
91 predicate = (IdEqualsPredicate) (documentIdEqualsPredicates.get(value));
92 } else {
93 predicate = new IdEqualsPredicate(key, value);
94 documentIdEqualsPredicates.put(value, predicate);
95 }
96 } else if (key.equals("collectionID")) {
97 if (collectionIdEqualsPredicates.containsKey(value)) {
98 predicate = (IdEqualsPredicate) collectionIdEqualsPredicates.get(value);
99 } else {
100 predicate = new IdEqualsPredicate(key, value);
101 collectionIdEqualsPredicates.put(value, predicate);
102 }
103 } // TODO other IDs
104 return predicate;
105 }
106
107 /**
108 * @param field
109 * @param query
110 * @return
111 */
112 private static QueryPredicate createQueryPredicate(String field,
113 String query) {
114 QueryPredicate predicate = new QueryPredicate(field, query);
115 if (field.equals("document_content")) {
116 documentContentQueryPredicates.put(query, predicate);
117 } else if (field.equals("document_title")) {
118 documentTitleQueryPredicates.put(query, predicate);
119 }
120 return predicate;
121 }
122
123 public static Collection getAllQueryPredicates() {
124 Set result = new TreeSet();
125 result.addAll(documentTitleQueryPredicates.values());
126 result.addAll(documentContentQueryPredicates.values());
127 return Collections.unmodifiableSet(result);
128 }
129
130 public static Collection getQueryPredicates(String field) {
131 Set result = new TreeSet();
132 if (field.equals("document_title")) {
133 result.addAll(documentTitleQueryPredicates.values());
134 } else if (field.equals("document_content")) {
135 result.addAll(documentContentQueryPredicates.values());
136 }
137 return Collections.unmodifiableSet(result);
138 }
139
140 public static Collection getAllSubstringMatchPredicates() {
141 return Collections.unmodifiableCollection(collectionNameMatchPredicates.values());
142 }
143
144}
Note: See TracBrowser for help on using the repository browser.