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