From c75f89dba2712cba5bc8cec02479cac43197e65c Mon Sep 17 00:00:00 2001 From: rsudev Date: Tue, 26 Nov 2013 23:57:26 +0100 Subject: Fix #3441, 'Visited' not saved for own waypoints To disambiguate own waypoints in merge, generate a unique prefix on addition to the cache --- main/src/cgeo/geocaching/Geocache.java | 33 +++++++++++++++++++++- main/src/cgeo/geocaching/export/GpxSerializer.java | 2 +- 2 files changed, 33 insertions(+), 2 deletions(-) (limited to 'main') diff --git a/main/src/cgeo/geocaching/Geocache.java b/main/src/cgeo/geocaching/Geocache.java index 731f214..c589e9b 100644 --- a/main/src/cgeo/geocaching/Geocache.java +++ b/main/src/cgeo/geocaching/Geocache.java @@ -53,9 +53,11 @@ import java.util.Collections; import java.util.Date; import java.util.EnumSet; import java.util.HashMap; +import java.util.HashSet; import java.util.List; import java.util.Locale; import java.util.Map; +import java.util.Set; import java.util.regex.Pattern; /** @@ -63,6 +65,7 @@ import java.util.regex.Pattern; */ public class Geocache implements ICache, IWaypoint { + private static final int OWN_WP_PREFIX_OFFSET = 17; private long updated = 0; private long detailedUpdate = 0; private long visitedDate = 0; @@ -1209,6 +1212,7 @@ public class Geocache implements ICache, IWaypoint { waypoint.setGeocode(geocode); if (waypoint.getId() < 0) { // this is a new waypoint + assignUniquePrefix(waypoint); waypoints.add(waypoint); if (waypoint.isFinalWithCoords()) { finalDefined = true; @@ -1216,7 +1220,13 @@ public class Geocache implements ICache, IWaypoint { } else { // this is a waypoint being edited final int index = getWaypointIndex(waypoint); if (index >= 0) { - waypoints.remove(index); + Waypoint oldWaypoint = waypoints.remove(index); + waypoint.setPrefix(oldWaypoint.getPrefix()); + //migration + if (StringUtils.isBlank(waypoint.getPrefix()) + || StringUtils.equalsIgnoreCase(waypoint.getPrefix(), Waypoint.PREFIX_OWN)) { + assignUniquePrefix(waypoint); + } } waypoints.add(waypoint); // when waypoint was edited, finalDefined may have changed @@ -1225,6 +1235,27 @@ public class Geocache implements ICache, IWaypoint { return saveToDatabase && DataStore.saveWaypoint(waypoint.getId(), geocode, waypoint); } + /* + * Assigns a unique two-digit (compatibility with gc.com) + * prefix within the scope of this cache. + */ + private void assignUniquePrefix(Waypoint waypoint) { + // gather existing prefixes + Set assignedPrefixes = new HashSet(); + for (Waypoint wp : waypoints) { + assignedPrefixes.add(wp.getPrefix()); + } + + for (int i = OWN_WP_PREFIX_OFFSET; i < 100; i++) { + String prefixCandidate = StringUtils.leftPad(String.valueOf(i), 2, '0'); + if (!assignedPrefixes.contains(prefixCandidate)) { + waypoint.setPrefix(prefixCandidate); + break; + } + } + + } + public boolean hasWaypoints() { return !waypoints.isEmpty(); } diff --git a/main/src/cgeo/geocaching/export/GpxSerializer.java b/main/src/cgeo/geocaching/export/GpxSerializer.java index 7d6067e..ecc687f 100644 --- a/main/src/cgeo/geocaching/export/GpxSerializer.java +++ b/main/src/cgeo/geocaching/export/GpxSerializer.java @@ -179,7 +179,7 @@ public final class GpxSerializer { } // Prefixes must be unique. There use numeric strings as prefixes in OWN waypoints where they are missing for (final Waypoint wp : ownWaypoints) { - if (StringUtils.isBlank(wp.getPrefix()) || StringUtils.equalsIgnoreCase("OWN", wp.getPrefix())) { + if (StringUtils.isBlank(wp.getPrefix()) || StringUtils.equalsIgnoreCase(Waypoint.PREFIX_OWN, wp.getPrefix())) { maxPrefix++; wp.setPrefix(StringUtils.leftPad(String.valueOf(maxPrefix), 2, '0')); } -- cgit v1.1