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 BaseColumns from '../contract/BaseColumns'; 17import DataColumns from '../contract/DataColumns'; 18import DAOperation from './DAOperation'; 19import { HiLog } from '../../../../../../common/src/main/ets/util/HiLog'; 20 21const TAG = "ValuesDelta"; 22 23/** 24 * Contact details editing model 25 */ 26export default class ValuesDelta { 27 private readonly _idColumn: string = BaseColumns.ID; 28 before: Map<string, any>; 29 after: Map<string, any>; 30 constructor() { 31 } 32 33 static fromValues(values: Map<string, any>) { 34 let valuesDelta = new ValuesDelta(); 35 valuesDelta.before = values; 36 valuesDelta.after = new Map(); 37 return valuesDelta; 38 } 39 40 getValue(key: string) { 41 let result; 42 if (this.after != undefined && this.after.has(key)) { 43 result = this.after.get(key); 44 } else if (this.before != undefined && this.before.has(key)) { 45 result = this.before.get(key); 46 } 47 return result 48 } 49 50 putValue(key: string, value: any) { 51 if (this.after == undefined) { 52 this.after = new Map(); 53 } 54 this.after.set(key, value); 55 } 56 57 isInsert() { 58 return!this.beforeExists() && (this.after != undefined); 59 } 60 61 isDelete() { 62 return this.beforeExists() && (this.after == undefined); 63 } 64 65 isUpdate() { 66 if (!this.beforeExists() || this.after == undefined || this.after.size == 0) { 67 return false; 68 } 69 for (let key of this.after.keys()) { 70 let newValue = this.after.get(key); 71 let oldValue = this.before.get(key); 72 let mimetypeId = this.before.get(DataColumns.TYPE_ID); 73 if (this.isObjectsEqual(oldValue, newValue, mimetypeId, key)) { 74 return true; 75 } 76 } 77 return false; 78 } 79 80 private isObjectsEqual(oldValue: any, newValue: any, mimetype: number, key: string) { 81 return oldValue == newValue; 82 } 83 84 buildDiff(targetUri: string) { 85 if (targetUri == undefined) { 86 return undefined; 87 } 88 89 let opt; 90 if (this.isInsert()) { 91 this.after.delete(this._idColumn); 92 opt = DAOperation.newInsert(targetUri); 93 opt.valuesBucket = this.mapToObj(this.after); 94 } else if (this.isDelete()) { 95 opt = DAOperation.newDelete(targetUri); 96 opt.predicates = this._idColumn + "=" + this.getValue(this._idColumn); 97 } else if (this.isUpdate) { 98 opt = DAOperation.newUpdate(targetUri); 99 opt.predicates = this._idColumn + "=" + this.getValue(this._idColumn); 100 opt.valuesBucket = this.mapToObj(this.after); 101 } else { 102 HiLog.i(TAG, 'buildDiff do nothing.'); 103 } 104 return opt; 105 } 106 107 private mapToObj(map: Map<string, any>) { 108 let obj = {}; 109 for (let [key, value] of map) { 110 obj[key] = value; 111 } 112 return obj; 113 } 114 115 116 private beforeExists() { 117 return this.before != undefined && this.before.has(this._idColumn); 118 } 119}