view gnv-artifacts/src/main/java/de/intevation/gnv/transition/TransitionBase.java @ 58:f31343d80d53

Artifact.advance initial implementiert gnv-artifacts/trunk@40 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Tim Englich <tim.englich@intevation.de>
date Tue, 08 Sep 2009 14:07:59 +0000
parents f01592cd6419
children 2c5d8f5bced1
line wrap: on
line source
/**
 *
 */
package de.intevation.gnv.transition;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.apache.log4j.Logger;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import de.intevation.artifactdatabase.Config;
import de.intevation.gnv.artifacts.GNVArtifactBase;
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.transition.exception.TransitionException;

/**
 * @author Tim Englich <tim.englich@intevation.de>
 *
 */
public abstract class TransitionBase implements Transition {
    /**
     * the logger, used to log exceptions and additonaly information
     */
    private static Logger log = Logger.getLogger(GNVArtifactBase.class);
    
    private String id = null;
    
    private String description = null;
    


    protected String queryID = null;
    
    private Collection<String> reachableTransitions = null;
    
    private Collection<String> inputValueNames = null;
    
    private Map<String,InputValue> inputValues = null;
    
    private Transition parent = null;
    
    private Map<String,InputData> inputData = null;
    
    private Collection<Object> descibeData = null;
    /**
     * Constructor
     */
    public TransitionBase() {
        super();
    }

    /**
     * @see de.intevation.gnv.transition.Transition#getID()
     */
    public String getID() {
        return this.id;
    }
    
    /**
     * @see de.intevation.gnv.transition.Transition#getDescription()
     */
    public String getDescription() {
        return this.description;
    }
    
    /**
     * @see de.intevation.gnv.transition.Transition#reachableTransitions()
     */
    public Collection<String> reachableTransitions() {
        return this.reachableTransitions;
    }

    /**
     * @see de.intevation.gnv.transition.Transition#getRequiredInputValues()
     */
    public Collection<InputValue> getRequiredInputValues() {
        return this.inputValues.values();
    }

    /**
     * @see de.intevation.gnv.transition.Transition#setup(org.w3c.dom.Node)
     */
    public void setup(Node configuration) {
        
        this.id = Config.getStringXPath(configuration,"@id");
        this.description = Config.getStringXPath(configuration,"@description");
        
        log.info("Transition-ID = "+ this.id);
        NodeList nodes = Config.getNodeSetXPath(configuration,"reachableTransitions/transition");
        this.reachableTransitions = new ArrayList<String>(nodes.getLength());
        for (int i = 0 ; i < nodes.getLength(); i++){
            String reachableTransition = nodes.item(i).getTextContent();
            log.info("ReachableTransition ==> "+ reachableTransition);
            this.reachableTransitions.add(reachableTransition);
            
        }
        
        NodeList inputValuesNodes = Config.getNodeSetXPath(configuration,"inputvalues/inputvalue");
        this.inputValues = new HashMap<String,InputValue>(inputValuesNodes.getLength());
        this.inputValueNames = new ArrayList<String>(inputValuesNodes.getLength());
        for (int i = 0 ; i < inputValuesNodes.getLength(); i++){
            Node inputValueNode = inputValuesNodes.item(i);
            InputValue inputValue = new DefaultInputValue(Config.getStringXPath(inputValueNode,"@name"), Config.getStringXPath(inputValueNode,"@type"));
            log.debug(inputValue.toString());
            this.inputValues.put(inputValue.getName(),inputValue);
            this.inputValueNames.add(inputValue.getName());
        }
        
        this.queryID = Config.getStringXPath(configuration,"queryID");
        log.info("QueryID ==> "+ this.queryID);
        
    }

    /**
     * @see de.intevation.gnv.transition.Transition#getParent()
     */
    public Transition getParent() {
        return this.parent;
    }

    /**
     * @see de.intevation.gnv.transition.Transition#setParent(de.intevation.gnv.transition.Transition)
     */
    public void setParent(Transition transition) {
        this.parent = transition;
    }

    /**
     * @see de.intevation.gnv.transition.Transition#putInputData(java.util.Collection)
     */
    public void putInputData(Collection<InputData> inputData) throws TransitionException {
        log.debug("TransitionBase.putInputData");
        if (inputData != null){
            Iterator<InputData> it = inputData.iterator();
            while(it.hasNext()){
                InputData tmpItem = it.next();
                InputValue inputValue = this.inputValues.get(tmpItem.getName());
                if (inputValue != null){
                    if (this.inputData == null){
                        this.inputData = new HashMap<String,InputData>(inputData.size());
                        // TODO validate Value; und Valueconcatenieren
                        this.inputData.put(tmpItem.getName(),tmpItem);
                    }
                    
                }else{
                    String errMsg = "No Inputvalue given for Inputdata "+ tmpItem.getName();
                    log.warn(errMsg+ "Value will be ignored");
                    throw new TransitionException(errMsg);
                    
                }
            }
        }else{
            log.warn("No Inputdata given");
        }
    
    }

    /**
     * @see de.intevation.gnv.transition.Transition#isTransitionReachable(java.lang.String)
     */
    public boolean isTransitionReachable(String transitionID) {
        log.debug("TransitionBase.isTransitionReachable");
        boolean returnValue = false;
        Iterator<String> transitions = reachableTransitions.iterator();
        while (transitions.hasNext()){
            if(transitions.next().equals(transitionID)){
                log.debug("Transition "+transitionID+" wird unterst�tzt.");
                returnValue = true;
                break;
            }
        }
        return returnValue;
    }

    /**
     * @see de.intevation.gnv.transition.Transition#advance()
     */
    public void advance() throws TransitionException {
        log.debug("TransitionBase.advance");
        try {
            String[] filterValues = new String[this.inputValueNames.size()];
            Iterator<String> it = this.inputValueNames.iterator();
            int i = 0;
            while (it.hasNext()){
                filterValues[i++] = this.inputData.get(it.next()).getValue();
            }
            QueryExecutor queryExecutor = QueryExecutorFactory.getInstance().getQueryExecutor();
            Collection<Result> result = queryExecutor.executeQuery(this.queryID, filterValues);
            if (this.descibeData == null){
                this.descibeData = new ArrayList<Object>();
            }
            this.descibeData.add(result);
        } catch (QueryException e) {
            log.error(e,e);
            throw new TransitionException(e);
        }
    }

    /**
     * @see de.intevation.gnv.transition.Transition#getDescibeData()
     */
    public Collection<Object> getDescibeData() {
        return this.descibeData;
    }

    /**
     * @see de.intevation.gnv.transition.Transition#setDescibeData(java.util.Collection)
     */
    public void setDescibeData(Collection<Object> descibeData) {
        this.descibeData = descibeData;
        
    }
}

http://dive4elements.wald.intevation.org