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