teichmann@5861: /* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde
teichmann@5861: * Software engineering by Intevation GmbH
teichmann@5861: *
teichmann@5993: * This file is Free Software under the GNU AGPL (>=v3)
teichmann@5861: * and comes with ABSOLUTELY NO WARRANTY! Check out the
teichmann@5993: * documentation coming with Dive4Elements River for details.
teichmann@5861: */
teichmann@5861:
teichmann@5835: package org.dive4elements.river.client.server;
ingo@14:
ingo@870: import java.util.ArrayList;
ingo@870: import java.util.List;
ingo@870:
ingo@870: import javax.xml.xpath.XPathConstants;
ingo@870:
ingo@1367: import org.apache.log4j.Logger;
teichmann@5835: import org.dive4elements.artifacts.common.ArtifactNamespaceContext;
gernotbelger@9071: import org.dive4elements.artifacts.common.utils.XMLUtils;
teichmann@5835: import org.dive4elements.artifacts.httpclient.utils.ArtifactCreator;
teichmann@5835: import org.dive4elements.river.client.shared.model.Artifact;
teichmann@5835: import org.dive4elements.river.client.shared.model.CalculationMessage;
teichmann@5835: import org.dive4elements.river.client.shared.model.ChartArtifact;
teichmann@5835: import org.dive4elements.river.client.shared.model.DefaultArtifact;
teichmann@5835: import org.dive4elements.river.client.shared.model.FixAnalysisArtifact;
teichmann@5835: import org.dive4elements.river.client.shared.model.GaugeDischargeCurveArtifact;
gernotbelger@9071: import org.dive4elements.river.client.shared.model.MINFOArtifact;
teichmann@5835: import org.dive4elements.river.client.shared.model.MapArtifact;
gernotbelger@8854: import org.dive4elements.river.client.shared.model.SINFOArtifact;
teichmann@5835: import org.dive4elements.river.client.shared.model.StaticSQRelationArtifact;
gernotbelger@8995: import org.dive4elements.river.client.shared.model.UINFOArtifact;
teichmann@5835: import org.dive4elements.river.client.shared.model.WINFOArtifact;
gernotbelger@9071: import org.w3c.dom.Document;
gernotbelger@9071: import org.w3c.dom.Element;
gernotbelger@9071: import org.w3c.dom.NodeList;
ingo@14:
ingo@14: /**
ingo@14: * An implementation of an {@link ArtifactCreator}. This class uses the document
ingo@14: * that is returned by the artifact server to parse important information (like
ingo@14: * uuid, hash) and returns a new {@link Artifact} instance.
ingo@14: *
ingo@14: * @author Ingo Weinzierl
ingo@14: */
ingo@14: public class FLYSArtifactCreator implements ArtifactCreator {
ingo@14:
gernotbelger@9071: private static final Logger log = Logger.getLogger(FLYSArtifactCreator.class);
ingo@1367:
gernotbelger@9071: /** The XPath to the artifact's uuid. */
ingo@16: public static final String XPATH_UUID = "/art:result/art:uuid/@art:value";
ingo@16:
gernotbelger@9071: /** The XPath to the artifact's hash value. */
ingo@16: public static final String XPATH_HASH = "/art:result/art:hash/@art:value";
ingo@16:
gernotbelger@9071: /** The XPath to the artifact's name value. */
ingo@227: public static final String XPATH_NAME = "/art:result/art:name/@art:value";
ingo@227:
gernotbelger@9071: /**
gernotbelger@9071: * The XPath to the value that determines if the artifact is processing in
gernotbelger@9071: * background.
gernotbelger@9071: */
gernotbelger@9071: public static final String XPATH_BACKGROUND_VALUE = "/art:result/art:background-processing/@art:value";
ingo@870:
gernotbelger@9071: /** The XPath that points to the (if existing) background messages. */
gernotbelger@9071: public static final String XPATH_BACKGROUND = "/art:result/art:background-processing";
ingo@16:
ingo@14: /**
ingo@14: * Creates a new instance of an {@link ArtifactCreator}.
ingo@14: */
ingo@14: public FLYSArtifactCreator() {
ingo@14: }
ingo@14:
ingo@14: /**
ingo@14: * This concreate implementation returns an instance of {@link Artifact}
ingo@14: * that is used in the FLYS GWT Client code.
ingo@14: *
gernotbelger@9071: * @param doc
gernotbelger@9071: * A document that describes the artifact that has been created
gernotbelger@9071: * in the artifact server.
ingo@14: *
ingo@14: * @return an instance if {@link Artifact}.
ingo@14: */
gernotbelger@9071: @Override
gernotbelger@9071: public Object create(final Document doc) {
gernotbelger@9071: final Artifact artifact = extractArtifact(doc);
gernotbelger@9071: artifact.setArtifactDescription(ArtifactDescriptionFactory.createArtifactDescription(doc));
ingo@16:
ingo@16: return artifact;
ingo@16: }
ingo@16:
ingo@16: /**
ingo@16: * This method extracts the UUID und HASH information of the returned
ingo@16: * artifact document.
ingo@16: *
gernotbelger@9071: * @param doc
gernotbelger@9071: * The result of the CREATE operation.
ingo@16: *
ingo@16: * @return an instance of an {@link Artifact}.
ingo@16: */
gernotbelger@9071: protected Artifact extractArtifact(final Document doc) {
teichmann@8203: log.debug("FLYSArtifactCreator - extractArtifact()");
ingo@16:
gernotbelger@9071: final String uuid = XMLUtils.xpathString(doc, XPATH_UUID, ArtifactNamespaceContext.INSTANCE);
ingo@16:
gernotbelger@9071: final String hash = XMLUtils.xpathString(doc, XPATH_HASH, ArtifactNamespaceContext.INSTANCE);
ingo@16:
gernotbelger@9071: String name = XMLUtils.xpathString(doc, XPATH_NAME, ArtifactNamespaceContext.INSTANCE);
ingo@227:
gernotbelger@9071: final String backgroundStr = XMLUtils.xpathString(doc, XPATH_BACKGROUND_VALUE, ArtifactNamespaceContext.INSTANCE);
ingo@862:
ingo@862: boolean background = false;
ingo@862: if (backgroundStr != null && backgroundStr.length() > 0) {
ingo@862: background = Boolean.valueOf(backgroundStr);
ingo@862: }
ingo@862:
gernotbelger@9071: final List msg = parseBackgroundMessages(doc);
ingo@870:
teichmann@8203: log.debug("NEW Artifact UUID: " + uuid);
teichmann@8203: log.debug("NEW Artifact HASH: " + hash);
teichmann@8203: log.debug("NEW Artifact NAME: " + name);
teichmann@8203: log.debug("NEW Artifact IN BACKGROUND: " + background);
ingo@227:
ingo@227: if (name == null) {
ingo@870: return new DefaultArtifact(uuid, hash, background, msg);
ingo@227: }
ingo@227:
ingo@227: name = name.trim();
ingo@227:
gernotbelger@9071: // FIXME: why do we have a super sophisticated artifact-framework if, in the end, module dependent stuff is still
gernotbelger@9071: // switched manually....
gernotbelger@8854: if (name.equals("winfo")) {
teichmann@8203: log.debug("+++++ NEW WINFO ARTIFACT.");
ingo@870: return new WINFOArtifact(uuid, hash, background, msg);
ingo@227: }
gernotbelger@8854:
gernotbelger@8854: if (name.equals("new_map")) {
teichmann@8203: log.debug("+++++ NEW MAP ARTIFACT.");
raimund@1513: return new MapArtifact(uuid, hash, background, msg);
raimund@1513: }
gernotbelger@9071:
gernotbelger@8854: if (name.equals("new_chart")) {
teichmann@8203: log.debug("+++++ NEW CHART ARTIFACT.");
raimund@1530: return new ChartArtifact(uuid, hash, background, msg);
raimund@1530: }
gernotbelger@9071:
gernotbelger@8854: if (name.equals("minfo")) {
teichmann@8203: log.debug("+++++ NEW MINFO ARTIFACT.");
ingo@2520: return new MINFOArtifact(uuid, hash, background, msg);
ingo@2520: }
gernotbelger@9071:
gernotbelger@8854: if (name.equals("fixanalysis")) {
teichmann@8203: log.debug("+++++ NEW FIXANALYSIS ARTIFACT.");
raimund@2541: return new FixAnalysisArtifact(uuid, hash, background, msg);
raimund@2541: }
gernotbelger@9071:
gernotbelger@8854: if (name.equals("gaugedischargecurve")) {
teichmann@8203: log.debug("+++++ NEW GAUGEDISCHARGECURVE ARTIFACT.");
bjoern@3865: return new GaugeDischargeCurveArtifact(uuid, hash, background, msg);
bjoern@3865: }
gernotbelger@9071:
gernotbelger@8854: if (name.equals("staticsqrelation")) {
teichmann@8203: log.debug("+++++ STATICSQRELATION ARTIFACT.");
rrenkert@5503: return new StaticSQRelationArtifact(uuid, hash, background, msg);
rrenkert@5503: }
ingo@16:
gernotbelger@8854: if (name.equals("sinfo")) {
gernotbelger@8854: log.debug("+++++ NEW SINFO ARTIFACT.");
gernotbelger@8854: return new SINFOArtifact(uuid, hash, background, msg);
gernotbelger@8854: }
gernotbelger@8854:
gernotbelger@8995: if (name.equals("uinfo")) {
gernotbelger@9071: log.debug("+++++ NEW UINFO ARTIFACT.");
gernotbelger@9071: return new UINFOArtifact(uuid, hash, background, msg);
gernotbelger@8995: }
gernotbelger@9071: if (name.equals("bundu")) {
gernotbelger@9071: log.debug("+++++ NEW BUNDU ARTIFACT.");
gernotbelger@9071: return new UINFOArtifact(uuid, hash, background, msg);
gernotbelger@9071: }
ingo@870: return new DefaultArtifact(uuid, hash, background, msg);
ingo@870: }
ingo@870:
gernotbelger@9071: public static List parseBackgroundMessages(final Document d) {
gernotbelger@9071: final NodeList list = (NodeList) XMLUtils.xpath(d, XPATH_BACKGROUND, XPathConstants.NODESET, ArtifactNamespaceContext.INSTANCE);
ingo@870:
gernotbelger@9071: final int len = list != null ? list.getLength() : 0;
ingo@870:
teichmann@8203: log.debug("Found " + len + " background messages.");
ingo@870:
gernotbelger@9071: final List res = new ArrayList(len);
ingo@870:
ingo@870: for (int i = 0; i < len; i++) {
gernotbelger@9071: final CalculationMessage msg = parseBackgroundMessage((Element) list.item(i));
ingo@870:
ingo@870: if (msg != null) {
ingo@870: res.add(msg);
ingo@870: }
ingo@870: }
ingo@870:
ingo@870: return res;
ingo@870: }
ingo@870:
gernotbelger@9071: public static CalculationMessage parseBackgroundMessage(final Element e) {
gernotbelger@9071: final String steps = e.getAttribute("art:steps");
gernotbelger@9071: final String currentStep = e.getAttribute("art:currentStep");
gernotbelger@9071: final String message = e.getTextContent();
ingo@870:
gernotbelger@9071: final int lenCurStep = currentStep != null ? currentStep.length() : 0;
gernotbelger@9071: final int lenSteps = steps != null ? steps.length() : 0;
gernotbelger@9071: final int lenMessage = message != null ? message.length() : 0;
ingo@870:
ingo@870: if (lenSteps > 0 && lenMessage > 0 && lenCurStep > 0) {
ingo@870: try {
gernotbelger@9071: return new CalculationMessage(Integer.parseInt(steps), Integer.parseInt(currentStep), message);
ingo@870:
ingo@870: }
gernotbelger@9071: catch (final NumberFormatException nfe) {
ingo@870: nfe.printStackTrace();
ingo@870: }
ingo@870: }
ingo@870:
ingo@870: return null;
ingo@14: }
ingo@14: }
ingo@14: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :