001package jmri.jmrit.logixng.actions.configurexml;
002
003import jmri.InstanceManager;
004import jmri.jmrit.logixng.DigitalActionManager;
005import jmri.jmrit.logixng.actions.For;
006
007import org.jdom2.Element;
008
009import jmri.jmrit.logixng.MaleSocket;
010
011/**
012 * Handle XML configuration for For objects.
013 *
014 * @author Bob Jacobsen Copyright: Copyright (c) 2004, 2008, 2010
015 * @author Daniel Bergqvist Copyright (C) 2020
016 */
017public class ForXml extends jmri.managers.configurexml.AbstractNamedBeanManagerConfigXML {
018
019    /**
020     * Default implementation for storing the contents of a For
021     *
022     * @param o Object to store, of type For
023     * @return Element containing the complete info
024     */
025    @Override
026    public Element store(Object o) {
027        For p = (For) o;
028
029        Element element = new Element("For");
030        element.setAttribute("class", this.getClass().getName());
031        element.addContent(new Element("systemName").addContent(p.getSystemName()));
032        
033        storeCommon(p, element);
034
035        Element e2 = new Element("InitSocket");
036        e2.addContent(new Element("socketName").addContent(p.getChild(0).getName()));
037        MaleSocket socket = p.getInitActionSocket().getConnectedSocket();
038        String socketSystemName;
039        if (socket != null) {
040            socketSystemName = socket.getSystemName();
041        } else {
042            socketSystemName = p.getInitActionSocketSystemName();
043        }
044        if (socketSystemName != null) {
045            e2.addContent(new Element("systemName").addContent(socketSystemName));
046        }
047        element.addContent(e2);
048
049        e2 = new Element("WhileSocket");
050        e2.addContent(new Element("socketName").addContent(p.getChild(1).getName()));
051        socket = p.getWhileExpressionSocket().getConnectedSocket();
052        if (socket != null) {
053            socketSystemName = socket.getSystemName();
054        } else {
055            socketSystemName = p.getWhileExpressionSocketSystemName();
056        }
057        if (socketSystemName != null) {
058            e2.addContent(new Element("systemName").addContent(socketSystemName));
059        }
060        element.addContent(e2);
061
062        e2 = new Element("AfterEachSocket");
063        e2.addContent(new Element("socketName").addContent(p.getChild(2).getName()));
064        socket = p.getAfterEachActionSocket().getConnectedSocket();
065        if (socket != null) {
066            socketSystemName = socket.getSystemName();
067        } else {
068            socketSystemName = p.getAfterEachExpressionSocketSystemName();
069        }
070        if (socketSystemName != null) {
071            e2.addContent(new Element("systemName").addContent(socketSystemName));
072        }
073        element.addContent(e2);
074
075        e2 = new Element("DoSocket");
076        e2.addContent(new Element("socketName").addContent(p.getChild(3).getName()));
077        socket = p.getDoActionSocket().getConnectedSocket();
078        if (socket != null) {
079            socketSystemName = socket.getSystemName();
080        } else {
081            socketSystemName = p.getDoExpressionSocketSystemName();
082        }
083        if (socketSystemName != null) {
084            e2.addContent(new Element("systemName").addContent(socketSystemName));
085        }
086        element.addContent(e2);
087
088        return element;
089    }
090    
091    @Override
092    public boolean load(Element shared, Element perNode) {
093        
094        String sys = getSystemName(shared);
095        String uname = getUserName(shared);
096        For h = new For(sys, uname);
097
098        loadCommon(h, shared);
099        
100        Element socketName = shared.getChild("InitSocket").getChild("socketName");
101        h.getInitActionSocket().setName(socketName.getTextTrim());
102        Element socketSystemName = shared.getChild("InitSocket").getChild("systemName");
103        if (socketSystemName != null) {
104            h.setInitActionSocketSystemName(socketSystemName.getTextTrim());
105        }
106        
107        socketName = shared.getChild("WhileSocket").getChild("socketName");
108        h.getWhileExpressionSocket().setName(socketName.getTextTrim());
109        socketSystemName = shared.getChild("WhileSocket").getChild("systemName");
110        if (socketSystemName != null) {
111            h.setWhileExpressionSocketSystemName(socketSystemName.getTextTrim());
112        }
113        
114        socketName = shared.getChild("AfterEachSocket").getChild("socketName");
115        h.getAfterEachActionSocket().setName(socketName.getTextTrim());
116        socketSystemName = shared.getChild("AfterEachSocket").getChild("systemName");
117        if (socketSystemName != null) {
118            h.setAfterEachActionSocketSystemName(socketSystemName.getTextTrim());
119        }
120        
121        socketName = shared.getChild("DoSocket").getChild("socketName");
122        h.getDoActionSocket().setName(socketName.getTextTrim());
123        socketSystemName = shared.getChild("DoSocket").getChild("systemName");
124        if (socketSystemName != null) {
125            h.setDoActionSocketSystemName(socketSystemName.getTextTrim());
126        }
127        
128        InstanceManager.getDefault(DigitalActionManager.class).registerAction(h);
129        return true;
130    }
131    
132//    private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(ForXml.class);
133}