001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.gui.conflict.tags;
003
004import java.util.LinkedList;
005import java.util.List;
006
007import org.openstreetmap.josm.command.ChangePropertyCommand;
008import org.openstreetmap.josm.command.Command;
009import org.openstreetmap.josm.data.osm.AbstractPrimitive;
010import org.openstreetmap.josm.data.osm.OsmPrimitive;
011import org.openstreetmap.josm.data.osm.TagCollection;
012
013/**
014 * Combine primitives conflicts resolver.
015 * @since 11772
016 */
017public class CombinePrimitiveResolver {
018
019    private final TagConflictResolverModel modelTagConflictResolver;
020    private final RelationMemberConflictResolverModel modelRelConflictResolver;
021
022    /**
023     * Constructs a new {@code CombinePrimitiveResolver}.
024     * @param tagModel tag conflict resolver model
025     * @param relModel relation member conflict resolver model
026     */
027    public CombinePrimitiveResolver(TagConflictResolverModel tagModel, RelationMemberConflictResolverModel relModel) {
028        this.modelTagConflictResolver = tagModel;
029        this.modelRelConflictResolver = relModel;
030    }
031
032    /**
033     * Builds conflicts resolution commands for the given target primitive.
034     * @param targetPrimitive target primitive
035     * @return list of conflicts resolution commands
036     */
037    public List<Command> buildResolutionCommands(OsmPrimitive targetPrimitive) {
038        List<Command> cmds = new LinkedList<>();
039
040        TagCollection allResolutions = modelTagConflictResolver.getAllResolutions();
041        if (!allResolutions.isEmpty()) {
042            cmds.addAll(buildTagChangeCommand(targetPrimitive, allResolutions));
043        }
044        for (String p : AbstractPrimitive.getDiscardableKeys()) {
045            if (targetPrimitive.get(p) != null) {
046                cmds.add(new ChangePropertyCommand(targetPrimitive, p, null));
047            }
048        }
049
050        if (modelRelConflictResolver.getNumDecisions() > 0) {
051            cmds.addAll(modelRelConflictResolver.buildResolutionCommands(targetPrimitive));
052        }
053
054        return cmds;
055    }
056
057    /**
058     * Builds the list of tag change commands.
059     * @param primitive target primitive
060     * @param tc all resolutions
061     * @return the list of tag change commands
062     */
063    protected List<Command> buildTagChangeCommand(OsmPrimitive primitive, TagCollection tc) {
064        List<Command> cmds = new LinkedList<>();
065        for (String key : tc.getKeys()) {
066            if (tc.hasUniqueEmptyValue(key)) {
067                if (primitive.get(key) != null) {
068                    cmds.add(new ChangePropertyCommand(primitive, key, null));
069                }
070            } else {
071                String value = tc.getJoinedValues(key);
072                if (!value.equals(primitive.get(key))) {
073                    cmds.add(new ChangePropertyCommand(primitive, key, value));
074                }
075            }
076        }
077        return cmds;
078    }
079}