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