001package jmri.jmrit.logixng.actions.configurexml;
002
003import java.util.AbstractMap;
004import java.util.ArrayList;
005import java.util.List;
006import java.util.Map;
007
008import jmri.InstanceManager;
009import jmri.jmrit.logixng.DigitalActionManager;
010import jmri.jmrit.logixng.MaleSocket;
011import jmri.jmrit.logixng.actions.Sequence;
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) 2020
020 */
021public class SequenceXml extends jmri.managers.configurexml.AbstractNamedBeanManagerConfigXML {
022
023    public SequenceXml() {
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        Sequence p = (Sequence) o;
035
036        Element element = new Element("Sequence");
037        element.setAttribute("class", this.getClass().getName());
038        element.addContent(new Element("systemName").addContent(p.getSystemName()));
039        
040        storeCommon(p, element);
041
042        Element e2 = new Element("StartSocket");
043        e2.addContent(new Element("socketName").addContent(p.getChild(0).getName()));
044        MaleSocket socket = p.getStartExpressionSocket().getConnectedSocket();
045        String socketSystemName;
046        if (socket != null) {
047            socketSystemName = socket.getSystemName();
048        } else {
049            socketSystemName = p.getStartExpressionSocketSystemName();
050        }
051        if (socketSystemName != null) {
052            e2.addContent(new Element("systemName").addContent(socketSystemName));
053        }
054        element.addContent(e2);
055
056        e2 = new Element("StopSocket");
057        e2.addContent(new Element("socketName").addContent(p.getChild(1).getName()));
058        socket = p.getStopExpressionSocket().getConnectedSocket();
059        if (socket != null) {
060            socketSystemName = socket.getSystemName();
061        } else {
062            socketSystemName = p.getStopExpressionSocketSystemName();
063        }
064        if (socketSystemName != null) {
065            e2.addContent(new Element("systemName").addContent(socketSystemName));
066        }
067        element.addContent(e2);
068
069        e2 = new Element("ResetSocket");
070        e2.addContent(new Element("socketName").addContent(p.getChild(2).getName()));
071        socket = p.getResetExpressionSocket().getConnectedSocket();
072        if (socket != null) {
073            socketSystemName = socket.getSystemName();
074        } else {
075            socketSystemName = p.getResetExpressionSocketSystemName();
076        }
077        if (socketSystemName != null) {
078            e2.addContent(new Element("systemName").addContent(socketSystemName));
079        }
080        element.addContent(e2);
081
082        Element e = new Element("Expressions");
083        for (int i=0; i < p.getNumExpressions(); i++) {
084            e2 = new Element("Socket");
085            e2.addContent(new Element("socketName").addContent(p.getExpressionSocket(i).getName()));
086            socket = p.getExpressionSocket(i).getConnectedSocket();
087            if (socket != null) {
088                socketSystemName = socket.getSystemName();
089            } else {
090                socketSystemName = p.getExpressionSocketSystemName(i);
091            }
092            if (socketSystemName != null) {
093                e2.addContent(new Element("systemName").addContent(socketSystemName));
094            }
095            e.addContent(e2);
096        }
097        element.addContent(e);
098
099        e = new Element("Actions");
100        for (int i=0; i < p.getNumActions(); i++) {
101            e2 = new Element("Socket");
102            e2.addContent(new Element("socketName").addContent(p.getActionSocket(i).getName()));
103            socket = p.getActionSocket(i).getConnectedSocket();
104            if (socket != null) {
105                socketSystemName = socket.getSystemName();
106            } else {
107                socketSystemName = p.getActionSocketSystemName(i);
108            }
109            if (socketSystemName != null) {
110                e2.addContent(new Element("systemName").addContent(socketSystemName));
111            }
112            e.addContent(e2);
113        }
114        element.addContent(e);
115
116        element.addContent(new Element("startImmediately").addContent(p.getStartImmediately() ? "yes" : "no"));
117        element.addContent(new Element("runContinuously").addContent(p.getRunContinuously() ? "yes" : "no"));
118        
119        return element;
120    }
121    
122    @Override
123    public boolean load(Element shared, Element perNode) {
124        List<Map.Entry<String, String>> expressionSystemNames = new ArrayList<>();
125        
126        Element socketNameElement = shared.getChild("StartSocket").getChild("socketName");
127        String startSocketName = socketNameElement.getTextTrim();
128        Element socketSystemNameElement = shared.getChild("StartSocket").getChild("systemName");
129        String startSocketSystemName = null;
130        if (socketSystemNameElement != null) {
131            startSocketSystemName = socketSystemNameElement.getTextTrim();
132        }
133        
134        socketNameElement = shared.getChild("StopSocket").getChild("socketName");
135        String stopSocketName = socketNameElement.getTextTrim();
136        socketSystemNameElement = shared.getChild("StopSocket").getChild("systemName");
137        String stopSocketSystemName = null;
138        if (socketSystemNameElement != null) {
139            stopSocketSystemName = socketSystemNameElement.getTextTrim();
140        }
141        
142        socketNameElement = shared.getChild("ResetSocket").getChild("socketName");
143        String resetSocketName = socketNameElement.getTextTrim();
144        socketSystemNameElement = shared.getChild("ResetSocket").getChild("systemName");
145        String resetSocketSystemName = null;
146        if (socketSystemNameElement != null) {
147            resetSocketSystemName = socketSystemNameElement.getTextTrim();
148        }
149        
150        Element expressionElement = shared.getChild("Expressions");
151        for (Element socketElement : expressionElement.getChildren()) {
152            String socketName = socketElement.getChild("socketName").getTextTrim();
153            Element systemNameElement = socketElement.getChild("systemName");
154            String systemName = null;
155            if (systemNameElement != null) {
156                systemName = systemNameElement.getTextTrim();
157            }
158            expressionSystemNames.add(new AbstractMap.SimpleEntry<>(socketName, systemName));
159        }
160        
161        List<Map.Entry<String, String>> actionSystemNames = new ArrayList<>();
162        
163        Element actionElement = shared.getChild("Actions");
164        for (Element socketElement : actionElement.getChildren()) {
165            String socketName = socketElement.getChild("socketName").getTextTrim();
166            Element systemNameElement = socketElement.getChild("systemName");
167            String systemName = null;
168            if (systemNameElement != null) {
169                systemName = systemNameElement.getTextTrim();
170            }
171            actionSystemNames.add(new AbstractMap.SimpleEntry<>(socketName, systemName));
172        }
173        
174        String sys = getSystemName(shared);
175        String uname = getUserName(shared);
176        Sequence h = new Sequence(sys, uname, expressionSystemNames, actionSystemNames);
177        
178        loadCommon(h, shared);
179        
180        h.getChild(0).setName(startSocketName);
181        h.setStartExpressionSocketSystemName(startSocketSystemName);
182        
183        h.getChild(1).setName(stopSocketName);
184        h.setStopExpressionSocketSystemName(stopSocketSystemName);
185        
186        h.getChild(2).setName(resetSocketName);
187        h.setResetExpressionSocketSystemName(resetSocketSystemName);
188        
189        Element startImmediately = shared.getChild("startImmediately");
190        if (startImmediately != null) {
191            h.setStartImmediately("yes".equals(startImmediately.getTextTrim()));
192        } else {
193            h.setStartImmediately(false);
194        }
195        
196        Element runContinuously = shared.getChild("runContinuously");
197        if (runContinuously != null) {
198            h.setRunContinuously("yes".equals(runContinuously.getTextTrim()));
199        } else {
200            h.setRunContinuously(false);
201        }
202        
203        InstanceManager.getDefault(DigitalActionManager.class).registerAction(h);
204        return true;
205    }
206    
207//    private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(SequenceXml.class);
208}