view gnv/src/main/java/de/intevation/gnv/action/DescribeUIAction.java @ 1022:28a0628b11b0

Added license file and license header. gnv/trunk@1258 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Tue, 02 Nov 2010 17:15:08 +0000
parents 1b42a86184f6
children
line wrap: on
line source
/*
 * Copyright (c) 2010 by Intevation GmbH
 *
 * This program is free software under the LGPL (>=v2.1)
 * Read the file LGPL.txt coming with the software for details
 * or visit http://www.gnu.org/licenses/ if it does not exist.
 */

package de.intevation.gnv.action;

import java.util.Locale;
import java.util.ResourceBundle;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.xml.xpath.XPathConstants;

import org.apache.log4j.Logger;
import org.apache.struts.Globals;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.w3c.dom.Node;

import de.intevation.gnv.action.sessionmodel.SessionModel;
import de.intevation.gnv.action.sessionmodel.SessionModelFactory;
import de.intevation.gnv.artifactdatabase.client.ArtifactDatabaseClient;
import de.intevation.gnv.artifactdatabase.client.ArtifactDatabaseClientFactory;
import de.intevation.gnv.artifactdatabase.objects.ArtifactDescription;
import de.intevation.gnv.artifactdatabase.objects.ArtifactObject;
import de.intevation.gnv.util.ArtifactNamespaceContext;
import de.intevation.gnv.util.XMLUtils;
import de.intevation.gnv.util.XSLTransformer;

/**
 * This controller is called to fetch the current artifact description using the
 * describe operation. The describe document is used to feed an XSL transformer
 * that parses the xml and creats html output representing the parameter panel.
 * The html code is stored as attribute on the request object which is queried
 * in a jsp page later.
 *
 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
 */
public class DescribeUIAction extends ArtifactDatabaseActionBase {

    public static final String XPATH_DYNAMIC_UI = "art:dynamic";
    public static final String XPATH_STATIC_UI  = "art:static";

    public static final String XSL_SHEET_DYNAMIC =
        "WEB-INF/config/templates/describe-ui.xsl";

    public static final String XSL_SHEET_STATIC =
        "WEB-INF/config/templates/describe-ui-static.xsl";

    /**
     * the logger, used to log exceptions and additonaly information
     */
    private static Logger logger = Logger.getLogger(DescribeUIAction.class);


    /**
     * Constructor
     */
    public DescribeUIAction() {
        super();
    }


    @Override
    public ActionForward execute(
        ActionMapping       mapping,
        ActionForm          form,
        HttpServletRequest  request,
        HttpServletResponse response
    ) throws Exception
    {
        if (isSessionExhausted(request)) {
            // session timed out before
            return sessionExhaustedForward(mapping, form, request, response);
        }

        logger.info("describe user interface");
        try {
            // render describe document and create user interface
            SessionModel sm = SessionModelFactory.getInstance().getSessionModel(
                request);
            Locale tmp             = sm.getCurrentLocale();
            Locale locale          = tmp != null ? tmp : request.getLocale();

            ArtifactDatabaseClient adc = ArtifactDatabaseClientFactory
                .getInstance().getArtifactDatabaseClient(locale);
            request.getSession().setAttribute(Globals.LOCALE_KEY, locale);
            ArtifactObject artifactFactory = sm.getSelectedArtifactFactory();

            if (artifactFactory == null) {
                logger.error("No connection to artifact server.");
                request.setAttribute(
                    CommunicationKeys.REQUEST_EXCEPTION_MESSAGE_ID,
                    "java.io.ioexception..connection.refused");

                return super.getExceptionForward(mapping);
            }

            ArtifactDescription artifactDescription =
                adc.getCurrentStepDescription(
                    artifactFactory,
                    sm.getCurrentArtifact(),
                    true);

            Node currentUI = artifactDescription.getCurrentUI();
            if (currentUI != null) {
                ResourceBundle res = ResourceBundle.getBundle(
                    "applicationMessages", locale);
                String editText           = res.getString(
                    "gnviewer.history.back.button");
                String exampleLinestring = res.getString(
                    "gnviewer.example.linestring");
                String examplePolygon = res.getString(
                    "gnviewer.example.polygon");

                XSLTransformer transformer = new XSLTransformer();

                String url = response.encodeURL(
                    mapping.findForward("back").getPath());
                transformer.addParameter("back-url", url);
                transformer.addParameter("edit", editText);
                transformer.addParameter("example-polygon", examplePolygon);
                transformer.addParameter(
                    "example-linestring",
                    exampleLinestring);

                String fisUrl = response.encodeURL(
                    mapping.findForward("selectfis").getPath());
                transformer.addParameter("selectfis", fisUrl);

                // fetch dynamic part from describe document and transform it
                Node dynamicNode = (Node) XMLUtils.xpath(
                    currentUI,
                    XPATH_DYNAMIC_UI,
                    XPathConstants.NODE,
                    ArtifactNamespaceContext.INSTANCE
                );

                String ui = transformer.transform(
                    dynamicNode,
                    "UTF-8",
                    request.getRealPath(XSL_SHEET_DYNAMIC
                ));

                if (ui != null && ui.length() > 1)
                    request.setAttribute("ui", ui);

                // fetch static part from describe document and transform it
                Node staticNode = (Node) XMLUtils.xpath(
                    currentUI,
                    XPATH_STATIC_UI,
                    XPathConstants.NODE,
                    ArtifactNamespaceContext.INSTANCE
                );

                String staticUI = transformer.transform(
                    staticNode,
                    "UTF-8",
                    request.getRealPath(XSL_SHEET_STATIC));

                if (staticUI != null && staticUI.length() > 1)
                    request.setAttribute("staticui", staticUI);
            }

            return super.execute(mapping, form, request, response);
        }
        catch (Exception e) {
            logger.error(e, e);
            request.setAttribute(
                CommunicationKeys.REQUEST_EXCEPTION_MESSAGE_ID,
                e.getMessage());

            return super.getExceptionForward(mapping);
        }
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :

http://dive4elements.wald.intevation.org