1 /*
2 * Copyright (c) 2023 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
16 #include "cycle_task.h"
17 #include <memory>
18 #include "dfs_error.h"
19 #include "gallery_data_sync/data_convertor.h"
20 #include "data_syncer.h"
21 #include "data_sync_manager.h"
22 #include "data_syncer_rdb_col.h"
23 #include "data_syncer_rdb_store.h"
24 #include "cycle_task_runner.h"
25 #include "tasks/save_subscription_task.h"
26 #include "utils_log.h"
27
28 namespace OHOS {
29 namespace FileManagement {
30 namespace CloudSync {
SaveSubscriptionTask(std::shared_ptr<DataSyncManager> dataSyncManager)31 SaveSubscriptionTask::SaveSubscriptionTask(std::shared_ptr<DataSyncManager> dataSyncManager)
32 : CycleTask("save_subscription_task", "", CycleTaskRunner::ONE_DAY, dataSyncManager) {}
33
RunTask(int32_t userId)34 int32_t SaveSubscriptionTask::RunTask(int32_t userId)
35 {
36 std::shared_ptr<NativeRdb::ResultSet> resultSet = nullptr;
37 RETURN_ON_ERR(DataSyncerRdbStore::GetInstance().QueryDataSyncer(userId, resultSet));
38
39 while (resultSet->GoToNextRow() == E_OK) {
40 int32_t ret = DataConvertor::GetString(BUNDLE_NAME, bundleName_, *resultSet);
41 if (ret != E_OK) {
42 LOGE("get bundle name failed");
43 continue;
44 }
45 auto dataSyncer = dataSyncManager_->GetDataSyncer(bundleName_, userId);
46 if (!dataSyncer) {
47 LOGE("Get dataSyncer failed, bundleName: %{private}s", bundleName_.c_str());
48 return E_INVAL_ARG;
49 }
50 ret = dataSyncManager_->InitSdk(userId, bundleName_, dataSyncer);
51 if (ret != E_OK) {
52 return ret;
53 }
54 if (!dataSyncer) {
55 LOGE("Get dataSyncer failed, bundleName: %{private}s", bundleName_.c_str());
56 return E_INVAL_ARG;
57 }
58 dataSyncer->SaveSubscription();
59 }
60 return E_OK;
61 }
62
63 } // namespace CloudSync
64 } // namespace FileManagement
65 } // namespace OHOS