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.InputStream;
007import java.text.MessageFormat;
008
009import org.openstreetmap.josm.data.osm.DataSet;
010import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
011import org.openstreetmap.josm.data.osm.history.HistoryDataSet;
012import org.openstreetmap.josm.gui.progress.ProgressMonitor;
013import org.openstreetmap.josm.tools.CheckParameterUtil;
014
015/**
016 * Reads the history of an {@link org.openstreetmap.josm.data.osm.OsmPrimitive} from the OSM API server.
017 *
018 */
019public class OsmServerHistoryReader extends OsmServerReader {
020
021    private final OsmPrimitiveType primitiveType;
022    private final long id;
023
024    /**
025     * constructor
026     *
027     * @param type the type of the primitive whose history is to be fetched from the server.
028     *   Must not be null.
029     * @param id the id of the primitive
030     *
031     *  @throws IllegalArgumentException if type is null
032     */
033    public OsmServerHistoryReader(OsmPrimitiveType type, long id) {
034        CheckParameterUtil.ensureParameterNotNull(type, "type");
035        if (id < 0)
036            throw new IllegalArgumentException(MessageFormat.format("Parameter ''{0}'' >= 0 expected. Got ''{1}''.", "id", id));
037        this.primitiveType = type;
038        this.id = id;
039    }
040
041    /**
042     * don't use - not implemented!
043     *
044     */
045    @Override
046    public DataSet parseOsm(ProgressMonitor progressMonitor) throws OsmTransferException {
047        return null;
048    }
049
050    /**
051     * Fetches the history from the OSM API and parses it
052     * @param progressMonitor progress monitor
053     *
054     * @return the data set with the parsed history data
055     * @throws OsmTransferException if an exception occurs
056     */
057    public HistoryDataSet parseHistory(ProgressMonitor progressMonitor) throws OsmTransferException {
058        progressMonitor.beginTask("");
059        try {
060            progressMonitor.indeterminateSubTask(tr("Contacting OSM Server..."));
061            StringBuilder sb = new StringBuilder();
062            sb.append(primitiveType.getAPIName())
063            .append('/').append(id).append("/history");
064
065            try (InputStream in = getInputStream(sb.toString(), 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 (Exception e) {
075            if (cancel)
076                return null;
077            throw new OsmTransferException(e);
078        } finally {
079            progressMonitor.finishTask();
080            activeConnection = null;
081        }
082    }
083}