001package jmri.jmrit.logixng.actions.configurexml;
002
003import org.jdom2.Element;
004
005import jmri.InstanceManager;
006import jmri.configurexml.JmriConfigureXmlException;
007import jmri.jmrit.logixng.DigitalActionManager;
008import jmri.jmrit.logixng.MaleSocket;
009import jmri.jmrit.logixng.NamedBeanAddressing;
010import jmri.jmrit.logixng.NamedTable;
011import jmri.jmrit.logixng.TableRowOrColumn;
012import jmri.jmrit.logixng.actions.TableForEach;
013import jmri.jmrit.logixng.util.configurexml.LogixNG_SelectNamedBeanXml;
014import jmri.jmrit.logixng.util.parser.ParserException;
015
016/**
017 * Handle XML configuration for TableForEach objects.
018 *
019 * @author Bob Jacobsen Copyright: Copyright (c) 2004, 2008, 2010
020 * @author Daniel Bergqvist Copyright (C) 2019
021 */
022public class TableForEachXml extends jmri.managers.configurexml.AbstractNamedBeanManagerConfigXML {
023
024    public TableForEachXml() {
025    }
026
027    /**
028     * Default implementation for storing the contents of a SE8cSignalHead
029     *
030     * @param o Object to store, of type TripleTurnoutSignalHead
031     * @return Element containing the complete info
032     */
033    @Override
034    public Element store(Object o) {
035        TableForEach p = (TableForEach) o;
036
037        Element element = new Element("TableForEach");
038        element.setAttribute("class", this.getClass().getName());
039        element.addContent(new Element("systemName").addContent(p.getSystemName()));
040
041        storeCommon(p, element);
042
043        element.addContent(new Element("localVariable").addContent(p.getLocalVariableName()));
044
045        var selectNamedBeanXml = new LogixNG_SelectNamedBeanXml<NamedTable>();
046        element.addContent(selectNamedBeanXml.store(p.getSelectNamedBean(), "namedBean"));
047
048        element.addContent(new Element("rowOrColumnAddressing").addContent(p.getRowOrColumnAddressing().name()));
049        element.addContent(new Element("rowOrColumnName").addContent(p.getRowOrColumnName()));
050        element.addContent(new Element("rowOrColumnReference").addContent(p.getRowOrColumnReference()));
051        element.addContent(new Element("rowOrColumnLocalVariable").addContent(p.getRowOrColumnLocalVariable()));
052        element.addContent(new Element("rowOrColumnFormula").addContent(p.getRowOrColumnFormula()));
053
054        element.addContent(new Element("tableRowOrColumn").addContent(p.getRowOrColumn().name()));
055
056        Element e2 = new Element("Socket");
057        e2.addContent(new Element("socketName").addContent(p.getChild(0).getName()));
058        MaleSocket socket = p.getSocket().getConnectedSocket();
059        String socketSystemName;
060        if (socket != null) {
061            socketSystemName = socket.getSystemName();
062        } else {
063            socketSystemName = p.getSocketSystemName();
064        }
065        if (socketSystemName != null) {
066            e2.addContent(new Element("systemName").addContent(socketSystemName));
067        }
068        element.addContent(e2);
069
070        return element;
071    }
072
073    @Override
074    public boolean load(Element shared, Element perNode) throws JmriConfigureXmlException {
075
076        String sys = getSystemName(shared);
077        String uname = getUserName(shared);
078        TableForEach h = new TableForEach(sys, uname);
079
080        loadCommon(h, shared);
081
082        Element tableRowOrColumnElement = shared.getChild("tableRowOrColumn");
083        TableRowOrColumn tableRowOrColumn =
084                TableRowOrColumn.valueOf(tableRowOrColumnElement.getTextTrim());
085        h.setRowOrColumn(tableRowOrColumn);
086
087        Element socketName = shared.getChild("Socket").getChild("socketName");
088        h.getChild(0).setName(socketName.getTextTrim());
089        Element socketSystemName = shared.getChild("Socket").getChild("systemName");
090        if (socketSystemName != null) {
091            h.setSocketSystemName(socketSystemName.getTextTrim());
092        }
093
094        var selectNamedBeanXml = new LogixNG_SelectNamedBeanXml<NamedTable>();
095        selectNamedBeanXml.load(shared.getChild("namedBean"), h.getSelectNamedBean());
096        selectNamedBeanXml.loadLegacy(shared, h.getSelectNamedBean(), "table", "tableAddressing", "tableReference", "tableLocalVariable", "tableFormula");
097
098        try {
099            Element elem = shared.getChild("rowOrColumnAddressing");
100            if (elem != null) {
101                h.setRowOrColumnAddressing(NamedBeanAddressing.valueOf(elem.getTextTrim()));
102            }
103
104            Element rowOrColumnName = shared.getChild("rowOrColumnName");
105            if (rowOrColumnName != null) h.setRowOrColumnName(rowOrColumnName.getTextTrim());
106
107            elem = shared.getChild("rowOrColumnReference");
108            if (elem != null) h.setRowOrColumnReference(elem.getTextTrim());
109
110            elem = shared.getChild("rowOrColumnLocalVariable");
111            if (elem != null) h.setRowOrColumnLocalVariable(elem.getTextTrim());
112
113            elem = shared.getChild("rowOrColumnFormula");
114            if (elem != null) h.setRowOrColumnFormula(elem.getTextTrim());
115
116        } catch (ParserException e) {
117            throw new JmriConfigureXmlException(e);
118        }
119
120        Element rowOrColumnName = shared.getChild("rowOrColumnName");
121        if (rowOrColumnName != null) {
122            h.setRowOrColumnName(rowOrColumnName.getTextTrim());
123        }
124
125        Element localVariable = shared.getChild("localVariable");
126        if (localVariable != null) {
127            h.setLocalVariableName(localVariable.getTextTrim());
128        }
129
130        InstanceManager.getDefault(DigitalActionManager.class).registerAction(h);
131        return true;
132    }
133
134//    private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(TableForEachXml.class);
135}