# HG changeset patch # User Ingo Weinzierl # Date 1298038992 0 # Node ID f8a5f2c5e2b7c4ea10b9cb4eaa4bd4a505b264c7 # Parent eb425ab34fd8b4ee46fbcc20f57f7f3d6fc87f15 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 diff -r eb425ab34fd8 -r f8a5f2c5e2b7 flys-client/ChangeLog --- a/flys-client/ChangeLog Fri Feb 18 08:32:38 2011 +0000 +++ b/flys-client/ChangeLog Fri Feb 18 14:23:12 2011 +0000 @@ -1,3 +1,13 @@ +2011-02-18 Ingo Weinzierl + + * src/main/java/de/intevation/flys/client/server/ArtifactDescriptionFactory.java: + New. This factory is used to create an ArtifactDescription based on an + DESCRIBE document returned by the artifact server. + + * src/main/java/de/intevation/flys/client/server/FLYSArtifactCreator.java: A + new DefaultArtifact with an ArtifactDescription that contains the dynamic + UI part is returned by the create() method now. + 2011-02-17 Ingo Weinzierl * pom.xml: This client uses the artifacts http-client for the communication diff -r eb425ab34fd8 -r f8a5f2c5e2b7 flys-client/src/main/java/de/intevation/flys/client/server/ArtifactDescriptionFactory.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/flys-client/src/main/java/de/intevation/flys/client/server/ArtifactDescriptionFactory.java Fri Feb 18 14:23:12 2011 +0000 @@ -0,0 +1,119 @@ +package de.intevation.flys.client.server; + +import java.util.ArrayList; +import java.util.List; + +import javax.xml.xpath.XPathConstants; + +import org.w3c.dom.Document; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; + +import de.intevation.artifacts.common.ArtifactNamespaceContext; +import de.intevation.artifacts.common.utils.ClientProtocolUtils; +import de.intevation.artifacts.common.utils.XMLUtils; + +import de.intevation.flys.client.shared.model.ArtifactDescription; +import de.intevation.flys.client.shared.model.Data; +import de.intevation.flys.client.shared.model.DataItem; +import de.intevation.flys.client.shared.model.DefaultData; +import de.intevation.flys.client.shared.model.DefaultDataItem; +import de.intevation.flys.client.shared.model.DefaultArtifactDescription; + + +/** + * This factory class helps creating an {@link ArtifactDescription} based on the + * DESCRIBE document of an artifact returned by the artifact server. Use the + * {@link createArtifactDescription(org.w3c.dom.Document)} method with the + * DESCRIBE document to create such an {@link ArtifactDescription}. + * + * @author Ingo Weinzierl + */ +public class ArtifactDescriptionFactory { + + public static final String XPATH_STATE_NAME = "@art:name"; + + /** + * This method creates the {@link ArtifactDescription} of the DESCRIBE + * document doc. + * + * @param doc A DESCRIBE document. + * + * @return the {@link ArtifactDescription}. + */ + public static ArtifactDescription createArtifactDescription(Document doc) { + System.out.println("ArtifactDescriptionFactory - create()"); + + Node currentState = ClientProtocolUtils.getCurrentState(doc); + Node staticNode = ClientProtocolUtils.getStaticUI(doc); + Node dynamicNode = ClientProtocolUtils.getDynamicUI(doc); + + String state = (String) XMLUtils.xpath( + currentState, + XPATH_STATE_NAME, + XPathConstants.STRING, + ArtifactNamespaceContext.INSTANCE); + System.out.println("Current state name: " + state); + + Data currentData = extractCurrentData(dynamicNode); + + // TODO parse the static ui part + return new DefaultArtifactDescription(null, currentData, state, null); + } + + + /** + * This method extracts the data that the user is able to enter in the + * current state of the artifact. + * + * @param dynamicNode The dynamic node of the DESCRIBE document. + * + * @return A {@link Data} object that represents the data which might be + * entered by the user in the current state or null, if no data might be + * entered. + */ + protected static Data extractCurrentData(Node dynamicNode) { + System.out.println("ArtifactDescriptionFactory - extractCurrentData()"); + + Node data = ClientProtocolUtils.getSelectNode(dynamicNode); + NodeList choices = ClientProtocolUtils.getItemNodes(data); + String label = ClientProtocolUtils.getLabel(data); + + DataItem[] dataItems = extractCurrentDataItems(choices); + + return new DefaultData(label, null, null, dataItems); + } + + + /** + * This method extract the {@link DataItem}s of the DESCRIBE document. + * + * @param items The items in the DESCRIBE document. + * + * @return the {@link DataItem}s. + */ + protected static DataItem[] extractCurrentDataItems(NodeList items) { + System.out.println( + "ArtifactDescriptionFactory - extractCurrentDataItems()"); + + if (items == null || items.getLength() == 0) { + System.out.println("No data items found."); + return null; + } + + int count = items.getLength(); + + List dataItems = new ArrayList(count); + + for (int i = 0; i < count; i++) { + Node item = items.item(i); + String label = ClientProtocolUtils.getLabel(item); + String value = ClientProtocolUtils.getValue(item); + + dataItems.add(new DefaultDataItem(label, null, value)); + } + + return (DataItem[]) dataItems.toArray(new DataItem[count]); + } +} +// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : diff -r eb425ab34fd8 -r f8a5f2c5e2b7 flys-client/src/main/java/de/intevation/flys/client/server/FLYSArtifactCreator.java --- a/flys-client/src/main/java/de/intevation/flys/client/server/FLYSArtifactCreator.java Fri Feb 18 08:32:38 2011 +0000 +++ b/flys-client/src/main/java/de/intevation/flys/client/server/FLYSArtifactCreator.java Fri Feb 18 14:23:12 2011 +0000 @@ -2,6 +2,9 @@ import org.w3c.dom.Document; +import de.intevation.artifacts.common.utils.XMLUtils; +import de.intevation.artifacts.common.ArtifactNamespaceContext; + import de.intevation.artifacts.httpclient.utils.ArtifactCreator; import de.intevation.flys.client.shared.model.Artifact; @@ -17,6 +20,13 @@ */ public class FLYSArtifactCreator implements ArtifactCreator { + /** The XPath to the artifact's uuid.*/ + public static final String XPATH_UUID = "/art:result/art:uuid/@art:value"; + + /** The XPath to the artifact's hash value.*/ + public static final String XPATH_HASH = "/art:result/art:hash/@art:value"; + + /** * Creates a new instance of an {@link ArtifactCreator}. */ @@ -34,7 +44,35 @@ * @return an instance if {@link Artifact}. */ public Object create(Document doc) { - return new DefaultArtifact("TODO-UUID", "TODO-HASH"); + Artifact artifact = extractArtifact(doc); + artifact.setArtifactDescription( + ArtifactDescriptionFactory.createArtifactDescription(doc)); + + return artifact; + } + + + /** + * This method extracts the UUID und HASH information of the returned + * artifact document. + * + * @param doc The result of the CREATE operation. + * + * @return an instance of an {@link Artifact}. + */ + protected Artifact extractArtifact(Document doc) { + System.out.println("FLYSArtifactCreator - extractArtifact()"); + + String uuid = XMLUtils.xpathString( + doc, XPATH_UUID, ArtifactNamespaceContext.INSTANCE); + + String hash = XMLUtils.xpathString( + doc, XPATH_HASH, ArtifactNamespaceContext.INSTANCE); + + System.out.println("NEW Artifact UUID: " + uuid); + System.out.println("NEW Artifact HASH: " + hash); + + return new DefaultArtifact(uuid, hash); } } // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :