001package jmri.implementation;
002
003import java.util.Date;
004import java.util.List;
005import javax.annotation.Nonnull;
006
007import jmri.*;
008import jmri.managers.ProxyIdTagManager;
009
010/**
011 * Abstract implementation of {@link jmri.IdTag} containing code common to all
012 * concrete implementations. This implementation implements {@link jmri.Reportable}.
013 *
014 * @author  Matthew Harris Copyright (C) 2011
015 * @since 2.11.4
016 */
017public abstract class AbstractIdTag extends AbstractNamedBean implements IdTag, Reportable  {
018
019    protected Reporter whereLastSeen = null;
020
021    protected Date whenLastSeen = null;
022    protected String prefix = null;
023
024    public AbstractIdTag(String systemName) {
025        super(systemName);
026    }
027
028    public AbstractIdTag(String systemName, String userName) {
029        super(systemName, userName);
030    }
031
032    @Override
033    @Nonnull
034    public String getTagID() {
035        if(prefix == null) {
036            try {
037                prefix = findPrefix();
038            } catch ( NullPointerException | BadSystemNameException e) {
039                // if there isn't a ProxyIDTag Manager, assume the first D in the
040                //  system name is the type letter.
041                return mSystemName.substring(mSystemName.indexOf('D') + 1);
042
043            }
044        }
045        return mSystemName.substring(prefix.length()+1);
046    }
047
048    private String findPrefix() {
049        var mgr = InstanceManager.getDefault(IdTagManager.class);
050        if (mgr instanceof ProxyIdTagManager) {
051            List<Manager<IdTag>> managerList = ((ProxyIdTagManager)mgr).getManagerList();
052            for (Manager<IdTag> m : managerList) {
053                if (m.getBySystemName(mSystemName) != null) {
054                    return m.getSystemPrefix();
055                }
056            }
057        }
058        throw new BadSystemNameException();
059    }
060
061    @Override
062    public Reporter getWhereLastSeen() {
063        return this.whereLastSeen;
064    }
065
066    @Override
067    public Date getWhenLastSeen() {
068        if (this.whenLastSeen == null) {
069            return null;
070        } else {
071            return (Date) this.whenLastSeen.clone();  // Date is mutable, so return copy
072        }
073    }
074
075    /**
076     * The IDTag version of toReportString returns a string consisting
077     * of the user name (if defined) or Tag ID followed by the associated
078     * list of property values.
079     */
080    @Override
081    public String toReportString() {
082        String userName = getUserName();
083        StringBuilder sb = new StringBuilder();
084        if(userName == null || userName.isEmpty()){
085           sb.append(getTagID());
086        } else {
087          sb.append(userName);
088        }
089
090        // check to see if any properties have been added
091        // If we have properties, so append the values to the
092        // end of the report, seperated by spaces.
093        getPropertyKeys().forEach(s -> {
094            sb.append(" ");
095            sb.append(getProperty(s));
096        });
097        return sb.toString();
098    }
099
100    @Override
101    @Nonnull
102    public String getBeanType() {
103        return Bundle.getMessage("BeanNameReporter");
104    }
105
106}