aboutsummaryrefslogtreecommitdiffstats
path: root/main/src/cgeo/geocaching/PersonalNote.java
blob: 129b765662e40779fd6cb3a3da818c15b00f4bf2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package cgeo.geocaching;

import org.apache.commons.lang3.StringUtils;


public class PersonalNote {
    private static final String SEPARATOR = "\n--\n";
    private String cgeoNote;
    private String providerNote;
    private boolean isOffline;

    private PersonalNote() {
        // Empty default constructor
    }

    public PersonalNote(final Geocache cache) {
        this.isOffline = cache.isOffline();
        final String personalNote = cache.getPersonalNote();
        if (StringUtils.isEmpty(personalNote)) {
            return;
        }
        final String[] notes = StringUtils.splitByWholeSeparator(personalNote, SEPARATOR);
        if (notes.length > 1) {
            this.cgeoNote = notes[0];
            this.providerNote = notes[1];
        } else {
            this.providerNote = notes[0];
        }
    }

    public final PersonalNote mergeWith(final PersonalNote other) {
        if (StringUtils.isEmpty(cgeoNote) && StringUtils.isEmpty(other.cgeoNote)) {
            return mergeOnlyProviderNotes(other);
        }
        final PersonalNote result = new PersonalNote();
        if (cgeoNote != null && other.cgeoNote != null) {
            if (other.isOffline) {
                result.cgeoNote = other.cgeoNote;
            } else {
                result.cgeoNote = cgeoNote;
            }
        }
        if (other.cgeoNote != null) {
            result.cgeoNote = other.cgeoNote;
        } else {
            result.cgeoNote = cgeoNote;
        }
        if (providerNote != null && other.providerNote != null) {
            if (isOffline) {
                result.providerNote = providerNote;
            } else {
                result.providerNote = other.providerNote;
            }
        }
        if (providerNote != null) {
            result.providerNote = providerNote;
        } else {
            result.providerNote = other.providerNote;
        }
        return result;
    }

    /**
     * Merge different provider notes from c:geo and provider.
     *
     * @param other
     *            The note to merge
     * @return PersonalNote The merged note
     */
    private PersonalNote mergeOnlyProviderNotes(final PersonalNote other) {
        final PersonalNote result = new PersonalNote();
        if (StringUtils.isNotEmpty(other.providerNote) && StringUtils.isNotEmpty(providerNote)) {
            // Don't overwrite a stored personal note if provider note is different.
            // Prevents the local personal note from being overwritten by a truncated note from GC.com.
            if (StringUtils.startsWith(other.providerNote, providerNote)) {
                result.providerNote = other.providerNote;
                return result;
            }
            if (other.isOffline) {
                result.cgeoNote = other.providerNote;
                result.providerNote = providerNote;
            } else {
                result.cgeoNote = providerNote;
                result.providerNote = other.providerNote;
            }
        }
        return result;
    }

    @Override
    public final String toString() {
        final StringBuilder builder = new StringBuilder();
        if (cgeoNote != null) {
            builder.append(cgeoNote).append(SEPARATOR);
        }
        builder.append(providerNote);
        return builder.toString();
    }

    public final String getCgeoNote() {
        return cgeoNote;
    }

    public final String getProviderNote() {
        return providerNote;
    }

}