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.Organisation;
10 import org.cacert.gigi.email.EmailProvider;
11 import org.cacert.gigi.localisation.Language;
12 import org.cacert.gigi.output.template.Form;
13 import org.cacert.gigi.output.template.SprintfCommand;
14 import org.cacert.gigi.output.template.Template;
15 import org.cacert.gigi.pages.LoginPage;
17 public class CreateOrgForm extends Form {
19 private final static Template t = new Template(CreateOrgForm.class.getResource("CreateOrgForm.templ"));
21 private Organisation result;
23 private String o = "";
25 private String c = "";
27 private String st = "";
29 private String l = "";
31 private String email = "";
33 private String optionalName = "";
35 private String postalAddress = "";
37 private boolean isEdit = false;
39 public CreateOrgForm(HttpServletRequest hsr) {
43 public CreateOrgForm(HttpServletRequest hsr, Organisation t) {
51 email = t.getContactEmail();
52 optionalName = t.getOptionalName();
53 postalAddress = t.getPostalAddress();
57 public boolean submit(PrintWriter out, HttpServletRequest req) throws GigiApiException {
58 String action = req.getParameter("action");
63 if (action.equals("new")) {
65 checkOrganisationData(req);
66 Organisation ne = new Organisation(o, c, st, l, email, optionalName, postalAddress, LoginPage.getUser(req));
69 } else if (action.equals("updateOrganisationData")) {
70 checkOrganisationData(req);
71 result.updateOrgData(email, optionalName, postalAddress);
73 } else if (action.equals("updateCertificateData")) {
75 result.updateCertData(o, c, st, l);
82 private void checkOrganisationData(HttpServletRequest req) throws GigiApiException {
83 email = extractParam(req, "contact");
84 optionalName = extractParam(req, "optionalName");
85 postalAddress = extractParam(req, "postalAddress");
86 if ( !EmailProvider.MAIL.matcher(email).matches()) {
87 throw new GigiApiException("Contact email is not a valid email address");
91 private void checkCertData(HttpServletRequest req) throws GigiApiException {
92 o = extractParam(req, "O");
93 c = extractParam(req, "C");
94 st = extractParam(req, "ST");
95 l = extractParam(req, "L");
97 if (o.length() > 64 || o.length() < 1) {
98 throw new GigiApiException(SprintfCommand.createSimple("{0} not given or longer than {1} characters", "Organisation name", 64));
100 if (c.length() != 2) {
101 throw new GigiApiException(SprintfCommand.createSimple("{0} not given or not exactly {1} characters long", "Country code", 2));
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));
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));
113 private String extractParam(HttpServletRequest req, String name) {
114 String parameter = req.getParameter(name);
115 if (parameter == null) {
118 return parameter.trim();
121 public Organisation getResult() {
126 protected void outputContent(PrintWriter out, Language l, Map<String, Object> vars) {
130 vars.put("L", this.l);
131 vars.put("email", email);
132 vars.put("optionalName", optionalName);
133 vars.put("postalAddress", postalAddress);
135 vars.put("edit", true);
137 t.output(out, l, vars);