1 | |
package org.paneris.jammyjoes.controller; |
2 | |
|
3 | |
import java.util.Enumeration; |
4 | |
import java.util.List; |
5 | |
import java.util.Vector; |
6 | |
|
7 | |
import org.melati.poem.Persistent; |
8 | |
import org.melati.template.ServletTemplateContext; |
9 | |
import org.melati.poem.util.EnumUtils; |
10 | |
import org.melati.util.StringUtils; |
11 | |
import org.paneris.jammyjoes.model.Age; |
12 | |
import org.paneris.jammyjoes.model.JammyjoesDatabase; |
13 | |
|
14 | |
public class TemplateContextSearchCriteria implements SearchCriteria { |
15 | |
|
16 | |
private ParameterProvider provider; |
17 | |
private JammyjoesDatabase db; |
18 | |
Logger logger; |
19 | |
private String andOr; |
20 | |
|
21 | 0 | public TemplateContextSearchCriteria(ServletTemplateContext tc, String[] pathInfo, JammyjoesDatabase db, Logger logger, String andOr) { |
22 | 0 | provider = new ParameterProvider(tc, pathInfo); |
23 | 0 | this.db = db; |
24 | 0 | this.logger = logger; |
25 | 0 | this.andOr = andOr; |
26 | 0 | } |
27 | |
|
28 | |
public void constrain(List constraints) { |
29 | 0 | if (!constrainTroid(constraints)) { |
30 | 0 | constrainOutOfStock(constraints); |
31 | 0 | constrainDescription(constraints); |
32 | 0 | constrainType(constraints); |
33 | 0 | constrainSupplier(constraints); |
34 | 0 | constrainManufacturer(constraints); |
35 | 0 | constrainPrice(constraints); |
36 | 0 | constrainAge(constraints); |
37 | 0 | constrainSale(constraints); |
38 | |
} |
39 | 0 | } |
40 | |
|
41 | |
|
42 | |
private void constrainSale(List constraints) { |
43 | 0 | String sale = provider.getParam("field_sale", 7); |
44 | 0 | if (sale != null) { |
45 | 0 | constraints.add(q("saleprice") + " IS NOT NULL"); |
46 | |
} |
47 | 0 | } |
48 | |
|
49 | |
public void constrainDicontinued(List constraints) { |
50 | 0 | constraints.add(q("status") + " != " + db.getProductStatusTable().getDiscontinued().getTroid()); |
51 | 0 | } |
52 | |
|
53 | |
public String order() { |
54 | 0 | return null; |
55 | |
} |
56 | |
|
57 | |
public boolean constrainTroid(List constraints) { |
58 | 0 | String troidString = provider.getParam("troid", 0); |
59 | 0 | Integer troid = null; |
60 | |
try { |
61 | 0 | troid = getInt(troidString); |
62 | 0 | } catch (NumberFormatException e) { |
63 | 0 | Enumeration en = db.getProductTable().getNameColumn().selectionWhereEq(troidString); |
64 | 0 | if (en.hasMoreElements()) { |
65 | 0 | troid = ((Persistent) en.nextElement()).getTroid(); |
66 | |
} |
67 | 0 | } |
68 | 0 | if (troid != null) { |
69 | 0 | constraints.add(q("id") + " = " + troid); |
70 | 0 | logger.logTroid(troid); |
71 | 0 | return true; |
72 | |
} |
73 | 0 | return false; |
74 | |
} |
75 | |
|
76 | |
public void constrainPrice(List constraints) { |
77 | 0 | String price = provider.getParam("field_price", 3); |
78 | 0 | if (price != null) { |
79 | 0 | String[] prices = StringUtils.split(price, '_'); |
80 | 0 | constraints.add(q("retailpriceincvat") + " >= " + prices[0]); |
81 | 0 | constraints.add(q("retailpriceincvat") + " <= " + prices[1]); |
82 | 0 | logger.logPrice(price); |
83 | |
} |
84 | 0 | } |
85 | |
|
86 | |
public void constrainOutOfStock(List constraints) { |
87 | 0 | String outofstock = provider.getParam("field_outofstock", 6); |
88 | 0 | if (outofstock == null) { |
89 | 0 | constraints.add(q("stocklevel") + " > 0"); |
90 | |
} |
91 | 0 | } |
92 | |
|
93 | |
public void constrainDescription(List constraints) { |
94 | 0 | String description = provider.getParam("field_description", 4); |
95 | 0 | if (description != null) { |
96 | 0 | description = description.trim(); |
97 | 0 | logger.logDescription(description); |
98 | 0 | String[] parts = StringUtils.split(description, ' '); |
99 | 0 | Vector subwhere = new Vector(); |
100 | 0 | String descClause = makeClause(db, "description", parts); |
101 | 0 | String nameClause = makeClause(db, "name", parts); |
102 | 0 | String typeClause = makeClause(db, "type", parts); |
103 | 0 | if (descClause != null) |
104 | 0 | subwhere.add(descClause); |
105 | 0 | if (nameClause != null) |
106 | 0 | subwhere.add(nameClause); |
107 | 0 | if (typeClause != null) |
108 | 0 | subwhere.add( |
109 | |
q("type") |
110 | |
+ " IN (SELECT " |
111 | |
+ q("id") |
112 | |
+ " FROM " |
113 | |
+ q("type") |
114 | |
+ " WHERE " |
115 | |
+ typeClause |
116 | |
+ ")"); |
117 | 0 | subwhere.add( |
118 | |
q("type2") |
119 | |
+ " IN (SELECT " |
120 | |
+ q("id") |
121 | |
+ " FROM " |
122 | |
+ q("type") |
123 | |
+ " WHERE " |
124 | |
+ typeClause |
125 | |
+ ")"); |
126 | 0 | if (!subwhere.isEmpty()) { |
127 | 0 | constraints.add("(" + EnumUtils.concatenated(" OR ", subwhere.elements()) + ")"); |
128 | |
} |
129 | |
} |
130 | 0 | } |
131 | |
|
132 | |
public void constrainType(List constraints) { |
133 | 0 | String typeString = provider.getParam("field_type", 1); |
134 | 0 | Integer type = null; |
135 | |
try { |
136 | 0 | type = getInt(typeString); |
137 | 0 | } catch (NumberFormatException e) { |
138 | 0 | Enumeration en = db.getTypeTable().getTypeColumn().selectionWhereEq(typeString); |
139 | 0 | if (en.hasMoreElements()) |
140 | 0 | type = ((Persistent) en.nextElement()).getTroid(); |
141 | 0 | } |
142 | 0 | if (type != null) |
143 | 0 | constraints.add("(" + q("type") + " = " + type + " OR " + q("type2") + " = " + type + ")"); |
144 | 0 | } |
145 | |
|
146 | |
public String q(String name) { |
147 | 0 | StringBuffer b = new StringBuffer(); |
148 | 0 | StringUtils.appendQuoted(b, name, '"'); |
149 | 0 | return b.toString(); |
150 | |
} |
151 | |
|
152 | |
public String e(String value) { |
153 | 0 | StringBuffer b = new StringBuffer(); |
154 | 0 | StringUtils.appendQuoted(b, value, '\''); |
155 | 0 | return b.toString(); |
156 | |
} |
157 | |
|
158 | |
private Integer getInt(String in) { |
159 | 0 | return in == null ? null : new Integer(in); |
160 | |
} |
161 | |
|
162 | |
private String makeClause(JammyjoesDatabase db, String field, String[] parts) { |
163 | 0 | Vector clause = new Vector(); |
164 | 0 | for (int i = 0; i < parts.length; i++) { |
165 | 0 | String part = parts[i]; |
166 | 0 | if (!part.equals("")) { |
167 | 0 | clause.add(db.getDbms().caseInsensitiveRegExpSQL(q(field), e(part))); |
168 | |
} |
169 | |
} |
170 | 0 | if (clause.isEmpty()) |
171 | 0 | return null; |
172 | 0 | return "(" + EnumUtils.concatenated(" " + andOr + " ", clause.elements()) + ")"; |
173 | |
} |
174 | |
|
175 | |
public void constrainSupplier(List constraints) { |
176 | 0 | String supplierString = provider.getParam("field_supplier", 9); |
177 | 0 | Integer supplier = null; |
178 | |
try { |
179 | 0 | supplier = getInt(supplierString); |
180 | 0 | } catch (NumberFormatException e) { |
181 | 0 | Enumeration en = db.getSupplierTable().getNameColumn().selectionWhereEq(supplierString); |
182 | 0 | if (en.hasMoreElements()) |
183 | 0 | supplier = ((Persistent) en.nextElement()).getTroid(); |
184 | 0 | } |
185 | 0 | if (supplier != null) |
186 | 0 | constraints.add(q("supplier") + " = " + supplier); |
187 | 0 | } |
188 | |
|
189 | |
public void constrainManufacturer(List constraints) { |
190 | 0 | String manufacturerString = provider.getParam("field_manufacturer", 8); |
191 | 0 | Integer manufacturer = null; |
192 | |
try { |
193 | 0 | manufacturer = getInt(manufacturerString); |
194 | 0 | } catch (NumberFormatException e) { |
195 | 0 | Enumeration en = |
196 | |
db.getManufacturerTable().getNameColumn().selectionWhereEq(manufacturerString); |
197 | 0 | if (en.hasMoreElements()) |
198 | 0 | manufacturer = ((Persistent) en.nextElement()).getTroid(); |
199 | 0 | } |
200 | 0 | if (manufacturer != null) |
201 | 0 | constraints.add(q("manufacturer") + " = " + manufacturer); |
202 | 0 | } |
203 | |
|
204 | |
public void constrainAge(List constraints) { |
205 | 0 | String ageString = provider.getParam("field_age", 2); |
206 | 0 | Integer minAge = null; |
207 | 0 | Integer maxAge = null; |
208 | 0 | if (ageString != null) { |
209 | 0 | int hyphen = ageString.indexOf('_'); |
210 | 0 | if (hyphen > 0) { |
211 | 0 | String[] ages = StringUtils.split(ageString, '_'); |
212 | 0 | minAge = new Integer(ages[0]); |
213 | 0 | maxAge = new Integer(ages[1]); |
214 | 0 | } else { |
215 | 0 | Enumeration en = db.getAgeTable().getNameColumn().selectionWhereEq(ageString); |
216 | 0 | if (en.hasMoreElements()) { |
217 | 0 | Age theAge = (Age)en.nextElement(); |
218 | 0 | minAge = theAge.getMinage(); |
219 | 0 | maxAge = theAge.getMaxage(); |
220 | |
} |
221 | |
} |
222 | |
} |
223 | 0 | if (minAge != null && maxAge != null) { |
224 | 0 | constraints .add( |
225 | |
"((" |
226 | |
+ minAge |
227 | |
+ " >= " |
228 | |
+ q("minage") |
229 | |
+ " AND " |
230 | |
+ minAge |
231 | |
+ " <= " |
232 | |
+ q("maxage") |
233 | |
+ " ) OR (" |
234 | |
+ maxAge |
235 | |
+ " >= " |
236 | |
+ q("minage") |
237 | |
+ " AND " |
238 | |
+ maxAge |
239 | |
+ " <= " |
240 | |
+ q("maxage") |
241 | |
+ "))"); |
242 | 0 | logger.logAge(ageString); |
243 | |
} |
244 | 0 | } |
245 | |
|
246 | |
} |