View Javadoc

1   package org.paneris.bibliomania;
2   
3   import java.util.Locale;
4   
5   import javax.servlet.ServletConfig;
6   import javax.servlet.ServletException;
7   
8   import org.melati.Melati;
9   import org.melati.servlet.Form;
10  import org.melati.poem.BaseFieldAttributes;
11  import org.melati.poem.Field;
12  import org.melati.poem.FieldAttributes;
13  import org.melati.template.ServletTemplateContext;
14  import org.melati.util.MelatiException;
15  import org.paneris.bibliomania.shopping.BibliomaniaShoppingTrolley;
16  import org.paneris.melati.shopping.MelatiShoppingConfig;
17  import org.paneris.melati.shopping.ShoppingTrolley;
18  import org.webmacro.servlet.WebContext;
19  
20  public class Shop extends BibliomaniaServlet {
21  
22    /**
23     * 
24     */
25    private static final long serialVersionUID = 1L;
26    MelatiShoppingConfig config;
27    BibliomaniaDatabase db;
28  
29    protected String bibliomaniaHandle(Melati melati, WebContext context)
30      throws Exception {
31  
32      ServletTemplateContext tc = melati.getServletTemplateContext();
33      db = (BibliomaniaDatabase)melati.getDatabase();
34      if (Form.getFieldNulled(tc, "login") != null) {
35        assertRegisteredUser(db);
36      }
37      BibliomaniaShoppingTrolley trolley =
38        (BibliomaniaShoppingTrolley)ShoppingTrolley.getInstance(melati, config);
39      BibContext bibcontext = (BibContext)melati.getPoemContext();
40      User user = (User)melati.getUser();
41  
42      Locale locale = null;
43      Currency currency = null;
44      if (context.getForm("changelocale") != null) {
45        currency =
46          (Currency)db.getCurrencyTable().getObject(
47            Form.getIntegerField(tc, "field_currency"));
48        locale = currency.toLocale();
49        trolley.setLocale(locale);
50      } else {
51        locale = trolley.getLocale();
52      }
53      currency = trolley.getCurrency();
54      if (user.isRegisteredUser(db))
55        user.setCurrency(currency);
56      context.put(
57        "currencies",
58        new Field(
59          currency.getTroid(),
60          new BaseFieldAttributes(
61            (FieldAttributes) ((UserTable)db.getUserTable()).getCurrencyColumn(),
62            false)));
63      context.put("currency", currency);
64      context.put(
65        "acceptlanguage",
66        melati.getRequest().getHeader("accept-language"));
67      context.put("trolley", trolley);
68      db.setupContext(melati, context, (Unit)melati.getObject());
69      String template = "shopping/";
70      template += bibcontext.getTable().substring(0, 1).toUpperCase()
71        + bibcontext.getTable().substring(1, bibcontext.getTable().length())
72        + ".wm";
73      return template;
74    }
75  
76    public void init(ServletConfig conf) throws ServletException {
77      super.init(conf);
78      try {
79        config = new MelatiShoppingConfig();
80      } catch (MelatiException e) {
81        throw new ServletException(e.toString());
82      }
83    }
84  
85  }