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