view gnv-artifacts/src/main/java/de/intevation/gnv/artifacts/GNVArtifactBase.java @ 67:0e9762ebd18d

Refactor ChartFactory so that it is able to be compiled without errors. gnv-artifacts/trunk@50 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Tim Englich <tim.englich@intevation.de>
date Wed, 09 Sep 2009 13:14:12 +0000
parents 5db77e0a8594
children d117fd4b82e5
line wrap: on
line source
/**
 *
 */
package de.intevation.gnv.artifacts;

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.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import de.intevation.artifactdatabase.Config;
import de.intevation.artifactdatabase.DefaultArtifact;
import de.intevation.gnv.artifacts.context.GNVArtifactContext;
import de.intevation.gnv.transition.DefaultInputData;
import de.intevation.gnv.transition.InputData;
import de.intevation.gnv.transition.InputValue;
import de.intevation.gnv.transition.OutputMode;
import de.intevation.gnv.transition.OutputTransition;
import de.intevation.gnv.transition.Transition;
import de.intevation.gnv.transition.TransitionFactory;
import de.intevation.gnv.transition.exception.TransitionException;

/**
 * @author Tim Englich <tim.englich@intevation.de>
 *
 */
public abstract class GNVArtifactBase extends DefaultArtifact {
    /**
     * the logger, used to log exceptions and additonaly information
     */
    private static Logger log = Logger.getLogger(GNVArtifactBase.class);
    /**
     * The UID of this Class
     */
    private static final long serialVersionUID = -8907096744400741458L;
    
    /**
     * The Identifier for the Replacement of the Artifactname
     */
    public static final String XPATH_IDENTIFIER_REPLACE = "IDENTIFIER";
    
    /**
     * The XPATH to the XML-Fragment that should be used for the Configuration
     */
    public static final String XPATH_ARTIFACT_CONFIGURATION= "/artifact-database/artifacts/artifact[@name='"+XPATH_IDENTIFIER_REPLACE+"']";
    
    
    /**
     * The current Transition
     */
    protected Transition current = null;
    
    /**
     * The Transitions that can be used
     */
    protected Map<String, Transition> transitions = null;

    /**
     * The Name of the Artifact
     */
    protected String name = null;
    
    /**
     * Constructor
     */
    public GNVArtifactBase() {
        super();
    }
    
    /**
     * @see de.intevation.artifactdatabase.DefaultArtifact#advance(org.w3c.dom.Document, java.lang.Object)
     */
    @Override
    public Document advance(Document target, Object context) {
        Document result = super.newDocument();
        if (this.current != null){
            String transitionName = this.readTransitionName(target);
            if (this.current.isTransitionReachable(transitionName)){
                // 1. Pr�fung ob Transition valide ist
                if (this.current.validate()){
                    
                    try {
                        Transition nextStep = this.transitions.get(transitionName);
                        // 2.Ergebnisse Berechnen
                        this.current.advance();
                        // 3. Ergebnisse �bergeben
                        nextStep.setDescibeData(this.current.getDescibeData());
                        nextStep.putInputData(this.current.getInputData());
                        // 4. Umschalten auf neue Transistion
                        this.current = nextStep;
                    } catch (TransitionException e) {
                        log.error(e,e);
                        // TODO: Errormmessage senden.
                    }
                    
                }else{
                    log.error("Advance nicht m�glich, da die Bedingungen f�r den �bergang " +
                                "in den neuen Zustand noch nicht gegeben ist.");
                    // TODO: Errormmessage senden.
                }
                
            }else{
                log.error("Transitions�bergang wird nicht unterst�tzt.");
                // TODO: Errormmessage senden.
            }
        }else{
            log.error("Kein Transitionsschritt aktiviert.");
            // TODO: Errormmessage senden.
        }
        return result;
    }

    protected String readTransitionName(Document document) {
        String returnValue = Config.getStringXPath(document, "action/target/@name");
        return returnValue;
    }

    protected Node getConfigurationFragment(Document document){
        log.debug("GNVArtifactBase.getConfigurationFragment");
        String xpathQuery = XPATH_ARTIFACT_CONFIGURATION.replaceAll(XPATH_IDENTIFIER_REPLACE, this.name);
        log.debug(xpathQuery);
        return Config.getNodeXPath(document,xpathQuery);
    }
    
    /**
     * @see de.intevation.artifactdatabase.DefaultArtifact#feed(org.w3c.dom.Document, java.lang.Object)
     */
    @Override
    public Document feed(Document target, Object context) {
        Document result = super.newDocument();
        try {
            if (this.current != null){
                this.current.putInputData(this.parseInputData(target));
                // TODO Ergebnisdokument erzeugen.
            }
        } catch (TransitionException e) {
            log.error(e,e);
            //TODO: Fehlerdokumenterzeugen.
        }
        return result;
    }
    
