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