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