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