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.NamedTable;
008import jmri.jmrit.logixng.NamedTableManager;
009import jmri.jmrit.logixng.actions.ActionTable;
010import jmri.jmrit.logixng.actions.ActionTable.ConstantType;
011import jmri.jmrit.logixng.util.configurexml.LogixNG_SelectNamedBeanXml;
012import jmri.jmrit.logixng.util.configurexml.LogixNG_SelectTableXml;
013import jmri.jmrit.logixng.util.parser.ParserException;
014
015import org.jdom2.Element;
016
017/**
018 * Handle XML configuration for ActionTable objects.
019 *
020 * @author Bob Jacobsen Copyright: Copyright (c) 2004, 2008, 2010
021 * @author Daniel Bergqvist Copyright (C) 2019
022 */
023public class ActionTableXml extends jmri.managers.configurexml.AbstractNamedBeanManagerConfigXML {
024
025    public ActionTableXml() {
026    }
027
028    /**
029     * Default implementation for storing the contents of a ActionTable
030     *
031     * @param o Object to store, of type ActionTable
032     * @return Element containing the complete info
033     */
034    @Override
035    public Element store(Object o) {
036        ActionTable p = (ActionTable) o;
037
038        LogixNG_SelectTableXml selectTableXml = new LogixNG_SelectTableXml();
039
040        Element element = new Element("ActionTable");   // NOI18N
041        element.setAttribute("class", this.getClass().getName());   // NOI18N
042        element.addContent(new Element("systemName").addContent(p.getSystemName()));    // NOI18N
043
044        storeCommon(p, element);
045
046        var selectMemoryNamedBeanXml = new LogixNG_SelectNamedBeanXml<Memory>();
047        var selectBlockNamedBeanXml = new LogixNG_SelectNamedBeanXml<Block>();
048        var selectReporterNamedBeanXml = new LogixNG_SelectNamedBeanXml<Reporter>();
049
050        element.addContent(selectTableXml.store(p.getSelectTableToSet(), "tableToSet"));
051
052        element.addContent(selectMemoryNamedBeanXml.store(p.getSelectMemoryNamedBean(), "memoryNamedBean"));
053        element.addContent(new Element("listenToMemory").addContent(p.getListenToMemory() ? "yes" : "no"));
054
055        element.addContent(selectBlockNamedBeanXml.store(p.getSelectBlockNamedBean(), "blockNamedBean"));
056        element.addContent(new Element("listenToBlock").addContent(p.getListenToBlock() ? "yes" : "no"));
057
058        element.addContent(selectReporterNamedBeanXml.store(p.getSelectReporterNamedBean(), "reporterNamedBean"));
059        element.addContent(new Element("listenToReporter").addContent(p.getListenToReporter() ? "yes" : "no"));
060
061        element.addContent(new Element("variableOperation").addContent(p.getVariableOperation().name()));   // NOI18N
062
063        element.addContent(new Element("constantType").addContent(p.getConstantType().name()));   // NOI18N
064        element.addContent(new Element("constant").addContent(p.getConstantValue()));   // NOI18N
065        element.addContent(new Element("otherVariable").addContent(p.getOtherLocalVariable())); // NOI18N
066        element.addContent(new Element("reference").addContent(p.getReference())); // NOI18N
067        element.addContent(new Element("formula").addContent(p.getFormula()));  // NOI18N
068
069        element.addContent(selectTableXml.store(p.getSelectTable(), "table"));
070
071        return element;
072    }
073
074    @Override
075    public boolean load(Element shared, Element perNode) throws JmriConfigureXmlException {
076        String sys = getSystemName(shared);
077        String uname = getUserName(shared);
078        ActionTable h = new ActionTable(sys, uname);
079
080        LogixNG_SelectTableXml selectTableXml = new LogixNG_SelectTableXml();
081
082        loadCommon(h, shared);
083
084        var selectMemoryNamedBeanXml = new LogixNG_SelectNamedBeanXml<Memory>();
085        var selectBlockNamedBeanXml = new LogixNG_SelectNamedBeanXml<Block>();
086        var selectReporterNamedBeanXml = new LogixNG_SelectNamedBeanXml<Reporter>();
087
088        selectTableXml.load(shared.getChild("tableToSet"), h.getSelectTableToSet());
089
090        selectMemoryNamedBeanXml.load(shared.getChild("memoryNamedBean"), h.getSelectMemoryNamedBean());
091        selectBlockNamedBeanXml.load(shared.getChild("blockNamedBean"), h.getSelectBlockNamedBean());
092        selectReporterNamedBeanXml.load(shared.getChild("reporterNamedBean"), h.getSelectReporterNamedBean());
093
094        Element listenToMemoryElem = shared.getChild("listenToMemory");
095        if (listenToMemoryElem != null) {
096            h.setListenToMemory("yes".equals(listenToMemoryElem.getTextTrim()));
097        }
098
099        Element listenToBlockElem = shared.getChild("listenToBlock");
100        if (listenToBlockElem != null) {
101            h.setListenToBlock("yes".equals(listenToBlockElem.getTextTrim()));
102        }
103
104        Element listenToReporterElem = shared.getChild("listenToReporter");
105        if (listenToReporterElem != null) {
106            h.setListenToReporter("yes".equals(listenToReporterElem.getTextTrim()));
107        }
108
109        Element queryType = shared.getChild("variableOperation");   // NOI18N
110        if (queryType != null) {
111            try {
112                h.setVariableOperation(ActionTable.VariableOperation.valueOf(queryType.getTextTrim()));
113            } catch (ParserException e) {
114                log.error("cannot set variable operation: {}", queryType.getTextTrim(), e);  // NOI18N
115            }
116        }
117
118        Element constantType = shared.getChild("constantType"); // NOI18N
119        if (constantType != null) {
120            h.setConstantType(ConstantType.valueOf(constantType.getTextTrim()));
121        }
122
123        Element constant = shared.getChild("constant"); // NOI18N
124        if (constant != null) {
125            h.setConstantValue(constant.getTextTrim());
126        }
127
128        Element otherTableCell = shared.getChild("otherTableCell"); // NOI18N
129        if (otherTableCell != null) {
130            boolean result = false;
131            String ref = otherTableCell.getTextTrim();
132            if (!ref.isEmpty()) {
133                String[] refParts = ref.substring(1).split("[\\[\\]]");  // Remove first { and then split on [ and ]
134//                System.out.format("refParts.length: %d, '%s', '%s'%n", refParts.length, refParts[0], refParts[1]);
135                if (refParts.length == 3) {
136                    String table = refParts[0];
137                    String[] rowColumnParts = refParts[1].split(",");
138                    if (rowColumnParts.length == 2) {
139                        String row = rowColumnParts[0];
140                        String column = rowColumnParts[1];
141//                        System.out.format("Table: '%s', row: '%s', column: '%s'%n", table, row, column);
142
143                        h.getSelectTable().setTableNameAddressing(NamedBeanAddressing.Direct);
144                        if (table != null) {
145                            NamedTable t = InstanceManager.getDefault(NamedTableManager.class).getNamedTable(table);
146                            if (t != null) h.getSelectTable().setTable(t);
147                            else h.getSelectTable().removeTable();
148                        }
149                        h.getSelectTable().setTableRowAddressing(NamedBeanAddressing.Direct);
150                        h.getSelectTable().setTableRowName(row);
151                        h.getSelectTable().setTableColumnAddressing(NamedBeanAddressing.Direct);
152                        h.getSelectTable().setTableColumnName(column);
153                        result = true;
154                    }
155                }
156                if (!result) throw new JmriConfigureXmlException("otherTableCell has invalid value: "+ref);
157            }
158        }
159
160        selectTableXml.load(shared.getChild("table"), h.getSelectTable());
161
162        Element otherVariable = shared.getChild("otherVariable");   // NOI18N
163        if (otherVariable != null) {
164            h.setOtherLocalVariable(otherVariable.getTextTrim());
165        }
166
167        Element reference = shared.getChild("reference");   // NOI18N
168        if (reference != null) {
169            h.setReference(reference.getTextTrim());
170        }
171
172        Element formula = shared.getChild("formula");   // NOI18N
173        if (formula != null) {
174            try {
175                h.setFormula(formula.getTextTrim());
176            } catch (ParserException e) {
177                log.error("cannot set data: {}", formula.getTextTrim(), e);  // NOI18N
178            }
179        }
180
181        InstanceManager.getDefault(DigitalActionManager.class).registerAction(h);
182        return true;
183    }
184
185    private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(ActionTableXml.class);
186}