001package jmri.configurexml;
002
003import java.util.List;
004import jmri.*;
005import jmri.configurexml.turnoutoperations.TurnoutOperationXml;
006import org.jdom2.Element;
007import org.slf4j.Logger;
008import org.slf4j.LoggerFactory;
009
010/**
011 * @author John Harper
012 *
013 */
014public class TurnoutOperationManagerXml extends jmri.configurexml.AbstractXmlAdapter {
015
016    public TurnoutOperationManagerXml() {
017    }
018
019    public void setStoreElementClass(Element elem) {
020        elem.setAttribute("class", getClass().getName());
021    }
022
023    @Override
024    public boolean load(Element sharedOperations, Element perNodeOperations) {
025        boolean result = true;
026        TurnoutOperationManager manager = InstanceManager.getDefault(TurnoutOperationManager.class);
027        if (sharedOperations.getAttribute("automate") != null) {
028            try {
029                manager.setDoOperations(sharedOperations.getAttribute("automate").getValue().equals("true"));
030            } catch (NumberFormatException ex) {
031                result = false;
032            }
033        }
034        List<Element> operationsList = sharedOperations.getChildren("operation");
035        log.debug("Found {} Operations", operationsList.size());
036        for (Element oper : operationsList) {
037            TurnoutOperationXml.loadOperation(oper);
038        }
039        return result;
040    }
041
042    @Override
043    public Element store(Object o) {
044        Element elem = new Element("operations");
045        if (o instanceof TurnoutOperationManager) {
046            TurnoutOperationManager manager = (TurnoutOperationManager) o;
047            elem.setAttribute("automate", String.valueOf(manager.getDoOperations()));
048            TurnoutOperation[] operations = manager.getTurnoutOperations();
049            for (TurnoutOperation op : operations) {
050                if (!op.isNonce()) {  // nonces are stored with their respective turnouts
051                    TurnoutOperationXml adapter = TurnoutOperationXml.getAdapter(op);
052                    if (adapter != null) {
053                        Element opElem = adapter.store(op);
054                        if (opElem != null) {
055                            elem.addContent(opElem);
056                        }
057                    }
058                }
059            }
060        }
061        return elem;
062    }
063
064    private final static Logger log = LoggerFactory.getLogger(TurnoutOperationManagerXml.class);
065
066}