view gnv-artifacts/src/main/java/de/intevation/gnv/transition/TransitionBase.java @ 66:bf883222b675

Added JfreeChart Dependency to pom-file gnv-artifacts/trunk@49 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Tim Englich <tim.englich@intevation.de>
date Wed, 09 Sep 2009 12:03:00 +0000
parents 6ae2d4134da3
children d117fd4b82e5
line wrap: on
line source
/**
 *
 */
package de.intevation.gnv.transition;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.GregorianCalendar;
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.Element;
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.geobackend.util.DateUtils;
import de.intevation.gnv.transition.describedata.MinMaxDescribeData;
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);
    
    public static final String XFORM_URL = "http://www.w3.org/2002/xforms";
    public static final String XFORM_PREFIX = "xform";
    
    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;
    
    protected 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");
                    
                }
            }
        }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()){
                String value = it.next();
                InputData data = this.inputData.get(value);
                filterValues[i++] = data.getValue();
            }
            QueryExecutor queryExecutor = QueryExecutorFactory.getInstance().getQueryExecutor();
            Collection<Result> result = queryExecutor.executeQuery(this.queryID, filterValues);
            this.purifyResult(result);
        } catch (QueryException e) {
            log.error(e,e);
            throw new TransitionException(e);
        }
    }

    /**
     * @param result
     */
    protected void purifyResult(Collection<Result> result) {
        if (this.descibeData == null){
            this.descibeData = new ArrayList<Object>();
        }
        this.descibeData.add(result);
    }
    
    /**
     * @see de.intevation.gnv.transition.Transition#describe(org.w3c.dom.Document, org.w3c.dom.Node)
     */
    public void describe(Document document, Node rootNode) {
        
        if(this.descibeData != null){
            Iterator<Object> it = this.descibeData.iterator();
            while (it.hasNext()){
                Object o = it.next();
                if (o instanceof Collection<?>){
                    Element selectNode = this.createXFormElement(document,"select");
                    // TODO: HACK:
                    // BESSERE L�SUNG FINDEN
                    Object[] names = this.inputValueNames.toArray();
                    String name = names[names.length-1].toString();
                    
                    selectNode.setAttribute("ref", name);
                    
                    Element lableNode = this.createXFormElement(document, "label");
                    lableNode.setTextContent(name);
                    Element choiceNode = this.createXFormElement(document, "choices");
                    
                    Collection<Result> values = (Collection)o;
                    Iterator<Result> resultIt = values.iterator();
                    while (resultIt.hasNext()){
                        Result result = resultIt.next();
                        Element itemNode = this.createXFormElement(document, "item");
                        
                        
                        Element choiceLableNode = this.createXFormElement(document, "label");
                        choiceLableNode.setTextContent(result.getString("VALUE"));
                        itemNode.appendChild(choiceLableNode);
                        
                        Element choicValueNode = this.createXFormElement(document, "value");
                        choicValueNode.setTextContent(result.getString("KEY"));
                        itemNode.appendChild(choicValueNode);
                        
                        choiceNode.appendChild(itemNode);
                    }
                    selectNode.appendChild(lableNode);
                    selectNode.appendChild(choiceNode);
                    rootNode.appendChild(selectNode);
                }else if (o instanceof MinMaxDescribeData){
                    MinMaxDescribeData descibeData = (MinMaxDescribeData)o;
                    Object min = descibeData.getMinValue();
                    Object max = descibeData.getMaxValue();
                    if (min instanceof GregorianCalendar){
                        Date d = ((GregorianCalendar)min).getTime();
                        min = DateUtils.getPatternedDateAmer(d);
                    }
                    
                    if (max instanceof GregorianCalendar){
                        Date d = ((GregorianCalendar)max).getTime();
                        max = DateUtils.getPatternedDateAmer(d);
                    }
                    
                    Element inputMinNode = this.createXFormElement(document, "input");
                    inputMinNode.setAttribute("ref", "minvalue");
                    Element inputMinLableNode = this.createXFormElement(document, "label");
                    inputMinLableNode.setTextContent("minvalue");
                    inputMinNode.appendChild(inputMinLableNode);
                    
                    Element inputMinValueNode = this.createXFormElement(document, "value");
                    inputMinValueNode.setTextContent(min.toString());
                    inputMinNode.appendChild(inputMinValueNode);
                    
                    Element inputMaxNode = this.createXFormElement(document, "input");
                    inputMaxNode.setAttribute("ref", "maxvalue");
                    Element inputMaxLableNode = this.createXFormElement(document, "label");
                    inputMaxLableNode.setTextContent("maxvalue");
                    inputMaxNode.appendChild(inputMaxLableNode);
                    
                    Element inputMaxValueNode = this.createXFormElement(document, "value");
                    inputMaxValueNode.setTextContent(max.toString());
                    inputMaxNode.appendChild(inputMaxValueNode);
                    
                    rootNode.appendChild(inputMinNode);
                    rootNode.appendChild(inputMaxNode);
                    
                }

            }
        }
       
    }

    private Element createXFormElement(Document document, String name) {
        Element node = document.createElementNS(XFORM_URL, name);
        node.setPrefix(XFORM_PREFIX);
        return node;
    }

    /**
     * @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;
        
    }

    /**
     * @see de.intevation.gnv.transition.Transition#getInputData()
     */
    public Collection<InputData> getInputData() throws TransitionException {
        return this.inputData.values();
    }
}

http://dive4elements.wald.intevation.org