comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/CalculationSelect.java @ 430:7ab81ff32111 2.3

merged flys-artifacts/2.3
author Thomas Arendsen Hein <thomas@intevation.de>
date Fri, 28 Sep 2012 12:14:10 +0200
parents b33ba2cd4a3e
children 929137ee8154
comparison
equal deleted inserted replaced
290:a6f56ed9238b 430:7ab81ff32111
1 package de.intevation.flys.artifacts.states;
2
3 import java.util.Map;
4
5 import org.apache.log4j.Logger;
6
7 import org.w3c.dom.Element;
8
9 import de.intevation.artifacts.Artifact;
10 import de.intevation.artifacts.CallContext;
11 import de.intevation.artifacts.CallMeta;
12
13 import de.intevation.artifacts.common.utils.XMLUtils;
14
15 import de.intevation.artifactdatabase.ProtocolUtils;
16 import de.intevation.artifactdatabase.data.StateData;
17
18 import de.intevation.flys.artifacts.resources.Resources;
19
20 /**
21 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
22 */
23 public class CalculationSelect extends DefaultState {
24
25 /** The logger that is used in this class.*/
26 private static Logger logger = Logger.getLogger(CalculationSelect.class);
27
28
29 /** Constant value for the reference line calculation.*/
30 public static final String CALCULATION_SURFACE_CURVE =
31 "calc.surface.curve";
32
33 /** Constant value for the differences calculation.*/
34 public static final String CALCULATION_DURATION_CURVE =
35 "calc.duration.curve";
36
37 /** Constant value for the flood map calculation.*/
38 public static final String CALCULATION_FLOOD_MAP =
39 "calc.flood.map";
40
41 /** Constant value for the profile calculation.*/
42 public static final String CALCULATION_DISCHARGE_LONGITUDINAL_CURVE =
43 "calc.discharge.longitudinal.section";
44
45 /** Constant value for the state discharge curve calculation.*/
46 public static final String CALCULATION_DISCHARGE_CURVE =
47 "calc.discharge.curve";
48
49 /** An array that holds all available calculation modes.*/
50 public static final String[] CALCULATIONS = {
51 CALCULATION_SURFACE_CURVE,
52 CALCULATION_FLOOD_MAP,
53 CALCULATION_DISCHARGE_CURVE,
54 CALCULATION_DURATION_CURVE,
55 CALCULATION_DISCHARGE_LONGITUDINAL_CURVE };
56
57
58 /** Error message that is thrown if no mode has been chosen.*/
59 public static final String ERROR_NO_CALCULATION_MODE =
60 "error_feed_no_calculation_mode";
61
62 /** Error message that is thrown if an invalid calculation mode has been
63 * chosen.*/
64 public static final String ERROR_INVALID_CALCULATION_MODE =
65 "error_feed_invalid_calculation_mode";
66
67
68 protected Element[] createItems(
69 XMLUtils.ElementCreator cr,
70 Artifact artifact,
71 String name,
72 CallContext context)
73 {
74 CallMeta meta = context.getMeta();
75 Element[] calcs = new Element[CALCULATIONS.length];
76
77 int i = 0;
78
79 for (String calc: CALCULATIONS) {
80 calcs[i++] = createItem(
81 cr, new String[] {
82 Resources.getMsg(meta, calc, calc),
83 calc
84 });
85 }
86
87 return calcs;
88 }
89
90
91 protected Element createItem(XMLUtils.ElementCreator cr, Object obj) {
92 Element item = ProtocolUtils.createArtNode(cr, "item", null, null);
93 Element label = ProtocolUtils.createArtNode(cr, "label", null, null);
94 Element value = ProtocolUtils.createArtNode(cr, "value", null, null);
95
96 String[] arr = (String[]) obj;
97
98 label.setTextContent(arr[0]);
99 value.setTextContent(arr[1]);
100
101 item.appendChild(label);
102 item.appendChild(value);
103
104 return item;
105 }
106
107
108 @Override
109 public boolean validate(Artifact artifact, CallContext context)
110 throws IllegalArgumentException
111 {
112 logger.debug("CalculationSelect.validate");
113
114 Map<String, StateData> data = getData();
115
116 String calc = (String) data.get("calculation_mode").getValue();
117
118 if (calc == null) {
119 throw new IllegalArgumentException(ERROR_NO_CALCULATION_MODE);
120 }
121
122 calc = calc.trim().toLowerCase();
123
124 for (String mode: CALCULATIONS) {
125 if (mode.equals(calc)) {
126 return true;
127 }
128 }
129
130 throw new IllegalArgumentException(ERROR_INVALID_CALCULATION_MODE);
131 }
132 }
133 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :

http://dive4elements.wald.intevation.org