001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.io;
003
004import static org.openstreetmap.josm.tools.I18n.tr;
005
006import java.io.IOException;
007import java.io.InputStream;
008import java.text.MessageFormat;
009
010import org.openstreetmap.josm.data.osm.DataSet;
011import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
012import org.openstreetmap.josm.data.osm.history.HistoryDataSet;
013import org.openstreetmap.josm.gui.progress.ProgressMonitor;
014import org.openstreetmap.josm.tools.CheckParameterUtil;
015import org.xml.sax.SAXException;
016
017/**
018 * Reads the history of an {@link org.openstreetmap.josm.data.osm.OsmPrimitive} from the OSM API server.
019 *
020 */
021public class OsmServerHistoryReader extends OsmServerReader {
022
023    private final OsmPrimitiveType primitiveType;
024    private final long id;
025
026    /**
027     * constructor
028     *
029     * @param type the type of the primitive whose history is to be fetched from the server.
030     *   Must not be null.
031     * @param id the id of the primitive
032     *
033     *  @throws IllegalArgumentException if type is null
034     */
035    public OsmServerHistoryReader(OsmPrimitiveType type, long id) {
036        CheckParameterUtil.ensureParameterNotNull(type, "type");
037        if (id < 0)
038            throw new IllegalArgumentException(MessageFormat.format("Parameter ''{0}'' >= 0 expected. Got ''{1}''.", "id", id));
039        this.primitiveType = type;
040        this.id = id;
041    }
042
043    /**
044     * don't use - not implemented!
045     *
046     */
047    @Override
048    public DataSet parseOsm(ProgressMonitor progressMonitor) throws OsmTransferException {
049        return null;
050    }
051
052    /**
053     * Fetches the history from the OSM API and parses it
054     * @param progressMonitor progress monitor
055     *
056     * @return the data set with the parsed history data
057     * @throws OsmTransferException if an exception occurs
058     */
059    public HistoryDataSet parseHistory(ProgressMonitor progressMonitor) throws OsmTransferException {
060        progressMonitor.beginTask("");
061        try {
062            progressMonitor.indeterminateSubTask(tr("Contacting OSM Server..."));
063            final String urlStr = primitiveType.getAPIName() + '/' + id + "/history";
064
065            try (InputStream in = getInputStream(urlStr, progressMonitor.createSubTaskMonitor(1, true))) {
066                if (in == null)
067                    return null;
068                progressMonitor.indeterminateSubTask(tr("Downloading history..."));
069                OsmHistoryReader reader = new OsmHistoryReader(in);
070                return reader.parse(progressMonitor.createSubTaskMonitor(1, true));
071            }
072        } catch (OsmTransferException e) {
073            throw e;
074        } catch (IOException | SAXException e) {
075            if (cancel)
076                return null;
077            throw new OsmTransferException(e);
078        } finally {
079            progressMonitor.finishTask();
080            activeConnection = null;
081        }
082    }
083}