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 
16 #ifndef MEDIALIBRARY_APP_URI_PERMISSION_OPERATIONS
17 #define MEDIALIBRARY_APP_URI_PERMISSION_OPERATIONS
18 
19 #include <string>
20 #include <unordered_map>
21 #include "media_app_uri_permission_column.h"
22 #include "media_app_uri_sensitive_column.h"
23 #include "file_asset.h"
24 #include "medialibrary_command.h"
25 #include "rdb_predicates.h"
26 #include "result_set.h"
27 #include "datashare_values_bucket.h"
28 #include "medialibrary_rdb_transaction.h"
29 
30 namespace OHOS {
31 namespace Media {
32 #define EXPORT __attribute__ ((visibility ("default")))
33 
34 EXPORT const std::unordered_map<std::string, int> APP_URI_PERMISSION_MEMBER_MAP = {
35     {AppUriPermissionColumn::ID, MEMBER_TYPE_INT32},
36     {AppUriPermissionColumn::APP_ID, MEMBER_TYPE_STRING},
37     {AppUriPermissionColumn::FILE_ID, MEMBER_TYPE_INT32},
38     {AppUriPermissionColumn::URI_TYPE, MEMBER_TYPE_INT32},
39     {AppUriPermissionColumn::PERMISSION_TYPE, MEMBER_TYPE_INT32},
40     {AppUriPermissionColumn::DATE_MODIFIED, MEMBER_TYPE_INT64}};
41 
42 class MediaLibraryAppUriPermissionOperations {
43 public:
44     EXPORT static const int ERROR;
45     EXPORT static const int SUCCEED;
46     EXPORT static const int ALREADY_EXIST;
47     EXPORT static const int NO_DATA_EXIST;
48 
49     EXPORT static int32_t HandleInsertOperation(MediaLibraryCommand &cmd);
50     EXPORT static int32_t BatchInsert(MediaLibraryCommand &cmd,
51         const std::vector<DataShare::DataShareValuesBucket> &values);
52     EXPORT static int32_t BatchInsertInner(MediaLibraryCommand &cmd,
53         const std::vector<DataShare::DataShareValuesBucket> &values,
54         std::shared_ptr<TransactionOperations> trans);
55     EXPORT static int32_t DeleteOperation(NativeRdb::RdbPredicates &predicates);
56     EXPORT static std::shared_ptr<OHOS::NativeRdb::ResultSet> QueryOperation(
57         DataShare::DataSharePredicates &predicates, std::vector<std::string> &fetchColumns);
58 private:
59     /**
60      * query newData before insert, use this method.
61      * @param resultFlag ERROR: query newData error.
62      *                   NO_DATA: newData not exist in database.
63      *                   ALREADY_EXIST: newData already exist in database.
64      */
65     static std::shared_ptr<OHOS::NativeRdb::ResultSet> QueryNewData(
66         OHOS::NativeRdb::ValuesBucket &valueBucket, int &resultFlag);
67     /**
68      * get the value of the int type corresponding to {@code column} from {@code valueBucket}.
69      * @param result target value
70      * @return true: Successfully obtained the value.
71      *         false: failed to get the value.
72      */
73     static bool GetIntFromValuesBucket(OHOS::NativeRdb::ValuesBucket &valueBucket,
74         const std::string &column, int &result);
75     /**
76      * @param resultSetDB must contain id value.
77      * @param valueBucketParam must contain permissionType value.
78      */
79     static int UpdatePermissionType(std::shared_ptr<OHOS::NativeRdb::ResultSet> &resultSetDB,
80         int &permissionTypeParam, std::shared_ptr<TransactionOperations> trans = nullptr);
81     static bool IsValidPermissionType(int &permissionType);
82     static bool CanOverride(int &permissionTypeParam, int &permissionTypeDB);
83     static bool IsPhotoExist(int &photoFileId);
84     static bool IsPhotosAllExist(const std::vector<DataShare::DataShareValuesBucket> &values);
85 };
86 } // namespace Media
87 } // namespace OHOS
88 
89 #endif // MEDIALIBRARY_APP_URI_PERMISSION_OPERATIONS