1 package org.cacert.gigi.pages.orga;
3 import java.io.PrintWriter;
6 import javax.servlet.http.HttpServletRequest;
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;
20 public class CreateOrgForm extends Form {
22 private final static Template t = new Template(CreateOrgForm.class.getResource("CreateOrgForm.templ"));
24 private Organisation result;
26 private String o = "";
28 private String c = "";
30 private String st = "";
32 private String l = "";
34 private String email = "";
36 private String optionalName = "";
38 private String postalAddress = "";
40 private boolean isEdit = false;
42 private CountryCode[] countryCode;
44 public CreateOrgForm(HttpServletRequest hsr) {
47 countryCode = CountryCode.getCountryCodes(CountryCodeType.CODE_2_CHARS);
48 } catch (GigiApiException e) {
49 throw new Error(e); // should not happen
53 public CreateOrgForm(HttpServletRequest hsr, Organisation t) {
61 email = t.getContactEmail();
62 optionalName = t.getOptionalName();
63 postalAddress = t.getPostalAddress();
67 public boolean submit(PrintWriter out, HttpServletRequest req) throws GigiApiException {
68 String action = req.getParameter("action");
73 if (action.equals("new")) {
75 checkOrganisationData(req);
76 Organisation ne = new Organisation(o, c, st, l, email, optionalName, postalAddress, LoginPage.getUser(req));
79 } else if (action.equals("updateOrganisationData")) {
80 checkOrganisationData(req);
81 result.updateOrgData(email, optionalName, postalAddress);
83 } else if (action.equals("updateCertificateData")) {
85 result.updateCertData(o, c, st, l);
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");
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");
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));
111 CountryCode.checkCountryCode(c, CountryCodeType.CODE_2_CHARS);
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));
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));
122 private String extractParam(HttpServletRequest req, String name) {
123 String parameter = req.getParameter(name);
124 if (parameter == null) {
127 return parameter.trim();
130 public Organisation getResult() {
135 protected void outputContent(PrintWriter out, Language l, Map<String, Object> vars) {
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() {
148 public boolean next(Language l, Map<String, Object> vars) {
149 if (i >= countryCode.length) {
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");
159 vars.put("selected", "");
164 // vars.put("countryCode", countryCode);
166 vars.put("edit", true);
168 t.output(out, l, vars);