• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024-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 #define MLOG_TAG "StoryOperation"
16 
17 #include "medialibrary_story_operations.h"
18 
19 #include "media_log.h"
20 #include "medialibrary_db_const.h"
21 #include "medialibrary_errno.h"
22 #include "medialibrary_unistore_manager.h"
23 #include "medialibrary_data_manager_utils.h"
24 #include "medialibrary_notify.h"
25 #include "media_file_utils.h"
26 #include "photo_album_column.h"
27 #include "result_set_utils.h"
28 #include "story_album_column.h"
29 #include "vision_column_comm.h"
30 
31 using namespace std;
32 using namespace OHOS::NativeRdb;
33 
34 namespace OHOS {
35 namespace Media {
GetHighlightId(const string & whereClause,const vector<string> & whereArgs)36 static int32_t GetHighlightId(const string &whereClause, const vector<string> &whereArgs)
37 {
38     if (whereArgs.empty()) {
39         MEDIA_ERR_LOG("whereArgs is empty");
40         return E_HAS_DB_ERROR;
41     }
42     size_t pos = whereClause.find(ID);
43     if (pos == string::npos) {
44         MEDIA_ERR_LOG("whereClause is invalid");
45         return E_HAS_DB_ERROR;
46     }
47     size_t argsIndex = 0;
48     for (size_t i = 0; i < pos; i++) {
49         if (whereClause[i] == '?') {
50             argsIndex++;
51         }
52     }
53     if (argsIndex > whereArgs.size() - 1) {
54         MEDIA_ERR_LOG("whereArgs is invalid");
55         return E_HAS_DB_ERROR;
56     }
57     auto albumId = whereArgs[argsIndex];
58     if (MediaLibraryDataManagerUtils::IsNumber(albumId)) {
59         return atoi(albumId.c_str());
60     }
61     return E_HAS_DB_ERROR;
62 }
63 
GetHighlightAlbumId(const string & id,int32_t & albumId)64 static void GetHighlightAlbumId(const string &id, int32_t &albumId)
65 {
66     auto rdbStore = MediaLibraryUnistoreManager::GetInstance().GetRdbStore();
67     CHECK_AND_RETURN_LOG(rdbStore != nullptr, "rdbStore is nullptr!");
68     const std::string queryAlbumId = "SELECT album_id FROM " + HIGHLIGHT_ALBUM_TABLE + " WHERE id = " + id;
69     shared_ptr<NativeRdb::ResultSet> resultSet = rdbStore->QuerySql(queryAlbumId);
70     CHECK_AND_RETURN_LOG(resultSet != nullptr, "resultSet is nullptr on get highlight album_id");
71     CHECK_AND_EXECUTE(resultSet->GoToNextRow() != NativeRdb::E_OK,
72         albumId = GetInt32Val(PhotoAlbumColumns::ALBUM_ID, resultSet));
73     resultSet->Close();
74 }
75 
NotifyStoryAlbum(MediaLibraryCommand & cmd)76 static void NotifyStoryAlbum(MediaLibraryCommand &cmd)
77 {
78     auto whereClause = cmd.GetAbsRdbPredicates()->GetWhereClause();
79     auto whereArgs = cmd.GetAbsRdbPredicates()->GetWhereArgs();
80     auto id = GetHighlightId(whereClause, whereArgs);
81     CHECK_AND_RETURN_LOG(id > 0, "highlight id invalid");
82     int32_t albumId = 0;
83     GetHighlightAlbumId(to_string(id), albumId);
84     CHECK_AND_RETURN_LOG(albumId > 0, "highlight album_id invalid");
85     MEDIA_INFO_LOG("NotifyStoryAlbum, album id is %{public}d", albumId);
86 
87     auto watch = MediaLibraryNotify::GetInstance();
88     CHECK_AND_RETURN_LOG(watch != nullptr, "Can not get MediaLibraryNotify Instance");
89     watch->Notify(MediaFileUtils::GetUriByExtrConditions(
90         PhotoAlbumColumns::ANALYSIS_ALBUM_URI_PREFIX, to_string(albumId)), NotifyType::NOTIFY_UPDATE);
91 }
92 
InsertOperation(MediaLibraryCommand & cmd)93 int32_t MediaLibraryStoryOperations::InsertOperation(MediaLibraryCommand &cmd)
94 {
95     auto rdbStore = MediaLibraryUnistoreManager::GetInstance().GetRdbStore();
96     if (rdbStore == nullptr) {
97         MEDIA_ERR_LOG("Story insert operation, rdbStore is null.");
98         return E_HAS_DB_ERROR;
99     }
100     int64_t outRowId = -1;
101     int32_t errCode = rdbStore->Insert(cmd, outRowId);
102     if (errCode != NativeRdb::E_OK || outRowId < 0) {
103         MEDIA_ERR_LOG("Story Insert into db failed, errCode = %{public}d", errCode);
104         return E_HAS_DB_ERROR;
105     }
106     NotifyStoryAlbum(cmd);
107     return static_cast<int32_t>(outRowId);
108 }
109 
UpdateOperation(MediaLibraryCommand & cmd)110 int32_t MediaLibraryStoryOperations::UpdateOperation(MediaLibraryCommand &cmd)
111 {
112     auto rdbStore = MediaLibraryUnistoreManager::GetInstance().GetRdbStore();
113     if (rdbStore == nullptr) {
114         MEDIA_ERR_LOG("Story update operation, rdbStore is null.");
115         return E_HAS_DB_ERROR;
116     }
117     int32_t updateRows = -1;
118     int32_t errCode = rdbStore->Update(cmd, updateRows);
119     if (errCode != NativeRdb::E_OK || updateRows < 0) {
120         MEDIA_ERR_LOG("Story Update db failed, errCode = %{public}d", errCode);
121         return E_HAS_DB_ERROR;
122     }
123     NotifyStoryAlbum(cmd);
124     return static_cast<int32_t>(updateRows);
125 }
126 
DeleteOperation(MediaLibraryCommand & cmd)127 int32_t MediaLibraryStoryOperations::DeleteOperation(MediaLibraryCommand &cmd)
128 {
129     auto rdbStore = MediaLibraryUnistoreManager::GetInstance().GetRdbStore();
130     if (rdbStore == nullptr) {
131         MEDIA_ERR_LOG("Story delete operation, rdbStore is null.");
132         return E_HAS_DB_ERROR;
133     }
134     int32_t deleteRows = -1;
135     int32_t errCode = rdbStore->Delete(cmd, deleteRows);
136     if (errCode != NativeRdb::E_OK || deleteRows < 0) {
137         MEDIA_ERR_LOG("Story Delete db failed, errCode = %{public}d", errCode);
138         return E_HAS_DB_ERROR;
139     }
140     return static_cast<int32_t>(deleteRows);
141 }
142 
QueryOperation(MediaLibraryCommand & cmd,const std::vector<std::string> & columns)143 shared_ptr<NativeRdb::ResultSet> MediaLibraryStoryOperations::QueryOperation(MediaLibraryCommand &cmd,
144     const std::vector<std::string> &columns)
145 {
146     auto rdbStore = MediaLibraryUnistoreManager::GetInstance().GetRdbStore();
147     if (rdbStore == nullptr) {
148         MEDIA_ERR_LOG("Story query operation, rdbStore is null.");
149         return nullptr;
150     }
151     return rdbStore->Query(cmd, columns);
152 }
153 } // namespace Media
154 } // namespace OHOS
155