001package jmri.jmrit.logixng.expressions.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.expressions.DigitalCallModule;
011import jmri.jmrit.logixng.util.configurexml.LogixNG_SelectNamedBeanXml;
012
013import org.jdom2.Element;
014
015/**
016 * Handle XML configuration for ExpressionLightXml 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("CallDigitalExpressionModule");
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            elementParameter.addContent(new Element("initalValueType").addContent(pd._initialValueType.name()));
050            elementParameter.addContent(new Element("initialValueData").addContent(pd._initialValueData));
051            elementParameter.addContent(new Element("returnValueType").addContent(pd._returnValueType.name()));
052            elementParameter.addContent(new Element("returnValueData").addContent(pd._returnValueData));
053            parameters.addContent(elementParameter);
054        }
055        element.addContent(parameters);
056
057        return element;
058    }
059
060    @Override
061    public boolean load(Element shared, Element perNode) throws JmriConfigureXmlException {
062        String sys = getSystemName(shared);
063        String uname = getUserName(shared);
064        DigitalCallModule h = new DigitalCallModule(sys, uname);
065
066        loadCommon(h, shared);
067
068        var selectNamedBeanXml = new LogixNG_SelectNamedBeanXml<Module>();
069        selectNamedBeanXml.load(shared.getChild("namedBean"), h.getSelectNamedBean());
070        selectNamedBeanXml.loadLegacy(shared, h.getSelectNamedBean(), "module", null, null, null, null);
071
072        List<Element> parameterList = shared.getChild("Parameters").getChildren();  // NOI18N
073        log.debug("Found {} parameters", parameterList.size() );  // NOI18N
074
075        for (Element e : parameterList) {
076            Element elementName = e.getChild("name");
077
078            SymbolTable.InitialValueType initialValueType = null;
079            Element elementType = e.getChild("initalValueType");
080            if (elementType != null) {
081                initialValueType = SymbolTable.InitialValueType.valueOf(elementType.getTextTrim());
082            }
083
084            Element elementInitialValueData = e.getChild("initialValueData");
085
086            Module.ReturnValueType returnValueType = null;
087            elementType = e.getChild("returnValueType");
088            if (elementType != null) {
089                returnValueType = Module.ReturnValueType.valueOf(elementType.getTextTrim());
090            }
091
092            Element elementReturnValueData = e.getChild("returnValueData");
093
094            if (elementName == null) throw new IllegalArgumentException("Element 'name' does not exists");
095            if (initialValueType == null) throw new IllegalArgumentException("Element 'initalValueType' does not exists");
096            if (elementInitialValueData == null) throw new IllegalArgumentException("Element 'initialValueData' does not exists");
097            if (returnValueType == null) throw new IllegalArgumentException("Element 'returnValueType' does not exists");
098            if (elementReturnValueData == null) throw new IllegalArgumentException("Element 'returnValueData' does not exists");
099
100            h.addParameter(elementName.getTextTrim(),
101                    initialValueType, elementInitialValueData.getTextTrim(),
102                    returnValueType, elementReturnValueData.getTextTrim());
103        }
104
105        InstanceManager.getDefault(DigitalExpressionManager.class).registerExpression(h);
106        return true;
107    }
108
109    private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(DigitalCallModuleXml.class);
110}