1 package org.paneris.jammyjoes.controller;
2
3 import java.util.Enumeration;
4
5 import org.melati.Melati;
6 import org.melati.poem.NoSuchRowPoemException;
7 import org.melati.template.ServletTemplateContext;
8 import org.melati.util.StringUtils;
9 import org.paneris.jammyjoes.model.JammyjoesDatabase;
10 import org.paneris.jammyjoes.model.Product;
11 import org.paneris.jammyjoes.servlet.JammyJoesMelatiServlet;
12
13 public class Toy extends JammyJoesMelatiServlet {
14
15 private static final long serialVersionUID = 1L;
16
17 protected String jammyjoesRequest(Melati melati, ServletTemplateContext context)
18 throws Exception {
19
20 JammyjoesDatabase db = (JammyjoesDatabase) melati.getDatabase();
21 String template = "view/Product.wm";
22
23 String[] pathInfo = melati.getPathInfoParts();
24
25 String troidString = pathInfo[0];
26 if (troidString.endsWith(".html")) troidString = troidString.substring(0,troidString.length()-5);
27 Integer i = new Integer(troidString);
28
29 try {
30 Product p = (Product)db.getProductTable().getObject(i);
31 if (pathInfo.length > 1) {
32 String next = pathInfo[1];
33 if (next.equals("Next.html")) {
34 String basics = "stocklevel > 0 AND retailpriceincvat IS NOT NULL AND status = 0";
35 Enumeration r = db.getProductTable().selection(basics + " AND name > " + q(p.getName()), null, false);
36 if (r.hasMoreElements()) {
37 p = (Product)r.nextElement();
38 } else {
39 Enumeration s = db.getProductTable().selection(basics, null, false);
40 if (s.hasMoreElements()) p = (Product)s.nextElement();
41 }
42 }
43 }
44 context.put("product",p);
45 } catch (NoSuchRowPoemException e) {
46 template = "view/RedirectHome.wm";
47 }
48 return template;
49 }
50
51 public String q(String name) {
52 StringBuffer b = new StringBuffer();
53 StringUtils.appendQuoted(b, name, '\'');
54 return b.toString();
55 }
56
57 }