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