Mercurial > dive4elements > river
comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/FLYSArtifact.java @ 121:e0ded17a4846
Implemented the feed() operation.
flys-artifacts/trunk@1442 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Ingo Weinzierl <ingo.weinzierl@intevation.de> |
---|---|
date | Wed, 09 Mar 2011 14:13:57 +0000 |
parents | 84c0b151203e |
children | d3b8b0b1d010 |
comparison
equal
deleted
inserted
replaced
120:5243ac559e16 | 121:e0ded17a4846 |
---|---|
1 package de.intevation.flys.artifacts; | 1 package de.intevation.flys.artifacts; |
2 | 2 |
3 import java.util.HashMap; | |
3 import java.util.List; | 4 import java.util.List; |
5 import java.util.Map; | |
6 | |
7 import javax.xml.xpath.XPathConstants; | |
4 | 8 |
5 import org.apache.log4j.Logger; | 9 import org.apache.log4j.Logger; |
6 | 10 |
7 import org.w3c.dom.Document; | 11 import org.w3c.dom.Document; |
12 import org.w3c.dom.Element; | |
13 import org.w3c.dom.Node; | |
14 import org.w3c.dom.NodeList; | |
8 | 15 |
9 import de.intevation.artifacts.ArtifactFactory; | 16 import de.intevation.artifacts.ArtifactFactory; |
10 import de.intevation.artifacts.CallContext; | 17 import de.intevation.artifacts.CallContext; |
11 | 18 |
12 import de.intevation.artifacts.common.ArtifactNamespaceContext; | 19 import de.intevation.artifacts.common.ArtifactNamespaceContext; |
13 import de.intevation.artifacts.common.utils.XMLUtils; | 20 import de.intevation.artifacts.common.utils.XMLUtils; |
14 | 21 |
15 import de.intevation.artifactdatabase.DefaultArtifact; | 22 import de.intevation.artifactdatabase.DefaultArtifact; |
23 import de.intevation.artifactdatabase.data.DefaultStateData; | |
16 import de.intevation.artifactdatabase.data.StateData; | 24 import de.intevation.artifactdatabase.data.StateData; |
17 import de.intevation.artifactdatabase.state.State; | 25 import de.intevation.artifactdatabase.state.State; |
18 import de.intevation.artifactdatabase.state.StateEngine; | 26 import de.intevation.artifactdatabase.state.StateEngine; |
19 | 27 |
20 import de.intevation.flys.artifacts.context.FLYSContext; | 28 import de.intevation.flys.artifacts.context.FLYSContext; |
29 | 37 |
30 /** The logger that is used in this artifact.*/ | 38 /** The logger that is used in this artifact.*/ |
31 private static Logger logger = Logger.getLogger(FLYSArtifact.class); | 39 private static Logger logger = Logger.getLogger(FLYSArtifact.class); |
32 | 40 |
33 | 41 |
34 /** The XPath to the name of the artifact in its configuration. */ | 42 /** The XPath that points to the input data elements of the FEED document.*/ |
35 public static final String XPATH_ARTIFACT_NAME = "@name"; | 43 public static final String XPATH_FEED_INPUT = |
44 "/art:action/art:data/art:input"; | |
36 | 45 |
37 | 46 |
38 /** The identifier of the current state. */ | 47 /** The identifier of the current state. */ |
39 protected String currentStateId; | 48 protected String currentStateId; |
40 | 49 |
41 /** The name of the artifact.*/ | 50 /** The name of the artifact.*/ |
42 protected String name; | 51 protected String name; |
52 | |
53 /** The data that have been inserted into this artifact.*/ | |
54 protected Map<String, StateData> data; | |
55 | |
56 | |
57 /** | |
58 * The default constructor that creates an empty FLYSArtifact. | |
59 */ | |
60 public FLYSArtifact() { | |
61 data = new HashMap<String, StateData>(); | |
62 } | |
63 | |
64 | |
65 /** | |
66 * Returns the name of the concrete artifact. | |
67 * | |
68 * @return the name of the concrete artifact. | |
69 */ | |
70 public abstract String getName(); | |
71 | |
72 | |
73 /** | |
74 * Returns the FLYSContext from context object. | |
75 * | |
76 * @param context The CallContext or the FLYSContext. | |
77 * | |
78 * @return the FLYSContext. | |
79 */ | |
80 protected FLYSContext getFlysContext(Object context) { | |
81 return context instanceof FLYSContext | |
82 ? (FLYSContext) context | |
83 : (FLYSContext) ((CallContext) context).globalContext(); | |
84 } | |
43 | 85 |
44 | 86 |
45 /** | 87 /** |
46 * Initialize the artifact and insert new data if <code>data</code> contains | 88 * Initialize the artifact and insert new data if <code>data</code> contains |
47 * information necessary for this artifact. | 89 * information necessary for this artifact. |
60 { | 102 { |
61 logger.debug("Setup this artifact with the uuid: " + identifier); | 103 logger.debug("Setup this artifact with the uuid: " + identifier); |
62 | 104 |
63 super.setup(identifier, factory, context, data); | 105 super.setup(identifier, factory, context, data); |
64 | 106 |
65 String name = XMLUtils.xpathString( | |
66 data, XPATH_ARTIFACT_NAME, ArtifactNamespaceContext.INSTANCE); | |
67 setName(name); | |
68 | |
69 FLYSContext flysContext = (FLYSContext) context; | 107 FLYSContext flysContext = (FLYSContext) context; |
70 StateEngine engine = (StateEngine) flysContext.get( | 108 StateEngine engine = (StateEngine) flysContext.get( |
71 FLYSContext.STATE_ENGINE_KEY); | 109 FLYSContext.STATE_ENGINE_KEY); |
72 | 110 |
111 String name = getName(); | |
112 | |
113 logger.debug("Set initial state for artifact '" + name + "'"); | |
73 List<State> states = engine.getStates(name); | 114 List<State> states = engine.getStates(name); |
74 | 115 |
75 setCurrentState(states.get(0)); | 116 setCurrentState(states.get(0)); |
76 } | 117 } |
77 | 118 |
84 * | 125 * |
85 * @return a document that contains a SUCCESS or FAILURE message. | 126 * @return a document that contains a SUCCESS or FAILURE message. |
86 */ | 127 */ |
87 @Override | 128 @Override |
88 public Document feed(Document target, CallContext context) { | 129 public Document feed(Document target, CallContext context) { |
89 Document result = XMLUtils.newDocument(); | 130 Document doc = XMLUtils.newDocument(); |
90 | 131 |
91 // TODO IMPLEMENT ME | 132 XMLUtils.ElementCreator creator = new XMLUtils.ElementCreator( |
92 | 133 doc, |
93 return result; | 134 ArtifactNamespaceContext.NAMESPACE_URI, |
94 } | 135 ArtifactNamespaceContext.NAMESPACE_PREFIX); |
95 | 136 |
96 | 137 Element result = creator.create("result"); |
97 /** | 138 doc.appendChild(result); |
98 * This method returns the name of the concrete artifact. | 139 |
99 * | 140 try { |
100 * @return the name of the concrete artifact. | 141 saveData(target, XPATH_FEED_INPUT); |
101 */ | 142 creator.addAttr(result, "type", "SUCCESS", true); |
102 public String getName() { | 143 } |
103 return name; | 144 catch (IllegalArgumentException iae) { |
104 } | 145 creator.addAttr(result, "type", "FAILURE", true); |
105 | 146 |
106 | 147 // TODO I18N this message - getMessage() returns a lookup string, no |
107 | 148 // human readable error message |
108 /** | 149 result.setTextContent(iae.getMessage()); |
109 * This method sets the name of this artifact. | 150 } |
110 * | 151 |
111 * @param name the name for this artifact. | 152 return doc; |
112 */ | |
113 protected void setName(String name) { | |
114 this.name = name; | |
115 } | 153 } |
116 | 154 |
117 | 155 |
118 /** | 156 /** |
119 * Returns the identifier of the current state. | 157 * Returns the identifier of the current state. |
131 * @param id the identifier of a state. | 169 * @param id the identifier of a state. |
132 */ | 170 */ |
133 protected void setCurrentStateId(String id) { | 171 protected void setCurrentStateId(String id) { |
134 currentStateId = id; | 172 currentStateId = id; |
135 } | 173 } |
136 | |
137 | 174 |
138 | 175 |
139 /** | 176 /** |
140 * Set the current state of this artifact. <b>NOTE</b>We don't store the | 177 * Set the current state of this artifact. <b>NOTE</b>We don't store the |
141 * State object itself - which is not necessary - but its identifier. So | 178 * State object itself - which is not necessary - but its identifier. So |
153 * Returns the current state of the artifact. | 190 * Returns the current state of the artifact. |
154 * | 191 * |
155 * @return the current State of the artifact. | 192 * @return the current State of the artifact. |
156 */ | 193 */ |
157 protected State getCurrentState(Object context) { | 194 protected State getCurrentState(Object context) { |
158 FLYSContext flysContext = (FLYSContext) context; | 195 FLYSContext flysContext = getFlysContext(context); |
159 StateEngine engine = (StateEngine) flysContext.get( | 196 StateEngine engine = (StateEngine) flysContext.get( |
160 FLYSContext.STATE_ENGINE_KEY); | 197 FLYSContext.STATE_ENGINE_KEY); |
161 | 198 |
162 return engine.getState(getCurrentStateId()); | 199 return engine.getState(getCurrentStateId()); |
163 } | 200 } |
164 | 201 |
165 | 202 |
166 /** | 203 /** |
167 * This method extracts the data that is contained in the FEED document. | 204 * Adds a new StateData item to the data pool of this artifact. |
205 * | |
206 * @param name the name of the data object. | |
207 * @param data the data object itself. | |
208 */ | |
209 protected void addData(String name, StateData data) { | |
210 this.data.put(name, data); | |
211 } | |
212 | |
213 | |
214 /** | |
215 * This method stores the data that is contained in the FEED document. | |
168 * | 216 * |
169 * @param feed The FEED document. | 217 * @param feed The FEED document. |
170 * @param xpath The XPath that points to the data nodes. | 218 * @param xpath The XPath that points to the data nodes. |
171 * | 219 */ |
172 * @return a StateData array. | 220 public void saveData(Document feed, String xpath) |
173 */ | 221 throws IllegalArgumentException |
174 public StateData[] extractData(Document feed, String xpath) { | 222 { |
175 | 223 if (feed == null || xpath == null || xpath.length() == 0) { |
176 // TODO IMPLEMENT ME | 224 throw new IllegalArgumentException("feed.no.input.data"); |
177 return null; | 225 } |
226 | |
227 NodeList nodes = (NodeList) XMLUtils.xpath( | |
228 feed, | |
229 xpath, | |
230 XPathConstants.NODESET, | |
231 ArtifactNamespaceContext.INSTANCE); | |
232 | |
233 if (nodes == null || nodes.getLength() == 0) { | |
234 throw new IllegalArgumentException("feed.no.input.data"); | |
235 } | |
236 | |
237 int count = nodes.getLength(); | |
238 logger.debug("Try to save " + count + " data items."); | |
239 | |
240 for (int i = 0; i < count; i++) { | |
241 Node node = nodes.item(i); | |
242 | |
243 String name = XMLUtils.xpathString( | |
244 node, "@art:name", ArtifactNamespaceContext.INSTANCE); | |
245 String value = XMLUtils.xpathString( | |
246 node, "@art:value", ArtifactNamespaceContext.INSTANCE); | |
247 | |
248 if (name != null && value != null) { | |
249 logger.debug("Save data item for '" + name + "' : " + value); | |
250 | |
251 // TODO ADD INPUT VALIDATION! | |
252 addData(name, new DefaultStateData(name, null, null, value)); | |
253 } | |
254 } | |
178 } | 255 } |
179 } | 256 } |
180 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : | 257 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |