10fbfc30aSopenharmony_ci/**
20fbfc30aSopenharmony_ci * @file Describe the file
30fbfc30aSopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd.
40fbfc30aSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
50fbfc30aSopenharmony_ci * you may not use this file except in compliance with the License.
60fbfc30aSopenharmony_ci * You may obtain a copy of the License at
70fbfc30aSopenharmony_ci *
80fbfc30aSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
90fbfc30aSopenharmony_ci *
100fbfc30aSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
110fbfc30aSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
120fbfc30aSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
130fbfc30aSopenharmony_ci * See the License for the specific language governing permissions and
140fbfc30aSopenharmony_ci * limitations under the License.
150fbfc30aSopenharmony_ci */
160fbfc30aSopenharmony_ci
170fbfc30aSopenharmony_ciimport { Log } from '@ohos/common/src/main/ets/utils/Log';
180fbfc30aSopenharmony_ciimport { insertDefaultCalendar } from '@ohos/datamanager/src/main/ets/processor/calendars/CalendarsProcessor';
190fbfc30aSopenharmony_ciimport CalendarDataHelper from '@ohos/datamanager/src/main/ets/utils/CalendarDataHelper';
200fbfc30aSopenharmony_ciimport getTableByUri from '@ohos/datamanager/src/main/ets/utils/CalendarUriHelper';
210fbfc30aSopenharmony_ciimport factory from '@ohos/datamanager/src/main/ets/processor/DatabaseProcessorFactory';
220fbfc30aSopenharmony_ciimport data_rdb from '@ohos.data.relationalStore';
230fbfc30aSopenharmony_ciimport dataSharePredicates from '@ohos.data.dataSharePredicates';
240fbfc30aSopenharmony_ci
250fbfc30aSopenharmony_cilet rdbStore: data_rdb.RdbStore;
260fbfc30aSopenharmony_cilet TAG = 'DataShareAbilityDelegate';
270fbfc30aSopenharmony_ci
280fbfc30aSopenharmony_ci/**
290fbfc30aSopenharmony_ci * the delegate of CalendarData's DatabaseProcessor
300fbfc30aSopenharmony_ci *
310fbfc30aSopenharmony_ci * @since 2022-06-15
320fbfc30aSopenharmony_ci */
330fbfc30aSopenharmony_ciclass DataShareAbilityDelegate {
340fbfc30aSopenharmony_ci  async init(): Promise<boolean> {
350fbfc30aSopenharmony_ci    Log.info(TAG, 'init start');
360fbfc30aSopenharmony_ci    try {
370fbfc30aSopenharmony_ci      rdbStore = await CalendarDataHelper.getInstance().getRdbStore() as data_rdb.RdbStore;
380fbfc30aSopenharmony_ci      await insertDefaultCalendar(rdbStore);
390fbfc30aSopenharmony_ci    } catch (err) {
400fbfc30aSopenharmony_ci      Log.error(TAG, 'init err');
410fbfc30aSopenharmony_ci      return false;
420fbfc30aSopenharmony_ci    }
430fbfc30aSopenharmony_ci    Log.info(TAG, 'init end');
440fbfc30aSopenharmony_ci    return true;
450fbfc30aSopenharmony_ci  }
460fbfc30aSopenharmony_ci
470fbfc30aSopenharmony_ci  insertByHighAuthority(uri: string, value: data_rdb.ValuesBucket, callback: Function) {
480fbfc30aSopenharmony_ci    if (!rdbStore) {
490fbfc30aSopenharmony_ci      this.getDb();
500fbfc30aSopenharmony_ci      Log.warn(TAG, `insertByHighAuthority uri: ${uri} rdbStore is null`)
510fbfc30aSopenharmony_ci      return;
520fbfc30aSopenharmony_ci    }
530fbfc30aSopenharmony_ci    Log.info(TAG, `insertByHighAuthority uri: ${uri}`);
540fbfc30aSopenharmony_ci    let table = getTableByUri(uri);
550fbfc30aSopenharmony_ci    Log.info(TAG, `insertByHighAuthority table: ${table}`);
560fbfc30aSopenharmony_ci
570fbfc30aSopenharmony_ci    const processor = factory.getDatabaseProcessor(table);
580fbfc30aSopenharmony_ci    if (processor !== null && processor !== undefined) {
590fbfc30aSopenharmony_ci      processor.insertByHighAuthority(rdbStore, uri, value, callback);
600fbfc30aSopenharmony_ci      return;
610fbfc30aSopenharmony_ci    } else {
620fbfc30aSopenharmony_ci      Log.error(TAG, 'insertByHighAuthority with invalid processor');
630fbfc30aSopenharmony_ci    }
640fbfc30aSopenharmony_ci  }
650fbfc30aSopenharmony_ci
660fbfc30aSopenharmony_ci  batchInsertByHighAuthority(uri: string, value: data_rdb.ValuesBucket[], callback: Function) {
670fbfc30aSopenharmony_ci    if (!rdbStore) {
680fbfc30aSopenharmony_ci      this.getDb();
690fbfc30aSopenharmony_ci      Log.warn(TAG, `insert uri: ${uri} rdbStore is null`)
700fbfc30aSopenharmony_ci      return;
710fbfc30aSopenharmony_ci    }
720fbfc30aSopenharmony_ci    Log.info(TAG, `insert uri: ${uri}`);
730fbfc30aSopenharmony_ci    let table = getTableByUri(uri);
740fbfc30aSopenharmony_ci    Log.info(TAG, `insert table: ${table}`);
750fbfc30aSopenharmony_ci
760fbfc30aSopenharmony_ci    const processor = factory.getDatabaseProcessor(table);
770fbfc30aSopenharmony_ci    if (processor !== null && processor !== undefined) {
780fbfc30aSopenharmony_ci      processor.batchInsertByHighAuthority(rdbStore, uri, value, callback);
790fbfc30aSopenharmony_ci      return;
800fbfc30aSopenharmony_ci    } else {
810fbfc30aSopenharmony_ci      Log.error(TAG, 'insert with invalid processor');
820fbfc30aSopenharmony_ci    }
830fbfc30aSopenharmony_ci  }
840fbfc30aSopenharmony_ci
850fbfc30aSopenharmony_ci  async getDb() {
860fbfc30aSopenharmony_ci    rdbStore = await CalendarDataHelper.getInstance().getRdbStore() as data_rdb.RdbStore;
870fbfc30aSopenharmony_ci  }
880fbfc30aSopenharmony_ci
890fbfc30aSopenharmony_ci  insertByLowAuthority(uri: string, value: data_rdb.ValuesBucket, callback: Function) {
900fbfc30aSopenharmony_ci    if (!rdbStore) {
910fbfc30aSopenharmony_ci      Log.warn(TAG, `insertByLowAuthority uri: ${uri} rdbStore is null`)
920fbfc30aSopenharmony_ci      this.getDb();
930fbfc30aSopenharmony_ci      return;
940fbfc30aSopenharmony_ci    }
950fbfc30aSopenharmony_ci    Log.info(TAG, `insertByLowAuthority uri: ${uri}`);
960fbfc30aSopenharmony_ci    let table = getTableByUri(uri);
970fbfc30aSopenharmony_ci    Log.info(TAG, `insertByLowAuthority table: ${table}`);
980fbfc30aSopenharmony_ci
990fbfc30aSopenharmony_ci    const processor = factory.getDatabaseProcessor(table);
1000fbfc30aSopenharmony_ci    if (processor !== null && processor !== undefined) {
1010fbfc30aSopenharmony_ci      processor.insertByLowAuthority(rdbStore, uri, value, callback);
1020fbfc30aSopenharmony_ci      return;
1030fbfc30aSopenharmony_ci    } else {
1040fbfc30aSopenharmony_ci      Log.error(TAG, 'insertByLowAuthority with invalid processor');
1050fbfc30aSopenharmony_ci    }
1060fbfc30aSopenharmony_ci  }
1070fbfc30aSopenharmony_ci
1080fbfc30aSopenharmony_ci  batchInsertByLowAuthority(uri: string, value: data_rdb.ValuesBucket[], callback: Function) {
1090fbfc30aSopenharmony_ci
1100fbfc30aSopenharmony_ci    if (!rdbStore) {
1110fbfc30aSopenharmony_ci      Log.warn(TAG, `insert uri: ${uri} rdbStore is null`)
1120fbfc30aSopenharmony_ci      this.getDb();
1130fbfc30aSopenharmony_ci      return;
1140fbfc30aSopenharmony_ci    }
1150fbfc30aSopenharmony_ci    Log.info(TAG, `insert uri: ${uri}`);
1160fbfc30aSopenharmony_ci    let table = getTableByUri(uri);
1170fbfc30aSopenharmony_ci    Log.info(TAG, `insert table: ${table}`);
1180fbfc30aSopenharmony_ci
1190fbfc30aSopenharmony_ci    const processor = factory.getDatabaseProcessor(table);
1200fbfc30aSopenharmony_ci    if (processor !== null && processor !== undefined) {
1210fbfc30aSopenharmony_ci      processor.batchInsertByLowAuthority(rdbStore, uri, value, callback);
1220fbfc30aSopenharmony_ci      return;
1230fbfc30aSopenharmony_ci    } else {
1240fbfc30aSopenharmony_ci      Log.error(TAG, 'insert with invalid processor');
1250fbfc30aSopenharmony_ci    }
1260fbfc30aSopenharmony_ci  }
1270fbfc30aSopenharmony_ci
1280fbfc30aSopenharmony_ci  deleteByHighAuthority(uri: string, predicates: dataSharePredicates.DataSharePredicates, callback: Function) {
1290fbfc30aSopenharmony_ci    if (!rdbStore) {
1300fbfc30aSopenharmony_ci      this.getDb();
1310fbfc30aSopenharmony_ci      Log.warn(TAG, `deleteByHighAuthority uri: ${uri} rdbStore is null`)
1320fbfc30aSopenharmony_ci      return;
1330fbfc30aSopenharmony_ci    }
1340fbfc30aSopenharmony_ci    Log.info(TAG, `deleteByHighAuthority uri: ${uri}`);
1350fbfc30aSopenharmony_ci    let table = getTableByUri(uri);
1360fbfc30aSopenharmony_ci    Log.info(TAG, `deleteByHighAuthority table: ${table}`);
1370fbfc30aSopenharmony_ci
1380fbfc30aSopenharmony_ci    const processor = factory.getDatabaseProcessor(table);
1390fbfc30aSopenharmony_ci    if (processor !== null && processor !== undefined) {
1400fbfc30aSopenharmony_ci      processor.deleteByHighAuthority(rdbStore, uri, predicates, callback);
1410fbfc30aSopenharmony_ci      return;
1420fbfc30aSopenharmony_ci    } else {
1430fbfc30aSopenharmony_ci      Log.error(TAG, 'deleteByHighAuthority with invalid processor');
1440fbfc30aSopenharmony_ci    }
1450fbfc30aSopenharmony_ci  }
1460fbfc30aSopenharmony_ci
1470fbfc30aSopenharmony_ci  deleteByLowAuthority(uri: string, predicates: dataSharePredicates.DataSharePredicates, callback: Function) {
1480fbfc30aSopenharmony_ci    if (!rdbStore) {
1490fbfc30aSopenharmony_ci      this.getDb();
1500fbfc30aSopenharmony_ci      Log.warn(TAG, `deleteByLowAuthority uri: ${uri} rdbStore is null`)
1510fbfc30aSopenharmony_ci      return;
1520fbfc30aSopenharmony_ci    }
1530fbfc30aSopenharmony_ci    Log.info(TAG, `deleteByLowAuthority uri: ${uri}`);
1540fbfc30aSopenharmony_ci    let table = getTableByUri(uri);
1550fbfc30aSopenharmony_ci    Log.info(TAG, `deleteByLowAuthority table: ${table}`);
1560fbfc30aSopenharmony_ci
1570fbfc30aSopenharmony_ci    const processor = factory.getDatabaseProcessor(table);
1580fbfc30aSopenharmony_ci    if (processor !== null && processor !== undefined) {
1590fbfc30aSopenharmony_ci      processor.deleteByLowAuthority(rdbStore, uri, predicates, callback);
1600fbfc30aSopenharmony_ci      return;
1610fbfc30aSopenharmony_ci    } else {
1620fbfc30aSopenharmony_ci      Log.error(TAG, 'deleteByLowAuthority with invalid processor');
1630fbfc30aSopenharmony_ci    }
1640fbfc30aSopenharmony_ci  }
1650fbfc30aSopenharmony_ci
1660fbfc30aSopenharmony_ci  updateByHighAuthority(uri: string, value: data_rdb.ValuesBucket,
1670fbfc30aSopenharmony_ci                        predicates: dataSharePredicates.DataSharePredicates, callback: Function) {
1680fbfc30aSopenharmony_ci    if (!rdbStore) {
1690fbfc30aSopenharmony_ci      this.getDb();
1700fbfc30aSopenharmony_ci      Log.warn(TAG, `updateByHighAuthority uri: ${uri} rdbStore is null`)
1710fbfc30aSopenharmony_ci      return;
1720fbfc30aSopenharmony_ci    }
1730fbfc30aSopenharmony_ci    Log.info(TAG, `updateByHighAuthority uri: ${uri}`);
1740fbfc30aSopenharmony_ci    let table = getTableByUri(uri);
1750fbfc30aSopenharmony_ci    Log.info(TAG, `updateByHighAuthority table: ${table}`);
1760fbfc30aSopenharmony_ci
1770fbfc30aSopenharmony_ci    const processor = factory.getDatabaseProcessor(table);
1780fbfc30aSopenharmony_ci    if (processor !== null && processor !== undefined) {
1790fbfc30aSopenharmony_ci      processor.updateByHighAuthority(rdbStore, uri, value, predicates, callback);
1800fbfc30aSopenharmony_ci      return;
1810fbfc30aSopenharmony_ci    } else {
1820fbfc30aSopenharmony_ci      Log.error(TAG, 'updateByHighAuthority with invalid processor');
1830fbfc30aSopenharmony_ci    }
1840fbfc30aSopenharmony_ci  }
1850fbfc30aSopenharmony_ci
1860fbfc30aSopenharmony_ci  updateByLowAuthority(uri: string, value: data_rdb.ValuesBucket,
1870fbfc30aSopenharmony_ci                       predicates: dataSharePredicates.DataSharePredicates, callback: Function) {
1880fbfc30aSopenharmony_ci    if (!rdbStore) {
1890fbfc30aSopenharmony_ci      this.getDb();
1900fbfc30aSopenharmony_ci      Log.warn(TAG, `updateByLowAuthority uri: ${uri} rdbStore is null`)
1910fbfc30aSopenharmony_ci      return;
1920fbfc30aSopenharmony_ci    }
1930fbfc30aSopenharmony_ci    Log.info(TAG, `updateByLowAuthority uri: ${uri}`);
1940fbfc30aSopenharmony_ci    let table = getTableByUri(uri);
1950fbfc30aSopenharmony_ci    Log.info(TAG, `updateByLowAuthority table: ${table}`);
1960fbfc30aSopenharmony_ci
1970fbfc30aSopenharmony_ci    const processor = factory.getDatabaseProcessor(table);
1980fbfc30aSopenharmony_ci    if (processor !== null && processor !== undefined) {
1990fbfc30aSopenharmony_ci      processor.updateByLowAuthority(rdbStore, uri, value, predicates, callback);
2000fbfc30aSopenharmony_ci      return;
2010fbfc30aSopenharmony_ci    } else {
2020fbfc30aSopenharmony_ci      Log.error(TAG, 'updateByLowAuthority with invalid processor');
2030fbfc30aSopenharmony_ci    }
2040fbfc30aSopenharmony_ci  }
2050fbfc30aSopenharmony_ci
2060fbfc30aSopenharmony_ci  queryByHighAuthority(uri: string, columns: Array<string>,
2070fbfc30aSopenharmony_ci                       predicates: dataSharePredicates.DataSharePredicates, callback: Function) {
2080fbfc30aSopenharmony_ci    if (!rdbStore) {
2090fbfc30aSopenharmony_ci      this.getDb();
2100fbfc30aSopenharmony_ci      Log.warn(TAG, `queryByHighAuthority uri: ${uri} rdbStore is null`)
2110fbfc30aSopenharmony_ci      return;
2120fbfc30aSopenharmony_ci    }
2130fbfc30aSopenharmony_ci    Log.info(TAG, `queryByHighAuthority uri: ${uri}`);
2140fbfc30aSopenharmony_ci    const table = getTableByUri(uri);
2150fbfc30aSopenharmony_ci    Log.info(TAG, `queryByHighAuthority table: ${table}`);
2160fbfc30aSopenharmony_ci
2170fbfc30aSopenharmony_ci    const processor = factory.getDatabaseProcessor(table);
2180fbfc30aSopenharmony_ci    if (processor !== null && processor !== undefined) {
2190fbfc30aSopenharmony_ci      processor.queryByHighAuthority(rdbStore, uri, columns, predicates, callback);
2200fbfc30aSopenharmony_ci      return;
2210fbfc30aSopenharmony_ci    } else {
2220fbfc30aSopenharmony_ci      Log.error(TAG, 'queryByHighAuthority with invalid processor');
2230fbfc30aSopenharmony_ci    }
2240fbfc30aSopenharmony_ci  }
2250fbfc30aSopenharmony_ci
2260fbfc30aSopenharmony_ci  queryByLowAuthority(uri: string, columns: Array<string>,
2270fbfc30aSopenharmony_ci                      predicates: dataSharePredicates.DataSharePredicates, callback: Function) {
2280fbfc30aSopenharmony_ci    if (!rdbStore) {
2290fbfc30aSopenharmony_ci      this.getDb();
2300fbfc30aSopenharmony_ci      Log.warn(TAG, `queryByLowAuthority uri: ${uri} rdbStore is null`)
2310fbfc30aSopenharmony_ci      return;
2320fbfc30aSopenharmony_ci    }
2330fbfc30aSopenharmony_ci    Log.info(TAG, `queryByLowAuthority uri: ${uri}`);
2340fbfc30aSopenharmony_ci    const table = getTableByUri(uri);
2350fbfc30aSopenharmony_ci    Log.info(TAG, `queryByLowAuthority table: ${table}`);
2360fbfc30aSopenharmony_ci
2370fbfc30aSopenharmony_ci    const processor = factory.getDatabaseProcessor(table);
2380fbfc30aSopenharmony_ci    if (processor !== null && processor !== undefined) {
2390fbfc30aSopenharmony_ci      processor.queryByLowAuthority(rdbStore, uri, columns, predicates, callback);
2400fbfc30aSopenharmony_ci      return;
2410fbfc30aSopenharmony_ci    } else {
2420fbfc30aSopenharmony_ci      Log.error(TAG, 'queryByLowAuthority with invalid processor');
2430fbfc30aSopenharmony_ci    }
2440fbfc30aSopenharmony_ci  }
2450fbfc30aSopenharmony_ci}
2460fbfc30aSopenharmony_ci
2470fbfc30aSopenharmony_ciexport default new DataShareAbilityDelegate()