001package jmri.jmrit.logixng.actions.configurexml;
002
003import jmri.*;
004import jmri.configurexml.JmriConfigureXmlException;
005import jmri.jmrit.logix.Warrant;
006import jmri.jmrit.logixng.DigitalActionManager;
007import jmri.jmrit.logixng.NamedBeanAddressing;
008import jmri.jmrit.logixng.actions.ActionWarrant;
009import jmri.jmrit.logixng.util.configurexml.LogixNG_SelectEnumXml;
010import jmri.jmrit.logixng.util.configurexml.LogixNG_SelectNamedBeanXml;
011import jmri.jmrit.logixng.util.parser.ParserException;
012
013import org.jdom2.Element;
014
015/**
016 * Handle XML configuration for Warrant objects.
017 *
018 * @author Bob Jacobsen Copyright: Copyright (c) 2004, 2008, 2010
019 * @author Daniel Bergqvist Copyright 2021
020 * @author Dave Sand Copyright (C) 2021
021 * @author Pete Cressman Copyright (C) 2022
022 */
023public class ActionWarrantXml extends jmri.managers.configurexml.AbstractNamedBeanManagerConfigXML {
024
025    public ActionWarrantXml() {
026    }
027
028    /**
029     * Default implementation for storing the contents of a Warrant
030     *
031     * @param o Object to store, of type TriggerWarrant
032     * @return Element containing the complete info
033     */
034    @Override
035    public Element store(Object o) {
036        ActionWarrant p = (ActionWarrant) o;
037
038        Element element = new Element("ActionWarrant");
039        element.setAttribute("class", this.getClass().getName());
040        element.addContent(new Element("systemName").addContent(p.getSystemName()));
041
042        storeCommon(p, element);
043
044        var selectNamedBeanXml = new LogixNG_SelectNamedBeanXml<Warrant>();
045        element.addContent(selectNamedBeanXml.store(p.getSelectNamedBean(), "namedBean"));
046
047        var selectMemoryNamedBeanXml = new LogixNG_SelectNamedBeanXml<Memory>();
048        element.addContent(selectMemoryNamedBeanXml.store(p.getSelectMemoryNamedBean(), "memoryNamedBean"));
049
050        var selectEnumXml = new LogixNG_SelectEnumXml<ActionWarrant.DirectOperation>();
051        element.addContent(selectEnumXml.store(p.getSelectEnum(), "operation"));
052
053        element.addContent(new Element("dataAddressing").addContent(p.getDataAddressing().name()));
054        element.addContent(new Element("dataReference").addContent(p.getDataReference()));
055        element.addContent(new Element("dataLocalVariable").addContent(p.getDataLocalVariable()));
056        element.addContent(new Element("dataFormula").addContent(p.getDataFormula()));
057
058        element.addContent(new Element("trainIdName").addContent(p.getTrainData()));
059        element.addContent(new Element("controlAutoTrain").addContent(p.getControlAutoTrain().name()));
060
061        return element;
062    }
063
064    @Override
065    public boolean load(Element shared, Element perNode) throws JmriConfigureXmlException {     // Test class that inherits this class throws exception
066        String sys = getSystemName(shared);
067        String uname = getUserName(shared);
068        ActionWarrant h = new ActionWarrant(sys, uname);
069
070        loadCommon(h, shared);
071
072        var selectNamedBeanXml = new LogixNG_SelectNamedBeanXml<Warrant>();
073        var selectEnumXml = new LogixNG_SelectEnumXml<ActionWarrant.DirectOperation>();
074
075        var selectMemoryNamedBeanXml = new LogixNG_SelectNamedBeanXml<Memory>();
076
077        selectNamedBeanXml.load(shared.getChild("namedBean"), h.getSelectNamedBean());
078        selectNamedBeanXml.loadLegacy(shared, h.getSelectNamedBean(), "warrant");
079        selectMemoryNamedBeanXml.load(shared.getChild("memoryNamedBean"), h.getSelectMemoryNamedBean());
080
081        selectEnumXml.load(shared.getChild("operation"), h.getSelectEnum());
082        selectEnumXml.loadLegacy(
083                shared, h.getSelectEnum(),
084                "operationAddressing",
085                "operationDirect",
086                "operationReference",
087                "operationLocalVariable",
088                "operationFormula");
089
090        try {
091            Element elem = shared.getChild("dataAddressing");
092            if (elem != null) {
093                h.setDataAddressing(NamedBeanAddressing.valueOf(elem.getTextTrim()));
094            }
095
096            elem = shared.getChild("dataReference");
097            if (elem != null) h.setDataReference(elem.getTextTrim());
098
099            elem = shared.getChild("dataLocalVariable");
100            if (elem != null) h.setDataLocalVariable(elem.getTextTrim());
101
102            elem = shared.getChild("dataFormula");
103            if (elem != null) h.setDataFormula(elem.getTextTrim());
104
105
106            elem = shared.getChild("trainIdName");
107            if (elem != null) h.setTrainData(elem.getTextTrim());
108
109            elem = shared.getChild("controlAutoTrain");
110            if (elem != null) {
111                h.setControlAutoTrain(ActionWarrant.ControlAutoTrain.valueOf(elem.getTextTrim()));
112            }
113
114        } catch (ParserException e) {
115            throw new JmriConfigureXmlException(e);
116        }
117
118        InstanceManager.getDefault(DigitalActionManager.class).registerAction(h);
119        return true;
120    }
121
122//    private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(ActionWarrantXml.class);
123}