1 package club.wpia.gigi.pages.orga;
3 import java.io.PrintWriter;
6 import javax.servlet.http.HttpServletRequest;
8 import club.wpia.gigi.GigiApiException;
9 import club.wpia.gigi.dbObjects.Organisation;
10 import club.wpia.gigi.email.EmailProvider;
11 import club.wpia.gigi.localisation.Language;
12 import club.wpia.gigi.output.CountrySelector;
13 import club.wpia.gigi.output.template.Form;
14 import club.wpia.gigi.output.template.SprintfCommand;
15 import club.wpia.gigi.output.template.Template;
16 import club.wpia.gigi.pages.LoginPage;
18 public class CreateOrgForm extends Form {
20 private final static Template t = new Template(CreateOrgForm.class.getResource("CreateOrgForm.templ"));
22 private Organisation result;
24 private String o = "";
26 private String st = "";
28 private String l = "";
30 private String email = "";
32 private String optionalName = "";
34 private String postalAddress = "";
36 private boolean isEdit = false;
38 private CountrySelector cs;
40 public CreateOrgForm(HttpServletRequest hsr) {
42 cs = new CountrySelector("C", false);
45 public CreateOrgForm(HttpServletRequest hsr, Organisation t) {
51 cs = new CountrySelector("C", false, t.getCountry());
55 email = t.getContactEmail();
56 optionalName = t.getOptionalName();
57 postalAddress = t.getPostalAddress();
61 public SubmissionResult submit(HttpServletRequest req) throws GigiApiException {
62 String action = req.getParameter("action");
64 throw new GigiApiException("No action given.");
67 if (action.equals("new")) {
69 checkOrganisationData(req);
70 Organisation ne = new Organisation(o, cs.getCountry(), st, l, email, optionalName, postalAddress, LoginPage.getUser(req));
72 } else if (action.equals("updateOrganisationData")) {
73 checkOrganisationData(req);
74 result.updateOrgData(email, optionalName, postalAddress);
75 } else if (action.equals("updateCertificateData")) {
77 result.updateCertData(o, cs.getCountry(), st, l);
79 throw new GigiApiException("No valid action given.");
81 return new RedirectResult(ViewOrgPage.DEFAULT_PATH + "/" + result.getId());
84 private void checkOrganisationData(HttpServletRequest req) throws GigiApiException {
85 email = extractParam(req, "contact");
86 optionalName = extractParam(req, "optionalName");
87 postalAddress = extractParam(req, "postalAddress");
88 if ( !EmailProvider.isValidMailAddress(email)) {
89 throw new GigiApiException("Contact email is not a valid email address");
93 private void checkCertData(HttpServletRequest req) throws GigiApiException {
94 o = extractParam(req, "O");
95 st = extractParam(req, "ST");
96 l = extractParam(req, "L");
98 if (o.length() > 64 || o.length() < 1) {
99 throw new GigiApiException(SprintfCommand.createSimple("{0} not given or longer than {1} characters", "Organisation name", 64));
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();
122 protected void outputContent(PrintWriter out, Language l, Map<String, Object> vars) {
126 vars.put("L", this.l);
127 vars.put("email", email);
128 vars.put("optionalName", optionalName);
129 vars.put("postalAddress", postalAddress);
130 vars.put("countryCode", cs);
132 vars.put("edit", true);
134 t.output(out, l, vars);