001package jmri.jmrit.logixng.actions.configurexml;
002
003import jmri.*;
004import jmri.configurexml.JmriConfigureXmlException;
005import jmri.jmrit.logixng.DigitalActionManager;
006import jmri.jmrit.logixng.NamedBeanAddressing;
007import jmri.jmrit.logixng.actions.ActionDispatcher;
008import jmri.jmrit.logixng.actions.ActionTurnout;
009import jmri.jmrit.logixng.util.configurexml.LogixNG_SelectEnumXml;
010import jmri.jmrit.logixng.util.parser.ParserException;
011
012import org.jdom2.Element;
013
014/**
015 * Handle XML configuration for ActionDispatcher objects.
016 *
017 * @author Bob Jacobsen Copyright: Copyright (c) 2004, 2008, 2010
018 * @author Daniel Bergqvist Copyright 2021
019 * @author Dave Sand Copyright (C) 2021
020 */
021public class ActionDispatcherXml extends jmri.managers.configurexml.AbstractNamedBeanManagerConfigXML {
022
023    public ActionDispatcherXml() {
024    }
025
026    /**
027     * Default implementation for storing the contents of a Dispatcher action
028     *
029     * @param o Object to store, of type ActionDispatcher
030     * @return Element containing the complete info
031     */
032    @Override
033    public Element store(Object o) {
034        ActionDispatcher p = (ActionDispatcher) o;
035
036        Element element = new Element("ActionDispatcher");
037        element.setAttribute("class", this.getClass().getName());
038        element.addContent(new Element("systemName").addContent(p.getSystemName()));
039
040        storeCommon(p, element);
041
042        var selectEnumXml = new LogixNG_SelectEnumXml<ActionDispatcher.DirectOperation>();
043
044        String trainInfoFileName = p.getTrainInfoFileName();
045        if (trainInfoFileName != null) {
046            element.addContent(new Element("trainInfoFileName").addContent(trainInfoFileName));
047        }
048
049        element.addContent(new Element("addressing").addContent(p.getAddressing().name()));
050        element.addContent(new Element("reference").addContent(p.getReference()));
051        element.addContent(new Element("localVariable").addContent(p.getLocalVariable()));
052        element.addContent(new Element("formula").addContent(p.getFormula()));
053
054        element.addContent(selectEnumXml.store(p.getSelectEnum(), "operation"));
055
056        element.addContent(new Element("dataAddressing").addContent(p.getDataAddressing().name()));
057        element.addContent(new Element("dataReference").addContent(p.getDataReference()));
058        element.addContent(new Element("dataLocalVariable").addContent(p.getDataLocalVariable()));
059        element.addContent(new Element("dataFormula").addContent(p.getDataFormula()));
060
061        element.addContent(new Element("resetOption").addContent(p.getResetOption() ? "true" : "false"));
062        element.addContent(new Element("terminateOption").addContent(p.getTerminateOption() ? "true" : "false"));
063        element.addContent(new Element("trainPriority").addContent(String.valueOf(p.getTrainPriority())));
064
065        return element;
066    }
067
068    @Override
069    public boolean load(Element shared, Element perNode) throws JmriConfigureXmlException {     // Test class that inherits this class throws exception
070        String sys = getSystemName(shared);
071        String uname = getUserName(shared);
072        ActionDispatcher h = new ActionDispatcher(sys, uname);
073
074        loadCommon(h, shared);
075
076        var selectEnumXml = new LogixNG_SelectEnumXml<ActionDispatcher.DirectOperation>();
077
078        selectEnumXml.load(shared.getChild("operation"), h.getSelectEnum());
079        selectEnumXml.loadLegacy(
080                shared, h.getSelectEnum(),
081                "operationAddressing",
082                "operationDirect",
083                "operationReference",
084                "operationLocalVariable",
085                "operationFormula");
086
087        try {
088            Element elem = shared.getChild("trainInfoFileName");
089            if (elem != null) {
090                h.setTrainInfoFileName(elem.getTextTrim());
091            }
092
093            elem = shared.getChild("addressing");
094            if (elem != null) {
095                h.setAddressing(NamedBeanAddressing.valueOf(elem.getTextTrim()));
096            }
097
098            elem = shared.getChild("reference");
099            if (elem != null) h.setReference(elem.getTextTrim());
100
101            elem = shared.getChild("localVariable");
102            if (elem != null) h.setLocalVariable(elem.getTextTrim());
103
104            elem = shared.getChild("formula");
105            if (elem != null) h.setFormula(elem.getTextTrim());
106
107
108            elem = shared.getChild("dataAddressing");
109            if (elem != null) {
110                h.setDataAddressing(NamedBeanAddressing.valueOf(elem.getTextTrim()));
111            }
112
113            elem = shared.getChild("dataReference");
114            if (elem != null) h.setDataReference(elem.getTextTrim());
115
116            elem = shared.getChild("dataLocalVariable");
117            if (elem != null) h.setDataLocalVariable(elem.getTextTrim());
118
119            elem = shared.getChild("dataFormula");
120            if (elem != null) h.setDataFormula(elem.getTextTrim());
121
122
123            elem = shared.getChild("resetOption");
124            h.setResetOption((elem != null) ? elem.getTextTrim().equals("true") : false);
125
126            elem = shared.getChild("terminateOption");
127            h.setTerminateOption((elem != null) ? elem.getTextTrim().equals("true") : false);
128
129            elem = shared.getChild("trainPriority");
130            if (elem != null) h.setTrainPriority(Integer.parseInt(elem.getTextTrim()));
131
132
133        } catch (ParserException e) {
134            throw new JmriConfigureXmlException(e);
135        }
136
137        InstanceManager.getDefault(DigitalActionManager.class).registerAction(h);
138        return true;
139    }
140
141//    private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(ActionDispatcherXml.class);
142}