summaryrefslogtreecommitdiffstats
path: root/mobile/android/services/src/main/java/org/mozilla/gecko/sync/repositories/domain/FormHistoryRecord.java
blob: e7ca70cb415f31170f0fbf0f41c27e40ef158c39 (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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

package org.mozilla.gecko.sync.repositories.domain;

import org.mozilla.gecko.background.common.log.Logger;
import org.mozilla.gecko.sync.ExtendedJSONObject;
import org.mozilla.gecko.sync.Utils;
import org.mozilla.gecko.sync.repositories.android.RepoUtils;

/**
 * A FormHistoryRecord represents a saved form element.
 *
 * I map a <code>fieldName</code> string to a <code>value</code> string.
 *
 * @see "<a href='http://dxr.mozilla.org/services-central/source/services-central/services/sync/modules/engines/forms.js'>http://dxr.mozilla.org/services-central/source/services-central/services/sync/modules/engines/forms.js</a>."
 */
public class FormHistoryRecord extends Record {
  private static final String LOG_TAG = "FormHistoryRecord";

  public static final String  COLLECTION_NAME = "forms";
  private static final String PAYLOAD_NAME    = "name";
  private static final String PAYLOAD_VALUE   = "value";
  public static final long FORMS_TTL = 3 * 365 * 24 * 60 * 60;   // Three years in seconds.

  /**
   * The name of the saved form field.
   */
  public String fieldName;

  /**
   * The value of the saved form field.
   */
  public String fieldValue;

  public FormHistoryRecord(String guid, String collection, long lastModified, boolean deleted) {
    super(guid, collection, lastModified, deleted);
    this.ttl = FORMS_TTL;
  }

  public FormHistoryRecord(String guid, String collection, long lastModified) {
    this(guid, collection, lastModified, false);
  }

  public FormHistoryRecord(String guid, String collection) {
    this(guid, collection, 0, false);
  }

  public FormHistoryRecord(String guid) {
    this(guid, COLLECTION_NAME, 0, false);
  }

  public FormHistoryRecord() {
    this(Utils.generateGuid(), COLLECTION_NAME, 0, false);
  }

  @Override
  public Record copyWithIDs(String guid, long androidID) {
    FormHistoryRecord out = new FormHistoryRecord(guid, this.collection, this.lastModified, this.deleted);
    out.androidID = androidID;
    out.sortIndex = this.sortIndex;

    // Copy FormHistoryRecord fields.
    out.fieldName = this.fieldName;
    out.fieldValue = this.fieldValue;

    return out;
  }

  @Override
  public void populatePayload(ExtendedJSONObject payload) {
    putPayload(payload, PAYLOAD_NAME,  this.fieldName);
    putPayload(payload, PAYLOAD_VALUE, this.fieldValue);
  }

  @Override
  public void initFromPayload(ExtendedJSONObject payload) {
    this.fieldName  = payload.getString(PAYLOAD_NAME);
    this.fieldValue = payload.getString(PAYLOAD_VALUE);
  }

  /**
   * We consider two form history records to be congruent if they represent the
   * same form element regardless of times used.
   */
  @Override
  public boolean congruentWith(Object o) {
    if (!(o instanceof FormHistoryRecord)) {
      return false;
    }
    FormHistoryRecord other = (FormHistoryRecord) o;
    if (!super.congruentWith(other)) {
      return false;
    }
    return RepoUtils.stringsEqual(this.fieldName, other.fieldName) &&
           RepoUtils.stringsEqual(this.fieldValue, other.fieldValue);
  }

  @Override
  public boolean equalPayloads(Object o) {
    if (!(o instanceof FormHistoryRecord)) {
      Logger.debug(LOG_TAG, "Not a FormHistoryRecord: " + o.getClass());
      return false;
    }
    FormHistoryRecord other = (FormHistoryRecord) o;
    if (!super.equalPayloads(other)) {
      Logger.debug(LOG_TAG, "super.equalPayloads returned false.");
      return false;
    }

    if (this.deleted) {
      // FormHistoryRecords are equal if they are both deleted (which
      // they are, since super.equalPayloads is true) and have the
      // same GUID.
      if (other.deleted) {
        return RepoUtils.stringsEqual(this.guid, other.guid);
      }
      return false;
    }

    return RepoUtils.stringsEqual(this.fieldName,  other.fieldName) &&
           RepoUtils.stringsEqual(this.fieldValue, other.fieldValue);
  }

  public FormHistoryRecord log(String logTag) {
    try {
      Logger.debug(logTag, "Returning form history record " + guid + " (" + androidID + ")");
      Logger.debug(logTag, "> Last modified: " + lastModified);
      if (Logger.LOG_PERSONAL_INFORMATION) {
        Logger.pii(logTag, "> Field name:    " + fieldName);
        Logger.pii(logTag, "> Field value:   " + fieldValue);
      }
    } catch (Exception e) {
      Logger.debug(logTag, "Exception logging form history record " + this, e);
    }
    return this;
  }
}