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 BaseColumns from '../contract/BaseColumns'; 178779efd5Sopenharmony_ciimport DataColumns from '../contract/DataColumns'; 188779efd5Sopenharmony_ciimport DAOperation from './DAOperation'; 198779efd5Sopenharmony_ciimport { HiLog } from '../../../../../../common/src/main/ets/util/HiLog'; 208779efd5Sopenharmony_ci 218779efd5Sopenharmony_ciconst TAG = 'ValuesDelta'; 228779efd5Sopenharmony_ci 238779efd5Sopenharmony_ci/** 248779efd5Sopenharmony_ci * Contact details editing model 258779efd5Sopenharmony_ci */ 268779efd5Sopenharmony_ciexport default class ValuesDelta { 278779efd5Sopenharmony_ci private readonly _idColumn: string = BaseColumns.ID; 288779efd5Sopenharmony_ci before: Map<string, any>; 298779efd5Sopenharmony_ci after: Map<string, any>; 308779efd5Sopenharmony_ci constructor() { 318779efd5Sopenharmony_ci } 328779efd5Sopenharmony_ci 338779efd5Sopenharmony_ci static fromValues(values: Map<string, any>) { 348779efd5Sopenharmony_ci let valuesDelta = new ValuesDelta(); 358779efd5Sopenharmony_ci valuesDelta.before = values; 368779efd5Sopenharmony_ci valuesDelta.after = new Map(); 378779efd5Sopenharmony_ci return valuesDelta; 388779efd5Sopenharmony_ci } 398779efd5Sopenharmony_ci 408779efd5Sopenharmony_ci getValue(key: string) { 418779efd5Sopenharmony_ci let result; 428779efd5Sopenharmony_ci if (this.after != undefined && this.after.has(key)) { 438779efd5Sopenharmony_ci result = this.after.get(key); 448779efd5Sopenharmony_ci } else if (this.before != undefined && this.before.has(key)) { 458779efd5Sopenharmony_ci result = this.before.get(key); 468779efd5Sopenharmony_ci } 478779efd5Sopenharmony_ci return result 488779efd5Sopenharmony_ci } 498779efd5Sopenharmony_ci 508779efd5Sopenharmony_ci putValue(key: string, value: any) { 518779efd5Sopenharmony_ci if (this.after == undefined) { 528779efd5Sopenharmony_ci this.after = new Map(); 538779efd5Sopenharmony_ci } 548779efd5Sopenharmony_ci this.after.set(key, value); 558779efd5Sopenharmony_ci } 568779efd5Sopenharmony_ci 578779efd5Sopenharmony_ci isInsert() { 588779efd5Sopenharmony_ci return!this.beforeExists() && (this.after != undefined); 598779efd5Sopenharmony_ci } 608779efd5Sopenharmony_ci 618779efd5Sopenharmony_ci isDelete() { 628779efd5Sopenharmony_ci return this.beforeExists() && (this.after == undefined); 638779efd5Sopenharmony_ci } 648779efd5Sopenharmony_ci 658779efd5Sopenharmony_ci isUpdate() { 668779efd5Sopenharmony_ci if (!this.beforeExists() || this.after == undefined || this.after.size == 0) { 678779efd5Sopenharmony_ci return false; 688779efd5Sopenharmony_ci } 698779efd5Sopenharmony_ci for (let key of this.after.keys()) { 708779efd5Sopenharmony_ci let newValue = this.after.get(key); 718779efd5Sopenharmony_ci let oldValue = this.before.get(key); 728779efd5Sopenharmony_ci let mimetypeId = this.before.get(DataColumns.TYPE_ID); 738779efd5Sopenharmony_ci if (this.isObjectsEqual(oldValue, newValue, mimetypeId, key)) { 748779efd5Sopenharmony_ci return true; 758779efd5Sopenharmony_ci } 768779efd5Sopenharmony_ci } 778779efd5Sopenharmony_ci return false; 788779efd5Sopenharmony_ci } 798779efd5Sopenharmony_ci 808779efd5Sopenharmony_ci private isObjectsEqual(oldValue: any, newValue: any, mimetype: number, key: string) { 818779efd5Sopenharmony_ci return oldValue == newValue; 828779efd5Sopenharmony_ci } 838779efd5Sopenharmony_ci 848779efd5Sopenharmony_ci buildDiff(targetUri: string) { 858779efd5Sopenharmony_ci if (targetUri == undefined) { 868779efd5Sopenharmony_ci return undefined; 878779efd5Sopenharmony_ci } 888779efd5Sopenharmony_ci 898779efd5Sopenharmony_ci let opt; 908779efd5Sopenharmony_ci if (this.isInsert()) { 918779efd5Sopenharmony_ci this.after.delete(this._idColumn); 928779efd5Sopenharmony_ci opt = DAOperation.newInsert(targetUri); 938779efd5Sopenharmony_ci opt.valuesBucket = this.mapToObj(this.after); 948779efd5Sopenharmony_ci } else if (this.isDelete()) { 958779efd5Sopenharmony_ci opt = DAOperation.newDelete(targetUri); 968779efd5Sopenharmony_ci opt.predicates = this._idColumn + '=' + this.getValue(this._idColumn); 978779efd5Sopenharmony_ci } else if (this.isUpdate) { 988779efd5Sopenharmony_ci opt = DAOperation.newUpdate(targetUri); 998779efd5Sopenharmony_ci opt.predicates = this._idColumn + '=' + this.getValue(this._idColumn); 1008779efd5Sopenharmony_ci opt.valuesBucket = this.mapToObj(this.after); 1018779efd5Sopenharmony_ci } else { 1028779efd5Sopenharmony_ci HiLog.i(TAG, 'buildDiff do nothing.'); 1038779efd5Sopenharmony_ci } 1048779efd5Sopenharmony_ci return opt; 1058779efd5Sopenharmony_ci } 1068779efd5Sopenharmony_ci 1078779efd5Sopenharmony_ci private mapToObj(map: Map<string, any>) { 1088779efd5Sopenharmony_ci let obj = {}; 1098779efd5Sopenharmony_ci for (let [key, value] of map) { 1108779efd5Sopenharmony_ci obj[key] = value; 1118779efd5Sopenharmony_ci } 1128779efd5Sopenharmony_ci return obj; 1138779efd5Sopenharmony_ci } 1148779efd5Sopenharmony_ci 1158779efd5Sopenharmony_ci 1168779efd5Sopenharmony_ci private beforeExists() { 1178779efd5Sopenharmony_ci return this.before != undefined && this.before.has(this._idColumn); 1188779efd5Sopenharmony_ci } 1198779efd5Sopenharmony_ci}