001package jmri.jmrit.logixng.expressions.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.DigitalExpressionManager;
010import jmri.jmrit.logixng.MaleSocket;
011import jmri.jmrit.logixng.expressions.And;
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 AndXml extends jmri.managers.configurexml.AbstractNamedBeanManagerConfigXML {
022
023    public AndXml() {
024    }
025
026    /**
027     * Default implementation for storing the contents of a ActionMany
028     *
029     * @param o Object to store, of type ActionMany
030     * @return Element containing the complete info
031     */
032    @Override
033    public Element store(Object o) {
034        And p = (And) o;
035
036        Element element = new Element("And");
037        element.setAttribute("class", this.getClass().getName());
038        element.addContent(new Element("systemName").addContent(p.getSystemName()));
039
040        storeCommon(p, element);
041
042        element.setAttribute("type", p.getType().name());
043
044        Element e = new Element("Expressions");
045        for (int i=0; i < p.getChildCount(); i++) {
046            Element e2 = new Element("Socket");
047            e2.addContent(new Element("socketName").addContent(p.getChild(i).getName()));
048            MaleSocket socket = p.getChild(i).getConnectedSocket();
049            String socketSystemName;
050            if (socket != null) {
051                socketSystemName = socket.getSystemName();
052            } else {
053                socketSystemName = p.getExpressionSystemName(i);
054            }
055            if (socketSystemName != null) {
056                e2.addContent(new Element("systemName").addContent(socketSystemName));
057            }
058            e.addContent(e2);
059        }
060        element.addContent(e);
061
062        return element;
063    }
064
065    @Override
066    public boolean load(Element shared, Element perNode) {
067        List<Map.Entry<String, String>> expressionSystemNames = new ArrayList<>();
068
069        Element actionElement = shared.getChild("Expressions");
070        for (Element socketElement : actionElement.getChildren()) {
071            String socketName = socketElement.getChild("socketName").getTextTrim();
072            Element systemNameElement = socketElement.getChild("systemName");
073            String systemName = null;
074            if (systemNameElement != null) {
075                systemName = systemNameElement.getTextTrim();
076            }
077            expressionSystemNames.add(new AbstractMap.SimpleEntry<>(socketName, systemName));
078        }
079
080        // put it together
081        String sys = getSystemName(shared);
082        String uname = getUserName(shared);
083        And h = new And(sys, uname, expressionSystemNames);
084
085        loadCommon(h, shared);
086
087        if (shared.getAttribute("type") != null) {
088            String typeStr = shared.getAttribute("type").getValue();
089            h.setType(And.Type.valueOf(typeStr));
090        }
091
092        InstanceManager.getDefault(DigitalExpressionManager.class).registerExpression(h);
093        return true;
094    }
095
096//    private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(AndXml.class);
097}