Mercurial > dive4elements > river
comparison flys-client/src/main/java/org/dive4elements/river/client/server/FLYSArtifactCreator.java @ 5834:f507086aa94b
Repaired internal references.
author | Sascha L. Teichmann <teichmann@intevation.de> |
---|---|
date | Thu, 25 Apr 2013 12:31:32 +0200 |
parents | flys-client/src/main/java/de/intevation/flys/client/server/FLYSArtifactCreator.java@b660090b417d |
children | 821a02bbfb4e |
comparison
equal
deleted
inserted
replaced
5833:a2bdc0f524e8 | 5834:f507086aa94b |
---|---|
1 package de.intevation.flys.client.server; | |
2 | |
3 import java.util.ArrayList; | |
4 import java.util.List; | |
5 | |
6 import javax.xml.xpath.XPathConstants; | |
7 | |
8 import org.w3c.dom.Document; | |
9 import org.w3c.dom.Element; | |
10 import org.w3c.dom.NodeList; | |
11 | |
12 import org.apache.log4j.Logger; | |
13 | |
14 import de.intevation.artifacts.common.utils.XMLUtils; | |
15 import de.intevation.artifacts.common.ArtifactNamespaceContext; | |
16 | |
17 import de.intevation.artifacts.httpclient.utils.ArtifactCreator; | |
18 | |
19 import de.intevation.flys.client.shared.model.Artifact; | |
20 import de.intevation.flys.client.shared.model.CalculationMessage; | |
21 import de.intevation.flys.client.shared.model.ChartArtifact; | |
22 import de.intevation.flys.client.shared.model.DefaultArtifact; | |
23 import de.intevation.flys.client.shared.model.FixAnalysisArtifact; | |
24 import de.intevation.flys.client.shared.model.GaugeDischargeCurveArtifact; | |
25 import de.intevation.flys.client.shared.model.MapArtifact; | |
26 import de.intevation.flys.client.shared.model.MINFOArtifact; | |
27 import de.intevation.flys.client.shared.model.StaticSQRelationArtifact; | |
28 import de.intevation.flys.client.shared.model.WINFOArtifact; | |
29 | |
30 | |
31 /** | |
32 * An implementation of an {@link ArtifactCreator}. This class uses the document | |
33 * that is returned by the artifact server to parse important information (like | |
34 * uuid, hash) and returns a new {@link Artifact} instance. | |
35 * | |
36 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a> | |
37 */ | |
38 public class FLYSArtifactCreator implements ArtifactCreator { | |
39 | |
40 private static final Logger logger = | |
41 Logger.getLogger(FLYSArtifactCreator.class); | |
42 | |
43 | |
44 /** The XPath to the artifact's uuid.*/ | |
45 public static final String XPATH_UUID = "/art:result/art:uuid/@art:value"; | |
46 | |
47 /** The XPath to the artifact's hash value.*/ | |
48 public static final String XPATH_HASH = "/art:result/art:hash/@art:value"; | |
49 | |
50 /** The XPath to the artifact's name value.*/ | |
51 public static final String XPATH_NAME = "/art:result/art:name/@art:value"; | |
52 | |
53 /** The XPath to the value that determines if the artifact is processing in | |
54 * background.*/ | |
55 public static final String XPATH_BACKGROUND_VALUE = | |
56 "/art:result/art:background-processing/@art:value"; | |
57 | |
58 /** The XPath that points to the (if existing) background messages.*/ | |
59 public static final String XPATH_BACKGROUND = | |
60 "/art:result/art:background-processing"; | |
61 | |
62 | |
63 /** | |
64 * Creates a new instance of an {@link ArtifactCreator}. | |
65 */ | |
66 public FLYSArtifactCreator() { | |
67 } | |
68 | |
69 | |
70 /** | |
71 * This concreate implementation returns an instance of {@link Artifact} | |
72 * that is used in the FLYS GWT Client code. | |
73 * | |
74 * @param doc A document that describes the artifact that has been created | |
75 * in the artifact server. | |
76 * | |
77 * @return an instance if {@link Artifact}. | |
78 */ | |
79 public Object create(Document doc) { | |
80 Artifact artifact = extractArtifact(doc); | |
81 artifact.setArtifactDescription( | |
82 ArtifactDescriptionFactory.createArtifactDescription(doc)); | |
83 | |
84 return artifact; | |
85 } | |
86 | |
87 | |
88 /** | |
89 * This method extracts the UUID und HASH information of the returned | |
90 * artifact document. | |
91 * | |
92 * @param doc The result of the CREATE operation. | |
93 * | |
94 * @return an instance of an {@link Artifact}. | |
95 */ | |
96 protected Artifact extractArtifact(Document doc) { | |
97 logger.debug("FLYSArtifactCreator - extractArtifact()"); | |
98 | |
99 String uuid = XMLUtils.xpathString( | |
100 doc, XPATH_UUID, ArtifactNamespaceContext.INSTANCE); | |
101 | |
102 String hash = XMLUtils.xpathString( | |
103 doc, XPATH_HASH, ArtifactNamespaceContext.INSTANCE); | |
104 | |
105 String name = XMLUtils.xpathString( | |
106 doc, XPATH_NAME, ArtifactNamespaceContext.INSTANCE); | |
107 | |
108 String backgroundStr = XMLUtils.xpathString( | |
109 doc, XPATH_BACKGROUND_VALUE, ArtifactNamespaceContext.INSTANCE); | |
110 | |
111 boolean background = false; | |
112 if (backgroundStr != null && backgroundStr.length() > 0) { | |
113 background = Boolean.valueOf(backgroundStr); | |
114 } | |
115 | |
116 List<CalculationMessage> msg = parseBackgroundMessages(doc); | |
117 | |
118 logger.debug("NEW Artifact UUID: " + uuid); | |
119 logger.debug("NEW Artifact HASH: " + hash); | |
120 logger.debug("NEW Artifact NAME: " + name); | |
121 logger.debug("NEW Artifact IN BACKGROUND: " + background); | |
122 | |
123 if (name == null) { | |
124 return new DefaultArtifact(uuid, hash, background, msg); | |
125 } | |
126 | |
127 name = name.trim(); | |
128 | |
129 if (name.length() > 0 && name.equals("winfo")) { | |
130 logger.debug("+++++ NEW WINFO ARTIFACT."); | |
131 return new WINFOArtifact(uuid, hash, background, msg); | |
132 } | |
133 else if (name.length() > 0 && name.equals("new_map")) { | |
134 logger.debug("+++++ NEW MAP ARTIFACT."); | |
135 return new MapArtifact(uuid, hash, background, msg); | |
136 } | |
137 else if (name.length() > 0 && name.equals("new_chart")) { | |
138 logger.debug("+++++ NEW CHART ARTIFACT."); | |
139 return new ChartArtifact(uuid, hash, background, msg); | |
140 } | |
141 else if (name.length() > 0 && name.equals("minfo")) { | |
142 logger.debug("+++++ NEW MINFO ARTIFACT."); | |
143 return new MINFOArtifact(uuid, hash, background, msg); | |
144 } | |
145 else if (name.length() > 0 && name.equals("fixanalysis")) { | |
146 logger.debug("+++++ NEW FIXANALYSIS ARTIFACT."); | |
147 return new FixAnalysisArtifact(uuid, hash, background, msg); | |
148 } | |
149 else if (name.length() > 0 && name.equals("gaugedischargecurve")) { | |
150 logger.debug("+++++ NEW GAUGEDISCHARGECURVE ARTIFACT."); | |
151 return new GaugeDischargeCurveArtifact(uuid, hash, background, msg); | |
152 } | |
153 else if (name.length() > 0 && name.equals("staticsqrelation")) { | |
154 logger.debug("+++++ STATICSQRELATION ARTIFACT."); | |
155 return new StaticSQRelationArtifact(uuid, hash, background, msg); | |
156 } | |
157 | |
158 return new DefaultArtifact(uuid, hash, background, msg); | |
159 } | |
160 | |
161 | |
162 public static List<CalculationMessage> parseBackgroundMessages(Document d) { | |
163 NodeList list = (NodeList) XMLUtils.xpath( | |
164 d, XPATH_BACKGROUND, XPathConstants.NODESET, | |
165 ArtifactNamespaceContext.INSTANCE); | |
166 | |
167 int len = list != null ? list.getLength() : 0; | |
168 | |
169 logger.debug("Found " + len + " background messages."); | |
170 | |
171 List<CalculationMessage> res = new ArrayList<CalculationMessage>(len); | |
172 | |
173 for (int i = 0; i < len; i++) { | |
174 CalculationMessage msg = parseBackgroundMessage( | |
175 (Element) list.item(i)); | |
176 | |
177 if (msg != null) { | |
178 res.add(msg); | |
179 } | |
180 } | |
181 | |
182 return res; | |
183 } | |
184 | |
185 | |
186 public static CalculationMessage parseBackgroundMessage(Element e) { | |
187 String steps = e.getAttribute("art:steps"); | |
188 String currentStep = e.getAttribute("art:currentStep"); | |
189 String message = e.getTextContent(); | |
190 | |
191 int lenCurStep = currentStep != null ? currentStep.length() : 0; | |
192 int lenSteps = steps != null ? steps.length() : 0; | |
193 int lenMessage = message != null ? message.length() : 0; | |
194 | |
195 if (lenSteps > 0 && lenMessage > 0 && lenCurStep > 0) { | |
196 try { | |
197 return new CalculationMessage( | |
198 Integer.parseInt(steps), | |
199 Integer.parseInt(currentStep), | |
200 message); | |
201 | |
202 } | |
203 catch (NumberFormatException nfe) { | |
204 nfe.printStackTrace(); | |
205 } | |
206 } | |
207 | |
208 return null; | |
209 } | |
210 } | |
211 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |