Package it.freedomotic.reactions

Source Code of it.freedomotic.reactions.ReactionConverter

/**
*
* Copyright (c) 2009-2013 Freedomotic team http://freedomotic.com
*
* This file is part of Freedomotic
*
* This Program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation; either version 2, or (at your option) any later version.
*
* This Program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* Freedomotic; see the file COPYING. If not, see
* <http://www.gnu.org/licenses/>.
*/
package it.freedomotic.reactions;

import java.util.ArrayList;

import com.thoughtworks.xstream.converters.Converter;
import com.thoughtworks.xstream.converters.MarshallingContext;
import com.thoughtworks.xstream.converters.UnmarshallingContext;
import com.thoughtworks.xstream.io.HierarchicalStreamReader;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
import it.freedomotic.core.Condition;
import java.util.Iterator;
import java.util.List;
import java.util.logging.Logger;

/**
*
* @author Enrico
*/
public class ReactionConverter
        implements Converter {

    @Override
    public void marshal(Object o, HierarchicalStreamWriter writer, MarshallingContext mc) {
        Reaction rea = (Reaction) o;
        writer.startNode("trigger");
        writer.setValue(rea.getTrigger().getName());
        writer.endNode();

        //start conditions
        if (rea.getConditions() != null && !rea.getConditions().isEmpty()) {
            writer.startNode("conditions");

            for (Condition c : rea.getConditions()) {
                if (c != null) {
                    //start condition
                    writer.startNode("condition");
                    //start target
                    writer.startNode("target");
                    writer.setValue(c.getTarget());
                    writer.endNode();
                    //end target
                    //start statement
                    Statement statement = c.getStatement();
                    writer.startNode("statement");
                    writer.startNode("logical");
                    writer.setValue(statement.getLogical());
                    writer.endNode(); //</logical>
                    writer.startNode("attribute");
                    writer.setValue(statement.getAttribute());
                    writer.endNode(); //</attribute>
                    writer.startNode("operand");
                    writer.setValue(statement.getOperand());
                    writer.endNode(); //</operand>
                    writer.startNode("value");
                    writer.setValue(statement.getValue());
                    writer.endNode(); //</value>
                    writer.endNode();
                    //end statement
                    writer.endNode();
                    //end condition
                }
            }
            writer.endNode();
            //end conditions
        }

        writer.startNode("sequence");

        for (Command c : rea.getCommands()) {
            if (c != null) {
                writer.startNode("command");
                writer.setValue(c.getName());
                writer.endNode(); //end command
            }
        }

        writer.endNode(); //end sequence
    }

    @Override
    public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext uc) {
        Trigger t;
        List<Condition> conditions = new ArrayList<Condition>();
        ArrayList<Command> list = new ArrayList<Command>();

        reader.moveDown(); //goes down to <trigger>
        String triggerName = reader.getValue();
        t = TriggerPersistence.getTrigger(triggerName.trim());
        reader.moveUp(); //up to root

        //go down to conditions or sequence
        reader.moveDown();
        if (reader.getNodeName().equalsIgnoreCase("conditions")) {
            while (reader.hasMoreChildren()) {
                //read a single condition
                reader.moveDown(); //move down to condition
                reader.moveDown(); //move down to target
                Condition condition = new Condition();
                condition.setTarget(reader.getValue().trim());
                reader.moveUp(); //move up to condition
                //parse statement
                ArrayList<String> statementValues = new ArrayList<String>();
                reader.moveDown(); //move down to statement
                while (reader.hasMoreChildren()) { //childs of statement (logical, attribute, ...)
                    reader.moveDown(); //move down to statement property
                    System.out.println("    " + reader.getValue().trim());
                    statementValues.add(reader.getValue());
                    reader.moveUp(); //move up to statement
                }
                reader.moveUp();//move up to condition
                Statement stm = new Statement();
                stm.create(statementValues.get(0),
                        statementValues.get(1),
                        statementValues.get(2),
                        statementValues.get(3));
                condition.setStatement(stm);
                conditions.add(condition);
                reader.moveUp(); //move up to conditions
            }
            reader.moveUp(); //move up to root
            reader.moveDown();//move down to sequence
        }
        while (reader.hasMoreChildren()) {
            reader.moveDown(); //move down to command
            Command c = CommandPersistence.getCommand(reader.getValue().trim());
            if (c != null) {
                list.add(c);
            }
            reader.moveUp(); //move up to sequence
        }
        reader.moveUp(); //move uo to root
        return new Reaction(t, conditions, list);
    }

    @Override
    public boolean canConvert(Class type) {
        return (type == Reaction.class);
    }
}
TOP

Related Classes of it.freedomotic.reactions.ReactionConverter

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.