]> WPIA git - gigi.git/blob - src/org/cacert/gigi/output/CountrySelector.java
upd: narrowing type-safety around Organisation
[gigi.git] / src / org / cacert / gigi / output / CountrySelector.java
1 package org.cacert.gigi.output;
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.localisation.Language;
12 import org.cacert.gigi.output.template.Outputable;
13 import org.cacert.gigi.output.template.Template;
14
15 public class CountrySelector implements Outputable {
16
17     private static final Template t = new Template(CountrySelector.class.getResource("CountrySelector.templ"));
18
19     private CountryCode[] all = CountryCode.getCountryCodes(CountryCodeType.CODE_2_CHARS);
20
21     private String name;
22
23     private CountryCode selected;
24
25     private boolean optional;
26
27     public CountrySelector(String name, boolean optional) {
28         this.name = name;
29         this.optional = optional;
30     }
31
32     public CountrySelector(String name, boolean optional, CountryCode state) {
33         this(name, optional);
34         selected = state == null ? null : state.convertToCountryCodeType(CountryCodeType.CODE_2_CHARS);
35         if (state.getCountryCodeType() != CountryCodeType.CODE_2_CHARS) {
36             throw new IllegalArgumentException("Got country code of illegal type.");
37         }
38         selected = state;
39     }
40
41     public void update(HttpServletRequest r) throws GigiApiException {
42         String vS = r.getParameter(name);
43
44         selected = null;
45
46         if (vS == null || vS.equals("invalid")) {
47             if (optional) {
48                 return;
49             } else {
50                 throw new GigiApiException("Country code required.");
51             }
52         }
53
54         selected = CountryCode.getCountryCode(vS, CountryCodeType.CODE_2_CHARS);
55     }
56
57     @Override
58     public void output(PrintWriter out, Language l, Map<String, Object> vars) {
59         vars.put("countryCode", new ArrayIterable<CountryCode>(all) {
60
61             @Override
62             public void apply(CountryCode t, Language l, Map<String, Object> vars) {
63                 vars.put("cc", t.getCountryCode());
64                 vars.put("display", t.getCountry());
65                 if (selected != null && t.getCountryCode().equals(selected.getCountryCode())) {
66                     vars.put("selected", "selected");
67                 } else {
68                     vars.put("selected", "");
69                 }
70             }
71
72         });
73
74         vars.put("optional", optional);
75         vars.put("name", name);
76
77         t.output(out, l, vars);
78     }
79
80     public CountryCode getCountry() {
81         return selected;
82     }
83
84 }