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