• 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 #define MLOG_TAG "SmartAlbum"
16 
17 #include "medialibrary_smartalbum_operations.h"
18 #include "media_log.h"
19 #include "medialibrary_errno.h"
20 #include "medialibrary_smartalbum_map_db.h"
21 
22 using namespace std;
23 using namespace OHOS::NativeRdb;
24 using namespace OHOS::DataShare;
25 
26 namespace OHOS {
27 namespace Media {
InsertAlbumInfoUtil(const ValuesBucket & valuesBucket,shared_ptr<RdbStore> rdbStore,const MediaLibrarySmartAlbumDb & smartAlbumDbOprn)28 int32_t InsertAlbumInfoUtil(const ValuesBucket &valuesBucket,
29                             shared_ptr<RdbStore> rdbStore,
30                             const MediaLibrarySmartAlbumDb &smartAlbumDbOprn)
31 {
32     ValueObject valueObject;
33     int32_t id = 0;
34     int32_t insertId = const_cast<MediaLibrarySmartAlbumDb &>(smartAlbumDbOprn).
35         InsertSmartAlbumInfo(valuesBucket, rdbStore);
36     if (insertId > 0) {
37         ValuesBucket values;
38         values.PutInt(CATEGORY_SMARTALBUMMAP_DB_CATEGORY_ID, id);
39         values.PutInt(CATEGORY_SMARTALBUMMAP_DB_ALBUM_ID, insertId);
40         const_cast<MediaLibrarySmartAlbumDb &>(smartAlbumDbOprn).InsertCategorySmartAlbumInfo(values, rdbStore);
41     }
42     return insertId;
43 }
DeleteAlbumInfoUtil(const ValuesBucket & valuesBucket,shared_ptr<RdbStore> rdbStore,const MediaLibrarySmartAlbumDb & smartAlbumDbOprn)44 int32_t DeleteAlbumInfoUtil(const ValuesBucket &valuesBucket,
45                             shared_ptr<RdbStore> rdbStore,
46                             const MediaLibrarySmartAlbumDb &smartAlbumDbOprn)
47 {
48     ValuesBucket values = const_cast<ValuesBucket &>(valuesBucket);
49     ValueObject valueObject;
50     int32_t albumId = 0;
51     MediaLibrarySmartAlbumMapDb smartAlbumMapDbOprn;
52     if (values.GetObject(SMARTALBUM_DB_ID, valueObject)) {
53         valueObject.GetInt(albumId);
54     }
55     MEDIA_ERR_LOG("mediasmart albumId = %{private}d", albumId);
56 
57     int32_t deleteErrorCode = const_cast<MediaLibrarySmartAlbumDb &>(smartAlbumDbOprn)
58     .DeleteSmartAlbumInfo(albumId, rdbStore);
59     if (deleteErrorCode != -1) {
60         smartAlbumMapDbOprn.DeleteAllSmartAlbumMapInfo(albumId, rdbStore);
61     }
62     return deleteErrorCode;
63 }
64 
HandleSmartAlbumOperations(const string & oprn,const ValuesBucket & valuesBucket,const shared_ptr<RdbStore> & rdbStore)65 int32_t MediaLibrarySmartAlbumOperations::HandleSmartAlbumOperations(const string &oprn,
66                                                                      const ValuesBucket &valuesBucket,
67                                                                      const shared_ptr<RdbStore> &rdbStore)
68 {
69     MEDIA_ERR_LOG("HandleSmartAlbumOperations");
70     ValuesBucket values = const_cast<ValuesBucket &>(valuesBucket);
71     MediaLibrarySmartAlbumDb smartAlbumDbOprn;
72     int32_t errCode = E_FAIL;
73     ValueObject valueObject;
74     if (oprn == MEDIA_SMARTALBUMOPRN_CREATEALBUM) {
75         errCode = InsertAlbumInfoUtil(values, rdbStore, smartAlbumDbOprn);
76     } else if (oprn == MEDIA_SMARTALBUMOPRN_DELETEALBUM) {
77         errCode = DeleteAlbumInfoUtil(values, rdbStore, smartAlbumDbOprn);
78     }
79     return errCode;
80 }
81 } // namespace Media
82 } // namespace OHOS
83