comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/RiverSelect.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 82bd39f27569
children 929137ee8154
comparison
equal deleted inserted replaced
290:a6f56ed9238b 430:7ab81ff32111
1 package de.intevation.flys.artifacts.states;
2
3 import java.util.List;
4 import java.util.Map;
5
6 import org.apache.log4j.Logger;
7
8 import org.w3c.dom.Element;
9 import org.w3c.dom.Node;
10
11 import de.intevation.artifacts.Artifact;
12 import de.intevation.artifacts.CallContext;
13
14 import de.intevation.artifacts.common.utils.XMLUtils;
15
16 import de.intevation.artifactdatabase.ProtocolUtils;
17 import de.intevation.artifactdatabase.data.StateData;
18
19 import de.intevation.flys.model.River;
20
21 import de.intevation.flys.artifacts.model.RiverFactory;
22 import de.intevation.flys.artifacts.resources.Resources;
23
24
25 /**
26 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
27 */
28 public class RiverSelect extends DefaultState {
29
30 /** The logger used in this class. */
31 private static Logger logger = Logger.getLogger(RiverSelect.class);
32
33
34 /** Error message that is thrown if no river was found based on a given
35 * name.*/
36 public static final String ERROR_NO_SUCH_RIVER =
37 "error_feed_no_such_river";
38
39 /** Error message that is thrown if no river was found based on a given
40 * name.*/
41 public static final String ERROR_NO_RIVER_SELECTED =
42 "error_feed_no_river_selected";
43
44
45 /**
46 * The default constructor that initializes an empty State object.
47 */
48 public RiverSelect() {
49 }
50
51
52 /**
53 * Initialize the state based on the state node in the configuration.
54 *
55 * @param config The state configuration node.
56 */
57 public void setup(Node config) {
58 super.setup(config);
59 }
60
61
62 protected Element createData(
63 XMLUtils.ElementCreator cr,
64 Artifact artifact,
65 StateData data,
66 CallContext context)
67 {
68 Element select = ProtocolUtils.createArtNode(
69 cr, "select",
70 new String[] { "uiprovider" },
71 new String[] { "select_with_map" });
72 cr.addAttr(select, "name", data.getName(), true);
73
74 Element label = ProtocolUtils.createArtNode(
75 cr, "label", null, null);
76
77 Element choices = ProtocolUtils.createArtNode(
78 cr, "choices", null, null);
79
80 select.appendChild(label);
81
82 label.setTextContent(Resources.getMsg(
83 context.getMeta(),
84 getID(),
85 getID()));
86
87 return select;
88 }
89
90
91 protected Element[] createItems(
92 XMLUtils.ElementCreator cr,
93 Artifact artifact,
94 String name,
95 CallContext context)
96 {
97 List<River> rivers = RiverFactory.getRivers();
98 Element[] items = new Element[rivers.size()];
99
100 int idx = 0;
101 for (River river: rivers) {
102 items[idx++] = createRiverItem(cr, river);
103 }
104
105 return items;
106 }
107
108
109 /**
110 * This method creates a node that represents a river item. This node
111 * contains the label and the value that describe the river.
112 *
113 * @param cr The ElementCreator.
114 * @param river The river.
115 *
116 * @return the element that contains the information about the river.
117 */
118 protected Element createRiverItem(XMLUtils.ElementCreator cr, River river) {
119 Element item = ProtocolUtils.createArtNode(cr, "item", null, null);
120 Element label = ProtocolUtils.createArtNode(cr, "label", null, null);
121 Element value = ProtocolUtils.createArtNode(cr, "value", null, null);
122
123 label.setTextContent(river.getName());
124 value.setTextContent(river.getName());
125
126 item.appendChild(label);
127 item.appendChild(value);
128
129 return item;
130 }
131
132
133 @Override
134 public boolean validate(Artifact artifact, CallContext context)
135 throws IllegalArgumentException
136 {
137 logger.debug("RiverSelect.validate");
138
139 Map<String, StateData> data = getData();
140
141 StateData dRiver = data.get("river");
142
143 if (dRiver == null || dRiver.getValue() == null) {
144 throw new IllegalArgumentException(ERROR_NO_RIVER_SELECTED);
145 }
146
147 River river = RiverFactory.getRiver((String) dRiver.getValue());
148
149 if (river == null) {
150 throw new IllegalArgumentException(ERROR_NO_SUCH_RIVER);
151 }
152
153 return true;
154 }
155
156
157 @Override
158 protected String getUIProvider() {
159 return "river_panel";
160 }
161 }
162 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :

http://dive4elements.wald.intevation.org