view flys-client/src/main/java/de/intevation/flys/client/server/FLYSArtifactCreator.java @ 4215:c179cd02177d

Logout the current user Add code to the logout button to remove the current user from the session and to redirect the browser window to the login page. Part of flys/issue916 (Logout: "Abmelden" Knopf mit Funktion belegen)
author Björn Ricks <bjoern.ricks@intevation.de>
date Tue, 23 Oct 2012 09:59:16 +0200
parents cfbc80cc0ce4
children b660090b417d
line wrap: on
line source
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.Element;
import org.w3c.dom.NodeList;

import org.apache.log4j.Logger;

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;
import de.intevation.flys.client.shared.model.CalculationMessage;
import de.intevation.flys.client.shared.model.ChartArtifact;
import de.intevation.flys.client.shared.model.DefaultArtifact;
import de.intevation.flys.client.shared.model.FixAnalysisArtifact;
import de.intevation.flys.client.shared.model.GaugeDischargeCurveArtifact;
import de.intevation.flys.client.shared.model.MapArtifact;
import de.intevation.flys.client.shared.model.MINFOArtifact;
import de.intevation.flys.client.shared.model.WINFOArtifact;


/**
 * An implementation of an {@link ArtifactCreator}. This class uses the document
 * that is returned by the artifact server to parse important information (like
 * uuid, hash) and returns a new {@link Artifact} instance.
 *
 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
 */
public class FLYSArtifactCreator implements ArtifactCreator {

    private static final Logger logger =
        Logger.getLogger(FLYSArtifactCreator.class);


    /** 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";

    /** The XPath to the artifact's name value.*/
    public static final String XPATH_NAME = "/art:result/art:name/@art:value";

    /** The XPath to the value that determines if the artifact is processing in
     * background.*/
    public static final String XPATH_BACKGROUND_VALUE =
        "/art:result/art:background-processing/@art:value";

    /** The XPath that points to the (if existing) background messages.*/
    public static final String XPATH_BACKGROUND =
        "/art:result/art:background-processing";


    /**
     * Creates a new instance of an {@link ArtifactCreator}.
     */
    public FLYSArtifactCreator() {
    }


    /**
     * This concreate implementation returns an instance of {@link Artifact}
     * that is used in the FLYS GWT Client code.
     *
     * @param doc A document that describes the artifact that has been created
     * in the artifact server.
     *
     * @return an instance if {@link Artifact}.
     */
    public Object create(Document doc) {
        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) {
        logger.debug("FLYSArtifactCreator - extractArtifact()");

        String uuid = XMLUtils.xpathString(
            doc, XPATH_UUID, ArtifactNamespaceContext.INSTANCE);

        String hash = XMLUtils.xpathString(
            doc, XPATH_HASH, ArtifactNamespaceContext.INSTANCE);

        String name = XMLUtils.xpathString(
            doc, XPATH_NAME, ArtifactNamespaceContext.INSTANCE);

        String backgroundStr = XMLUtils.xpathString(
            doc, XPATH_BACKGROUND_VALUE, ArtifactNamespaceContext.INSTANCE);

        boolean background = false;
        if (backgroundStr != null && backgroundStr.length() > 0) {
            background = Boolean.valueOf(backgroundStr);
        }

        List<CalculationMessage> msg = parseBackgroundMessages(doc);

        logger.debug("NEW Artifact UUID: " + uuid);
        logger.debug("NEW Artifact HASH: " + hash);
        logger.debug("NEW Artifact NAME: " + name);
        logger.debug("NEW Artifact IN BACKGROUND: " + background);

        if (name == null) {
            return new DefaultArtifact(uuid, hash, background, msg);
        }

        name = name.trim();

        if (name.length() > 0 && name.equals("winfo")) {
            logger.debug("+++++ NEW WINFO ARTIFACT.");
            return new WINFOArtifact(uuid, hash, background, msg);
        }
        else if (name.length() > 0 && name.equals("new_map")) {
            logger.debug("+++++ NEW MAP ARTIFACT.");
            return new MapArtifact(uuid, hash, background, msg);
        }
        else if (name.length() > 0 && name.equals("new_chart")) {
            logger.debug("+++++ NEW CHART ARTIFACT.");
            return new ChartArtifact(uuid, hash, background, msg);
        }
        else if (name.length() > 0 && name.equals("minfo")) {
            logger.debug("+++++ NEW MINFO ARTIFACT.");
            return new MINFOArtifact(uuid, hash, background, msg);
        }
        else if (name.length() > 0 && name.equals("fixanalysis")) {
            logger.debug("+++++ NEW FIXANALYSIS ARTIFACT.");
            return new FixAnalysisArtifact(uuid, hash, background, msg);
        }
        else if (name.length() > 0 && name.equals("gaugedischargecurve")) {
            logger.debug("+++++ NEW GAUGEDISCHARGECURVE ARTIFACT.");
            return new GaugeDischargeCurveArtifact(uuid, hash, background, msg);
        }

        return new DefaultArtifact(uuid, hash, background, msg);
    }


    public static List<CalculationMessage> parseBackgroundMessages(Document d) {
        NodeList list = (NodeList) XMLUtils.xpath(
            d, XPATH_BACKGROUND, XPathConstants.NODESET,
            ArtifactNamespaceContext.INSTANCE);

        int len = list != null ? list.getLength() : 0;

        logger.debug("Found " + len + " background messages.");

        List<CalculationMessage> res = new ArrayList<CalculationMessage>(len);

        for (int i = 0; i < len; i++) {
            CalculationMessage msg = parseBackgroundMessage(
                (Element) list.item(i));

            if (msg != null) {
                res.add(msg);
            }
        }

        return res;
    }


    public static CalculationMessage parseBackgroundMessage(Element e) {
        String steps       = e.getAttribute("art:steps");
        String currentStep = e.getAttribute("art:currentStep");
        String message     = e.getTextContent();

        int lenCurStep = currentStep != null ? currentStep.length() : 0;
        int lenSteps   = steps       != null ? steps.length()       : 0;
        int lenMessage = message     != null ? message.length()     : 0;

        if (lenSteps > 0 && lenMessage > 0 && lenCurStep > 0) {
            try {
                return new CalculationMessage(
                    Integer.parseInt(steps),
                    Integer.parseInt(currentStep),
                    message);

            }
            catch (NumberFormatException nfe) {
                nfe.printStackTrace();
            }
        }

        return null;
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org