1 /*
2  * Copyright (C) 2024 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 "tab_old_photos_restore.h"
17 
18 #include <string>
19 #include <vector>
20 #include <numeric>
21 #include <algorithm>
22 
23 #include "backup_const.h"
24 #include "media_log.h"
25 
26 namespace OHOS::Media {
Restore(std::shared_ptr<NativeRdb::RdbStore> & rdbStorePtr,const std::vector<FileInfo> fileInfos)27 int32_t TabOldPhotosRestore::Restore(
28     std::shared_ptr<NativeRdb::RdbStore> &rdbStorePtr, const std::vector<FileInfo> fileInfos)
29 {
30     if (rdbStorePtr ==  nullptr) {
31         MEDIA_ERR_LOG("rdbStorePtr is nullptr, Maybe init failed");
32         return NativeRdb::E_OK;
33     }
34     for (const auto &fileInfo : fileInfos) {
35         std::string executeSql = this->SQL_TAB_OLD_PHOTOS_INSERT;
36         std::vector<NativeRdb::ValueObject> bindArgs = { fileInfo.fileIdOld, fileInfo.oldPath, fileInfo.cloudPath };
37         int32_t ret = rdbStorePtr->ExecuteSql(executeSql, bindArgs);
38         if (ret != NativeRdb::E_OK) {
39             MEDIA_ERR_LOG("Media_Restore: TabOldPhotosRestore failed, ret=%{public}d, "
40                           "executeSql=%{public}s, bindArgs: %{public}s, Object: %{public}s",
41                 ret,
42                 executeSql.c_str(),
43                 this->ToString(bindArgs).c_str(),
44                 this->ToString(fileInfo).c_str());
45         }
46     }
47     return NativeRdb::E_OK;
48 }
49 
ToString(const std::vector<NativeRdb::ValueObject> & values)50 std::string TabOldPhotosRestore::ToString(const std::vector<NativeRdb::ValueObject> &values)
51 {
52     std::vector<std::string> result;
53     for (auto &value : values) {
54         std::string str;
55         value.GetString(str);
56         result.emplace_back(str + ", ");
57     }
58     return std::accumulate(result.begin(), result.end(), std::string());
59 }
60 
ToString(const FileInfo & fileInfo)61 std::string TabOldPhotosRestore::ToString(const FileInfo &fileInfo)
62 {
63     return "FileInfo[ fileId: " + std::to_string(fileInfo.fileIdOld) + ", displayName: " + fileInfo.displayName +
64         ", bundleName: " + std::to_string(fileInfo.fileSize) + ", fileType: " +
65         std::to_string(fileInfo.fileType) + " ]";
66 }
67 } // namespace OHOS::Media