18779efd5Sopenharmony_ci/** 28779efd5Sopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd. 38779efd5Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 48779efd5Sopenharmony_ci * you may not use this file except in compliance with the License. 58779efd5Sopenharmony_ci * You may obtain a copy of the License at 68779efd5Sopenharmony_ci * 78779efd5Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 88779efd5Sopenharmony_ci * 98779efd5Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 108779efd5Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 118779efd5Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 128779efd5Sopenharmony_ci * See the License for the specific language governing permissions and 138779efd5Sopenharmony_ci * limitations under the License. 148779efd5Sopenharmony_ci */ 158779efd5Sopenharmony_ci 168779efd5Sopenharmony_ciimport ValuesDelta from './ValuesDelta'; 178779efd5Sopenharmony_ciimport DAOperation from './DAOperation'; 188779efd5Sopenharmony_ciimport { RawContacts } from '../contract/RawContacts'; 198779efd5Sopenharmony_ciimport { Data } from '../contract/Data'; 208779efd5Sopenharmony_ciimport RawContact from '../entity/RawContact'; 218779efd5Sopenharmony_ci 228779efd5Sopenharmony_ciconst TAG = 'RawContactDelta'; 238779efd5Sopenharmony_ci 248779efd5Sopenharmony_ci/** 258779efd5Sopenharmony_ci * Contact details editing model 268779efd5Sopenharmony_ci */ 278779efd5Sopenharmony_ciexport default class RawContactDelta { 288779efd5Sopenharmony_ci // Stores fields in the rawContact table. 298779efd5Sopenharmony_ci values: ValuesDelta; 308779efd5Sopenharmony_ci readonly dataValues: Map<number, ValuesDelta[]> = new Map(); 318779efd5Sopenharmony_ci constructor() { 328779efd5Sopenharmony_ci this.values = new ValuesDelta(); 338779efd5Sopenharmony_ci } 348779efd5Sopenharmony_ci 358779efd5Sopenharmony_ci static fromRawContact(rawContact: RawContact) { 368779efd5Sopenharmony_ci if (rawContact == undefined) { 378779efd5Sopenharmony_ci return; 388779efd5Sopenharmony_ci } 398779efd5Sopenharmony_ci let rawContactDelta = new RawContactDelta(); 408779efd5Sopenharmony_ci rawContactDelta.values = ValuesDelta.fromValues(rawContact.values); 418779efd5Sopenharmony_ci for (let dataItem of rawContact.dataItems) { 428779efd5Sopenharmony_ci rawContactDelta.addDataValue(ValuesDelta.fromValues(dataItem.values)); 438779efd5Sopenharmony_ci } 448779efd5Sopenharmony_ci return rawContactDelta; 458779efd5Sopenharmony_ci } 468779efd5Sopenharmony_ci 478779efd5Sopenharmony_ci addDataValue(dataValue: ValuesDelta) { 488779efd5Sopenharmony_ci if (dataValue == undefined) { 498779efd5Sopenharmony_ci return; 508779efd5Sopenharmony_ci } 518779efd5Sopenharmony_ci let mimeTypeId = dataValue.getValue(Data.TYPE_ID); 528779efd5Sopenharmony_ci if (mimeTypeId == undefined ) { 538779efd5Sopenharmony_ci return; 548779efd5Sopenharmony_ci } 558779efd5Sopenharmony_ci let valuesDeltas = this.getMimeEntries(mimeTypeId); 568779efd5Sopenharmony_ci if (valuesDeltas == undefined) { 578779efd5Sopenharmony_ci valuesDeltas = [dataValue]; 588779efd5Sopenharmony_ci this.dataValues.set(mimeTypeId, valuesDeltas); 598779efd5Sopenharmony_ci } else { 608779efd5Sopenharmony_ci valuesDeltas.push(dataValue); 618779efd5Sopenharmony_ci } 628779efd5Sopenharmony_ci } 638779efd5Sopenharmony_ci 648779efd5Sopenharmony_ci private getMimeEntries(typeId: number) { 658779efd5Sopenharmony_ci let mimeEntries = this.dataValues.get(typeId); 668779efd5Sopenharmony_ci return mimeEntries; 678779efd5Sopenharmony_ci } 688779efd5Sopenharmony_ci 698779efd5Sopenharmony_ci isContactInsert () { 708779efd5Sopenharmony_ci return this.values.isInsert(); 718779efd5Sopenharmony_ci } 728779efd5Sopenharmony_ci 738779efd5Sopenharmony_ci isContactDelete () { 748779efd5Sopenharmony_ci return this.values.isDelete(); 758779efd5Sopenharmony_ci } 768779efd5Sopenharmony_ci 778779efd5Sopenharmony_ci buildDiff(diff: DAOperation[]) { 788779efd5Sopenharmony_ci let isContactInsert = this.isContactInsert(); 798779efd5Sopenharmony_ci let isContactDelete = this.isContactDelete(); 808779efd5Sopenharmony_ci let beforeId = this.values.getValue(RawContacts.ID); 818779efd5Sopenharmony_ci let firstIndex = diff.length; 828779efd5Sopenharmony_ci let opt = this.values.buildDiff(RawContacts.CONTENT_URI); 838779efd5Sopenharmony_ci diff.push(opt); 848779efd5Sopenharmony_ci if (isContactDelete) { 858779efd5Sopenharmony_ci return; 868779efd5Sopenharmony_ci } 878779efd5Sopenharmony_ci for (let mimeEntries of this.dataValues.values()) { 888779efd5Sopenharmony_ci for (let data of mimeEntries) { 898779efd5Sopenharmony_ci opt = data.buildDiff(Data.CONTENT_URI); 908779efd5Sopenharmony_ci if (data.isInsert()) { 918779efd5Sopenharmony_ci if (isContactInsert) { 928779efd5Sopenharmony_ci opt.withValueBackReferences(Data.RAW_CONTACT_ID, firstIndex); 938779efd5Sopenharmony_ci } else { 948779efd5Sopenharmony_ci opt.withValue(Data.RAW_CONTACT_ID, beforeId); 958779efd5Sopenharmony_ci } 968779efd5Sopenharmony_ci } 978779efd5Sopenharmony_ci diff.push(opt); 988779efd5Sopenharmony_ci } 998779efd5Sopenharmony_ci } 1008779efd5Sopenharmony_ci } 1018779efd5Sopenharmony_ci}