1 package org.paneris.jammyjoes.controller; 2 3 import java.util.Enumeration; 4 import java.util.Vector; 5 6 import javax.servlet.ServletConfig; 7 import javax.servlet.ServletException; 8 9 import org.melati.Melati; 10 import org.melati.poem.Field; 11 import org.melati.poem.Persistent; 12 import org.melati.poem.util.DumbPagedEnumeration; 13 import org.melati.poem.util.PagedEnumeration; 14 import org.melati.servlet.Form; 15 import org.melati.template.ServletTemplateContext; 16 import org.melati.util.MelatiException; 17 import org.melati.util.UTF8URLEncoder; 18 import org.paneris.jammyjoes.model.JammyjoesDatabase; 19 import org.paneris.jammyjoes.model.Product; 20 import org.paneris.jammyjoes.model.Supplier; 21 import org.paneris.jammyjoes.servlet.JammyJoesMelatiServlet; 22 import org.paneris.jammyjoes.shopping.MelatiShoppingConfig; 23 import org.paneris.jammyjoes.shopping.ShoppingTrolley; 24 import org.paneris.jammyjoes.util.JammyJoesUtil; 25 26 public class Search extends JammyJoesMelatiServlet { 27 28 private static final long serialVersionUID = 1L; 29 30 public MelatiShoppingConfig config; 31 private JammyjoesDatabase db; 32 33 public void init(ServletConfig conf) throws ServletException { 34 super.init(conf); 35 try { 36 config = new MelatiShoppingConfig(); 37 } catch (MelatiException e) { 38 throw new ServletException(e.toString()); 39 } 40 } 41 42 protected String jammyjoesRequest(Melati melati, ServletTemplateContext context) throws Exception { 43 44 db = (JammyjoesDatabase) melati.getDatabase(); 45 ServletTemplateContext tc = melati.getServletTemplateContext(); 46 String debug = ""; 47 String print = Form.getFormNulled(tc, "print"); 48 String inframe = Form.getFormNulled(tc, "inframe"); 49 String template = JammyJoesUtil.shoppingTemplate(melati, "Results.wm"); 50 if (print != null) 51 template = "view/PrintResults.wm"; 52 53 String[] pathInfo = melati.getPathInfoParts(); 54 55 Logger logger = new Logger(db.getSettingTable().get("UploadDir") + "/../"); 56 SearchCriteria andSearchCriteria = new TemplateContextSearchCriteria(tc,pathInfo,db,logger, "AND"); 57 58 ParameterProvider provider = new ParameterProvider(tc, pathInfo); 59 SearchType type = db.getTypeTable().fromParameter(provider); 60 SearchAge age = db.getAgeTable().fromParameter(provider); 61 SearchMaufacturer manufacturer = db.getManufacturerTable().fromParameter(provider); 62 63 String sale = getParam(tc, "field_sale", pathInfo, 7); 64 65 String supplierString = getParam(tc, "field_supplier", pathInfo, 9); 66 Integer supplier = null; 67 try { 68 supplier = getInt(supplierString); 69 if (supplier != null) 70 supplierString = ((Supplier) db.getSupplierTable().getObject(supplier)).getName(); 71 } catch (NumberFormatException e) { 72 Enumeration en = db.getSupplierTable().getNameColumn().selectionWhereEq(supplierString); 73 if (en.hasMoreElements()) 74 supplier = ((Persistent) en.nextElement()).getTroid(); 75 } 76 77 String troidString = getParam(tc, "troid", pathInfo, 0); 78 Integer troid = null; 79 try { 80 troid = getInt(troidString); 81 if (troid != null) 82 troidString = ((Product) db.getProductTable().getObject(troid)).getName(); 83 } catch (NumberFormatException e) {} 84 85 String price = getParam(tc, "field_price", pathInfo, 3); 86 String description = getParam(tc, "field_description", pathInfo, 4); 87 String outofstock = getParam(tc, "field_outofstock", pathInfo, 6); 88 Integer hitsPerPage = getInt(getParam(tc, "hits", pathInfo, 10)); 89 String startString = getParam(tc, "start", pathInfo, 11); 90 91 String go = Form.getFormNulled(tc, "go"); 92 93 logger.logType(type); 94 95 String path = 96 pathise(troidString) 97 + type.pathise() 98 + age.pathise() 99 + pathise(price) 100 + pathise(description) 101 + pathise("_") 102 + pathise(outofstock) 103 + pathise(sale) 104 + manufacturer.pathise() 105 + pathise(supplierString) 106 + pathise(hitsPerPage); 107 108 if (hitsPerPage == null) 109 hitsPerPage = new Integer(100); 110 111 context.put("path", path); 112 context.put("type", new Field(type, db.getProductTable().getTypeColumn())); 113 context.put("ages", makeVector(db.getAgeTable().selection())); 114 context.put("trolley", ShoppingTrolley.getInstance(melati, config)); 115 context.put("metaData", new TitleGenerator(type, age, manufacturer, troidString, description)); 116 context.put("metaRange", ""); 117 context.put("metaSupplier", notNull(supplierString)); 118 context.put("metaName", notNull(troidString)); 119 context.put("metaDescription", notNull(description)); 120 121 boolean run = false; 122 if (go != null 123 || troidString != null 124 || !type.isNull() 125 || !age.isNull() 126 || price != null 127 || description != null 128 || outofstock != null 129 || !manufacturer.isNull() 130 || sale != null 131 || supplier != null 132 || startString != null) 133 run = true; 134 135 if (pathInfo.length < 1 && !run) { 136 template = JammyJoesUtil.shoppingTemplate(melati, "Shopping.wm"); 137 if (print != null) 138 template = "view/PrintSearch.wm"; 139 } else { 140 logger.logUsersIp(melati.getRequest().getRemoteAddr()); 141 PagedEnumeration r = null; 142 if (run) { 143 r = new ProductSearch(db, 200, logger).doSearch(andSearchCriteria); 144 } 145 int start = 0; 146 if (startString != null) { 147 try { 148 start = new Integer(startString).intValue(); 149 } catch (NumberFormatException e) { 150 } 151 } 152 if (r != null){ 153 logger.writeLog(); 154 if (r.getTotalCount() == 0) { 155 SearchCriteria orSearchCriteria = new TemplateContextSearchCriteria(tc,pathInfo,db,logger, "OR"); 156 r = new ProductSearch(db, 200, logger).doSearch(orSearchCriteria); 157 } 158 if (r.getTotalCount() == 1) { 159 Product similarToMe = (Product)r.nextElement(); 160 Vector oneResult = new Vector(); 161 oneResult.add(similarToMe); 162 context.put("results", new DumbPagedEnumeration(oneResult.elements(), start, hitsPerPage.intValue(), 200)); 163 context.put("similarResults", new DumbPagedEnumeration(new ProductSearch(db, 10, new Logger(null)).doSearch(new SimilarSearchCriteria(similarToMe)), 0, 10, 10)); 164 } else { 165 melati.getSession().setAttribute("path", path + pathise(startString)); 166 context.put("results", new DumbPagedEnumeration(r, start, hitsPerPage.intValue(), 200)); 167 } 168 } 169 context.put("debug", debug); 170 } 171 return template; 172 } 173 174 private Vector makeVector(Enumeration enumeration) { 175 Vector result = new Vector(); 176 while (enumeration.hasMoreElements()) { 177 result.add(enumeration.nextElement()); 178 } 179 return result; 180 } 181 182 private String pathise(Object in) { 183 if (in == null) 184 return "/_"; 185 return "/" + UTF8URLEncoder.encode(in.toString()); 186 } 187 188 private String getParam(ServletTemplateContext tc, String name, String[] pathInfo, int pos) { 189 String value = Form.getFormNulled(tc, name); 190 if (value == null && pos > -1 && pos < pathInfo.length) { 191 value = pathInfo[pos]; 192 if (value != null) { 193 if (value.equals("") || value.equals("_")) { 194 value = null; 195 } else { 196 try { 197 value = UTF8URLEncoder.decode(value); 198 } catch (Exception e) { 199 } 200 } 201 } 202 } 203 return value; 204 } 205 206 private Integer getInt(String in) { 207 if (in != null) 208 return new Integer(in); 209 return null; 210 } 211 212 private String notNull(String in) { 213 if (in != null) 214 return in; 215 return ""; 216 } 217 218 }