1/**
2 * @file Describe the file
3 * Copyright (c) 2023 Huawei Device Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17import { EventColumns } from '@ohos/datastructure/src/main/ets/events/EventColumns';
18import { InstancesColumns } from '@ohos/datastructure/src/main/ets/instances/InstancesColumns';
19import { CalendarsColumns } from '@ohos/datastructure/src/main/ets/calendars/CalendarsColumns';
20import { ColorsColumns } from '@ohos/datastructure/src/main/ets/colors/ColorsColumns';
21import { RemindersColumns } from '@ohos/datastructure/src/main/ets/reminders/RemindersColumns';
22import { AttendeesColumns } from '@ohos/datastructure/src/main/ets/attendees/AttendeesColumns';
23import { CalendarAlertsColumns } from '@ohos/datastructure/src/main/ets/calendaralerts/CalendarAlertsColumns';
24import { CalendarCacheColumns } from '@ohos/datastructure/src/main/ets/calendarcache/CalendarCacheColumns';
25import { EventsRawTimesColumns } from '@ohos/datastructure/src/main/ets/eventsrawtimes/EventsRawTimesColumns';
26import {
27  ExtendCalendarEventColumns
28} from '@ohos/datastructure/src/main/ets/extendcalendarevent/ExtendCalendarEventColumns';
29import {
30  ExtendedPropertiesColumns
31} from '@ohos/datastructure/src/main/ets/extendedproperties/ExtendedPropertiesColumns';
32import { SyncStateColumns } from '@ohos/datastructure/src/main/ets/syncstate/SyncStateColumns';
33import { MapAddressHistoryColumns } from '@ohos/datastructure/src/main/ets/location/MapAddressHistoryColumns';
34import { SyncStateMetadataColumns } from '@ohos/datastructure/src/main/ets/syncstate/SyncStateMetadataColumns';
35import { CalendarMetaDataColumns } from '@ohos/datastructure/src/main/ets/calendarmetadata/CalendarMetaDataColumns';
36import { QUERY_START } from '@ohos/common/src/main/ets/utils/UrlUtils';
37import { Log } from '@ohos/common/src/main/ets/utils/Log';
38
39const DATA_SHARE_PREFIX: string = "datashare:///calendardata";
40
41const EVENTS_PATH: string = "Events";
42
43const CALENDARS_PATH: string = "Calendars";
44
45const ATTENDEES_PATH: string = "Attendees";
46
47const CALENDAR_ALERTS_PATH: string = "CalendarAlerts";
48
49const CALENDAR_CACHE_PATH: string = "CalendarCache";
50
51const CALENDAR_METADATA_PATH: string = "CalendarMetaData";
52
53const COLORS_PATH: string = "Colors";
54
55const EVENTS_RAW_TIME_PATH: string = "EventsRawTimes";
56
57const EXTEND_CALENDAR_EVENT_PATH: string = "ExtendCalendarEvent";
58
59const EXTENDED_PROPERTIES_PATH: string = "ExtendedProperties";
60
61export const INSTANCES_PATH: string = "Instances";
62
63const MAP_ADDRESS_HISTORY_PATH: string = "map_address_history";
64
65const REMINDERS_PATH: string = "Reminders";
66
67const SYNC_STATE_PATH: string = "_sync_state";
68
69const SYNC_STATE_METADATA_PATH: string = "_sync_state_metadata";
70
71export const URI_EVENTS: string = `${DATA_SHARE_PREFIX}/${EVENTS_PATH}`;
72
73const TAG = "CalendarUriHelper";
74
75/**
76 * transfer the user input uri to the specific table
77 *
78 * @param uri the user input uri
79 * @return the specific table
80 */
81export default function getTableByUri(uri: string): string {
82  Log.log(TAG, 'getTableByUri uri: ' + uri);
83  let path = getPathByUri(uri);
84  let tablePathName = getTablePathNameByPath(path);
85  return getTableNameByTablePath(tablePathName);
86}
87
88/**
89 * transfer the table's path to the specific table name
90 *
91 * @param table's path that is got from user input uri
92 * @return the specific table name
93 */
94function getTableNameByTablePath(tablePathName: string): string {
95  switch (tablePathName) {
96    case EVENTS_PATH:
97      return EventColumns.TABLE_NAME;
98    case CALENDARS_PATH:
99      return CalendarsColumns.TABLE_NAME;
100    case ATTENDEES_PATH:
101      return AttendeesColumns.TABLE_NAME;
102    case CALENDAR_ALERTS_PATH:
103      return CalendarAlertsColumns.TABLE_NAME;
104    case CALENDAR_CACHE_PATH:
105      return CalendarCacheColumns.TABLE_NAME;
106    case CALENDAR_METADATA_PATH:
107      return CalendarMetaDataColumns.TABLE_NAME;
108    case COLORS_PATH:
109      return ColorsColumns.TABLE_NAME;
110    case EVENTS_RAW_TIME_PATH:
111      return EventsRawTimesColumns.TABLE_NAME;
112    case EXTEND_CALENDAR_EVENT_PATH:
113      return ExtendCalendarEventColumns.TABLE_NAME;
114    case EXTENDED_PROPERTIES_PATH:
115      return ExtendedPropertiesColumns.TABLE_NAME;
116    case INSTANCES_PATH:
117      return InstancesColumns.TABLE_NAME;
118    case MAP_ADDRESS_HISTORY_PATH:
119      return MapAddressHistoryColumns.TABLE_NAME;
120    case REMINDERS_PATH:
121      return RemindersColumns.TABLE_NAME;
122    case SYNC_STATE_PATH:
123      return SyncStateColumns.TABLE_NAME;
124    case SYNC_STATE_METADATA_PATH:
125      return SyncStateMetadataColumns.TABLE_NAME;
126    default:
127      Log.log(TAG, 'getTablePathByTableName run in default case!');
128      return "";
129  }
130}
131
132/**
133 * transfer the user input uri to the specific resource path
134 *
135 * @param table's path that is got from user input uri
136 * @return the resource path
137 */
138function getPathByUri(uri: string): string {
139  // delete dataShare's prefix
140  if (uri.startsWith(DATA_SHARE_PREFIX)) {
141    uri = uri.split(DATA_SHARE_PREFIX).join("");
142  }
143
144  let endIndex = uri.indexOf(QUERY_START);
145  const totalLength = uri.length;
146  if (endIndex === -1 && totalLength > 0) {
147    return uri;
148  }
149
150  // in case of uri containing query condition like Instance's uri
151  if (endIndex < 1 || endIndex >= totalLength) {
152    return '';
153  }
154  const path = uri.substring(0, endIndex);
155  if (path === null || path === undefined || path.length < 1) {
156    return '';
157  }
158  return path;
159}
160
161/**
162 * transfer uri without prefix to the table's path
163 *
164 * @param user input uri without prefix
165 * @return the table path name
166 */
167function getTablePathNameByPath(path: string): string {
168  const array = path.split("/");
169  if (array === null || array === undefined || array.length < 1) {
170    return '';
171  }
172  return array[1];
173}
174