001package jmri.jmrit.logixng.actions.configurexml;
002
003import jmri.InstanceManager;
004import jmri.Memory;
005import jmri.Reporter;
006import jmri.configurexml.JmriConfigureXmlException;
007import jmri.jmrit.logixng.DigitalActionManager;
008import jmri.jmrit.logixng.NamedBeanAddressing;
009import jmri.jmrit.logixng.NamedTable;
010import jmri.jmrit.logixng.NamedTableManager;
011import jmri.jmrit.logixng.actions.ActionSetReporter;
012import jmri.jmrit.logixng.util.configurexml.LogixNG_SelectNamedBeanXml;
013import jmri.jmrit.logixng.util.configurexml.LogixNG_SelectTableXml;
014import jmri.jmrit.logixng.util.parser.ParserException;
015
016import org.jdom2.Element;
017
018/**
019 * Handle XML configuration for ActionSetReporter objects.
020 *
021 * @author Bob Jacobsen Copyright: Copyright (c) 2004, 2008, 2010
022 * @author Daniel Bergqvist Copyright (C) 2024
023 */
024public class ActionSetReporterXml extends jmri.managers.configurexml.AbstractNamedBeanManagerConfigXML {
025
026    public ActionSetReporterXml() {
027    }
028
029    /**
030     * Default implementation for storing the contents of a ActionSetReporter
031     *
032     * @param o Object to store, of type ActionSetReporter
033     * @return Element containing the complete info
034     */
035    @Override
036    public Element store(Object o) {
037        ActionSetReporter p = (ActionSetReporter) o;
038
039        LogixNG_SelectTableXml selectTableXml = new LogixNG_SelectTableXml();
040
041        Element element = new Element("ActionSetReporter");
042        element.setAttribute("class", this.getClass().getName());
043        element.addContent(new Element("systemName").addContent(p.getSystemName()));
044
045        storeCommon(p, element);
046
047        var selectNamedBeanXml = new LogixNG_SelectNamedBeanXml<Reporter>();
048        element.addContent(selectNamedBeanXml.store(p.getSelectNamedBean(), "namedBean"));
049
050        var selectOtherMemoryNamedBeanXml = new LogixNG_SelectNamedBeanXml<Memory>();
051        element.addContent(selectOtherMemoryNamedBeanXml.store(p.getSelectOtherMemoryNamedBean(), "otherMemoryNamedBean"));
052
053        element.addContent(new Element("reporterOperation").addContent(p.getReporterOperation().name()));
054
055        element.addContent(new Element("otherConstant").addContent(p.getConstantValue()));
056        element.addContent(new Element("otherVariable").addContent(p.getOtherLocalVariable()));
057        element.addContent(new Element("otherFormula").addContent(p.getOtherFormula()));
058
059        element.addContent(selectTableXml.store(p.getSelectTable(), "table"));
060
061        element.addContent(new Element("provideAnIdTag").addContent(p.isProvideAnIdTag()? "yes" : "no"));
062
063        return element;
064    }
065
066    @Override
067    public boolean load(Element shared, Element perNode) throws JmriConfigureXmlException {
068        String sys = getSystemName(shared);
069        String uname = getUserName(shared);
070        ActionSetReporter h = new ActionSetReporter(sys, uname);
071
072        LogixNG_SelectTableXml selectTableXml = new LogixNG_SelectTableXml();
073
074        loadCommon(h, shared);
075
076        var selectNamedBeanXml = new LogixNG_SelectNamedBeanXml<Reporter>();
077        selectNamedBeanXml.load(shared.getChild("namedBean"), h.getSelectNamedBean());
078        selectNamedBeanXml.loadLegacy(shared, h.getSelectNamedBean(), "memory");
079
080        var selectOtherMemoryNamedBeanXml = new LogixNG_SelectNamedBeanXml<Memory>();
081        selectOtherMemoryNamedBeanXml.load(shared.getChild("otherMemoryNamedBean"), h.getSelectOtherMemoryNamedBean());
082        selectOtherMemoryNamedBeanXml.loadLegacy(shared, h.getSelectOtherMemoryNamedBean(), "otherMemory", null, null, null, null);
083
084        Element queryType = shared.getChild("reporterOperation");
085        if (queryType != null) {
086            try {
087                h.setMemoryOperation(ActionSetReporter.ReporterOperation.valueOf(queryType.getTextTrim()));
088            } catch (ParserException e) {
089                log.error("cannot set reporter operation: {}", queryType.getTextTrim(), e);
090            }
091        }
092
093        try {
094            Element elem = shared.getChild("otherConstant");
095            if (elem != null) h.setOtherConstantValue(elem.getTextTrim());
096
097            elem = shared.getChild("otherTableCell");
098            if (elem != null) {
099                boolean result = false;
100                String ref = elem.getTextTrim();
101                if (!ref.isEmpty()) {
102                    String[] refParts = ref.substring(1).split("[\\[\\]]");  // Remove first { and then split on [ and ]
103//                      System.out.format("refParts.length: %d, '%s', '%s'%n", refParts.length, refParts[0], refParts[1]);
104                    if (refParts.length == 3) {
105                        String table = refParts[0];
106                        String[] rowColumnParts = refParts[1].split(",");
107                        if (rowColumnParts.length == 2) {
108                            String row = rowColumnParts[0];
109                            String column = rowColumnParts[1];
110//                                System.out.format("Table: '%s', row: '%s', column: '%s'%n", table, row, column);
111
112                            h.getSelectTable().setTableNameAddressing(NamedBeanAddressing.Direct);
113                            if (table != null) {
114                                NamedTable t = InstanceManager.getDefault(NamedTableManager.class).getNamedTable(table);
115                                if (t != null) h.getSelectTable().setTable(t);
116                                else h.getSelectTable().removeTable();
117                            }
118                            h.getSelectTable().setTableRowAddressing(NamedBeanAddressing.Direct);
119                            h.getSelectTable().setTableRowName(row);
120                            h.getSelectTable().setTableColumnAddressing(NamedBeanAddressing.Direct);
121                            h.getSelectTable().setTableColumnName(column);
122                            result = true;
123                        }
124                    }
125                    if (!result) throw new JmriConfigureXmlException("otherTableCell has invalid value: "+ref);
126                }
127            }
128
129            elem = shared.getChild("otherVariable");
130            if (elem != null) h.setOtherLocalVariable(elem.getTextTrim());
131
132            elem = shared.getChild("otherFormula");
133            if (elem != null) h.setOtherFormula(elem.getTextTrim());
134
135        } catch (ParserException e) {
136            throw new JmriConfigureXmlException(e);
137        }
138
139        selectTableXml.load(shared.getChild("table"), h.getSelectTable());
140
141        Element provideAnIdTagElem = shared.getChild("provideAnIdTag");
142        if (provideAnIdTagElem != null) {
143            h.setProvideAnIdTag("yes".equals(provideAnIdTagElem.getTextTrim()));
144        }
145
146        InstanceManager.getDefault(DigitalActionManager.class).registerAction(h);
147        return true;
148    }
149
150    private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(ActionSetReporterXml.class);
151}