1 package org.paneris.jammyjoes.controller;
2
3
4 import org.melati.Melati;
5 import org.melati.servlet.Form;
6 import org.melati.poem.AccessToken;
7 import org.melati.poem.PoemTask;
8 import org.melati.poem.PoemThread;
9 import org.melati.template.ServletTemplateContext;
10 import org.melati.util.Email;
11 import org.paneris.jammyjoes.model.JammyjoesDatabase;
12 import org.paneris.jammyjoes.model.User;
13 import org.paneris.jammyjoes.servlet.JammyJoesMelatiServlet;
14
15
16 public class Catalogue extends JammyJoesMelatiServlet {
17
18 private static final long serialVersionUID = 1L;
19
20 protected String jammyjoesRequest(Melati melati, ServletTemplateContext context)
21 throws Exception {
22
23 final JammyjoesDatabase db = (JammyjoesDatabase) melati.getDatabase();
24 ServletTemplateContext tc = melati.getServletTemplateContext();
25 String template = "view/CatalogueSuccess.wm";
26 String name = Form.getFormNulled(tc,"name");
27 String email = Form.getFormNulled(tc,"email");
28 String address = Form.getFormNulled(tc,"address");
29 String town = Form.getFormNulled(tc,"town");
30 String postcode = Form.getFormNulled(tc,"postcode");
31 String country = Form.getFormNulled(tc,"country");
32 String spam = Form.getFormNulled(tc,"spam");
33 User admin = (User)db.getUserTable().administratorUser();
34
35
36 final User newUser;
37 newUser = (User) db.getUserTable().newPersistent();
38 newUser.setName(name);
39 newUser.setEmail(email);
40 newUser.setAddress(address);
41 newUser.setTown(town);
42 newUser.setCountry(country);
43 newUser.setPostcode(postcode);
44 newUser.setSpam((spam != null));
45 newUser.generateDefaults();
46 PoemThread.withAccessToken(AccessToken.root, new PoemTask() {
47 public void run() {
48 db.getUserTable().create(newUser);
49 }
50 });
51
52 String message = name + " would like to receive a catalogue, so please send one to:\n\n";
53 message += name + "\n";
54 message += address + "\n";
55 message += town + "\n";
56 message += postcode + "\n\n";
57 message += email + "\n\n";
58 if (spam != null) message += "They would also like to continue to receive information about JammyJoes\n";
59
60 Email.send(db.getSettingTable().get(Email.SMTPSERVER), email, admin.getEmail(), "", "JammyJoes Catalogue Request", message);
61
62
63
64 return template;
65 }
66
67 }