Mercurial > dive4elements > river
comparison flys-client/src/main/java/de/intevation/flys/client/server/ArtifactDescriptionFactory.java @ 16:f8a5f2c5e2b7
The DESCRIBE document returned by the artifact server is parsed after calling create() of the artifact service and a new Artifact is created with an ArtifactDescription that contains the UUID, HASH, und the current Data.
flys-client/trunk@1329 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Ingo Weinzierl <ingo.weinzierl@intevation.de> |
---|---|
date | Fri, 18 Feb 2011 14:23:12 +0000 |
parents | |
children | a85bac235069 |
comparison
equal
deleted
inserted
replaced
15:eb425ab34fd8 | 16:f8a5f2c5e2b7 |
---|---|
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.Node; | |
10 import org.w3c.dom.NodeList; | |
11 | |
12 import de.intevation.artifacts.common.ArtifactNamespaceContext; | |
13 import de.intevation.artifacts.common.utils.ClientProtocolUtils; | |
14 import de.intevation.artifacts.common.utils.XMLUtils; | |
15 | |
16 import de.intevation.flys.client.shared.model.ArtifactDescription; | |
17 import de.intevation.flys.client.shared.model.Data; | |
18 import de.intevation.flys.client.shared.model.DataItem; | |
19 import de.intevation.flys.client.shared.model.DefaultData; | |
20 import de.intevation.flys.client.shared.model.DefaultDataItem; | |
21 import de.intevation.flys.client.shared.model.DefaultArtifactDescription; | |
22 | |
23 | |
24 /** | |
25 * This factory class helps creating an {@link ArtifactDescription} based on the | |
26 * DESCRIBE document of an artifact returned by the artifact server. Use the | |
27 * {@link createArtifactDescription(org.w3c.dom.Document)} method with the | |
28 * DESCRIBE document to create such an {@link ArtifactDescription}. | |
29 * | |
30 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a> | |
31 */ | |
32 public class ArtifactDescriptionFactory { | |
33 | |
34 public static final String XPATH_STATE_NAME = "@art:name"; | |
35 | |
36 /** | |
37 * This method creates the {@link ArtifactDescription} of the DESCRIBE | |
38 * document <i>doc</i>. | |
39 * | |
40 * @param doc A DESCRIBE document. | |
41 * | |
42 * @return the {@link ArtifactDescription}. | |
43 */ | |
44 public static ArtifactDescription createArtifactDescription(Document doc) { | |
45 System.out.println("ArtifactDescriptionFactory - create()"); | |
46 | |
47 Node currentState = ClientProtocolUtils.getCurrentState(doc); | |
48 Node staticNode = ClientProtocolUtils.getStaticUI(doc); | |
49 Node dynamicNode = ClientProtocolUtils.getDynamicUI(doc); | |
50 | |
51 String state = (String) XMLUtils.xpath( | |
52 currentState, | |
53 XPATH_STATE_NAME, | |
54 XPathConstants.STRING, | |
55 ArtifactNamespaceContext.INSTANCE); | |
56 System.out.println("Current state name: " + state); | |
57 | |
58 Data currentData = extractCurrentData(dynamicNode); | |
59 | |
60 // TODO parse the static ui part | |
61 return new DefaultArtifactDescription(null, currentData, state, null); | |
62 } | |
63 | |
64 | |
65 /** | |
66 * This method extracts the data that the user is able to enter in the | |
67 * current state of the artifact. | |
68 * | |
69 * @param dynamicNode The dynamic node of the DESCRIBE document. | |
70 * | |
71 * @return A {@link Data} object that represents the data which might be | |
72 * entered by the user in the current state or null, if no data might be | |
73 * entered. | |
74 */ | |
75 protected static Data extractCurrentData(Node dynamicNode) { | |
76 System.out.println("ArtifactDescriptionFactory - extractCurrentData()"); | |
77 | |
78 Node data = ClientProtocolUtils.getSelectNode(dynamicNode); | |
79 NodeList choices = ClientProtocolUtils.getItemNodes(data); | |
80 String label = ClientProtocolUtils.getLabel(data); | |
81 | |
82 DataItem[] dataItems = extractCurrentDataItems(choices); | |
83 | |
84 return new DefaultData(label, null, null, dataItems); | |
85 } | |
86 | |
87 | |
88 /** | |
89 * This method extract the {@link DataItem}s of the DESCRIBE document. | |
90 * | |
91 * @param items The items in the DESCRIBE document. | |
92 * | |
93 * @return the {@link DataItem}s. | |
94 */ | |
95 protected static DataItem[] extractCurrentDataItems(NodeList items) { | |
96 System.out.println( | |
97 "ArtifactDescriptionFactory - extractCurrentDataItems()"); | |
98 | |
99 if (items == null || items.getLength() == 0) { | |
100 System.out.println("No data items found."); | |
101 return null; | |
102 } | |
103 | |
104 int count = items.getLength(); | |
105 | |
106 List<DataItem> dataItems = new ArrayList<DataItem>(count); | |
107 | |
108 for (int i = 0; i < count; i++) { | |
109 Node item = items.item(i); | |
110 String label = ClientProtocolUtils.getLabel(item); | |
111 String value = ClientProtocolUtils.getValue(item); | |
112 | |
113 dataItems.add(new DefaultDataItem(label, null, value)); | |
114 } | |
115 | |
116 return (DataItem[]) dataItems.toArray(new DataItem[count]); | |
117 } | |
118 } | |
119 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |