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.Publish;
010import jmri.jmrix.mqtt.logixng.Publish.Retain;
011import jmri.jmrit.logixng.util.configurexml.LogixNG_SelectStringXml;
012
013import org.jdom2.Element;
014
015/**
016 * Handle XML configuration for Publish objects.
017 *
018 * @author Bob Jacobsen Copyright: Copyright (c) 2004, 2008, 2010
019 * @author Daniel Bergqvist Copyright (C) 2022
020 */
021public class PublishXml extends jmri.managers.configurexml.AbstractNamedBeanManagerConfigXML {
022
023    public PublishXml() {
024    }
025
026    /**
027     * Default implementation for storing the contents of a publish action.
028     *
029     * @param o Object to store, of type Publish
030     * @return Element containing the complete info
031     */
032    @Override
033    public Element store(Object o) {
034        Publish p = (Publish) o;
035
036        var selectTopicXml = new LogixNG_SelectStringXml();
037        var selectDataXml = new LogixNG_SelectStringXml();
038
039        Element element = new Element("MQTTPublish");
040        element.setAttribute("class", this.getClass().getName());
041        element.addContent(new Element("systemName").addContent(p.getSystemName()));
042
043        storeCommon(p, element);
044
045        if (p.getMemo() != null) {
046            element.addContent(new Element("systemConnection")
047                    .addContent(p.getMemo().getSystemPrefix()));
048        }
049
050        element.addContent(selectTopicXml.store(p.getSelectTopic(), "topic"));
051        element.addContent(selectDataXml.store(p.getSelectMessage(), "message"));
052
053        element.addContent(new Element("retain").addContent(p.getRetain().name()));
054
055        return element;
056    }
057
058    @Override
059    public boolean load(Element shared, Element perNode) throws JmriConfigureXmlException {
060        String sys = getSystemName(shared);
061        String uname = getUserName(shared);
062
063        MqttSystemConnectionMemo memo = null;
064
065        Element systemConnection = shared.getChild("systemConnection");
066        if (systemConnection != null) {
067            String systemConnectionName = systemConnection.getTextTrim();
068            List<MqttSystemConnectionMemo> systemConnections =
069                    jmri.InstanceManager.getList(MqttSystemConnectionMemo.class);
070
071            for (MqttSystemConnectionMemo m : systemConnections) {
072                if (m.getSystemPrefix().equals(systemConnectionName)) {
073                    memo = m;
074                    break;
075                }
076            }
077        }
078
079        Publish h = new Publish(sys, uname, memo);
080
081        var selectTopicXml = new LogixNG_SelectStringXml();
082        var selectDataXml = new LogixNG_SelectStringXml();
083
084        loadCommon(h, shared);
085
086        selectTopicXml.load(shared.getChild("topic"), h.getSelectTopic());
087        selectDataXml.load(shared.getChild("message"), h.getSelectMessage());
088
089        Element retainElem = shared.getChild("retain");
090        if (retainElem != null) {
091            h.setRetain(Retain.valueOf(retainElem.getTextTrim()));
092        }
093
094        InstanceManager.getDefault(DigitalActionManager.class).registerAction(h);
095        return true;
096    }
097
098//    private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(PublishXml.class);
099}