view gnv/src/main/java/de/intevation/gnv/action/DescribeUIAction.java @ 993:9b126bceb0b2

gnv/issue219: Added rendering of direct links. gnv/trunk@1194 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Thu, 10 Jun 2010 22:01:23 +0000
parents 9ff116474e7b
children 27029f0ec7e1
line wrap: on
line source
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;

import org.apache.log4j.Logger;

/**
 * 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 {

    private static Logger log = Logger
            .getLogger(DescribeUIAction.class);

    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();
    }

    public static String getInputException(HttpServletRequest request) {
        Object msg = request.getAttribute(CommunicationKeys.REQUEST_EXCEPTION_INPUT_ID);
        return msg != null ? msg.toString() : "";
    }


    @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 submitButton = res.getString(
                    "gnviewer.select.button.src");
                String exampleLinestring = res.getString(
                    "gnviewer.example.linestring");
                String examplePolygon = res.getString(
                    "gnviewer.example.polygon");

                XSLTransformer transformer = new XSLTransformer();

                String nextUrl = response.encodeURL(
                    mapping.findForward("next").getPath());

                String url = response.encodeURL(
                    mapping.findForward("back").getPath());


                transformer.addParameter(
                    "uuid",
                    sm.getCurrentArtifact().getDescription());

                transformer.addParameter(
                    "exception",
                    getInputException(request));


                Object ft = request.getAttribute("furthertargets");

                transformer.addParameter("further",
                    ft == null || (ft instanceof Boolean && ((Boolean)ft).booleanValue())
                    ? "true"
                    : "false");

                transformer.addParameter("next-url", nextUrl);
                transformer.addParameter("back-url", url);
                transformer.addParameter("edit", editText);
                transformer.addParameter("example-polygon", examplePolygon);
                transformer.addParameter(
                    "gnviewer-select-button-src",
                    submitButton);
                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.getParentNode(),
                    "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