001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.command;
003
004import static org.openstreetmap.josm.tools.I18n.marktr;
005import static org.openstreetmap.josm.tools.I18n.tr;
006import static org.openstreetmap.josm.tools.I18n.trn;
007
008import java.util.ArrayList;
009import java.util.Arrays;
010import java.util.Collection;
011import java.util.Collections;
012import java.util.HashMap;
013import java.util.HashSet;
014import java.util.LinkedList;
015import java.util.List;
016import java.util.Map;
017import java.util.Map.Entry;
018import java.util.Objects;
019import java.util.Set;
020import java.util.stream.Collectors;
021
022import javax.swing.Icon;
023
024import org.openstreetmap.josm.data.osm.DataSet;
025import org.openstreetmap.josm.data.osm.DefaultNameFormatter;
026import org.openstreetmap.josm.data.osm.IPrimitive;
027import org.openstreetmap.josm.data.osm.Node;
028import org.openstreetmap.josm.data.osm.OsmPrimitive;
029import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
030import org.openstreetmap.josm.data.osm.PrimitiveData;
031import org.openstreetmap.josm.data.osm.Relation;
032import org.openstreetmap.josm.data.osm.RelationMember;
033import org.openstreetmap.josm.data.osm.RelationToChildReference;
034import org.openstreetmap.josm.data.osm.Way;
035import org.openstreetmap.josm.data.osm.WaySegment;
036import org.openstreetmap.josm.tools.CheckParameterUtil;
037import org.openstreetmap.josm.tools.ImageProvider;
038import org.openstreetmap.josm.tools.Pair;
039import org.openstreetmap.josm.tools.Utils;
040
041/**
042 * A command to delete a number of primitives from the dataset.
043 * To be used correctly, this class requires an initial call to {@link #setDeletionCallback(DeletionCallback)} to
044 * allow interactive confirmation actions.
045 * @since 23
046 */
047public class DeleteCommand extends Command {
048    private static final class DeleteChildCommand implements PseudoCommand {
049        private final OsmPrimitive osm;
050
051        private DeleteChildCommand(OsmPrimitive osm) {
052            this.osm = osm;
053        }
054
055        @Override
056        public String getDescriptionText() {
057            return tr("Deleted ''{0}''", osm.getDisplayName(DefaultNameFormatter.getInstance()));
058        }
059
060        @Override
061        public Icon getDescriptionIcon() {
062            return ImageProvider.get(osm.getDisplayType());
063        }
064
065        @Override
066        public Collection<? extends OsmPrimitive> getParticipatingPrimitives() {
067            return Collections.singleton(osm);
068        }
069
070        @Override
071        public String toString() {
072            return "DeleteChildCommand [osm=" + osm + ']';
073        }
074    }
075
076    /**
077     * Called when a deletion operation must be checked and confirmed by user.
078     * @since 12749
079     */
080    public interface DeletionCallback {
081        /**
082         * Check whether user is about to delete data outside of the download area.
083         * Request confirmation if he is.
084         * @param primitives the primitives to operate on
085         * @param ignore {@code null} or a primitive to be ignored
086         * @return true, if operating on outlying primitives is OK; false, otherwise
087         */
088        boolean checkAndConfirmOutlyingDelete(Collection<? extends OsmPrimitive> primitives, Collection<? extends OsmPrimitive> ignore);
089
090        /**
091         * Confirm before deleting a relation, as it is a common newbie error.
092         * @param relations relation to check for deletion
093         * @return {@code true} if user confirms the deletion
094         * @since 12760
095         */
096        boolean confirmRelationDeletion(Collection<Relation> relations);
097
098        /**
099         * Confirm before removing a collection of primitives from their parent relations.
100         * @param references the list of relation-to-child references
101         * @return {@code true} if user confirms the deletion
102         * @since 12763
103         */
104        boolean confirmDeletionFromRelation(Collection<RelationToChildReference> references);
105
106        /**
107         * Confirm before removing a collection of primitives from their parent relations, with the probability of
108         * deleting the parents as well.
109         * @param references the list of relation-to-child references
110         * @param parentsToDelete the list of parents to delete (the boolean part will be {@code true} if the user wants
111         *                        to delete the relation).
112         * @return {@code true} if the user confirms the deletion
113         * @since 18395
114         */
115        default boolean confirmDeletionFromRelation(Collection<RelationToChildReference> references,
116                Collection<Pair<Relation, Boolean>> parentsToDelete) {
117            // This is a default method. Ensure that all the booleans are false.
118            parentsToDelete.forEach(pair -> pair.b = false);
119            return confirmDeletionFromRelation(references);
120        }
121    }
122
123    private static volatile DeletionCallback callback;
124
125    /**
126     * Sets the global {@link DeletionCallback}.
127     * @param deletionCallback the new {@code DeletionCallback}. Must not be null
128     * @throws NullPointerException if {@code deletionCallback} is null
129     * @since 12749
130     */
131    public static void setDeletionCallback(DeletionCallback deletionCallback) {
132        callback = Objects.requireNonNull(deletionCallback);
133    }
134
135    /**
136     * The primitives that get deleted.
137     */
138    private final Collection<? extends OsmPrimitive> toDelete;
139    private final Map<OsmPrimitive, PrimitiveData> clonedPrimitives = new HashMap<>();
140
141    /**
142     * Constructor. Deletes a collection of primitives in the current edit layer.
143     *
144     * @param data the primitives to delete. Must neither be null nor empty, and belong to a data set
145     * @throws IllegalArgumentException if data is null or empty
146     */
147    public DeleteCommand(Collection<? extends OsmPrimitive> data) {
148        this(data.iterator().next().getDataSet(), data);
149    }
150
151    /**
152     * Constructor. Deletes a single primitive in the current edit layer.
153     *
154     * @param data  the primitive to delete. Must not be null.
155     * @throws IllegalArgumentException if data is null
156     */
157    public DeleteCommand(OsmPrimitive data) {
158        this(Collections.singleton(data));
159    }
160
161    /**
162     * Constructor for a single data item. Use the collection constructor to delete multiple objects.
163     *
164     * @param dataset the data set context for deleting this primitive. Must not be null.
165     * @param data the primitive to delete. Must not be null.
166     * @throws IllegalArgumentException if data is null
167     * @throws IllegalArgumentException if layer is null
168     * @since 12718
169     */
170    public DeleteCommand(DataSet dataset, OsmPrimitive data) {
171        this(dataset, Collections.singleton(data));
172    }
173
174    /**
175     * Constructor for a collection of data to be deleted in the context of a specific data set
176     *
177     * @param dataset the dataset context for deleting these primitives. Must not be null.
178     * @param data the primitives to delete. Must neither be null nor empty.
179     * @throws IllegalArgumentException if dataset is null
180     * @throws IllegalArgumentException if data is null or empty
181     * @since 11240
182     */
183    public DeleteCommand(DataSet dataset, Collection<? extends OsmPrimitive> data) {
184        super(dataset);
185        CheckParameterUtil.ensureParameterNotNull(data, "data");
186        this.toDelete = data;
187        checkConsistency();
188    }
189
190    private void checkConsistency() {
191        if (toDelete.isEmpty()) {
192            throw new IllegalArgumentException(tr("At least one object to delete required, got empty collection"));
193        }
194        for (OsmPrimitive p : toDelete) {
195            if (p == null) {
196                throw new IllegalArgumentException("Primitive to delete must not be null");
197            } else if (p.getDataSet() == null) {
198                throw new IllegalArgumentException("Primitive to delete must be in a dataset");
199            }
200        }
201    }
202
203    @Override
204    public boolean executeCommand() {
205        ensurePrimitivesAreInDataset();
206
207        getAffectedDataSet().update(() -> {
208            // Make copy and remove all references (to prevent inconsistent dataset (delete referenced) while command is executed)
209            for (OsmPrimitive osm : toDelete) {
210                if (osm.isDeleted())
211                    throw new IllegalArgumentException(osm + " is already deleted");
212                clonedPrimitives.put(osm, osm.save());
213                IPrimitive.resetPrimitiveChildren(osm);
214            }
215
216            for (OsmPrimitive osm : toDelete) {
217                osm.setDeleted(true);
218            }
219        });
220        return true;
221    }
222
223    @Override
224    public void undoCommand() {
225        ensurePrimitivesAreInDataset();
226
227        getAffectedDataSet().update(() -> {
228            for (OsmPrimitive osm : toDelete) {
229                osm.setDeleted(false);
230            }
231
232            for (Entry<OsmPrimitive, PrimitiveData> entry : clonedPrimitives.entrySet()) {
233                entry.getKey().load(entry.getValue());
234            }
235        });
236    }
237
238    @Override
239    public void fillModifiedData(Collection<OsmPrimitive> modified, Collection<OsmPrimitive> deleted, Collection<OsmPrimitive> added) {
240        // Do nothing
241    }
242
243    private Set<OsmPrimitiveType> getTypesToDelete() {
244        return toDelete.stream().map(OsmPrimitiveType::from).collect(Collectors.toSet());
245    }
246
247    @Override
248    public String getDescriptionText() {
249        if (toDelete.size() == 1) {
250            OsmPrimitive primitive = toDelete.iterator().next();
251            String msg;
252            switch (OsmPrimitiveType.from(primitive)) {
253            case NODE: msg = marktr("Delete node {0}"); break;
254            case WAY: msg = marktr("Delete way {0}"); break;
255            case RELATION:msg = marktr("Delete relation {0}"); break;
256            default: throw new AssertionError();
257            }
258
259            return tr(msg, primitive.getDisplayName(DefaultNameFormatter.getInstance()));
260        } else {
261            Set<OsmPrimitiveType> typesToDelete = getTypesToDelete();
262            String msg;
263            if (typesToDelete.size() > 1) {
264                msg = trn("Delete {0} object", "Delete {0} objects", toDelete.size(), toDelete.size());
265            } else {
266                OsmPrimitiveType t = typesToDelete.iterator().next();
267                switch (t) {
268                case NODE: msg = trn("Delete {0} node", "Delete {0} nodes", toDelete.size(), toDelete.size()); break;
269                case WAY: msg = trn("Delete {0} way", "Delete {0} ways", toDelete.size(), toDelete.size()); break;
270                case RELATION: msg = trn("Delete {0} relation", "Delete {0} relations", toDelete.size(), toDelete.size()); break;
271                default: throw new AssertionError();
272                }
273            }
274            return msg;
275        }
276    }
277
278    @Override
279    public Icon getDescriptionIcon() {
280        if (toDelete.size() == 1)
281            return ImageProvider.get(toDelete.iterator().next().getDisplayType());
282        Set<OsmPrimitiveType> typesToDelete = getTypesToDelete();
283        if (typesToDelete.size() > 1)
284            return ImageProvider.get("data", "object");
285        else
286            return ImageProvider.get(typesToDelete.iterator().next());
287    }
288
289    @Override public Collection<PseudoCommand> getChildren() {
290        if (toDelete.size() == 1)
291            return null;
292        else {
293            return toDelete.stream().map(DeleteChildCommand::new).collect(Collectors.toList());
294        }
295    }
296
297    @Override public Collection<? extends OsmPrimitive> getParticipatingPrimitives() {
298        return toDelete;
299    }
300
301    /**
302     * Delete the primitives and everything they reference.
303     *
304     * If a node is deleted, the node and all ways and relations the node is part of are deleted as well.
305     * If a way is deleted, all relations the way is member of are also deleted.
306     * If a way is deleted, only the way and no nodes are deleted.
307     *
308     * @param selection The list of all object to be deleted.
309     * @param silent  Set to true if the user should not be bugged with additional dialogs
310     * @return command A command to perform the deletions, or null of there is nothing to delete.
311     * @throws IllegalArgumentException if layer is null
312     * @since 12718
313     */
314    public static Command deleteWithReferences(Collection<? extends OsmPrimitive> selection, boolean silent) {
315        if (Utils.isEmpty(selection)) return null;
316        Set<OsmPrimitive> parents = OsmPrimitive.getReferrer(selection);
317        parents.addAll(selection);
318
319        if (parents.isEmpty())
320            return null;
321        if (!silent && !callback.checkAndConfirmOutlyingDelete(parents, null))
322            return null;
323        return new DeleteCommand(parents);
324    }
325
326    /**
327     * Delete the primitives and everything they reference.
328     *
329     * If a node is deleted, the node and all ways and relations the node is part of are deleted as well.
330     * If a way is deleted, all relations the way is member of are also deleted.
331     * If a way is deleted, only the way and no nodes are deleted.
332     *
333     * @param selection The list of all object to be deleted.
334     * @return command A command to perform the deletions, or null of there is nothing to delete.
335     * @throws IllegalArgumentException if layer is null
336     * @since 12718
337     */
338    public static Command deleteWithReferences(Collection<? extends OsmPrimitive> selection) {
339        return deleteWithReferences(selection, false);
340    }
341
342    /**
343     * Try to delete all given primitives.
344     *
345     * If a node is used by a way, it's removed from that way. If a node or a way is used by a
346     * relation, inform the user and do not delete.
347     *
348     * If this would cause ways with less than 2 nodes to be created, delete these ways instead. If
349     * they are part of a relation, inform the user and do not delete.
350     *
351     * @param selection the objects to delete.
352     * @return command a command to perform the deletions, or null if there is nothing to delete.
353     * @since 12718
354     */
355    public static Command delete(Collection<? extends OsmPrimitive> selection) {
356        return delete(selection, true, false);
357    }
358
359    /**
360     * Replies the collection of nodes referred to by primitives in <code>primitivesToDelete</code> which
361     * can be deleted too. A node can be deleted if
362     * <ul>
363     *    <li>it is untagged (see {@link Node#isTagged()}</li>
364     *    <li>it is not referred to by other non-deleted primitives outside of  <code>primitivesToDelete</code></li>
365     * </ul>
366     * @param primitivesToDelete  the primitives to delete
367     * @return the collection of nodes referred to by primitives in <code>primitivesToDelete</code> which
368     * can be deleted too
369     */
370    protected static Collection<Node> computeNodesToDelete(Collection<OsmPrimitive> primitivesToDelete) {
371        Collection<Node> nodesToDelete = new HashSet<>();
372        for (Way way : Utils.filteredCollection(primitivesToDelete, Way.class)) {
373            for (Node n : way.getNodes()) {
374                if (n.isTagged()) {
375                    continue;
376                }
377                Collection<OsmPrimitive> referringPrimitives = n.getReferrers();
378                referringPrimitives.removeAll(primitivesToDelete);
379                if (referringPrimitives.stream().allMatch(OsmPrimitive::isDeleted)) {
380                    nodesToDelete.add(n);
381                }
382            }
383        }
384        return nodesToDelete;
385    }
386
387    /**
388     * Try to delete all given primitives.
389     *
390     * If a node is used by a way, it's removed from that way. If a node or a way is used by a
391     * relation, inform the user and do not delete.
392     *
393     * If this would cause ways with less than 2 nodes to be created, delete these ways instead. If
394     * they are part of a relation, inform the user and do not delete.
395     *
396     * @param selection the objects to delete.
397     * @param alsoDeleteNodesInWay <code>true</code> if nodes should be deleted as well
398     * @return command a command to perform the deletions, or null if there is nothing to delete.
399     * @since 12718
400     */
401    public static Command delete(Collection<? extends OsmPrimitive> selection, boolean alsoDeleteNodesInWay) {
402        return delete(selection, alsoDeleteNodesInWay, false /* not silent */);
403    }
404
405    /**
406     * Try to delete all given primitives.
407     *
408     * If a node is used by a way, it's removed from that way. If a node or a way is used by a
409     * relation, inform the user and do not delete.
410     *
411     * If this would cause ways with less than 2 nodes to be created, delete these ways instead. If
412     * they are part of a relation, inform the user and do not delete.
413     *
414     * @param selection the objects to delete.
415     * @param alsoDeleteNodesInWay <code>true</code> if nodes should be deleted as well
416     * @param silent set to true if the user should not be bugged with additional questions
417     * @return command a command to perform the deletions, or null if there is nothing to delete.
418     * @since 12718
419     */
420    public static Command delete(Collection<? extends OsmPrimitive> selection, boolean alsoDeleteNodesInWay, boolean silent) {
421        if (Utils.isEmpty(selection))
422            return null;
423
424        Set<OsmPrimitive> primitivesToDelete = new HashSet<>(selection);
425
426        Collection<Relation> relationsToDelete = Utils.filteredCollection(primitivesToDelete, Relation.class);
427        if (!relationsToDelete.isEmpty() && !silent && !callback.confirmRelationDeletion(relationsToDelete))
428            return null;
429
430        if (alsoDeleteNodesInWay) {
431            // delete untagged nodes only referenced by primitives in primitivesToDelete, too
432            Collection<Node> nodesToDelete = computeNodesToDelete(primitivesToDelete);
433            primitivesToDelete.addAll(nodesToDelete);
434        }
435
436        if (!silent && !callback.checkAndConfirmOutlyingDelete(primitivesToDelete, null))
437            return null;
438
439        Collection<Way> waysToBeChanged = primitivesToDelete.stream()
440                .flatMap(p -> p.referrers(Way.class))
441                .collect(Collectors.toSet());
442
443        Collection<Command> cmds = new LinkedList<>();
444        Set<Node> nodesToRemove = new HashSet<>(Utils.filteredCollection(primitivesToDelete, Node.class));
445        for (Way w : waysToBeChanged) {
446            if (primitivesToDelete.contains(w))
447                continue;
448            List<Node> remainingNodes = w.calculateRemoveNodes(nodesToRemove);
449            if (remainingNodes.size() < 2) {
450                primitivesToDelete.add(w);
451            } else {
452                cmds.add(new ChangeNodesCommand(w, remainingNodes));
453            }
454        }
455
456        // remove the objects from their parent relations
457        //
458        final Set<Relation> relationsToBeChanged = primitivesToDelete.stream()
459                .flatMap(p -> p.referrers(Relation.class))
460                .collect(Collectors.toSet());
461        final Set<Relation> additionalRelationsToDelete = new HashSet<>();
462        for (Relation cur : relationsToBeChanged) {
463            List<RelationMember> newMembers = cur.getMembers();
464            cur.getMembersFor(primitivesToDelete).forEach(newMembers::remove);
465            cmds.add(new ChangeMembersCommand(cur, newMembers));
466            // If we don't have any members, we probably ought to delete the relation as well.
467            if (newMembers.isEmpty()) {
468                additionalRelationsToDelete.add(cur);
469            }
470        }
471
472
473        // get a confirmation that the objects to delete can be removed from their parent relations
474        //
475        if (!silent) {
476            Set<RelationToChildReference> references = RelationToChildReference.getRelationToChildReferences(primitivesToDelete);
477            references.removeIf(ref -> ref.getParent().isDeleted());
478            final Collection<Pair<Relation, Boolean>> pairedRelations = additionalRelationsToDelete.stream()
479                    /*
480                     * Default to true, so that users have to make a choice to not delete.
481                     * Default implementation converts it to false, so this should be safe.
482                     */
483                    .map(relation -> new Pair<>(relation, true)).collect(Collectors.toSet());
484            if (!references.isEmpty() && !callback.confirmDeletionFromRelation(references, pairedRelations)) {
485                return null;
486            }
487            pairedRelations.stream().filter(pair -> Boolean.TRUE.equals(pair.b)).map(pair -> pair.a)
488                    .forEach(primitivesToDelete::add);
489        }
490
491        // build the delete command
492        //
493        if (!primitivesToDelete.isEmpty()) {
494            cmds.add(new DeleteCommand(primitivesToDelete));
495        }
496
497        return SequenceCommand.wrapIfNeeded(tr("Delete"), cmds);
498    }
499
500    /**
501     * Create a command that deletes a single way segment. The way may be split by this.
502     * @param ws The way segment that should be deleted
503     * @return A matching command to safely delete that segment.
504     * @since 12718
505     */
506    public static Command deleteWaySegment(WaySegment ws) {
507        if (ws.getWay().getNodesCount() < 3)
508            return delete(Collections.singleton(ws.getWay()), false);
509
510        if (ws.getWay().isClosed()) {
511            // If the way is circular (first and last nodes are the same), the way shouldn't be split
512
513            List<Node> n = new ArrayList<>();
514
515            n.addAll(ws.getWay().getNodes().subList(ws.getUpperIndex(), ws.getWay().getNodesCount() - 1));
516            n.addAll(ws.getWay().getNodes().subList(0, ws.getUpperIndex()));
517
518            return new ChangeNodesCommand(ws.getWay(), n);
519        }
520
521        List<Node> n1 = new ArrayList<>();
522        List<Node> n2 = new ArrayList<>();
523
524        n1.addAll(ws.getWay().getNodes().subList(0, ws.getUpperIndex()));
525        n2.addAll(ws.getWay().getNodes().subList(ws.getUpperIndex(), ws.getWay().getNodesCount()));
526
527        if (n1.size() < 2) {
528            return new ChangeNodesCommand(ws.getWay(), n2);
529        } else if (n2.size() < 2) {
530            return new ChangeNodesCommand(ws.getWay(), n1);
531        } else {
532            return SplitWayCommand.splitWay(ws.getWay(), Arrays.asList(n1, n2), Collections.<OsmPrimitive>emptyList());
533        }
534    }
535
536    @Override
537    public int hashCode() {
538        return Objects.hash(super.hashCode(), toDelete, clonedPrimitives);
539    }
540
541    @Override
542    public boolean equals(Object obj) {
543        if (this == obj) return true;
544        if (obj == null || getClass() != obj.getClass()) return false;
545        if (!super.equals(obj)) return false;
546        DeleteCommand that = (DeleteCommand) obj;
547        return Objects.equals(toDelete, that.toDelete) &&
548                Objects.equals(clonedPrimitives, that.clonedPrimitives);
549    }
550}