]> WPIA git - gigi.git/blob - src/org/cacert/gigi/pages/orga/CreateOrgForm.java
upd: make CountryCode a Multiton
[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.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;
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 st = "";
29
30     private String l = "";
31
32     private String email = "";
33
34     private String optionalName = "";
35
36     private String postalAddress = "";
37
38     private boolean isEdit = false;
39
40     private CountrySelector cs;
41
42     public CreateOrgForm(HttpServletRequest hsr) {
43         super(hsr);
44         cs = new CountrySelector("C", false);
45     }
46
47     public CreateOrgForm(HttpServletRequest hsr, Organisation t) {
48         this(hsr);
49         isEdit = true;
50         result = t;
51         o = t.getName();
52
53         CountryCode orgState = null;
54         try {
55             orgState = CountryCode.getCountryCode(t.getState(), CountryCodeType.CODE_2_CHARS);
56         } catch (GigiApiException e) {
57             throw new Error(e);
58         }
59         cs = new CountrySelector("C", false, orgState);
60
61         st = t.getProvince();
62         l = t.getCity();
63         email = t.getContactEmail();
64         optionalName = t.getOptionalName();
65         postalAddress = t.getPostalAddress();
66     }
67
68     @Override
69     public boolean submit(PrintWriter out, HttpServletRequest req) throws GigiApiException {
70         String action = req.getParameter("action");
71         if (action == null) {
72             return false;
73         }
74
75         if (action.equals("new")) {
76             checkCertData(req);
77             checkOrganisationData(req);
78             Organisation ne = new Organisation(o, cs.getCountry(), st, l, email, optionalName, postalAddress, LoginPage.getUser(req));
79             result = ne;
80             return true;
81         } else if (action.equals("updateOrganisationData")) {
82             checkOrganisationData(req);
83             result.updateOrgData(email, optionalName, postalAddress);
84             return true;
85         } else if (action.equals("updateCertificateData")) {
86             checkCertData(req);
87             result.updateCertData(o, cs.getCountry(), st, l);
88             return true;
89         }
90
91         return false;
92     }
93
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");
100         }
101     }
102
103     private void checkCertData(HttpServletRequest req) throws GigiApiException {
104         o = extractParam(req, "O");
105         st = extractParam(req, "ST");
106         l = extractParam(req, "L");
107
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));
110         }
111
112         cs.update(req);
113
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));
116         }
117
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));
120         }
121     }
122
123     private String extractParam(HttpServletRequest req, String name) {
124         String parameter = req.getParameter(name);
125         if (parameter == null) {
126             return "";
127         }
128         return parameter.trim();
129     }
130
131     public Organisation getResult() {
132         return result;
133     }
134
135     @Override
136     protected void outputContent(PrintWriter out, Language l, Map<String, Object> vars) {
137         vars.put("O", o);
138         vars.put("C", cs);
139         vars.put("ST", st);
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);
145         if (isEdit) {
146             vars.put("edit", true);
147         }
148         t.output(out, l, vars);
149     }
150 }