001    // License: GPL. For details, see LICENSE file.
002    package org.openstreetmap.josm.io;
003    
004    import static org.openstreetmap.josm.tools.I18n.tr;
005    
006    import java.io.InputStream;
007    import java.text.MessageFormat;
008    
009    import org.openstreetmap.josm.data.osm.DataSet;
010    import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
011    import org.openstreetmap.josm.data.osm.history.HistoryDataSet;
012    import org.openstreetmap.josm.gui.progress.ProgressMonitor;
013    import org.openstreetmap.josm.tools.CheckParameterUtil;
014    
015    /**
016     * Reads the history of an {@link OsmPrimitive} from the OSM API server.
017     *
018     */
019    public class OsmServerHistoryReader extends OsmServerReader {
020    
021        private OsmPrimitiveType primitiveType;
022        private 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         *  @exception IllegalArgumentException thrown, if type is null
032         */
033        public OsmServerHistoryReader(OsmPrimitiveType type, long id) throws IllegalArgumentException {
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         *
053         * @return the data set with the parsed history data
054         * @throws OsmTransferException thrown, if an exception occurs
055         */
056        public HistoryDataSet parseHistory(ProgressMonitor progressMonitor) throws OsmTransferException {
057            InputStream in = null;
058            progressMonitor.beginTask("");
059            try {
060                progressMonitor.indeterminateSubTask(tr("Contacting OSM Server..."));
061                StringBuffer sb = new StringBuffer();
062                sb.append(primitiveType.getAPIName())
063                .append("/").append(id).append("/history");
064    
065                in = getInputStream(sb.toString(), progressMonitor.createSubTaskMonitor(1, true));
066                if (in == null)
067                    return null;
068                progressMonitor.indeterminateSubTask(tr("Downloading history..."));
069                final OsmHistoryReader reader = new OsmHistoryReader(in);
070                HistoryDataSet data = reader.parse(progressMonitor.createSubTaskMonitor(1, true));
071                return data;
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                if (in != null) {
081                    try {
082                        in.close();
083                    } catch(Exception e) {}
084                    activeConnection = null;
085                }
086            }
087        }
088    }