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 #include <vector>
16 #include <string>
17 #include <unordered_set>
18 
19 #include "photo_album_restore.h"
20 #include "media_log.h"
21 #include "rdb_store.h"
22 #include "rdb_errno.h"
23 #include "result_set_utils.h"
24 #include "userfile_manager_types.h"
25 #include "backup_const.h"
26 #include "photo_album_dao.h"
27 
28 namespace OHOS::Media {
29 /**
30  * @brief get the data of gallery_album table, to restore the PhotoAlbum table
31  */
GetGalleryAlbums()32 std::vector<PhotoAlbumRestore::GalleryAlbumRowData> PhotoAlbumRestore::GetGalleryAlbums()
33 {
34     std::vector<PhotoAlbumRestore::GalleryAlbumRowData> result;
35     std::string querySql = this->SQL_GALLERY_ALBUM_SELECT;
36     int rowCount = 0;
37     int offset = 0;
38     int pageSize = 200;
39     do {
40         std::vector<NativeRdb::ValueObject> params = {offset, pageSize};
41         if (this->galleryRdb_ == nullptr) {
42             MEDIA_ERR_LOG("Media_Restore: galleryRdb_ is null.");
43             break;
44         }
45         auto resultSet = this->galleryRdb_->QuerySql(querySql, params);
46         if (resultSet == nullptr) {
47             MEDIA_ERR_LOG("Query resultSql is null.");
48             break;
49         }
50         while (resultSet->GoToNextRow() == NativeRdb::E_OK) {
51             PhotoAlbumRestore::GalleryAlbumRowData albumInfo;
52             albumInfo.albumId = GetStringVal(this->GALLERY_ALBUM_ID, resultSet);
53             albumInfo.albumName = GetStringVal(this->GALLERY_ALBUM_NAME, resultSet);
54             albumInfo.relativeBucketId = GetStringVal(this->GALLERY_ALBUM_BUCKET_ID, resultSet);
55             albumInfo.lPath = GetStringVal(this->GALLERY_ALBUM_lPATH, resultSet);
56             albumInfo.bundleName = GetStringVal(this->GALLERY_BUNDLE_NAME, resultSet);
57             albumInfo.priority = GetInt32Val(this->GALLERY_PRIORITY, resultSet);
58             result.emplace_back(albumInfo);
59         }
60         // Check if there are more rows to fetch.
61         resultSet->GetRowCount(rowCount);
62         offset += pageSize;
63     } while (rowCount > 0);
64     return result;
65 }
66 
67 /**
68  * @brief Find the albums that need to be restored.
69  */
GetAlbumsToRestore(const std::vector<PhotoAlbumDao::PhotoAlbumRowData> & photoAlbums,const std::vector<PhotoAlbumRestore::GalleryAlbumRowData> & galleryAlbums)70 std::vector<PhotoAlbumDao::PhotoAlbumRowData> PhotoAlbumRestore::GetAlbumsToRestore(
71     const std::vector<PhotoAlbumDao::PhotoAlbumRowData> &photoAlbums,
72     const std::vector<PhotoAlbumRestore::GalleryAlbumRowData> &galleryAlbums)
73 {
74     // cache the lPath of the albums in the gallery_media table
75     std::unordered_set<std::string> uniquePaths;
76     for (const auto &album : photoAlbums) {
77         uniquePaths.insert(album.lPath);
78     }
79     // filter gallery_media by lPath, find the albums that need to be restored
80     // Note 1, the album with the same lPath is considered to be the same album.
81     // Note 2, the lPath is case-sensitively matched here,
82     // for case-insensitive matching is performed in the SQL excution at the end.
83     std::vector<PhotoAlbumRestore::GalleryAlbumRowData> filteredAlbums;
84     std::copy_if(galleryAlbums.begin(),
85         galleryAlbums.end(),
86         std::back_inserter(filteredAlbums),
87         [&uniquePaths](const PhotoAlbumRestore::GalleryAlbumRowData &album) {
88             return uniquePaths.find(album.lPath) == uniquePaths.end();
89         });
90     // build the result
91     std::vector<PhotoAlbumDao::PhotoAlbumRowData> result;
92     for (const PhotoAlbumRestore::GalleryAlbumRowData &galleryAlbum : filteredAlbums) {
93         PhotoAlbumDao::PhotoAlbumRowData albumRowData;
94         albumRowData.albumType = static_cast<int32_t>(PhotoAlbumType::SOURCE);
95         albumRowData.albumSubType = static_cast<int32_t>(PhotoAlbumSubType::SOURCE_GENERIC);
96         albumRowData.albumName = galleryAlbum.albumName;
97         albumRowData.bundleName = galleryAlbum.bundleName;
98         albumRowData.lPath = galleryAlbum.lPath;
99         albumRowData.priority = galleryAlbum.priority;
100         result.emplace_back(albumRowData);
101     }
102     return result;
103 }
104 }  // namespace OHOS::Media