001package jmri.implementation;
002
003import java.text.DateFormat;
004import java.text.ParseException;
005import java.util.Calendar;
006import java.util.Date;
007import jmri.IdTagManager;
008import jmri.InstanceManager;
009import jmri.JmriException;
010import jmri.Reporter;
011import org.jdom2.Element;
012import org.slf4j.Logger;
013import org.slf4j.LoggerFactory;
014
015/**
016 * Concrete implementation of the {@link jmri.IdTag} interface for the Internal
017 * system.
018 * <hr>
019 * This file is part of JMRI.
020 * <p>
021 * JMRI is free software; you can redistribute it and/or modify it under the
022 * terms of version 2 of the GNU General Public License as published by the Free
023 * Software Foundation. See the "COPYING" file for a copy of this license.
024 * <p>
025 * JMRI is distributed in the hope that it will be useful, but WITHOUT ANY
026 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
027 * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
028 *
029 * @author Matthew Harris Copyright (C) 2011
030 * @since 2.11.4
031 */
032public class DefaultIdTag extends AbstractIdTag {
033
034    private int currentState = UNKNOWN;
035
036    public DefaultIdTag(String systemName) {
037        super(systemName);
038        setWhereLastSeen(null);
039    }
040
041    public DefaultIdTag(String systemName, String userName) {
042        super(systemName, userName);
043        setWhereLastSeen(null);
044    }
045
046    @Override
047    public final void setWhereLastSeen(Reporter r) {
048        Reporter oldWhere = this.whereLastSeen;
049        Date oldWhen = this.whenLastSeen;
050        this.whereLastSeen = r;
051        if (r != null) {
052            this.whenLastSeen = InstanceManager.getDefault(IdTagManager.class).isFastClockUsed()
053                    ? InstanceManager.getDefault(jmri.ClockControl.class).getTime()
054                    : Calendar.getInstance().getTime();
055        } else {
056            this.whenLastSeen = null;
057        }
058        setCurrentState(r != null ? SEEN : UNSEEN);
059        firePropertyChange("whereLastSeen", oldWhere, this.whereLastSeen); // NOI18N
060        firePropertyChange("whenLastSeen", oldWhen, this.whenLastSeen);    // NOI18N
061    }
062
063    private void setCurrentState(int state) {
064        try {
065            setState(state);
066        } catch (JmriException ex) {
067            log.warn("Problem setting state of IdTag {}", getSystemName());
068        }
069    }
070
071    @Override
072    public void setState(int s) throws JmriException {
073        this.currentState = s;
074    }
075
076    @Override
077    public int getState() {
078        return this.currentState;
079    }
080
081    @Override
082    public Element store(boolean storeState) {
083        Element e = new Element("idtag"); // NOI18N
084        // e.setAttribute("systemName", this.mSystemName); // not needed from 2.11.1
085        e.addContent(new Element("systemName").addContent(this.mSystemName)); // NOI18N
086        String uName = this.getUserName();
087        if (uName != null && !uName.isEmpty()) {
088            // e.setAttribute("userName", this.getUserName()); // not needed from 2.11.1
089            e.addContent(new Element("userName").addContent(uName)); // NOI18N
090        }
091        String comment = this.getComment();
092        if ((comment != null) && (!comment.isEmpty())) {
093            e.addContent(new Element("comment").addContent(comment)); // NOI18N
094        }
095        Reporter whereLast = this.getWhereLastSeen();
096        if (whereLast != null && storeState) {
097            e.addContent(new Element("whereLastSeen").addContent(whereLast.getSystemName())); // NOI18N
098        }
099        if (this.getWhenLastSeen() != null && storeState) {
100            e.addContent(new Element("whenLastSeen").addContent(DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM).format(this.getWhenLastSeen()))); // NOI18N
101        }
102        return e;
103    }
104
105    @Override
106    public void load(Element e) {
107        if (e.getName().equals("idtag")) { // NOI18N
108            if (log.isDebugEnabled()) {
109                log.debug("Load IdTag element for {}", this.getSystemName());
110            }
111            if (e.getChild("userName") != null) // NOI18N
112            {
113                this.setUserName(e.getChild("userName").getText()); // NOI18N
114            }
115            if (e.getChild("comment") != null) // NOI18N
116            {
117                this.setComment(e.getChild("comment").getText()); // NOI18N
118            }
119            if (e.getChild("whereLastSeen") != null) { // NOI18N
120                try {
121                    Reporter r = InstanceManager.getDefault(jmri.ReporterManager.class)
122                                    .provideReporter(e.getChild("whereLastSeen").getText()); // NOI18N
123                    this.setWhereLastSeen(r);
124                    this.whenLastSeen = null;
125                } catch (IllegalArgumentException ex) {
126                    log.warn("Failed to provide Turnout \"{}\" in load", e.getChild("whereLastSeen").getText());
127                }
128            }
129            if (e.getChild("whenLastSeen") != null) { // NOI18N
130                log.debug("When Last Seen: {}", e.getChild("whenLastSeen").getText());
131                try {
132                    this.whenLastSeen = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM).parse(e.getChild("whenLastSeen").getText()); // NOI18N
133                } catch (ParseException ex) {
134                    log.warn("Error parsing when last seen: {}", ex);
135                }
136            }
137        } else {
138            log.error("Not an IdTag element: {}", e.getName());
139        }
140    }
141
142    private static final Logger log = LoggerFactory.getLogger(DefaultIdTag.class);
143
144}