001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.gui.history;
003
004import static org.openstreetmap.josm.tools.I18n.marktr;
005import static org.openstreetmap.josm.tools.I18n.tr;
006
007import java.awt.Component;
008import java.io.IOException;
009import java.net.HttpURLConnection;
010import java.util.ArrayList;
011import java.util.Collection;
012import java.util.Iterator;
013import java.util.LinkedHashSet;
014import java.util.List;
015import java.util.Objects;
016import java.util.Set;
017
018import org.openstreetmap.josm.data.osm.Changeset;
019import org.openstreetmap.josm.data.osm.ChangesetCache;
020import org.openstreetmap.josm.data.osm.OsmPrimitive;
021import org.openstreetmap.josm.data.osm.PrimitiveId;
022import org.openstreetmap.josm.data.osm.history.History;
023import org.openstreetmap.josm.data.osm.history.HistoryDataSet;
024import org.openstreetmap.josm.data.osm.history.HistoryOsmPrimitive;
025import org.openstreetmap.josm.gui.ExceptionDialogUtil;
026import org.openstreetmap.josm.gui.PleaseWaitRunnable;
027import org.openstreetmap.josm.gui.progress.ProgressMonitor;
028import org.openstreetmap.josm.io.ChangesetQuery;
029import org.openstreetmap.josm.io.OsmApiException;
030import org.openstreetmap.josm.io.OsmServerChangesetReader;
031import org.openstreetmap.josm.io.OsmServerHistoryReader;
032import org.openstreetmap.josm.io.OsmTransferException;
033import org.openstreetmap.josm.tools.CheckParameterUtil;
034import org.xml.sax.SAXException;
035
036/**
037 * Loads the object history of a collection of objects from the server.
038 *
039 * It provides a fluent API for configuration.
040 *
041 * Sample usage:
042 *
043 * <pre>
044 *   HistoryLoadTask task = new HistoryLoadTask()
045 *      .add(node)
046 *      .add(way)
047 *      .add(relation)
048 *      .add(aHistoryItem);
049 *
050 *   MainApplication.worker.execute(task);
051 * </pre>
052 */
053public class HistoryLoadTask extends PleaseWaitRunnable {
054
055    private boolean canceled;
056    private Exception lastException;
057    private final Set<PrimitiveId> toLoad = new LinkedHashSet<>();
058    private HistoryDataSet loadedData;
059    private OsmServerHistoryReader reader;
060    private boolean getChangesetData = true;
061    private boolean collectMissing;
062    private final Set<PrimitiveId> missingPrimitives = new LinkedHashSet<>();
063
064    /**
065     * Constructs a new {@code HistoryLoadTask}.
066     */
067    public HistoryLoadTask() {
068        super(tr("Load history"), true);
069    }
070
071    /**
072     * Constructs a new {@code HistoryLoadTask}.
073     *
074     * @param parent the component to be used as reference to find the
075     * parent for {@link org.openstreetmap.josm.gui.PleaseWaitDialog}.
076     * Must not be <code>null</code>.
077     * @throws NullPointerException if parent is <code>null</code>
078     */
079    public HistoryLoadTask(Component parent) {
080        super(Objects.requireNonNull(parent, "parent"), tr("Load history"), true);
081    }
082
083    /**
084     * Adds an object whose history is to be loaded.
085     *
086     * @param pid  the primitive id. Must not be null. Id &gt; 0 required.
087     * @return this task
088     */
089    public HistoryLoadTask add(PrimitiveId pid) {
090        CheckParameterUtil.ensureThat(pid.getUniqueId() > 0, "id > 0");
091        toLoad.add(pid);
092        return this;
093    }
094
095    /**
096     * Adds an object to be loaded, the object is specified by a history item.
097     *
098     * @param primitive the history item
099     * @return this task
100     * @throws NullPointerException if primitive is null
101     */
102    public HistoryLoadTask add(HistoryOsmPrimitive primitive) {
103        return add(primitive.getPrimitiveId());
104    }
105
106    /**
107     * Adds an object to be loaded, the object is specified by an already loaded object history.
108     *
109     * @param history the history. Must not be null.
110     * @return this task
111     * @throws NullPointerException if history is null
112     */
113    public HistoryLoadTask add(History history) {
114        return add(history.getPrimitiveId());
115    }
116
117    /**
118     * Adds an object to be loaded, the object is specified by an OSM primitive.
119     *
120     * @param primitive the OSM primitive. Must not be null. primitive.getOsmId() &gt; 0 required.
121     * @return this task
122     * @throws NullPointerException if the primitive is null
123     * @throws IllegalArgumentException if primitive.getOsmId() &lt;= 0
124     */
125    public HistoryLoadTask add(OsmPrimitive primitive) {
126        CheckParameterUtil.ensureThat(primitive.getOsmId() > 0, "id > 0");
127        return add(primitive.getOsmPrimitiveId());
128    }
129
130    /**
131     * Adds a collection of objects to loaded, specified by a collection of OSM primitives.
132     *
133     * @param primitives the OSM primitives. Must not be <code>null</code>.
134     * <code>primitive.getId() &gt; 0</code> required.
135     * @return this task
136     * @throws NullPointerException if primitives is null
137     * @throws IllegalArgumentException if one of the ids in the collection &lt;= 0
138     * @since 16123
139     */
140    public HistoryLoadTask addPrimitiveIds(Collection<? extends PrimitiveId> primitives) {
141        primitives.forEach(this::add);
142        return this;
143    }
144
145    /**
146     * Adds a collection of objects to loaded, specified by a collection of OSM primitives.
147     *
148     * @param primitives the OSM primitives. Must not be <code>null</code>.
149     * <code>primitive.getId() &gt; 0</code> required.
150     * @return this task
151     * @throws NullPointerException if primitives is null
152     * @throws IllegalArgumentException if one of the ids in the collection &lt;= 0
153     * @since 16123
154     */
155    public HistoryLoadTask addOsmPrimitives(Collection<? extends OsmPrimitive> primitives) {
156        primitives.forEach(this::add);
157        return this;
158    }
159
160    @Override
161    protected void cancel() {
162        if (reader != null) {
163            reader.cancel();
164        }
165        canceled = true;
166    }
167
168    @Override
169    protected void finish() {
170        if (isCanceled())
171            return;
172        if (lastException != null) {
173            ExceptionDialogUtil.explainException(lastException);
174            return;
175        }
176        HistoryDataSet.getInstance().mergeInto(loadedData);
177    }
178
179    @Override
180    protected void realRun() throws SAXException, IOException, OsmTransferException {
181        loadedData = new HistoryDataSet();
182        int ticks = toLoad.size();
183        if (getChangesetData)
184            ticks *= 2;
185        try {
186            progressMonitor.setTicksCount(ticks);
187            for (PrimitiveId pid: toLoad) {
188                if (canceled) {
189                    break;
190                }
191                loadHistory(pid);
192            }
193        } catch (OsmTransferException e) {
194            lastException = e;
195        }
196    }
197
198    private void loadHistory(PrimitiveId pid) throws OsmTransferException {
199        String msg = getLoadingMessage(pid);
200        progressMonitor.indeterminateSubTask(tr(msg, Long.toString(pid.getUniqueId())));
201        reader = null;
202        HistoryDataSet ds = null;
203        try {
204            reader = new OsmServerHistoryReader(pid.getType(), pid.getUniqueId());
205            if (getChangesetData) {
206                ds = loadHistory(reader, progressMonitor);
207            } else {
208                ds = reader.parseHistory(progressMonitor.createSubTaskMonitor(1, false));
209            }
210        } catch (OsmApiException e) {
211            if (canceled)
212                return;
213            if (e.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND && collectMissing) {
214                missingPrimitives.add(pid);
215            } else {
216                throw e;
217            }
218        } catch (OsmTransferException e) {
219            if (canceled)
220                return;
221            throw e;
222        }
223        if (ds != null) {
224            loadedData.mergeInto(ds);
225        }
226    }
227
228    protected static HistoryDataSet loadHistory(OsmServerHistoryReader reader, ProgressMonitor progressMonitor) throws OsmTransferException {
229        HistoryDataSet ds = reader.parseHistory(progressMonitor.createSubTaskMonitor(1, false));
230        if (ds != null) {
231            // load corresponding changesets (mostly for changeset comment)
232            OsmServerChangesetReader changesetReader = new OsmServerChangesetReader();
233            List<Long> changesetIds = new ArrayList<>(ds.getChangesetIds());
234            Iterator<Long> iter = changesetIds.iterator();
235            while (iter.hasNext()) {
236                long id = iter.next();
237                Changeset cs = ChangesetCache.getInstance().get((int) id);
238                if (cs != null && !cs.isOpen()) {
239                    ds.putChangeset(cs);
240                    iter.remove();
241                }
242            }
243
244            // query changesets 100 by 100 (OSM API limit)
245            int n = ChangesetQuery.MAX_CHANGESETS_NUMBER;
246            for (int i = 0; i < changesetIds.size(); i += n) {
247                List<Changeset> downloadedCS = new ArrayList<>(changesetIds.size());
248                for (Changeset c : changesetReader.queryChangesets(
249                        new ChangesetQuery().forChangesetIds(changesetIds.subList(i, Math.min(i + n, changesetIds.size()))),
250                        progressMonitor.createSubTaskMonitor(1, false))) {
251                    ds.putChangeset(c);
252                    downloadedCS.add(c);
253                }
254                ChangesetCache.getInstance().update(downloadedCS);
255            }
256        }
257        return ds;
258    }
259
260    protected static String getLoadingMessage(PrimitiveId pid) {
261        switch (pid.getType()) {
262        case NODE:
263            return marktr("Loading history for node {0}");
264        case WAY:
265            return marktr("Loading history for way {0}");
266        case RELATION:
267            return marktr("Loading history for relation {0}");
268        default:
269            return "";
270        }
271    }
272
273    /**
274     * Determines if this task has ben canceled.
275     * @return {@code true} if this task has ben canceled
276     */
277    public boolean isCanceled() {
278        return canceled;
279    }
280
281    /**
282     * Returns the last exception that occurred during loading, if any.
283     * @return the last exception that occurred during loading, or {@code null}
284     */
285    public Exception getLastException() {
286        return lastException;
287    }
288
289    /**
290     * Determine if changeset information is needed. By default it is retrieved.
291     * @param b false means don't retrieve changeset data.
292     * @since 14763
293     */
294    public void setChangesetDataNeeded(boolean b) {
295        getChangesetData = b;
296    }
297
298    /**
299     * Determine if missing primitives should be collected. By default they are not collected
300     * and the first missing object terminates the task.
301     * @param b true means collect missing data and continue.
302     * @since 16205
303     */
304    public void setCollectMissing(boolean b) {
305        collectMissing = b;
306    }
307
308    /**
309     * replies the set of ids of all primitives for which a fetch request to the
310     * server was submitted but which are not available from the server (the server
311     * replied a return code of 404)
312     * @return the set of ids of missing primitives
313     * @since 16205
314     */
315    public Set<PrimitiveId> getMissingPrimitives() {
316        return missingPrimitives;
317    }
318
319}