001package jmri.jmrix.mqtt.logixng.configurexml;
002
003import java.util.List;
004
005import jmri.*;
006import jmri.configurexml.JmriConfigureXmlException;
007import jmri.jmrit.logixng.DigitalActionManager;
008import jmri.jmrix.mqtt.MqttSystemConnectionMemo;
009import jmri.jmrix.mqtt.logixng.Subscribe;
010
011import org.jdom2.Element;
012
013/**
014 * Handle XML configuration for Subscribe objects.
015 *
016 * @author Bob Jacobsen Copyright: Copyright (c) 2004, 2008, 2010
017 * @author Daniel Bergqvist Copyright (C) 2022
018 */
019public class SubscribeXml extends jmri.managers.configurexml.AbstractNamedBeanManagerConfigXML {
020
021    public SubscribeXml() {
022    }
023
024    /**
025     * Default implementation for storing the contents of a subscribe action.
026     *
027     * @param o Object to store, of type Publish
028     * @return Element containing the complete info
029     */
030    @Override
031    public Element store(Object o) {
032        Subscribe p = (Subscribe) o;
033
034        Element element = new Element("MQTTSubscribe");
035        element.setAttribute("class", this.getClass().getName());
036        element.addContent(new Element("systemName").addContent(p.getSystemName()));
037
038        storeCommon(p, element);
039
040        if (p.getMemo() != null) {
041            element.addContent(new Element("systemConnection")
042                    .addContent(p.getMemo().getSystemPrefix()));
043        }
044
045        if (p.getSubscribeToTopic() != null) {
046            element.addContent(new Element("subscribeToTopic")
047                    .addContent(p.getSubscribeToTopic()));
048        }
049
050        if (p.getLastTopicLocalVariable() != null) {
051            element.addContent(new Element("lastTopicLocalVariable")
052                    .addContent(p.getLastTopicLocalVariable()));
053        }
054        element.addContent(new Element("removeChannelFromLastTopic").addContent(p.getRemoveChannelFromLastTopic() ? "yes" : "no"));
055
056        if (p.getLastMessageLocalVariable() != null) {
057            element.addContent(new Element("lastMessageLocalVariable")
058                    .addContent(p.getLastMessageLocalVariable()));
059        }
060
061        return element;
062    }
063
064    @Override
065    public boolean load(Element shared, Element perNode) throws JmriConfigureXmlException {
066        String sys = getSystemName(shared);
067        String uname = getUserName(shared);
068
069        MqttSystemConnectionMemo memo = null;
070
071        Element systemConnection = shared.getChild("systemConnection");
072        if (systemConnection != null) {
073            String systemConnectionName = systemConnection.getTextTrim();
074            List<MqttSystemConnectionMemo> systemConnections =
075                    jmri.InstanceManager.getList(MqttSystemConnectionMemo.class);
076
077            for (MqttSystemConnectionMemo m : systemConnections) {
078                if (m.getSystemPrefix().equals(systemConnectionName)) {
079                    memo = m;
080                    break;
081                }
082            }
083        }
084
085        Subscribe h = new Subscribe(sys, uname, memo);
086
087        loadCommon(h, shared);
088
089        if (shared.getChild("subscribeToTopic") != null) {
090            h.setSubscribeToTopic(shared.getChild("subscribeToTopic").getTextTrim());
091        }
092        if (shared.getChild("lastTopicLocalVariable") != null) {
093            h.setLastTopicLocalVariable(shared.getChild("lastTopicLocalVariable").getTextTrim());
094        }
095        Element elem = shared.getChild("removeChannelFromLastTopic");  // NOI18N
096        h.setRemoveChannelFromLastTopic((elem != null) ? elem.getTextTrim().equals("yes") : false);  // NOI18N
097
098        if (shared.getChild("lastMessageLocalVariable") != null) {
099            h.setLastMessageLocalVariable(shared.getChild("lastMessageLocalVariable").getTextTrim());
100        }
101
102        InstanceManager.getDefault(DigitalActionManager.class).registerAction(h);
103        return true;
104    }
105
106//    private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(PublishXml.class);
107}