• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/**
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16import ValuesDelta from './ValuesDelta';
17import DAOperation from './DAOperation';
18import { RawContacts } from '../contract/RawContacts';
19import { Data } from '../contract/Data';
20import RawContact from '../entity/RawContact';
21
22const TAG = "RawContactDelta";
23
24/**
25 * Contact details editing model
26 */
27export default class RawContactDelta {
28  // Stores fields in the rawContact table.
29  values: ValuesDelta;
30  readonly dataValues: Map<number, ValuesDelta[]> = new Map();
31  constructor() {
32    this.values = new ValuesDelta();
33  }
34
35  static fromRawContact(rawContact: RawContact) {
36    if (rawContact == undefined) {
37      return;
38    }
39    let rawContactDelta = new RawContactDelta();
40    rawContactDelta.values = ValuesDelta.fromValues(rawContact.values);
41    for (let dataItem of rawContact.dataItems) {
42      rawContactDelta.addDataValue(ValuesDelta.fromValues(dataItem.values));
43    }
44    return rawContactDelta;
45  }
46
47  addDataValue(dataValue: ValuesDelta) {
48    if (dataValue == undefined) {
49      return;
50    }
51    let mimeTypeId = dataValue.getValue(Data.TYPE_ID);
52    if (mimeTypeId == undefined ) {
53      return;
54    }
55    let valuesDeltas = this.getMimeEntries(mimeTypeId);
56    if (valuesDeltas == undefined) {
57      valuesDeltas = [dataValue];
58      this.dataValues.set(mimeTypeId, valuesDeltas);
59    } else {
60      valuesDeltas.push(dataValue);
61    }
62  }
63
64  private getMimeEntries(typeId: number) {
65    let mimeEntries = this.dataValues.get(typeId);
66    return mimeEntries;
67  }
68
69  isContactInsert () {
70    return this.values.isInsert();
71  }
72
73  isContactDelete () {
74    return this.values.isDelete();
75  }
76
77  buildDiff(diff: DAOperation[]) {
78    let isContactInsert = this.isContactInsert();
79    let isContactDelete = this.isContactDelete();
80    let beforeId = this.values.getValue(RawContacts.ID);
81    let firstIndex = diff.length;
82    let opt = this.values.buildDiff(RawContacts.CONTENT_URI);
83    diff.push(opt);
84    if (isContactDelete) {
85      return;
86    }
87    for (let mimeEntries of this.dataValues.values()) {
88      for (let data of mimeEntries) {
89        opt = data.buildDiff(Data.CONTENT_URI);
90        if (data.isInsert()) {
91          if (isContactInsert) {
92            opt.withValueBackReferences(Data.RAW_CONTACT_ID, firstIndex);
93          } else {
94            opt.withValue(Data.RAW_CONTACT_ID, beforeId);
95          }
96        }
97        diff.push(opt);
98      }
99    }
100  }
101}