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.CountrySelector;
15 import org.cacert.gigi.output.template.Form;
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 st = "";
30 private String l = "";
32 private String email = "";
34 private String optionalName = "";
36 private String postalAddress = "";
38 private boolean isEdit = false;
40 private CountrySelector cs;
42 public CreateOrgForm(HttpServletRequest hsr) {
44 cs = new CountrySelector("C", false);
47 public CreateOrgForm(HttpServletRequest hsr, Organisation t) {
53 CountryCode orgState = null;
55 orgState = CountryCode.getCountryCode(t.getState(), CountryCodeType.CODE_2_CHARS);
56 } catch (GigiApiException e) {
59 cs = new CountrySelector("C", false, orgState);
63 email = t.getContactEmail();
64 optionalName = t.getOptionalName();
65 postalAddress = t.getPostalAddress();
69 public boolean submit(PrintWriter out, HttpServletRequest req) throws GigiApiException {
70 String action = req.getParameter("action");
75 if (action.equals("new")) {
77 checkOrganisationData(req);
78 Organisation ne = new Organisation(o, cs.getCountry(), st, l, email, optionalName, postalAddress, LoginPage.getUser(req));
81 } else if (action.equals("updateOrganisationData")) {
82 checkOrganisationData(req);
83 result.updateOrgData(email, optionalName, postalAddress);
85 } else if (action.equals("updateCertificateData")) {
87 result.updateCertData(o, cs.getCountry(), st, l);
94 private void checkOrganisationData(HttpServletRequest req) throws GigiApiException {
95 email = extractParam(req, "contact");
96 optionalName = extractParam(req, "optionalName");
97 postalAddress = extractParam(req, "postalAddress");
98 if ( !EmailProvider.isValidMailAddress(email)) {
99 throw new GigiApiException("Contact email is not a valid email address");
103 private void checkCertData(HttpServletRequest req) throws GigiApiException {
104 o = extractParam(req, "O");
105 st = extractParam(req, "ST");
106 l = extractParam(req, "L");
108 if (o.length() > 64 || o.length() < 1) {
109 throw new GigiApiException(SprintfCommand.createSimple("{0} not given or longer than {1} characters", "Organisation name", 64));
114 if (st.length() > 128 || st.length() < 1) {
115 throw new GigiApiException(SprintfCommand.createSimple("{0} not given or longer than {1} characters", "State/county", 128));
118 if (l.length() > 128 || l.length() < 1) {
119 throw new GigiApiException(SprintfCommand.createSimple("{0} not given or longer than {1} characters", "Town/suburb", 128));
123 private String extractParam(HttpServletRequest req, String name) {
124 String parameter = req.getParameter(name);
125 if (parameter == null) {
128 return parameter.trim();
131 public Organisation getResult() {
136 protected void outputContent(PrintWriter out, Language l, Map<String, Object> vars) {
140 vars.put("L", this.l);
141 vars.put("email", email);
142 vars.put("optionalName", optionalName);
143 vars.put("postalAddress", postalAddress);
144 vars.put("countryCode", cs);
146 vars.put("edit", true);
148 t.output(out, l, vars);