]> WPIA git - gigi.git/blob - src/club/wpia/gigi/pages/orga/CreateOrgForm.java
upd: rename package name and all references to it
[gigi.git] / src / club / wpia / gigi / pages / orga / CreateOrgForm.java
1 package club.wpia.gigi.pages.orga;
2
3 import java.io.PrintWriter;
4 import java.util.Map;
5
6 import javax.servlet.http.HttpServletRequest;
7
8 import club.wpia.gigi.GigiApiException;
9 import club.wpia.gigi.dbObjects.Organisation;
10 import club.wpia.gigi.email.EmailProvider;
11 import club.wpia.gigi.localisation.Language;
12 import club.wpia.gigi.output.CountrySelector;
13 import club.wpia.gigi.output.template.Form;
14 import club.wpia.gigi.output.template.SprintfCommand;
15 import club.wpia.gigi.output.template.Template;
16 import club.wpia.gigi.pages.LoginPage;
17
18 public class CreateOrgForm extends Form {
19
20     private final static Template t = new Template(CreateOrgForm.class.getResource("CreateOrgForm.templ"));
21
22     private Organisation result;
23
24     private String o = "";
25
26     private String st = "";
27
28     private String l = "";
29
30     private String email = "";
31
32     private String optionalName = "";
33
34     private String postalAddress = "";
35
36     private boolean isEdit = false;
37
38     private CountrySelector cs;
39
40     public CreateOrgForm(HttpServletRequest hsr) {
41         super(hsr);
42         cs = new CountrySelector("C", false);
43     }
44
45     public CreateOrgForm(HttpServletRequest hsr, Organisation t) {
46         this(hsr);
47         isEdit = true;
48         result = t;
49         o = t.getName();
50
51         cs = new CountrySelector("C", false, t.getCountry());
52
53         st = t.getProvince();
54         l = t.getCity();
55         email = t.getContactEmail();
56         optionalName = t.getOptionalName();
57         postalAddress = t.getPostalAddress();
58     }
59
60     @Override
61     public SubmissionResult submit(HttpServletRequest req) throws GigiApiException {
62         String action = req.getParameter("action");
63         if (action == null) {
64             throw new GigiApiException("No action given.");
65         }
66
67         if (action.equals("new")) {
68             checkCertData(req);
69             checkOrganisationData(req);
70             Organisation ne = new Organisation(o, cs.getCountry(), st, l, email, optionalName, postalAddress, LoginPage.getUser(req));
71             result = ne;
72         } else if (action.equals("updateOrganisationData")) {
73             checkOrganisationData(req);
74             result.updateOrgData(email, optionalName, postalAddress);
75         } else if (action.equals("updateCertificateData")) {
76             checkCertData(req);
77             result.updateCertData(o, cs.getCountry(), st, l);
78         } else {
79             throw new GigiApiException("No valid action given.");
80         }
81         return new RedirectResult(ViewOrgPage.DEFAULT_PATH + "/" + result.getId());
82     }
83
84     private void checkOrganisationData(HttpServletRequest req) throws GigiApiException {
85         email = extractParam(req, "contact");
86         optionalName = extractParam(req, "optionalName");
87         postalAddress = extractParam(req, "postalAddress");
88         if ( !EmailProvider.isValidMailAddress(email)) {
89             throw new GigiApiException("Contact email is not a valid email address");
90         }
91     }
92
93     private void checkCertData(HttpServletRequest req) throws GigiApiException {
94         o = extractParam(req, "O");
95         st = extractParam(req, "ST");
96         l = extractParam(req, "L");
97
98         if (o.length() > 64 || o.length() < 1) {
99             throw new GigiApiException(SprintfCommand.createSimple("{0} not given or longer than {1} characters", "Organisation name", 64));
100         }
101
102         cs.update(req);
103
104         if (st.length() > 128 || st.length() < 1) {
105             throw new GigiApiException(SprintfCommand.createSimple("{0} not given or longer than {1} characters", "State/county", 128));
106         }
107
108         if (l.length() > 128 || l.length() < 1) {
109             throw new GigiApiException(SprintfCommand.createSimple("{0} not given or longer than {1} characters", "Town/suburb", 128));
110         }
111     }
112
113     private String extractParam(HttpServletRequest req, String name) {
114         String parameter = req.getParameter(name);
115         if (parameter == null) {
116             return "";
117         }
118         return parameter.trim();
119     }
120
121     @Override
122     protected void outputContent(PrintWriter out, Language l, Map<String, Object> vars) {
123         vars.put("O", o);
124         vars.put("C", cs);
125         vars.put("ST", st);
126         vars.put("L", this.l);
127         vars.put("email", email);
128         vars.put("optionalName", optionalName);
129         vars.put("postalAddress", postalAddress);
130         vars.put("countryCode", cs);
131         if (isEdit) {
132             vars.put("edit", true);
133         }
134         t.output(out, l, vars);
135     }
136 }