]> WPIA git - gigi.git/blob - src/org/cacert/gigi/pages/orga/CreateOrgForm.java
fix: Use internal fields csr, csrType and dn
[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.CountryCode.CountryCodeType;
11 import org.cacert.gigi.dbObjects.Organisation;
12 import org.cacert.gigi.email.EmailProvider;
13 import org.cacert.gigi.localisation.Language;
14 import org.cacert.gigi.output.template.Form;
15 import org.cacert.gigi.output.template.IterableDataset;
16 import org.cacert.gigi.output.template.SprintfCommand;
17 import org.cacert.gigi.output.template.Template;
18 import org.cacert.gigi.pages.LoginPage;
19
20 public class CreateOrgForm extends Form {
21
22     private final static Template t = new Template(CreateOrgForm.class.getResource("CreateOrgForm.templ"));
23
24     private Organisation result;
25
26     private String o = "";
27
28     private String c = "";
29
30     private String st = "";
31
32     private String l = "";
33
34     private String email = "";
35
36     private String optionalName = "";
37
38     private String postalAddress = "";
39
40     private boolean isEdit = false;
41
42     private CountryCode[] countryCode;
43
44     public CreateOrgForm(HttpServletRequest hsr) {
45         super(hsr);
46         try {
47             countryCode = CountryCode.getCountryCodes(CountryCodeType.CODE_2_CHARS);
48         } catch (GigiApiException e) {
49             throw new Error(e); // should not happen
50         }
51     }
52
53     public CreateOrgForm(HttpServletRequest hsr, Organisation t) {
54         this(hsr);
55         isEdit = true;
56         result = t;
57         o = t.getName();
58         c = t.getState();
59         st = t.getProvince();
60         l = t.getCity();
61         email = t.getContactEmail();
62         optionalName = t.getOptionalName();
63         postalAddress = t.getPostalAddress();
64     }
65
66     @Override
67     public boolean submit(PrintWriter out, HttpServletRequest req) throws GigiApiException {
68         String action = req.getParameter("action");
69         if (action == null) {
70             return false;
71         }
72
73         if (action.equals("new")) {
74             checkCertData(req);
75             checkOrganisationData(req);
76             Organisation ne = new Organisation(o, c, st, l, email, optionalName, postalAddress, LoginPage.getUser(req));
77             result = ne;
78             return true;
79         } else if (action.equals("updateOrganisationData")) {
80             checkOrganisationData(req);
81             result.updateOrgData(email, optionalName, postalAddress);
82             return true;
83         } else if (action.equals("updateCertificateData")) {
84             checkCertData(req);
85             result.updateCertData(o, c, st, l);
86             return true;
87         }
88
89         return false;
90     }
91
92     private void checkOrganisationData(HttpServletRequest req) throws GigiApiException {
93         email = extractParam(req, "contact");
94         optionalName = extractParam(req, "optionalName");
95         postalAddress = extractParam(req, "postalAddress");
96         if ( !EmailProvider.MAIL.matcher(email).matches()) {
97             throw new GigiApiException("Contact email is not a valid email address");
98         }
99     }
100
101     private void checkCertData(HttpServletRequest req) throws GigiApiException {
102         o = extractParam(req, "O");
103         c = extractParam(req, "C").toUpperCase();
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         CountryCode.checkCountryCode(c, CountryCodeType.CODE_2_CHARS);
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", c);
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", new IterableDataset() {
144
145             int i = 0;
146
147             @Override
148             public boolean next(Language l, Map<String, Object> vars) {
149                 if (i >= countryCode.length) {
150                     return false;
151                 }
152                 CountryCode t = countryCode[i++];
153                 vars.put("id", t.getId());
154                 vars.put("cc", t.getCountryCode());
155                 vars.put("display", t.getCountry());
156                 if (t.getCountryCode().equals(c)) {
157                     vars.put("selected", "selected");
158                 } else {
159                     vars.put("selected", "");
160                 }
161                 return true;
162             }
163         });
164         // vars.put("countryCode", countryCode);
165         if (isEdit) {
166             vars.put("edit", true);
167         }
168         t.output(out, l, vars);
169     }
170 }