001package jmri.jmrit.logixng.actions.configurexml;
002
003import java.util.List;
004
005import jmri.InstanceManager;
006import jmri.configurexml.JmriConfigureXmlException;
007import jmri.jmrit.logixng.Module;
008import jmri.jmrit.logixng.*;
009import jmri.jmrit.logixng.Module.ParameterData;
010import jmri.jmrit.logixng.actions.DigitalCallModule;
011import jmri.jmrit.logixng.util.configurexml.LogixNG_SelectNamedBeanXml;
012
013import org.jdom2.Element;
014
015/**
016 * Handle XML configuration for ActionLightXml objects.
017 *
018 * @author Bob Jacobsen Copyright: Copyright (c) 2004, 2008, 2010
019 * @author Daniel Bergqvist Copyright (C) 2019
020 */
021public class DigitalCallModuleXml extends jmri.managers.configurexml.AbstractNamedBeanManagerConfigXML {
022
023    public DigitalCallModuleXml() {
024    }
025
026    /**
027     * Default implementation for storing the contents of a SE8cSignalHead
028     *
029     * @param o Object to store, of type TripleLightSignalHead
030     * @return Element containing the complete info
031     */
032    @Override
033    public Element store(Object o) {
034        DigitalCallModule p = (DigitalCallModule) o;
035
036        Element element = new Element("CallModule");
037        element.setAttribute("class", this.getClass().getName());
038        element.addContent(new Element("systemName").addContent(p.getSystemName()));
039
040        storeCommon(p, element);
041
042        var selectNamedBeanXml = new LogixNG_SelectNamedBeanXml<Module>();
043        element.addContent(selectNamedBeanXml.store(p.getSelectNamedBean(), "namedBean"));
044
045        Element parameters = new Element("Parameters");
046        for (ParameterData pd : p.getParameterData()) {
047            Element elementParameter = new Element("Parameter");
048            elementParameter.addContent(new Element("name").addContent(pd._name));
049            if (pd._initialValueType == null) pd._initialValueType = SymbolTable.InitialValueType.None;
050            elementParameter.addContent(new Element("initialValueType").addContent(pd._initialValueType.name()));
051            elementParameter.addContent(new Element("initialValueData").addContent(pd._initialValueData));
052            if (pd._returnValueType == null) pd._returnValueType = Module.ReturnValueType.None;
053            elementParameter.addContent(new Element("returnValueType").addContent(pd._returnValueType.name()));
054            elementParameter.addContent(new Element("returnValueData").addContent(pd._returnValueData));
055            parameters.addContent(elementParameter);
056        }
057        element.addContent(parameters);
058
059        return element;
060    }
061
062    @Override
063    public boolean load(Element shared, Element perNode) throws JmriConfigureXmlException {
064        String sys = getSystemName(shared);
065        String uname = getUserName(shared);
066        DigitalCallModule h = new DigitalCallModule(sys, uname);
067
068        loadCommon(h, shared);
069
070        var selectNamedBeanXml = new LogixNG_SelectNamedBeanXml<Module>();
071        selectNamedBeanXml.load(shared.getChild("namedBean"), h.getSelectNamedBean());
072        selectNamedBeanXml.loadLegacy(shared, h.getSelectNamedBean(), "module", null, null, null, null);
073
074        List<Element> parameterList = shared.getChild("Parameters").getChildren();  // NOI18N
075        log.debug("Found {} parameters", parameterList.size() );  // NOI18N
076
077        for (Element e : parameterList) {
078            Element elementName = e.getChild("name");
079
080            SymbolTable.InitialValueType initialValueType = null;
081            Element elementType = e.getChild("initialValueType");
082            if (elementType == null) {
083                elementType = e.getChild("initalValueType");    // Spelling error in previous versions of JMRI
084            }
085            if (elementType != null) {
086                initialValueType = SymbolTable.InitialValueType.valueOf(elementType.getTextTrim());
087            }
088
089            Element elementInitialValueData = e.getChild("initialValueData");
090
091            Module.ReturnValueType returnValueType = null;
092            elementType = e.getChild("returnValueType");
093            if (elementType != null) {
094                returnValueType = Module.ReturnValueType.valueOf(elementType.getTextTrim());
095            }
096
097            Element elementReturnValueData = e.getChild("returnValueData");
098
099            if (elementName == null) throw new IllegalArgumentException("Element 'name' does not exists");
100            if (initialValueType == null) throw new IllegalArgumentException("Element 'initialValueType' does not exists");
101            if (elementInitialValueData == null) throw new IllegalArgumentException("Element 'initialValueData' does not exists");
102            if (returnValueType == null) throw new IllegalArgumentException("Element 'returnValueType' does not exists");
103            if (elementReturnValueData == null) throw new IllegalArgumentException("Element 'returnValueData' does not exists");
104
105            h.addParameter(elementName.getTextTrim(),
106                    initialValueType, elementInitialValueData.getTextTrim(),
107                    returnValueType, elementReturnValueData.getTextTrim());
108        }
109
110        InstanceManager.getDefault(DigitalActionManager.class).registerAction(h);
111        return true;
112    }
113
114    private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(DigitalCallModuleXml.class);
115}