001package jmri.jmrit.logixng.actions.configurexml;
002
003import jmri.*;
004import jmri.configurexml.JmriConfigureXmlException;
005import jmri.jmrit.logix.OBlock;
006import jmri.jmrit.logixng.DigitalActionManager;
007import jmri.jmrit.logixng.NamedBeanAddressing;
008import jmri.jmrit.logixng.actions.ActionOBlock;
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 OBlock 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 */
022public class ActionOBlockXml extends jmri.managers.configurexml.AbstractNamedBeanManagerConfigXML {
023
024    public ActionOBlockXml() {
025    }
026
027    /**
028     * Default implementation for storing the contents of an OBlock
029     *
030     * @param o Object to store, of type ActionOBlock
031     * @return Element containing the complete info
032     */
033    @Override
034    public Element store(Object o) {
035        ActionOBlock p = (ActionOBlock) o;
036
037        Element element = new Element("ActionOBlock");
038        element.setAttribute("class", this.getClass().getName());
039        element.addContent(new Element("systemName").addContent(p.getSystemName()));
040
041        storeCommon(p, element);
042
043        var selectNamedBeanXml = new LogixNG_SelectNamedBeanXml<OBlock>();
044        element.addContent(selectNamedBeanXml.store(p.getSelectNamedBean(), "namedBean"));
045
046        var selectMemoryNamedBeanXml = new LogixNG_SelectNamedBeanXml<Memory>();
047        element.addContent(selectMemoryNamedBeanXml.store(p.getSelectMemoryNamedBean(), "memoryNamedBean"));
048
049        var selectEnumXml = new LogixNG_SelectEnumXml<ActionOBlock.DirectOperation>();
050        element.addContent(selectEnumXml.store(p.getSelectEnum(), "operation"));
051
052        element.addContent(new Element("dataAddressing").addContent(p.getDataAddressing().name()));
053        element.addContent(new Element("dataReference").addContent(p.getDataReference()));
054        element.addContent(new Element("dataLocalVariable").addContent(p.getDataLocalVariable()));
055        element.addContent(new Element("dataFormula").addContent(p.getDataFormula()));
056
057        element.addContent(new Element("oblockValue").addContent(p.getOBlockValue()));
058
059        return element;
060    }
061
062    @Override
063    public boolean load(Element shared, Element perNode) throws JmriConfigureXmlException {     // Test class that inherits this class throws exception
064        String sys = getSystemName(shared);
065        String uname = getUserName(shared);
066        ActionOBlock h = new ActionOBlock(sys, uname);
067
068        loadCommon(h, shared);
069
070        var selectNamedBeanXml = new LogixNG_SelectNamedBeanXml<OBlock>();
071        var selectEnumXml = new LogixNG_SelectEnumXml<ActionOBlock.DirectOperation>();
072
073        var selectMemoryNamedBeanXml = new LogixNG_SelectNamedBeanXml<Memory>();
074
075        selectNamedBeanXml.load(shared.getChild("namedBean"), h.getSelectNamedBean());
076        selectNamedBeanXml.loadLegacy(shared, h.getSelectNamedBean(), "oblock");
077        selectMemoryNamedBeanXml.load(shared.getChild("memoryNamedBean"), h.getSelectMemoryNamedBean());
078
079        selectEnumXml.load(shared.getChild("operation"), h.getSelectEnum());
080        selectEnumXml.loadLegacy(
081                shared, h.getSelectEnum(),
082                "operationAddressing",
083                "operationDirect",
084                "operationReference",
085                "operationLocalVariable",
086                "operationFormula");
087
088        try {
089            Element elem = shared.getChild("dataAddressing");
090            if (elem != null) {
091                h.setDataAddressing(NamedBeanAddressing.valueOf(elem.getTextTrim()));
092            }
093
094            elem = shared.getChild("dataReference");
095            if (elem != null) h.setDataReference(elem.getTextTrim());
096
097            elem = shared.getChild("dataLocalVariable");
098            if (elem != null) h.setDataLocalVariable(elem.getTextTrim());
099
100            elem = shared.getChild("dataFormula");
101            if (elem != null) h.setDataFormula(elem.getTextTrim());
102
103
104            elem = shared.getChild("oblockValue");
105            if (elem != null) h.setOBlockValue(elem.getTextTrim());
106
107        } catch (ParserException e) {
108            throw new JmriConfigureXmlException(e);
109        }
110
111        InstanceManager.getDefault(DigitalActionManager.class).registerAction(h);
112        return true;
113    }
114
115//    private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(ActionOBlockXml.class);
116}