1
2
3
4 package org.paneris.rimauresq;
5 import java.io.File;
6 import java.text.DateFormat;
7 import java.util.Date;
8 import java.util.Enumeration;
9 import java.util.Hashtable;
10 import java.util.Vector;
11 import org.melati.Melati;
12 import org.melati.servlet.Form;
13 import org.melati.PoemContext;
14 import org.melati.poem.AccessPoemException;
15 import org.melati.poem.AccessToken;
16 import org.melati.poem.ExecutingSQLPoemException;
17 import org.melati.poem.Initialiser;
18 import org.melati.poem.Persistent;
19 import org.melati.poem.PoemTask;
20 import org.melati.poem.PoemThread;
21 import org.melati.poem.Table;
22 import org.melati.poem.ValidationPoemException;
23 import org.melati.servlet.PathInfoException;
24 import org.melati.template.ServletTemplateContext;
25 import org.melati.util.Email;
26 import org.melati.util.MelatiStringWriter;
27 import org.paneris.rimauresq.model.RimauresqDatabase;
28 import org.paneris.rimauresq.model.User;
29
30
31
32
33
34
35
36 public class Trolley extends RimauresqServlet {
37
38
39 private static final long serialVersionUID = 1L;
40
41
42
43
44
45 public class Item {
46 protected Integer productId;
47 protected Integer quantity;
48
49
50
51
52 Item(Integer productId, Integer quantity) {
53 super();
54 setProductId(productId);
55 setQuantity(quantity);
56 }
57
58
59
60 public Integer getQuantity() {
61 return quantity;
62 }
63
64
65
66
67 protected void setQuantity(Integer quantity) {
68 this.quantity = quantity;
69 }
70
71
72
73 public Integer getProductId() {
74 return productId;
75 }
76
77
78
79 protected void setProductId(Integer productId) {
80 this.productId = productId;
81 }
82 }
83
84 User u;
85
86
87
88
89 public Trolley() {
90 super();
91 }
92
93
94
95
96
97
98
99 protected String doRimauresqTemplateRequest(Melati melati,
100 ServletTemplateContext templateContext) throws Exception {
101
102 templateContext.put("object", ((RimauresqDatabase)melati.getDatabase()).getThanksPage());
103
104
105 Vector orderedItems = new Vector();
106 Hashtable items = new Hashtable();
107 final Table t = melati.getTable();
108 final ServletTemplateContext tc = templateContext;
109 try {
110 PoemThread.withAccessToken(AccessToken.root, new PoemTask() {
111 public void run() {
112 u = (User)create(t, tc);
113 }
114 });
115 } catch (ExecutingSQLPoemException e) {
116 templateContext.put("error",
117 "Problem creating user:" + e);
118 return "thanks";
119 }
120 for (Enumeration e = melati.getRequest().getParameterNames(); e
121 .hasMoreElements();) {
122 String name = (String)e.nextElement();
123 if (name.startsWith("quantity")) {
124 String id = name.substring("quantity".length());
125 Integer idInt = new Integer(id);
126 Integer quantity = new Integer(1);
127 String quantityString = Form.getFormNulled(
128 melati.getServletTemplateContext(), name);
129 if (quantityString != null) {
130 quantity = new Integer(quantityString);
131 Item it = new Item(idInt, quantity);
132 orderedItems.add(it);
133 items.put(idInt, quantity);
134 }
135 }
136 }
137 templateContext.put("user", u);
138
139 if (items.size() > 0) {
140 templateContext.put("itemsOrdered", Boolean.TRUE);
141 } else {
142 if (!u.getNews().booleanValue()) {
143 templateContext.put("illogical", Boolean.TRUE);
144 return "thanks";
145 }
146 }
147 templateContext.put("items", items);
148 templateContext.put("orderedItems", orderedItems);
149 templateContext.put("comment",
150 Form.getFormNulled(
151 melati.getServletTemplateContext(), "comment"));
152 String smtpServer = getSetting(melati,"SMTPServer");
153 String websiteFromName = getSetting(melati,"WebsiteFromName");
154 String websiteFromEmail = getSetting(melati,"WebsiteFromEmail");
155 String from = mailAddress(websiteFromName, websiteFromEmail);
156
157 String salesAdminName = getSetting(melati,"SalesAdminName");
158 String salesAdminEmail = getSetting(melati,"SalesAdminEmail");
159 String newsAdminName = getSetting(melati,"NewsAdminName");
160 String newsAdminEmail = getSetting(melati,"NewsAdminEmail");
161 String to = null;
162 String subject = null;
163 if (orderedItems.size() > 0) {
164 to = mailAddress(salesAdminName, salesAdminEmail);
165 subject = "Rimauresq Website order";
166 }
167 if (u.getNews().booleanValue()) {
168 if (to != null) {
169 to += ", ";
170 subject += " and ";
171 } else {
172 to = "";
173 subject = "";
174 }
175 to += mailAddress(newsAdminName, newsAdminEmail);
176 to += ", ";
177 to += mailAddress("Tim Pizey", "timp@paneris.org");
178 subject += "News subscription";
179 }
180 String replyTo = "";
181 try {
182 Date now = new Date();
183
184 DateFormat df = DateFormat.getDateInstance();
185 templateContext.put("now", df.format(now));
186 templateContext.put("servlet",this);
187 templateContext.put("servlet",this);
188 templateContext.put("from",from);
189 templateContext.put("to",to);
190 templateContext.put("replyTo",replyTo);
191 templateContext.put("subject",subject);
192 String templateName = "org/paneris/rimauresq/view/Email.wm";
193 MelatiStringWriter sw = templateEngine.getStringWriter();
194 templateEngine.expandTemplate(sw,
195 templateName,
196 templateContext);
197 String message = sw.toString();
198
199 templateName = "org/paneris/rimauresq/view/HtmlEmail.wm";
200 sw = templateEngine.getStringWriter();
201 templateEngine.expandTemplate(sw,
202 templateName,
203 templateContext);
204 String htmlMessage = sw.toString();
205 File[] empty = {};
206 Email.sendAsHtmlWithAttachments(smtpServer, from,
207 to, replyTo,
208 subject,
209 message,
210 htmlMessage,
211 empty, empty);
212 } catch (Exception e) {
213 e.printStackTrace(System.err);
214 templateContext.put("error",
215 "Unexpected error: " + e);
216 }
217
218 return "thanks";
219 }
220
221
222
223 private String mailAddress(String name, String email) {
224 return name + " <" + email + ">";
225 }
226
227
228
229
230 protected Persistent create(Table table,
231 final ServletTemplateContext context) {
232 Persistent result = table.create(new Initialiser() {
233 public void init(Persistent object) throws AccessPoemException,
234 ValidationPoemException {
235 Form.extractFields(context, object);
236 }
237 });
238 result.postEdit(true);
239 return result;
240 }
241
242
243
244
245
246
247 protected PoemContext poemContext(Melati melati) throws PathInfoException {
248 PoemContext it = super.poemContext(melati);
249 it.setTable("user");
250 return it;
251 }
252
253 }