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.DigitalBooleanActionManager;
010import jmri.jmrit.logixng.MaleSocket;
011import jmri.jmrit.logixng.actions.DigitalBooleanMany;
012
013import org.jdom2.Element;
014
015import jmri.jmrit.logixng.DigitalBooleanActionBean;
016
017/**
018 * Handle XML configuration for ActionLightXml objects.
019 *
020 * @author Bob Jacobsen Copyright: Copyright (c) 2004, 2008, 2010
021 * @author Daniel Bergqvist Copyright (C) 2019
022 */
023public class DigitalBooleanManyXml extends jmri.managers.configurexml.AbstractNamedBeanManagerConfigXML {
024
025    /**
026     * Default implementation for storing the contents of a Many
027     *
028     * @param o Object to store, of type Many
029     * @return Element containing the complete info
030     */
031    @Override
032    public Element store(Object o) {
033        DigitalBooleanMany p = (DigitalBooleanMany) o;
034
035        Element element = new Element("DigitalBooleanMany");
036        element.setAttribute("class", this.getClass().getName());
037        element.addContent(new Element("systemName").addContent(p.getSystemName()));
038        
039        storeCommon(p, element);
040
041        Element e = new Element("Actions");
042        for (int i=0; i < p.getChildCount(); i++) {
043            Element e2 = new Element("Socket");
044            e2.addContent(new Element("socketName").addContent(p.getChild(i).getName()));
045            MaleSocket socket = p.getChild(i).getConnectedSocket();
046            String socketSystemName;
047            if (socket != null) {
048                socketSystemName = socket.getSystemName();
049            } else {
050                socketSystemName = p.getActionSystemName(i);
051            }
052            if (socketSystemName != null) {
053                e2.addContent(new Element("systemName").addContent(socketSystemName));
054            }
055            e.addContent(e2);
056        }
057        element.addContent(e);
058
059        return element;
060    }
061    
062    @Override
063    public boolean load(Element shared, Element perNode) {
064        
065        List<Map.Entry<String, String>> actionSystemNames = new ArrayList<>();
066        
067        Element actionElement = shared.getChild("Actions");
068        for (Element socketElement : actionElement.getChildren()) {
069            String socketName = socketElement.getChild("socketName").getTextTrim();
070            Element systemNameElement = socketElement.getChild("systemName");
071            String systemName = null;
072            if (systemNameElement != null) {
073                systemName = systemNameElement.getTextTrim();
074            }
075            actionSystemNames.add(new AbstractMap.SimpleEntry<>(socketName, systemName));
076        }
077        
078        // put it together
079        String sys = getSystemName(shared);
080        String uname = getUserName(shared);
081        DigitalBooleanActionBean h = new DigitalBooleanMany(sys, uname, actionSystemNames);
082
083        loadCommon(h, shared);
084
085        InstanceManager.getDefault(DigitalBooleanActionManager.class).registerAction(h);
086        
087//        log.warn("Register action: " + h.getSystemName() + ", " + h.getLongDescription());
088        return true;
089    }
090    
091    
092//    private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(ManyXml.class);
093
094}