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;
009
010import jmri.jmrit.logixng.StringActionBean;
011import jmri.jmrit.logixng.StringActionManager;
012import jmri.jmrit.logixng.MaleSocket;
013import jmri.jmrit.logixng.actions.StringMany;
014
015import org.jdom2.Element;
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 StringManyXml extends jmri.managers.configurexml.AbstractNamedBeanManagerConfigXML {
024
025    public StringManyXml() {
026//        jmri.managers.configurexml.DefaultConditionalManagerXml a;
027    }
028/*
029    @SuppressWarnings("unchecked")  // Reflection does not support generics
030    private List<ActionMany.ActionEntry> getActionEntry(Many actionMany)
031            throws IllegalAccessException, IllegalArgumentException, NoSuchFieldException {
032        
033        Field f = actionMany.getClass().getDeclaredField("actionEntries");
034        f.setAccessible(true);
035        return (List<ActionMany.ActionEntry>) f.get(actionMany);
036    }
037*/
038    /**
039     * Default implementation for storing the contents of a Many
040     *
041     * @param o Object to store, of type Many
042     * @return Element containing the complete info
043     */
044    @Override
045    public Element store(Object o) {
046        StringMany p = (StringMany) o;
047
048        Element element = new Element("StringMany");
049        element.setAttribute("class", this.getClass().getName());
050        element.addContent(new Element("systemName").addContent(p.getSystemName()));
051        
052        storeCommon(p, element);
053        
054        Element e = new Element("Actions");
055        for (int i=0; i < p.getChildCount(); i++) {
056            Element e2 = new Element("Socket");
057            e2.addContent(new Element("socketName").addContent(p.getChild(i).getName()));
058            MaleSocket socket = p.getChild(i).getConnectedSocket();
059            String socketSystemName;
060            if (socket != null) {
061                socketSystemName = socket.getSystemName();
062            } else {
063                socketSystemName = p.getActionSystemName(i);
064            }
065            if (socketSystemName != null) {
066                e2.addContent(new Element("systemName").addContent(socketSystemName));
067            }
068            e.addContent(e2);
069        }
070        element.addContent(e);
071
072        return element;
073    }
074    
075    @Override
076    public boolean load(Element shared, Element perNode) {
077        
078        List<Map.Entry<String, String>> actionSystemNames = new ArrayList<>();
079        
080        Element actionElement = shared.getChild("Actions");
081        for (Element socketElement : actionElement.getChildren()) {
082            String socketName = socketElement.getChild("socketName").getTextTrim();
083            Element systemNameElement = socketElement.getChild("systemName");
084            String systemName = null;
085            if (systemNameElement != null) {
086                systemName = systemNameElement.getTextTrim();
087            }
088            actionSystemNames.add(new AbstractMap.SimpleEntry<>(socketName, systemName));
089        }
090        
091        // put it together
092        String sys = getSystemName(shared);
093        String uname = getUserName(shared);
094        StringActionBean h = new StringMany(sys, uname, actionSystemNames);
095        
096        loadCommon(h, shared);
097        
098        InstanceManager.getDefault(StringActionManager.class).registerAction(h);
099        
100//        log.warn("Register action: " + h.getSystemName() + ", " + h.getLongDescription());
101        return true;
102    }
103    
104//    private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(ManyXml.class);
105
106}