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.sql.SQLException;
|
---|
12 | import java.util.*;
|
---|
13 | import java.util.Map;
|
---|
14 | import java.util.TreeMap;
|
---|
15 |
|
---|
16 | import 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 | */
|
---|
25 | public abstract class PredicateFactory {
|
---|
26 |
|
---|
27 | private static int id = 0;
|
---|
28 |
|
---|
29 | // documentID -> IdEqualsPredicate referring to it
|
---|
30 | private static Map documentIdEqualsPredicates = new TreeMap();
|
---|
31 | // collectionID -> IdEqualsPredicate referring to it
|
---|
32 | private static Map collectionIdEqualsPredicates = new TreeMap();
|
---|
33 |
|
---|
34 | // substring for collection_name -> SubstringMatchPredicate referring to it
|
---|
35 | private static Map collectionNameMatchPredicates = new TreeMap();
|
---|
36 |
|
---|
37 | // query -> QueryPredicates referring to it
|
---|
38 | private static Map documentTitleQueryPredicates = new TreeMap();
|
---|
39 | private static Map documentContentQueryPredicates = new TreeMap();
|
---|
40 |
|
---|
41 |
|
---|
42 | /**
|
---|
43 | * @param key
|
---|
44 | * @param value
|
---|
45 | * @return
|
---|
46 | * @throws ParseException
|
---|
47 | * @throws DatabaseException
|
---|
48 | * @throws SQLException
|
---|
49 | */
|
---|
50 | public static Predicate createPredicate(String key, String value)
|
---|
51 | throws SQLException, DatabaseException {
|
---|
52 | System.out.println("creating predicate for key " + key + " and value " + value);
|
---|
53 | if (!Predicate.isSingleValued(key)) {
|
---|
54 | return null;
|
---|
55 | }
|
---|
56 | Predicate result = null;
|
---|
57 | if (value == null || value.length() == 0) {
|
---|
58 | return null;
|
---|
59 | }
|
---|
60 |
|
---|
61 | if (key.equals("type")) {
|
---|
62 | return result;
|
---|
63 | } else if (key.endsWith("ID")) {
|
---|
64 | result = createIdEqualsPredicate(key, value);
|
---|
65 | } else if (key.startsWith("document")) {
|
---|
66 | result = createQueryPredicate(key, value);
|
---|
67 | } else if (key.endsWith("_name")) {
|
---|
68 | result = createSubstringMatchPredicate(key, value);
|
---|
69 | } else {
|
---|
70 | return null;
|
---|
71 | }
|
---|
72 | result.setID(result.saveToDatabase());
|
---|
73 | return result;
|
---|
74 | }
|
---|
75 |
|
---|
76 | public static Predicate createPredicate(String key, List values) throws SQLException, DatabaseException {
|
---|
77 | if (!Predicate.isMultiValued(key)) {
|
---|
78 | return null;
|
---|
79 | }
|
---|
80 | // TODO properly handle multi-valued predicates
|
---|
81 | return createPredicate(key, (String) values.get(0));
|
---|
82 | }
|
---|
83 |
|
---|
84 | /**
|
---|
85 | * @param key
|
---|
86 | * @param value
|
---|
87 | * @return
|
---|
88 | */
|
---|
89 | private static SubstringMatchPredicate createSubstringMatchPredicate(String key, String value) {
|
---|
90 | SubstringMatchPredicate predicate = null;
|
---|
91 | if (key.equals("collection_name")) {
|
---|
92 | if (collectionNameMatchPredicates.containsKey(value)) {
|
---|
93 | predicate = (SubstringMatchPredicate) collectionNameMatchPredicates.get(value);
|
---|
94 | } else {
|
---|
95 | predicate = new SubstringMatchPredicate(key, value);
|
---|
96 | collectionNameMatchPredicates.put(value, predicate);
|
---|
97 | }
|
---|
98 | } // TODO other fields than collection_name
|
---|
99 | return predicate;
|
---|
100 | }
|
---|
101 |
|
---|
102 | /**
|
---|
103 | * @param key
|
---|
104 | * @param value
|
---|
105 | * @return
|
---|
106 | */
|
---|
107 | private static IdEqualsPredicate createIdEqualsPredicate(String key, String value) {
|
---|
108 | IdEqualsPredicate predicate = null;
|
---|
109 | if (key.equals("documentID")) {
|
---|
110 | if (documentIdEqualsPredicates.containsKey(value)) {
|
---|
111 | predicate = (IdEqualsPredicate) (documentIdEqualsPredicates.get(value));
|
---|
112 | } else {
|
---|
113 | predicate = new IdEqualsPredicate(key, value);
|
---|
114 | documentIdEqualsPredicates.put(value, predicate);
|
---|
115 | }
|
---|
116 | } else if (key.equals("collectionID")) {
|
---|
117 | if (collectionIdEqualsPredicates.containsKey(value)) {
|
---|
118 | predicate = (IdEqualsPredicate) collectionIdEqualsPredicates.get(value);
|
---|
119 | } else {
|
---|
120 | predicate = new IdEqualsPredicate(key, value);
|
---|
121 | collectionIdEqualsPredicates.put(value, predicate);
|
---|
122 | }
|
---|
123 | } // TODO other IDs
|
---|
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 | } |
---|