1 package ar.com.jiji.kaya.query;
2
3 import java.util.ArrayList;
4 import java.util.Collections;
5 import java.util.HashMap;
6 import java.util.List;
7 import java.util.Map;
8
9 import ar.com.jiji.kaya.query.QueryItem.QueryOp;
10
11 /**
12 * Representa una consulta a realizar sobre los datos, independiente de que se
13 * use para accederlos. Usar esta clase solo en casos de queries muy dinamicos,
14 * en otro caso es mejor agregar metodos a los dao especificos para cada
15 * busqueda.
16 *
17 * @author lparravicini
18 * @version $Id: Query.java 71 2005-09-08 20:03:22Z lparravicini $
19 *
20 */
21 public class Query {
22
23 private static final String COL_DELIM = ",";
24
25 private static final String SEARCH_TOKENS_DELIM = " ";
26
27 private static final String SEARCH_COL_DELIM = ":";
28
29 List<QueryItem> query = new ArrayList<QueryItem>();
30
31 List<Query> orQuerys = new ArrayList<Query>();
32
33 Map<String, String> allowedCols = new HashMap<String, String>();
34
35 public Query() {
36 }
37
38 public Query(String allowedCols) throws QueryParseException {
39 String[] cols = allowedCols.trim().split(COL_DELIM);
40 for (int i = 0; i < cols.length; i++) {
41 String[] aux = cols[i].trim().split(SEARCH_COL_DELIM, 4);
42 if (aux.length != 2)
43 throw new QueryParseException(cols[i]);
44 this.allowedCols.put(aux[0].trim(), aux[1].trim());
45 }
46 }
47
48 public void addFromString(String query) throws QueryParseException,
49 InvalidColumnException {
50 if (query.trim().length() == 0)
51 return;
52
53 String[] queryTokens = query.trim().split(SEARCH_TOKENS_DELIM);
54 for (int i = 0; i < queryTokens.length; i++) {
55 String[] tokens = queryTokens[i].split(SEARCH_COL_DELIM, 4);
56 if (tokens.length > 2)
57 throw new QueryParseException(queryTokens[i]);
58
59 String columnName = null;
60 String search = null;
61 if (tokens.length == 2) {
62 columnName = tokens[0].trim();
63 search = tokens[1].trim();
64 } else
65 search = tokens[0].trim();
66 add(columnName, search, QueryItem.QueryOp.LIKE);
67 }
68 }
69
70 public List<QueryItem> getQuery() {
71 return Collections.unmodifiableList(query);
72 }
73
74 private boolean validColumn(String col) {
75 return (col == null || allowedCols.keySet().contains(col));
76 }
77
78 public void add(String query, QueryOp op) throws InvalidColumnException {
79 add(null, query, op);
80 }
81
82 /**
83 * Permite Agregar una query desde un string
84 */
85 public void add(String query, Object value) {
86 this.query.add(new QueryItem(query, value, QueryOp.CUSTOM));
87 }
88
89 public void add(String column, Object value, QueryOp op)
90 throws InvalidColumnException {
91 if (validColumn(column)) {
92 String col = getColumnExpression(column);
93 this.query.add(new QueryItem(col, value, op));
94 } else
95 throw new InvalidColumnException(column);
96 }
97
98 private String getColumnExpression(String column) {
99 return allowedCols.get(column);
100 }
101
102 public void clear() {
103 query.clear();
104 }
105
106 public Map getAllowedCols() {
107 return allowedCols;
108 }
109
110 public void addOrQuery(Query q) {
111 this.orQuerys.add(q);
112 }
113
114 public List<Query> getOrQuerys() {
115 return Collections.unmodifiableList(orQuerys);
116 }
117 }