001package jmri.jmrit.logixng.actions.configurexml;
002
003import org.jdom2.Element;
004
005import jmri.InstanceManager;
006import jmri.Memory;
007import jmri.configurexml.JmriConfigureXmlException;
008import jmri.jmrit.logixng.DigitalActionManager;
009import jmri.jmrit.logixng.MaleSocket;
010import jmri.jmrit.logixng.actions.ForEach;
011import jmri.jmrit.logixng.actions.CommonManager;
012import jmri.jmrit.logixng.actions.ForEach.UserSpecifiedSource;
013import jmri.jmrit.logixng.util.configurexml.LogixNG_SelectNamedBeanXml;
014import jmri.jmrit.logixng.util.configurexml.LogixNG_SelectStringXml;
015import jmri.jmrit.logixng.util.parser.ParserException;
016
017/**
018 * Handle XML configuration for ForEach objects.
019 *
020 * @author Bob Jacobsen Copyright: Copyright (c) 2004, 2008, 2010
021 * @author Daniel Bergqvist Copyright (C) 2019
022 */
023public class ForEachXml extends jmri.managers.configurexml.AbstractNamedBeanManagerConfigXML {
024
025    public ForEachXml() {
026    }
027
028    /**
029     * Default implementation for storing the contents of a SE8cSignalHead
030     *
031     * @param o Object to store, of type TripleTurnoutSignalHead
032     * @return Element containing the complete info
033     */
034    @Override
035    public Element store(Object o) {
036        ForEach p = (ForEach) o;
037
038        Element element = new Element("ForEach");
039        element.setAttribute("class", this.getClass().getName());
040        element.addContent(new Element("systemName").addContent(p.getSystemName()));
041
042        storeCommon(p, element);
043
044        var selectVariableXml = new LogixNG_SelectStringXml();
045        var selectMemoryNamedBeanXml = new LogixNG_SelectNamedBeanXml<Memory>();
046
047        element.addContent(new Element("useCommonSource").addContent(p.isUseCommonSource()? "yes" : "no"));
048        element.addContent(new Element("commonManager").addContent(p.getCommonManager().name()));   // NOI18N
049
050        element.addContent(new Element("operation").addContent(p.getUserSpecifiedSource().name()));   // NOI18N
051
052        element.addContent(selectVariableXml.store(p.getSelectVariable(), "otherVariable"));
053
054        element.addContent(selectMemoryNamedBeanXml.store(p.getSelectMemoryNamedBean(), "memoryNamedBean"));
055
056        element.addContent(new Element("formula").addContent(p.getFormula()));  // NOI18N
057
058        element.addContent(new Element("localVariable").addContent(p.getLocalVariableName()));
059
060        Element e2 = new Element("Socket");
061        e2.addContent(new Element("socketName").addContent(p.getChild(0).getName()));
062        MaleSocket socket = p.getSocket().getConnectedSocket();
063        String socketSystemName;
064        if (socket != null) {
065            socketSystemName = socket.getSystemName();
066        } else {
067            socketSystemName = p.getSocketSystemName();
068        }
069        if (socketSystemName != null) {
070            e2.addContent(new Element("systemName").addContent(socketSystemName));
071        }
072        element.addContent(e2);
073
074        return element;
075    }
076
077    @Override
078    public boolean load(Element shared, Element perNode) throws JmriConfigureXmlException {
079
080        String sys = getSystemName(shared);
081        String uname = getUserName(shared);
082        ForEach h = new ForEach(sys, uname);
083
084        loadCommon(h, shared);
085
086        var selectVariableXml = new LogixNG_SelectStringXml();
087        var selectMemoryNamedBeanXml = new LogixNG_SelectNamedBeanXml<Memory>();
088
089        Element useCommonSourceElem = shared.getChild("useCommonSource");
090        if (useCommonSourceElem != null) {
091            h.setUseCommonSource("yes".equals(useCommonSourceElem.getTextTrim()));
092        }
093
094        Element commonManagerType = shared.getChild("commonManager");   // NOI18N
095        if (commonManagerType != null) {
096            try {
097                h.setCommonManager(CommonManager.valueOf(commonManagerType.getTextTrim()));
098            } catch (ParserException e) {
099                log.error("cannot set variable operation: {}", commonManagerType.getTextTrim(), e);  // NOI18N
100            }
101        }
102
103        Element operationType = shared.getChild("operation");   // NOI18N
104        if (operationType != null) {
105            try {
106                h.setUserSpecifiedSource(UserSpecifiedSource.valueOf(operationType.getTextTrim()));
107            } catch (ParserException e) {
108                log.error("cannot set variable operation: {}", operationType.getTextTrim(), e);  // NOI18N
109            }
110        }
111
112        selectVariableXml.load(shared.getChild("otherVariable"), h.getSelectVariable());
113
114        selectMemoryNamedBeanXml.load(shared.getChild("memoryNamedBean"), h.getSelectMemoryNamedBean());
115
116        Element formula = shared.getChild("formula");   // NOI18N
117        if (formula != null) {
118            try {
119                h.setFormula(formula.getTextTrim());
120            } catch (ParserException e) {
121                log.error("cannot set data: {}", formula.getTextTrim(), e);  // NOI18N
122            }
123        }
124
125        Element localVariable = shared.getChild("localVariable");
126        if (localVariable != null) {
127            h.setLocalVariableName(localVariable.getTextTrim());
128        }
129
130        Element socketName = shared.getChild("Socket").getChild("socketName");
131        h.getChild(0).setName(socketName.getTextTrim());
132        Element socketSystemName = shared.getChild("Socket").getChild("systemName");
133        if (socketSystemName != null) {
134            h.setSocketSystemName(socketSystemName.getTextTrim());
135        }
136
137        InstanceManager.getDefault(DigitalActionManager.class).registerAction(h);
138        return true;
139    }
140
141    private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(ForEachXml.class);
142}