1 /*
2 * Copyright (C) 2021 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 "medialibrary_smartalbum_operations.h"
17 #include "medialibrary_object_utils.h"
18 #include "medialibrary_errno.h"
19 #include "media_file_utils.h"
20 #include "media_log.h"
21 #include "media_smart_album_column.h"
22 #include "media_smart_map_column.h"
23
24 using namespace std;
25 using namespace OHOS::NativeRdb;
26 using namespace OHOS::DataShare;
27
28 namespace OHOS {
29 namespace Media {
30 constexpr int32_t DEFAULT_SMARTID = -1;
CreateSmartAlbumOperation(MediaLibraryCommand & cmd)31 int32_t MediaLibrarySmartAlbumOperations::CreateSmartAlbumOperation(MediaLibraryCommand &cmd)
32 {
33 ValueObject valueObject;
34 if (!cmd.GetValueBucket().GetObject(SMARTALBUMMAP_DB_ALBUM_ID, valueObject)) {
35 MEDIA_ERR_LOG("Failed to get parentAlbumId");
36 return E_HAS_DB_ERROR;
37 }
38 int32_t parentAlbumId = DEFAULT_SMARTID;
39 valueObject.GetInt(parentAlbumId);
40 if (parentAlbumId > 0 && !MediaLibraryObjectUtils::IsSmartAlbumExistInDb(parentAlbumId)) {
41 MEDIA_ERR_LOG("Failed to get parent smart album, parentAlbumId = %{public}d", parentAlbumId);
42 return E_PARENT_SMARTALBUM_IS_NOT_EXISTED;
43 }
44 if (!cmd.GetValueBucket().GetObject(SMARTALBUM_DB_NAME, valueObject)) {
45 MEDIA_ERR_LOG("Failed to get albumName");
46 return E_HAS_DB_ERROR;
47 }
48 string albumName;
49 valueObject.GetString(albumName);
50 if (MediaFileUtils::CheckAlbumName(albumName) < 0) {
51 MEDIA_ERR_LOG("Smart album invalid, albumName = %{private}s", albumName.c_str());
52 return E_INVALID_VALUES;
53 }
54 ValuesBucket valuebucket;
55 valuebucket.PutString(SMARTALBUM_DB_NAME, albumName);
56 cmd.SetValueBucket(valuebucket);
57 return MediaLibraryObjectUtils::InsertInDb(cmd);
58 }
59
DeleteSmartAlbumOperation(MediaLibraryCommand & cmd)60 int32_t MediaLibrarySmartAlbumOperations::DeleteSmartAlbumOperation(MediaLibraryCommand &cmd)
61 {
62 ValueObject valueObject;
63 if (!cmd.GetValueBucket().GetObject(SMARTALBUM_DB_ID, valueObject)) {
64 MEDIA_ERR_LOG("Failed to get albumId");
65 return E_HAS_DB_ERROR;
66 }
67 int32_t albumId = DEFAULT_SMARTID;
68 valueObject.GetInt(albumId);
69 CHECK_AND_RETURN_RET_LOG(albumId > 0, E_GET_VALUEBUCKET_FAIL, "Failed to get smartAlbumId");
70 if (MediaLibraryObjectUtils::IsParentSmartAlbum(albumId)) {
71 return E_PARENT_SMARTALBUM_CAN_NOT_DELETE;
72 }
73 MediaLibraryCommand smartAlbumMapCmd(OperationObject::SMART_ALBUM_MAP, OperationType::DELETE);
74 smartAlbumMapCmd.GetAbsRdbPredicates()->EqualTo(SMARTALBUMMAP_DB_ALBUM_ID, to_string(albumId))->Or()->
75 EqualTo(SMARTALBUMMAP_DB_CHILD_ALBUM_ID, to_string(albumId));
76 int32_t errCode = MediaLibraryObjectUtils::DeleteInfoByIdInDb(smartAlbumMapCmd);
77 CHECK_AND_RETURN_RET_LOG(errCode > 0, E_DELETE_SMARTALBUM_MAP_FAIL, "Failed to delete smartAlbumMap");
78 MediaLibraryCommand smartAlbumCmd(OperationObject::SMART_ALBUM, OperationType::DELETE);
79 smartAlbumCmd.GetAbsRdbPredicates()->EqualTo(SMARTALBUM_DB_ID, to_string(albumId));
80 return MediaLibraryObjectUtils::DeleteInfoByIdInDb(smartAlbumCmd);
81 }
82
HandleSmartAlbumOperation(MediaLibraryCommand & cmd)83 int32_t MediaLibrarySmartAlbumOperations::HandleSmartAlbumOperation(MediaLibraryCommand &cmd)
84 {
85 int32_t errCode = E_ERR;
86 switch (cmd.GetOprnType()) {
87 case OperationType::CREATE:
88 errCode = CreateSmartAlbumOperation(cmd);
89 break;
90 case OperationType::DELETE:
91 errCode = DeleteSmartAlbumOperation(cmd);
92 break;
93 default:
94 MEDIA_WARN_LOG("Unknown operation type %{private}d", cmd.GetOprnType());
95 break;
96 }
97 return errCode;
98 }
99 } // namespace Media
100 } // namespace OHOS