view gnv-artifacts/src/main/java/de/intevation/gnv/state/OutputStateBase.java @ 521:1bf058f1a2d1

Generate seabed polygon to "Profilschnitte". gnv-artifacts/trunk@615 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Sun, 24 Jan 2010 20:24:03 +0000
parents cef17cc90fd0
children 6484464d2059
line wrap: on
line source
/**
 *
 */
package de.intevation.gnv.state;

import de.intevation.artifactdatabase.Config;

import de.intevation.artifacts.CallContext;
import de.intevation.artifacts.CallMeta;

import de.intevation.gnv.artifacts.cache.CacheFactory;

import de.intevation.gnv.artifacts.ressource.RessourceFactory;

import de.intevation.gnv.geobackend.base.Result;

import de.intevation.gnv.geobackend.base.query.QueryExecutor;
import de.intevation.gnv.geobackend.base.query.QueryExecutorFactory;

import de.intevation.gnv.geobackend.base.query.exception.QueryException;

import de.intevation.gnv.state.exception.StateException;

import java.io.OutputStream;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Locale;

import org.apache.log4j.Logger;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 * @author Tim Englich <tim.englich@intevation.de>
 * 
 */
public abstract class OutputStateBase 
extends               StateBase 
implements            OutputState
{
    public static final String XPATH_OUTPUT_MODE =
        "/art:action/art:out/@name";

    public static final String XPATH_MIME_TYPE   =
        "/art:action/art:out/art:mime-type/@value";

    /**
     * The UID of this Class
     */
    private static final long serialVersionUID = -1718732895737303823L;

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

    /**
     * The different Outputmodes which are provided by an OutputState
     */
    protected Collection<OutputMode> outputModes = null;
    
    protected String queryODVID = null;

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

    /**
     * @see de.intevation.gnv.state.OutputState#getOutputModes()
     */
    public Collection<OutputMode> getOutputModes() {
        log.debug("OutputStateBase.getOutputModes");
        return this.outputModes;
    }

    /**
     * @see de.intevation.gnv.state.StateBase#setup(org.w3c.dom.Node)
     */
    @Override
    public void setup(Node configuration) {
        log.debug("OutputStateBase.setup");
        super.setup(configuration);
        
        this.queryODVID = Config.getStringXPath(configuration,"queryID-odv");
        
        NodeList outputModeList = Config.getNodeSetXPath(configuration,
                "outputsModes/outputsMode");
        if (outputModeList != null) {
            log.debug(outputModeList.getLength() + " were found.");
            this.outputModes = new ArrayList<OutputMode>(outputModeList
                    .getLength());
            for (int i = 0; i < outputModeList.getLength(); i++) {
                Element currentNode = (Element)outputModeList.item(i);
                String name = currentNode.getAttribute("name");
                String description =currentNode.getAttribute("description");
                String mimeType = currentNode.getAttribute("mime-type");
                NodeList inputValuesList = Config.getNodeSetXPath(currentNode,
                        "parameters/inputvalue");
                Collection<InputValue> inputParameters = null;
                if (inputValuesList != null) {
                    inputParameters = new ArrayList<InputValue>(inputValuesList
                            .getLength());
                    for (int j = 0; j < inputValuesList.getLength(); j++) {
                        Element currentInputValuesNode = (Element)inputValuesList.item(j);
                        String inputValueName = currentInputValuesNode.getAttribute("name");
                        String inputValueType = currentInputValuesNode.getAttribute("type");
                        String defaultValue =currentInputValuesNode.getAttribute("value");
                        boolean isMultiselect = false;
                        InputValue inputValue = new DefaultInputValue(
                                inputValueName, inputValueType, defaultValue,
                                isMultiselect);
                        inputParameters.add(inputValue);
                    }
                }

                OutputMode outputMode = new DefaultOutputMode(name,
                        description, mimeType, inputParameters);
                log.debug(outputMode.toString());
                this.outputModes.add(outputMode);

            }
        }
    }

    /**
     * @see de.intevation.gnv.state.StateBase#advance()
     */
    @Override
    public void advance(String uuid, CallContext context)
    throws StateException
    {
    }

    @Override
    public void initialize(String uuid, CallContext context)
    throws StateException
    {
    }

    public void out(
        Document              format, 
        Collection<InputData> inputData,
        OutputStream          outputStream, 
        String                uuid, 
        CallMeta              callMeta
    )
    throws StateException 
    {
    }

    /**
     * @see de.intevation.gnv.state.OutputState#out(java.lang.String,
     *      java.util.Collection, java.io.OutputStream)
     */
    public void out(String outputMode, Collection<InputData> inputData,
                    OutputStream outputStream) throws StateException {
    }

    /**
     * @return
     */
    protected Object getChartResult(String uuid, CallContext callContext) {
        log.debug("OutputStateBase.getChartResult");
        Object result = null;
        if (CacheFactory.getInstance().isInitialized()) {
            String key = uuid + super.getID();
            log.debug("Hash for Queryelements: " + key);
            net.sf.ehcache.Element value = CacheFactory.getInstance().getCache().get(key);
            if (value != null) {
                result = value.getObjectValue();
            }else{
                result = this.getData(this.queryID);
                if (CacheFactory.getInstance().isInitialized()) {
                    CacheFactory.getInstance().getCache().put(new net.sf.ehcache.Element(key, result));
                }
                
            }
        }
        return result;
    }

    protected Object getChartFromCache(String uuid, CallContext callContext) {
        log.debug("Fetch chart [" + uuid + "] from cache");
        CacheFactory cacheFactory = CacheFactory.getInstance();
        if (cacheFactory.isInitialized()) {
            String key = "chart_" + uuid + super.getID();
            net.sf.ehcache.Element object = cacheFactory.getCache().get(key);

            if (object != null) {
                return object.getObjectValue();
            }
        }
        return null;
    }
    
    protected Collection<Result> getODVResult(String uuid) {
        log.debug("OutputStateBase.getODVResult");
        // TODO add Caching? I think it's not nessessary
        Collection<Result> returnValue = null;
        if (this.queryODVID != null){
            returnValue = this.getData(this.queryODVID);
        }else{
            log.warn("No Query for ODV Data is defined.");
        }
        return returnValue;
    }

    /**
     * @param returnValue
     * @return
     */
    private Collection<Result> getData(String queryID) {
        log.debug("OutputStateBase.getData");
        Collection<Result> returnValue = null;
        try {
            String[] filterValues = this.generateFilterValuesFromInputData();
            try {
                QueryExecutor queryExecutor = QueryExecutorFactory
                                              .getInstance()
                                              .getQueryExecutor();
                returnValue  = queryExecutor.executeQuery(queryID,filterValues);
            } catch (RuntimeException e) {
                log.error(e, e);
            }
        } catch (QueryException e) {
            log.error(e, e);
        }
        return returnValue;
    }
    
    protected void removeChartResult(String uuid) {
        log.debug("OutputStateBase.getChartResult");
        if (CacheFactory.getInstance().isInitialized()) {
            String key = uuid + super.getID();
            log.debug("Hash for Queryelements: " + key);
            net.sf.ehcache.Element value = CacheFactory.getInstance().getCache().get(key);
            if (value != null) {
                CacheFactory.getInstance().getCache().remove(key);
            }
        }
    }

    protected void removeChart(String uuid) {
        log.debug("OutputStateBase.removeChart from cache");

        CacheFactory cacheFactory = CacheFactory.getInstance();
        if (cacheFactory.isInitialized()) {
            String key = "chart_" + uuid + super.getID();
            net.sf.ehcache.Element object = cacheFactory.getCache().get(key);
            if (object != null)
                cacheFactory.getCache().remove(key);
        }
    }

    protected void purifyChart(Object chart, String uuid) {
        log.debug("Prufify chart [" + uuid + "]");
        CacheFactory cacheFactory = CacheFactory.getInstance();
        if (cacheFactory.isInitialized()) {
            String key = "chart_" + uuid + getID();
            cacheFactory.getCache().put(new net.sf.ehcache.Element(key, chart));
        }
    }

    /**
     * @see de.intevation.gnv.state.StateBase#putInputData(java.util.Collection, java.lang.String)
     */
    @Override
    public void putInputData(Collection<InputData> inputData, 
                             String uuid)
                                         throws StateException {
        log.debug("OutputStateBase.putInputData");
        this.removeChartResult(uuid);
        this.removeChart(uuid);
        super.putInputData(inputData, uuid);
    }

    public void out(String outputMode, Collection<InputData> inputData,
                    OutputStream outputStream, String uuid, CallMeta callMeta)
                                                                              throws StateException {
    }


    protected String getMessage(Locale locale, String key, String value) {
        return RessourceFactory.getInstance().getRessource(
            locale,
            key,
            value
        );
    }
}

http://dive4elements.wald.intevation.org