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 #define MLOG_TAG "PhotoDisplayNameOperation"
16 
17 #include "photo_displayname_operation.h"
18 
19 #include "display_name_info.h"
20 
21 namespace OHOS::Media {
SetTargetPhotoInfo(const PhotoAssetInfo & photoAssetInfo)22 PhotoDisplayNameOperation &PhotoDisplayNameOperation::SetTargetPhotoInfo(const PhotoAssetInfo &photoAssetInfo)
23 {
24     this->photoAssetInfo_ = photoAssetInfo;
25     return *this;
26 }
27 
FindDisplayName(const std::shared_ptr<MediaLibraryRdbStore> & rdbStore)28 std::string PhotoDisplayNameOperation::FindDisplayName(const std::shared_ptr<MediaLibraryRdbStore> &rdbStore)
29 {
30     return this->FindDislayName(rdbStore, this->photoAssetInfo_);
31 }
32 
FindDislayName(const std::shared_ptr<MediaLibraryRdbStore> & rdbStore,const PhotoAssetInfo & photoAssetInfo)33 std::string PhotoDisplayNameOperation::FindDislayName(
34     const std::shared_ptr<MediaLibraryRdbStore> &rdbStore, const PhotoAssetInfo &photoAssetInfo)
35 {
36     if (photoAssetInfo.displayName.empty() || rdbStore == nullptr) {
37         MEDIA_ERR_LOG("Media_Operation: displayName is empty. Object: %{public}s", photoAssetInfo.ToString().c_str());
38         return photoAssetInfo.displayName;
39     }
40     DisplayNameInfo displayNameInfo(photoAssetInfo);
41     std::string displayName = displayNameInfo.ToString();
42     int32_t retryCount = 0;
43     const int32_t MAX_RETRY_COUNT = 100;
44     while (IsDisplayNameExists(rdbStore, photoAssetInfo.ownerAlbumId, displayName)) {
45         displayName = displayNameInfo.Next();
46         if (retryCount++ > MAX_RETRY_COUNT) {
47             MEDIA_ERR_LOG("Media_Operation: can not find unique display after retry %{public}d", MAX_RETRY_COUNT);
48             break;
49         }
50     }
51     if (photoAssetInfo.displayName != displayName) {
52         MEDIA_INFO_LOG("Media_Operation: displayName changed from %{public}s to %{public}s",
53             photoAssetInfo.ToString().c_str(),
54             displayName.c_str());
55     }
56     return displayName;
57 }
58 
IsDisplayNameExists(const std::shared_ptr<MediaLibraryRdbStore> rdbStore,const int32_t ownerAlbumId,const std::string & displayName)59 bool PhotoDisplayNameOperation::IsDisplayNameExists(
60     const std::shared_ptr<MediaLibraryRdbStore> rdbStore, const int32_t ownerAlbumId, const std::string &displayName)
61 {
62     if (ownerAlbumId <= 0 || displayName.empty() || rdbStore == nullptr) {
63         return false;
64     }
65     std::string querySql = this->SQL_PHOTOS_TABLE_QUERY_DISPLAY_NAME;
66     const std::vector<NativeRdb::ValueObject> bindArgs = {ownerAlbumId, displayName};
67     auto resultSet = rdbStore->QuerySql(querySql, bindArgs);
68     if (resultSet == nullptr || resultSet->GoToFirstRow() != NativeRdb::E_OK) {
69         return false;
70     }
71     std::string displayNameInDb = GetStringVal(MediaColumn::MEDIA_NAME, resultSet);
72     return displayNameInDb.size() > 0;
73 }
74 }  // namespace OHOS::Media