10fbfc30aSopenharmony_ci/*
20fbfc30aSopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd.
30fbfc30aSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
40fbfc30aSopenharmony_ci * you may not use this file except in compliance with the License.
50fbfc30aSopenharmony_ci * You may obtain a copy of the License at
60fbfc30aSopenharmony_ci *
70fbfc30aSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
80fbfc30aSopenharmony_ci *
90fbfc30aSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
100fbfc30aSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
110fbfc30aSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
120fbfc30aSopenharmony_ci * See the License for the specific language governing permissions and
130fbfc30aSopenharmony_ci * limitations under the License.
140fbfc30aSopenharmony_ci */
150fbfc30aSopenharmony_ci
160fbfc30aSopenharmony_ci#include "native_calendar_manager.h"
170fbfc30aSopenharmony_ci#include "data_share_helper_manager.h"
180fbfc30aSopenharmony_ci#include "calendar_env.h"
190fbfc30aSopenharmony_ci#include "calendar_log.h"
200fbfc30aSopenharmony_ci#include "native_util.h"
210fbfc30aSopenharmony_ci
220fbfc30aSopenharmony_cinamespace OHOS::CalendarApi::Native {
230fbfc30aSopenharmony_ciDataShare::DataSharePredicates BuildCalendarFilter(const CalendarAccount& account);
240fbfc30aSopenharmony_ciconst CalendarAccount defaultAccount {"phone", "local", ""};
250fbfc30aSopenharmony_ciconst string calendarUrl = "datashare:///calendardata/Calendars";
260fbfc30aSopenharmony_ci
270fbfc30aSopenharmony_ciCalendarManager::CalendarManager()
280fbfc30aSopenharmony_ci{
290fbfc30aSopenharmony_ci    uint64_t tokenId = CalendarEnv::GetInstance().GetTokenId();
300fbfc30aSopenharmony_ci    auto bumdleName = CalendarEnv::GetInstance().GetBundleName();
310fbfc30aSopenharmony_ci    auto bundleName_tokeId = "?bundleName=" + bumdleName + "&tokenId=" + std::to_string(tokenId);
320fbfc30aSopenharmony_ci    m_calendarUri = std::make_unique<Uri>(calendarUrl + bundleName_tokeId);
330fbfc30aSopenharmony_ci}
340fbfc30aSopenharmony_ci
350fbfc30aSopenharmony_ciCalendarManager &CalendarManager::GetInstance()
360fbfc30aSopenharmony_ci{
370fbfc30aSopenharmony_ci    static CalendarManager instance;
380fbfc30aSopenharmony_ci    return instance;
390fbfc30aSopenharmony_ci}
400fbfc30aSopenharmony_ci
410fbfc30aSopenharmony_cibool CalendarManager::IsDefaultAccount(const CalendarAccount &account)
420fbfc30aSopenharmony_ci{
430fbfc30aSopenharmony_ci    return account.name == defaultAccount.name && account.type == defaultAccount.type;
440fbfc30aSopenharmony_ci}
450fbfc30aSopenharmony_ci
460fbfc30aSopenharmony_ciauto BuildValueCalendarAccount(const CalendarAccount &account)
470fbfc30aSopenharmony_ci{
480fbfc30aSopenharmony_ci    DataShare::DataShareValuesBucket valuesBucket;
490fbfc30aSopenharmony_ci    valuesBucket.Put("account_name", account.name);
500fbfc30aSopenharmony_ci    valuesBucket.Put("account_type", account.type);
510fbfc30aSopenharmony_ci    if (account.displayName) {
520fbfc30aSopenharmony_ci        valuesBucket.Put("calendar_displayName", account.displayName.value());
530fbfc30aSopenharmony_ci    }
540fbfc30aSopenharmony_ci
550fbfc30aSopenharmony_ci    return valuesBucket;
560fbfc30aSopenharmony_ci}
570fbfc30aSopenharmony_ci
580fbfc30aSopenharmony_cistd::shared_ptr<Calendar> CalendarManager::CreateCalendar(const CalendarAccount& account)
590fbfc30aSopenharmony_ci{
600fbfc30aSopenharmony_ci    auto valueEvent = BuildValueCalendarAccount(account);
610fbfc30aSopenharmony_ci    auto index = DataShareHelperManager::GetInstance().Insert(*(m_calendarUri.get()), valueEvent);
620fbfc30aSopenharmony_ci    LOG_DEBUG("Insert index %{public}d", index);
630fbfc30aSopenharmony_ci    if (index <= 0) {
640fbfc30aSopenharmony_ci        LOG_ERROR("Insert failed");
650fbfc30aSopenharmony_ci        return nullptr;
660fbfc30aSopenharmony_ci    }
670fbfc30aSopenharmony_ci    return std::make_shared<Calendar>(account, index);
680fbfc30aSopenharmony_ci}
690fbfc30aSopenharmony_ci
700fbfc30aSopenharmony_ci
710fbfc30aSopenharmony_ciDataShare::DataSharePredicates BuildCalendarFilter(const CalendarAccount& account)
720fbfc30aSopenharmony_ci{
730fbfc30aSopenharmony_ci    DumpCalendarAccount(account);
740fbfc30aSopenharmony_ci    DataShare::DataSharePredicates predicates;
750fbfc30aSopenharmony_ci    predicates.EqualTo("account_name", account.name);
760fbfc30aSopenharmony_ci    predicates.And();
770fbfc30aSopenharmony_ci    predicates.EqualTo("account_type", account.type);
780fbfc30aSopenharmony_ci    return predicates;
790fbfc30aSopenharmony_ci}
800fbfc30aSopenharmony_ci
810fbfc30aSopenharmony_cistd::shared_ptr<Calendar> CalendarManager::GetCalendar(const std::optional<CalendarAccount>& account)
820fbfc30aSopenharmony_ci{
830fbfc30aSopenharmony_ci    DataShare::DataSharePredicates predicates;
840fbfc30aSopenharmony_ci    if (account) {
850fbfc30aSopenharmony_ci        predicates = BuildCalendarFilter(account.value());
860fbfc30aSopenharmony_ci    } else {
870fbfc30aSopenharmony_ci        LOG_WARN("get defaultAccount");
880fbfc30aSopenharmony_ci        predicates = BuildCalendarFilter(defaultAccount);
890fbfc30aSopenharmony_ci    }
900fbfc30aSopenharmony_ci    std::vector<std::string> columns = {"_id", "account_name", "account_type", "calendar_displayName"};
910fbfc30aSopenharmony_ci    DataShare::DatashareBusinessError error;
920fbfc30aSopenharmony_ci    auto resultSet = DataShareHelperManager::GetInstance().Query(*(m_calendarUri.get()), predicates, columns, &error);
930fbfc30aSopenharmony_ci    if (!resultSet) {
940fbfc30aSopenharmony_ci        LOG_ERROR("query failed %{public}d, %{public}s", error.GetCode(), error.GetMessage().c_str());
950fbfc30aSopenharmony_ci        return nullptr;
960fbfc30aSopenharmony_ci    }
970fbfc30aSopenharmony_ci    auto calendarSet = ResultSetToCalendars(resultSet);
980fbfc30aSopenharmony_ci    if (calendarSet.empty()) {
990fbfc30aSopenharmony_ci        LOG_WARN("calendarSet empty");
1000fbfc30aSopenharmony_ci        return std::make_shared<Calendar>(-1);
1010fbfc30aSopenharmony_ci    }
1020fbfc30aSopenharmony_ci    LOG_INFO("GetCalendar successed");
1030fbfc30aSopenharmony_ci    return std::move(calendarSet.at(0));
1040fbfc30aSopenharmony_ci}
1050fbfc30aSopenharmony_ci
1060fbfc30aSopenharmony_cistd::vector<std::shared_ptr<Calendar>> CalendarManager::GetAllCalendars()
1070fbfc30aSopenharmony_ci{
1080fbfc30aSopenharmony_ci    std::vector<std::shared_ptr<Calendar>> results;
1090fbfc30aSopenharmony_ci    DataShare::DataSharePredicates predicates;
1100fbfc30aSopenharmony_ci    std::vector<std::string> columns = {"_id", "account_name", "account_type", "calendar_displayName"};
1110fbfc30aSopenharmony_ci    DataShare::DatashareBusinessError error;
1120fbfc30aSopenharmony_ci    auto queryResult = DataShareHelperManager::GetInstance().Query(*(m_calendarUri.get()), predicates, columns, &error);
1130fbfc30aSopenharmony_ci    if (!queryResult) {
1140fbfc30aSopenharmony_ci        LOG_ERROR("query failed %{public}d, %{public}s", error.GetCode(), error.GetMessage().c_str());
1150fbfc30aSopenharmony_ci        return results;
1160fbfc30aSopenharmony_ci    }
1170fbfc30aSopenharmony_ci    return ResultSetToCalendars(queryResult);
1180fbfc30aSopenharmony_ci}
1190fbfc30aSopenharmony_ci
1200fbfc30aSopenharmony_ci
1210fbfc30aSopenharmony_cibool CalendarManager::DeleteCalendar(const Calendar& calendar)
1220fbfc30aSopenharmony_ci{
1230fbfc30aSopenharmony_ci    DataShare::DataSharePredicates predicates;
1240fbfc30aSopenharmony_ci    predicates.EqualTo("_id", calendar.GetId());
1250fbfc30aSopenharmony_ci    auto result = DataShareHelperManager::GetInstance().Delete(*(m_calendarUri.get()), predicates);
1260fbfc30aSopenharmony_ci    LOG_INFO("DeleteCalendar %{public}d", result);
1270fbfc30aSopenharmony_ci    return result == 1;
1280fbfc30aSopenharmony_ci}
1290fbfc30aSopenharmony_ci
1300fbfc30aSopenharmony_ciint CalendarManager::DeleteAllCalendars()
1310fbfc30aSopenharmony_ci{
1320fbfc30aSopenharmony_ci    auto calendars = GetAllCalendars();
1330fbfc30aSopenharmony_ci    int count = 0;
1340fbfc30aSopenharmony_ci    for (const auto &calendar: calendars) {
1350fbfc30aSopenharmony_ci        if (DeleteCalendar(*calendar.get())) {
1360fbfc30aSopenharmony_ci            count +=1;
1370fbfc30aSopenharmony_ci        }
1380fbfc30aSopenharmony_ci    }
1390fbfc30aSopenharmony_ci    return count;
1400fbfc30aSopenharmony_ci}
1410fbfc30aSopenharmony_ci
1420fbfc30aSopenharmony_ci}
143