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 { EventColumns } from '@ohos/datastructure/src/main/ets/events/EventColumns';
180fbfc30aSopenharmony_ciimport { InstancesColumns } from '@ohos/datastructure/src/main/ets/instances/InstancesColumns';
190fbfc30aSopenharmony_ciimport { CalendarsColumns } from '@ohos/datastructure/src/main/ets/calendars/CalendarsColumns';
200fbfc30aSopenharmony_ciimport { ColorsColumns } from '@ohos/datastructure/src/main/ets/colors/ColorsColumns';
210fbfc30aSopenharmony_ciimport { RemindersColumns } from '@ohos/datastructure/src/main/ets/reminders/RemindersColumns';
220fbfc30aSopenharmony_ciimport { AttendeesColumns } from '@ohos/datastructure/src/main/ets/attendees/AttendeesColumns';
230fbfc30aSopenharmony_ciimport { CalendarAlertsColumns } from '@ohos/datastructure/src/main/ets/calendaralerts/CalendarAlertsColumns';
240fbfc30aSopenharmony_ciimport { CalendarCacheColumns } from '@ohos/datastructure/src/main/ets/calendarcache/CalendarCacheColumns';
250fbfc30aSopenharmony_ciimport { EventsRawTimesColumns } from '@ohos/datastructure/src/main/ets/eventsrawtimes/EventsRawTimesColumns';
260fbfc30aSopenharmony_ciimport {
270fbfc30aSopenharmony_ci  ExtendCalendarEventColumns
280fbfc30aSopenharmony_ci} from '@ohos/datastructure/src/main/ets/extendcalendarevent/ExtendCalendarEventColumns';
290fbfc30aSopenharmony_ciimport {
300fbfc30aSopenharmony_ci  ExtendedPropertiesColumns
310fbfc30aSopenharmony_ci} from '@ohos/datastructure/src/main/ets/extendedproperties/ExtendedPropertiesColumns';
320fbfc30aSopenharmony_ciimport { SyncStateColumns } from '@ohos/datastructure/src/main/ets/syncstate/SyncStateColumns';
330fbfc30aSopenharmony_ciimport { MapAddressHistoryColumns } from '@ohos/datastructure/src/main/ets/location/MapAddressHistoryColumns';
340fbfc30aSopenharmony_ciimport { SyncStateMetadataColumns } from '@ohos/datastructure/src/main/ets/syncstate/SyncStateMetadataColumns';
350fbfc30aSopenharmony_ciimport { CalendarMetaDataColumns } from '@ohos/datastructure/src/main/ets/calendarmetadata/CalendarMetaDataColumns';
360fbfc30aSopenharmony_ciimport { QUERY_START } from '@ohos/common/src/main/ets/utils/UrlUtils';
370fbfc30aSopenharmony_ciimport { Log } from '@ohos/common/src/main/ets/utils/Log';
380fbfc30aSopenharmony_ci
390fbfc30aSopenharmony_ciconst DATA_SHARE_PREFIX: string = "datashare:///calendardata";
400fbfc30aSopenharmony_ci
410fbfc30aSopenharmony_ciconst EVENTS_PATH: string = "Events";
420fbfc30aSopenharmony_ci
430fbfc30aSopenharmony_ciconst CALENDARS_PATH: string = "Calendars";
440fbfc30aSopenharmony_ci
450fbfc30aSopenharmony_ciconst ATTENDEES_PATH: string = "Attendees";
460fbfc30aSopenharmony_ci
470fbfc30aSopenharmony_ciconst CALENDAR_ALERTS_PATH: string = "CalendarAlerts";
480fbfc30aSopenharmony_ci
490fbfc30aSopenharmony_ciconst CALENDAR_CACHE_PATH: string = "CalendarCache";
500fbfc30aSopenharmony_ci
510fbfc30aSopenharmony_ciconst CALENDAR_METADATA_PATH: string = "CalendarMetaData";
520fbfc30aSopenharmony_ci
530fbfc30aSopenharmony_ciconst COLORS_PATH: string = "Colors";
540fbfc30aSopenharmony_ci
550fbfc30aSopenharmony_ciconst EVENTS_RAW_TIME_PATH: string = "EventsRawTimes";
560fbfc30aSopenharmony_ci
570fbfc30aSopenharmony_ciconst EXTEND_CALENDAR_EVENT_PATH: string = "ExtendCalendarEvent";
580fbfc30aSopenharmony_ci
590fbfc30aSopenharmony_ciconst EXTENDED_PROPERTIES_PATH: string = "ExtendedProperties";
600fbfc30aSopenharmony_ci
610fbfc30aSopenharmony_ciexport const INSTANCES_PATH: string = "Instances";
620fbfc30aSopenharmony_ci
630fbfc30aSopenharmony_ciconst MAP_ADDRESS_HISTORY_PATH: string = "map_address_history";
640fbfc30aSopenharmony_ci
650fbfc30aSopenharmony_ciconst REMINDERS_PATH: string = "Reminders";
660fbfc30aSopenharmony_ci
670fbfc30aSopenharmony_ciconst SYNC_STATE_PATH: string = "_sync_state";
680fbfc30aSopenharmony_ci
690fbfc30aSopenharmony_ciconst SYNC_STATE_METADATA_PATH: string = "_sync_state_metadata";
700fbfc30aSopenharmony_ci
710fbfc30aSopenharmony_ciexport const URI_EVENTS: string = `${DATA_SHARE_PREFIX}/${EVENTS_PATH}`;
720fbfc30aSopenharmony_ci
730fbfc30aSopenharmony_ciconst TAG = "CalendarUriHelper";
740fbfc30aSopenharmony_ci
750fbfc30aSopenharmony_ci/**
760fbfc30aSopenharmony_ci * transfer the user input uri to the specific table
770fbfc30aSopenharmony_ci *
780fbfc30aSopenharmony_ci * @param uri the user input uri
790fbfc30aSopenharmony_ci * @return the specific table
800fbfc30aSopenharmony_ci */
810fbfc30aSopenharmony_ciexport default function getTableByUri(uri: string): string {
820fbfc30aSopenharmony_ci  Log.log(TAG, 'getTableByUri uri: ' + uri);
830fbfc30aSopenharmony_ci  let path = getPathByUri(uri);
840fbfc30aSopenharmony_ci  let tablePathName = getTablePathNameByPath(path);
850fbfc30aSopenharmony_ci  return getTableNameByTablePath(tablePathName);
860fbfc30aSopenharmony_ci}
870fbfc30aSopenharmony_ci
880fbfc30aSopenharmony_ci/**
890fbfc30aSopenharmony_ci * transfer the table's path to the specific table name
900fbfc30aSopenharmony_ci *
910fbfc30aSopenharmony_ci * @param table's path that is got from user input uri
920fbfc30aSopenharmony_ci * @return the specific table name
930fbfc30aSopenharmony_ci */
940fbfc30aSopenharmony_cifunction getTableNameByTablePath(tablePathName: string): string {
950fbfc30aSopenharmony_ci  switch (tablePathName) {
960fbfc30aSopenharmony_ci    case EVENTS_PATH:
970fbfc30aSopenharmony_ci      return EventColumns.TABLE_NAME;
980fbfc30aSopenharmony_ci    case CALENDARS_PATH:
990fbfc30aSopenharmony_ci      return CalendarsColumns.TABLE_NAME;
1000fbfc30aSopenharmony_ci    case ATTENDEES_PATH:
1010fbfc30aSopenharmony_ci      return AttendeesColumns.TABLE_NAME;
1020fbfc30aSopenharmony_ci    case CALENDAR_ALERTS_PATH:
1030fbfc30aSopenharmony_ci      return CalendarAlertsColumns.TABLE_NAME;
1040fbfc30aSopenharmony_ci    case CALENDAR_CACHE_PATH:
1050fbfc30aSopenharmony_ci      return CalendarCacheColumns.TABLE_NAME;
1060fbfc30aSopenharmony_ci    case CALENDAR_METADATA_PATH:
1070fbfc30aSopenharmony_ci      return CalendarMetaDataColumns.TABLE_NAME;
1080fbfc30aSopenharmony_ci    case COLORS_PATH:
1090fbfc30aSopenharmony_ci      return ColorsColumns.TABLE_NAME;
1100fbfc30aSopenharmony_ci    case EVENTS_RAW_TIME_PATH:
1110fbfc30aSopenharmony_ci      return EventsRawTimesColumns.TABLE_NAME;
1120fbfc30aSopenharmony_ci    case EXTEND_CALENDAR_EVENT_PATH:
1130fbfc30aSopenharmony_ci      return ExtendCalendarEventColumns.TABLE_NAME;
1140fbfc30aSopenharmony_ci    case EXTENDED_PROPERTIES_PATH:
1150fbfc30aSopenharmony_ci      return ExtendedPropertiesColumns.TABLE_NAME;
1160fbfc30aSopenharmony_ci    case INSTANCES_PATH:
1170fbfc30aSopenharmony_ci      return InstancesColumns.TABLE_NAME;
1180fbfc30aSopenharmony_ci    case MAP_ADDRESS_HISTORY_PATH:
1190fbfc30aSopenharmony_ci      return MapAddressHistoryColumns.TABLE_NAME;
1200fbfc30aSopenharmony_ci    case REMINDERS_PATH:
1210fbfc30aSopenharmony_ci      return RemindersColumns.TABLE_NAME;
1220fbfc30aSopenharmony_ci    case SYNC_STATE_PATH:
1230fbfc30aSopenharmony_ci      return SyncStateColumns.TABLE_NAME;
1240fbfc30aSopenharmony_ci    case SYNC_STATE_METADATA_PATH:
1250fbfc30aSopenharmony_ci      return SyncStateMetadataColumns.TABLE_NAME;
1260fbfc30aSopenharmony_ci    default:
1270fbfc30aSopenharmony_ci      Log.log(TAG, 'getTablePathByTableName run in default case!');
1280fbfc30aSopenharmony_ci      return "";
1290fbfc30aSopenharmony_ci  }
1300fbfc30aSopenharmony_ci}
1310fbfc30aSopenharmony_ci
1320fbfc30aSopenharmony_ci/**
1330fbfc30aSopenharmony_ci * transfer the user input uri to the specific resource path
1340fbfc30aSopenharmony_ci *
1350fbfc30aSopenharmony_ci * @param table's path that is got from user input uri
1360fbfc30aSopenharmony_ci * @return the resource path
1370fbfc30aSopenharmony_ci */
1380fbfc30aSopenharmony_cifunction getPathByUri(uri: string): string {
1390fbfc30aSopenharmony_ci  // delete dataShare's prefix
1400fbfc30aSopenharmony_ci  if (uri.startsWith(DATA_SHARE_PREFIX)) {
1410fbfc30aSopenharmony_ci    uri = uri.split(DATA_SHARE_PREFIX).join("");
1420fbfc30aSopenharmony_ci  }
1430fbfc30aSopenharmony_ci
1440fbfc30aSopenharmony_ci  let endIndex = uri.indexOf(QUERY_START);
1450fbfc30aSopenharmony_ci  const totalLength = uri.length;
1460fbfc30aSopenharmony_ci  if (endIndex === -1 && totalLength > 0) {
1470fbfc30aSopenharmony_ci    return uri;
1480fbfc30aSopenharmony_ci  }
1490fbfc30aSopenharmony_ci
1500fbfc30aSopenharmony_ci  // in case of uri containing query condition like Instance's uri
1510fbfc30aSopenharmony_ci  if (endIndex < 1 || endIndex >= totalLength) {
1520fbfc30aSopenharmony_ci    return '';
1530fbfc30aSopenharmony_ci  }
1540fbfc30aSopenharmony_ci  const path = uri.substring(0, endIndex);
1550fbfc30aSopenharmony_ci  if (path === null || path === undefined || path.length < 1) {
1560fbfc30aSopenharmony_ci    return '';
1570fbfc30aSopenharmony_ci  }
1580fbfc30aSopenharmony_ci  return path;
1590fbfc30aSopenharmony_ci}
1600fbfc30aSopenharmony_ci
1610fbfc30aSopenharmony_ci/**
1620fbfc30aSopenharmony_ci * transfer uri without prefix to the table's path
1630fbfc30aSopenharmony_ci *
1640fbfc30aSopenharmony_ci * @param user input uri without prefix
1650fbfc30aSopenharmony_ci * @return the table path name
1660fbfc30aSopenharmony_ci */
1670fbfc30aSopenharmony_cifunction getTablePathNameByPath(path: string): string {
1680fbfc30aSopenharmony_ci  const array = path.split("/");
1690fbfc30aSopenharmony_ci  if (array === null || array === undefined || array.length < 1) {
1700fbfc30aSopenharmony_ci    return '';
1710fbfc30aSopenharmony_ci  }
1720fbfc30aSopenharmony_ci  return array[1];
1730fbfc30aSopenharmony_ci}
174