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 | */
|
---|
9 | package org.greenstone.gsdlas.profiles;
|
---|
10 |
|
---|
11 | import java.util.*;
|
---|
12 | import java.util.Map;
|
---|
13 | import 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 | */
|
---|
22 | public 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 (value == null || value.length() == 0) {
|
---|
49 | return null;
|
---|
50 | }
|
---|
51 |
|
---|
52 | if (key.equals("type")) {
|
---|
53 | return result;
|
---|
54 | } else if (key.endsWith("ID")) {
|
---|
55 | result = createIdEqualsPredicate(key, value);
|
---|
56 | } else if (key.startsWith("document")) {
|
---|
57 | result = createQueryPredicate(key, value);
|
---|
58 | } else if (key.endsWith("_name")) {
|
---|
59 | result = createSubstringMatchPredicate(key, value);
|
---|
60 | } else {
|
---|
61 | throw new ParseException("value " + value + " for field " + key
|
---|
62 | + " does not lead to a parseable predicate");
|
---|
63 | }
|
---|
64 | result.setID(id++);
|
---|
65 | return result;
|
---|
66 | }
|
---|
67 |
|
---|
68 | /**
|
---|
69 | * @param key
|
---|
70 | * @param value
|
---|
71 | * @return
|
---|
72 | */
|
---|
73 | private static SubstringMatchPredicate createSubstringMatchPredicate(String key, String value) {
|
---|
74 | SubstringMatchPredicate predicate = null;
|
---|
75 | if (key.equals("collection_name")) {
|
---|
76 | if (collectionNameMatchPredicates.containsKey(value)) {
|
---|
77 | predicate = (SubstringMatchPredicate) collectionNameMatchPredicates.get(value);
|
---|
78 | } else {
|
---|
79 | predicate = new SubstringMatchPredicate(key, value);
|
---|
80 | collectionNameMatchPredicates.put(value, predicate);
|
---|
81 | }
|
---|
82 | } // TODO other fields than collection_name
|
---|
83 | return predicate;
|
---|
84 | }
|
---|
85 |
|
---|
86 | /**
|
---|
87 | * @param key
|
---|
88 | * @param value
|
---|
89 | * @return
|
---|
90 | */
|
---|
91 | private static IdEqualsPredicate createIdEqualsPredicate(String key, String value) {
|
---|
92 | IdEqualsPredicate predicate = null;
|
---|
93 | if (key.equals("documentID")) {
|
---|
94 | if (documentIdEqualsPredicates.containsKey(value)) {
|
---|
95 | predicate = (IdEqualsPredicate) (documentIdEqualsPredicates.get(value));
|
---|
96 | } else {
|
---|
97 | predicate = new IdEqualsPredicate(key, value);
|
---|
98 | documentIdEqualsPredicates.put(value, predicate);
|
---|
99 | }
|
---|
100 | } else if (key.equals("collectionID")) {
|
---|
101 | if (collectionIdEqualsPredicates.containsKey(value)) {
|
---|
102 | predicate = (IdEqualsPredicate) collectionIdEqualsPredicates.get(value);
|
---|
103 | } else {
|
---|
104 | predicate = new IdEqualsPredicate(key, value);
|
---|
105 | collectionIdEqualsPredicates.put(value, predicate);
|
---|
106 | }
|
---|
107 | } // TODO other IDs
|
---|
108 | return predicate;
|
---|
109 | }
|
---|
110 |
|
---|
111 | /**
|
---|
112 | * @param field
|
---|
113 | * @param query
|
---|
114 | * @return
|
---|
115 | */
|
---|
116 | private static QueryPredicate createQueryPredicate(String field,
|
---|
117 | String query) {
|
---|
118 | QueryPredicate predicate = new QueryPredicate(field, query);
|
---|
119 | if (field.equals("document_content")) {
|
---|
120 | documentContentQueryPredicates.put(query, predicate);
|
---|
121 | } else if (field.equals("document_title")) {
|
---|
122 | documentTitleQueryPredicates.put(query, predicate);
|
---|
123 | }
|
---|
124 | return predicate;
|
---|
125 | }
|
---|
126 |
|
---|
127 | public static Collection getAllQueryPredicates() {
|
---|
128 | Set result = new TreeSet();
|
---|
129 | result.addAll(documentTitleQueryPredicates.values());
|
---|
130 | result.addAll(documentContentQueryPredicates.values());
|
---|
131 | return Collections.unmodifiableSet(result);
|
---|
132 | }
|
---|
133 |
|
---|
134 | public static Collection getQueryPredicates(String field) {
|
---|
135 | Set result = new TreeSet();
|
---|
136 | if (field.equals("document_title")) {
|
---|
137 | result.addAll(documentTitleQueryPredicates.values());
|
---|
138 | } else if (field.equals("document_content")) {
|
---|
139 | result.addAll(documentContentQueryPredicates.values());
|
---|
140 | }
|
---|
141 | return Collections.unmodifiableSet(result);
|
---|
142 | }
|
---|
143 |
|
---|
144 | public static Collection getAllSubstringMatchPredicates() {
|
---|
145 | return Collections.unmodifiableCollection(collectionNameMatchPredicates.values());
|
---|
146 | }
|
---|
147 |
|
---|
148 | } |
---|