    /**
     * @see de.intevation.artifactdatabase.DefaultArtifact#setup(java.lang.String, java.lang.Object)
     */
    @Override
    public void setup(String identifier, Object context) {
        log.debug("GNVArtifactBase.setup");
        super.setup(identifier, context);
        if (context instanceof GNVArtifactContext){
            GNVArtifactContext gnvContext = (GNVArtifactContext)context;
            Document doc = gnvContext.getConfig();
            Node artifactNode = this.getConfigurationFragment(doc);
            NodeList transitionList = Config.getNodeSetXPath(artifactNode, "transitions/transition");
            this.transitions = new HashMap<String, Transition>(transitionList.getLength());
            for (int i  = 0 ; i < transitionList.getLength(); i++){
                Transition tmpTransition = TransitionFactory.getInstance().createTransition(transitionList.item(i));
                if (tmpTransition != null){
                    this.transitions.put(tmpTransition.getID(), tmpTransition);
                    if (this.current == null){
                        this.current = tmpTransition;
                    }
                }
            }
            
        }
    }
    
    
    protected Document createDescibeOutput(){
        log.debug("GNVArtifactBase.createDescibeOutput");
        Document document = super.newDocument();
        Element rootNode = this.createRootNode(document);
        this.createHeader(rootNode, document, "describe");
        this.createOutputs(rootNode, document);
        this.createCurrentState(rootNode, document);
        this.createReachableStates(rootNode, document);
        this.createModel(rootNode, document);
        this.createUserInterface(rootNode, document);
        
        return document;
    }
    
    protected Element createRootNode(Document document){
        Element rootNode = createElement(document,"result");
        document.appendChild(rootNode);
        return rootNode;
    }
    
    protected void createHeader(Element parent, Document document, String documentType){
        Element typeNode = createElement(document,"type");
        typeNode.setAttribute("name", documentType);
        parent.appendChild(typeNode);
        
        Element uuidNode = createElement(document,"uuid");
        uuidNode.setAttribute("value", super.identifier);
        parent.appendChild(uuidNode);
        
        Element hashNode = createElement(document,"hash");
        hashNode.setAttribute("value", this.hash());
        parent.appendChild(hashNode);

        
    }
    protected void createReachableStates(Element parent,Document document){
        Element stateNode = createElement(document,"reachable-states");
        if (this.current != null){
            Iterator<String> states = this.current.reachableTransitions().iterator();
            while(states.hasNext()){
                String value = states.next();
                Element currentNode = createElement(document,"state");
                currentNode.setAttribute("name", value);
                currentNode.setAttribute("description", transitions.get(value).getDescription());
                stateNode.appendChild(currentNode);
            }
        }
        parent.appendChild(stateNode);
    }
    
    protected void createCurrentState(Element parent, Document document){
        Element stateNode = createElement(document,"state");
        stateNode.setAttribute("name", this.current.getID());
        stateNode.setAttribute("description", this.current.getDescription());
        parent.appendChild(stateNode);
    }
    
    
    protected void createModel(Element parent, Document document){
        Element modelNode = createElement(document,"model");
        if (this.current != null){
            Collection<InputValue>  inputValues = this.current.getRequiredInputValues();
            if (inputValues != null){
                Iterator<InputValue> it = inputValues.iterator();
                while(it.hasNext()){
                    InputValue inputValue = it.next();
                    Element inputNode = createElement(document,"input");
                    inputNode.setAttribute("name", inputValue.getName());
                    inputNode.setAttribute("type", inputValue.getType());
                    modelNode.appendChild(inputNode);
                }
            }
        }
        parent.appendChild(modelNode);
    }
    
    protected void createUserInterface(Element parent, Document document){
        Element uiNode = createElement(document,"ui");
        
        if (this.current != null){
            this.current.describe(document, uiNode);
        }
        
        parent.appendChild(uiNode);
    }
    
    protected void createOutputs(Element parent, Document document){
        log.debug("GNVArtifactBase.createOutputs");
        Element outputsNode = createElement(document,"outputs");
        if (this.current instanceof OutputTransition){
            Collection<OutputMode> outputModes = ((OutputTransition)this.current).getOutputModes();
            if (outputModes != null){
                Iterator<OutputMode> it = outputModes.iterator();
                while(it.hasNext()){
                    OutputMode outputMode = it.next();
                    log.debug("Write Outputnode for "+ outputMode.toString());
                    Element outputModeNode = createElement(document,"output");
                    outputModeNode.setAttribute("name", outputMode.getName());
                    outputModeNode.setAttribute("description", outputMode.getDescription());
                    outputModeNode.setAttribute("mime-type", outputMode.getMimeType());
                    outputsNode.appendChild(outputModeNode);
                }
            }else{
                log.warn("No Outputmodes given.");
            }
        }
        parent.appendChild(outputsNode);
    }

    /**
     * @param document
     * @return
     */
    private Element createElement(Document document, String name) {
        Element node = document.createElementNS(DefaultArtifact.NAMESPACE_URI, name);
        node.setPrefix(DefaultArtifact.NAMESPACE_PREFIX);
        return node;
    }
    
    protected Collection<InputData> parseInputData(Document document){
        log.debug("GNVArtifactBase.parseInputData");
        Collection<InputData> returnValue = null;
        
        NodeList inputElemets = Config.getNodeSetXPath(document, "action/data/input");
        if(inputElemets != null){
            returnValue = new ArrayList<InputData>(inputElemets.getLength());
            for (int i = 0; i < inputElemets.getLength(); i++){
                Node inputDataNode = inputElemets.item(i);
                InputData inputData = new DefaultInputData(Config.getStringXPath(inputDataNode,"@name"), Config.getStringXPath(inputDataNode,"@value"));
                log.debug(inputData.toString());
                returnValue.add(inputData);
            }
        }
        return returnValue;
    }
}

http://dive4elements.wald.intevation.org