View Javadoc

1   package org.paneris.jammyjoes.servlet;
2   
3   import java.sql.Timestamp;
4   
5   import org.melati.Melati;
6   import org.melati.servlet.Form;
7   import org.melati.PoemContext;
8   import org.melati.servlet.PathInfoException;
9   import org.melati.servlet.TemplateServlet;
10  import org.melati.template.ServletTemplateContext;
11  import org.paneris.jammyjoes.model.Affiliate;
12  import org.paneris.jammyjoes.model.AffiliateTransaction;
13  import org.paneris.jammyjoes.model.AffiliateTransactionTable;
14  import org.paneris.jammyjoes.model.JammyjoesDatabase;
15  import org.paneris.jammyjoes.util.JammyJoesContextUtil;
16  import org.paneris.jammyjoes.util.JammyJoesUtil;
17  import org.webmacro.servlet.WebContext;
18  
19  public abstract class JammyJoesMelatiServlet extends TemplateServlet {
20  
21    protected PoemContext poemContext(Melati melati) throws PathInfoException {
22      PoemContext context = new PoemContext();
23      context.setLogicalDatabase("jammyjoes");
24      return context;
25    }
26  
27    public String getSysAdminName() {
28      return "Tim Joyce";
29    }
30  
31    public String getSysAdminEmail() {
32      return "timj@hoop.co.uk";
33    }
34  
35    protected String doTemplateRequest(Melati melati, ServletTemplateContext templateContext) throws Exception {
36      logReferer(melati, templateContext);
37      JammyjoesDatabase db = (JammyjoesDatabase) melati.getDatabase();
38      templateContext.put("jjutil", new JammyJoesContextUtil());
39      templateContext.put("menutypes", db.getTypeTable().selection());
40      return jammyjoesRequest(melati, templateContext);
41    }
42  
43    protected abstract String jammyjoesRequest(Melati melati, ServletTemplateContext webContext) throws Exception;
44  
45    protected String addExtension(String templateName) {
46  
47      return templateName;
48    }
49  
50    private void logReferer(Melati melati, ServletTemplateContext context) {
51      try {
52        JammyjoesDatabase db = (JammyjoesDatabase) melati.getDatabase();
53        WebContext webContext = (WebContext)context.getContext();
54  
55        String affiliateId = JammyJoesUtil.getCookieValue(melati, "affiliate");
56        if (affiliateId == null) {
57          affiliateId = Form.getFormNulled(context,"affiliate");
58        }
59        if (affiliateId == null) {
60          return;
61        }
62        Affiliate affiliate = (Affiliate)db.getAffiliateTable().getObject(new Integer(affiliateId));
63        int days = affiliate.getCookiedays().intValue();
64        melati.getResponse().addCookie(
65            JammyJoesUtil.makeCookie("affiliate", affiliate.getTroid().toString(), days));
66  
67        String referrer = webContext.getCGI().getHTTP_REFERER();
68        if (isExcluded(referrer)) {
69          return;
70        }
71  
72        AffiliateTransactionTable transTable = db.getAffiliateTransactionTable();
73        AffiliateTransaction trans = (AffiliateTransaction) transTable.newPersistent();
74        trans.setAffiliate(affiliate);
75        trans.setHit(new Timestamp(new java.util.Date().getTime()));
76        trans.setReferrerURL(referrer);
77        trans.setLandingPage(melati.getRequest().getRequestURL().toString());
78        trans.setUserIpAddress(melati.getRequest().getRemoteAddr());
79        transTable.create(trans);
80      } catch (Exception e) {
81        e.printStackTrace(System.err);
82      }
83    }
84  
85    private boolean isExcluded(String referrer) {
86      if (referrer == null) {
87        return false;
88      }
89      if (referrer.startsWith("http://localhost")) {
90        return true;
91      }
92      if (referrer.startsWith("http://www.jammyjoes")) {
93        return true;
94      }
95      return false;
96    }
97  
98  }