1 /*
2  * Copyright (c) 2022 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 BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_INTERFACES_INNER_API_REMINDER_STORE_H
17 #define BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_INTERFACES_INNER_API_REMINDER_STORE_H
18 
19 #include <vector>
20 
21 #include "notification_bundle_option.h"
22 #include "reminder_request.h"
23 #include "rdb_errno.h"
24 #include "rdb_helper.h"
25 #include "rdb_open_callback.h"
26 #include "rdb_store_config.h"
27 
28 namespace OHOS {
29 namespace Notification {
30 class ReminderStore {
31 public:
ReminderStore()32     ReminderStore() {};
~ReminderStore()33     virtual ~ReminderStore() {};
34 
35 public:
36     int32_t Init();
37     int32_t Delete(const int32_t reminderId);
38     int32_t Delete(const std::string& pkg, const int32_t userId, const int32_t uid);
39     int32_t DeleteUser(const int32_t userId);
40     int32_t UpdateOrInsert(const sptr<ReminderRequest>& reminder, const sptr<NotificationBundleOption>& bundleOption);
41     int32_t GetMaxId();
42     std::vector<sptr<ReminderRequest>> GetAllValidReminders();
43 
44 public:
45     static void GetUInt8Val(const std::shared_ptr<NativeRdb::ResultSet>& resultSet,
46         const std::string& name, uint8_t& value);
47     static void GetUInt16Val(const std::shared_ptr<NativeRdb::ResultSet>& resultSet,
48         const std::string& name, uint16_t& value);
49     static void GetInt32Val(const std::shared_ptr<NativeRdb::ResultSet>& resultSet,
50         const std::string& name, int32_t& value);
51     static void GetInt64Val(const std::shared_ptr<NativeRdb::ResultSet>& resultSet,
52         const std::string& name, int64_t& value);
53     static void GetUInt64Val(const std::shared_ptr<NativeRdb::ResultSet>& resultSet,
54         const std::string& name, uint64_t& value);
55     static void GetStringVal(const std::shared_ptr<NativeRdb::ResultSet>& resultSet,
56         const std::string& name, std::string& value);
57 
58     static const int32_t STATE_OK;
59     static const int32_t STATE_FAIL;
60 
61     static const std::string REMINDER_DB_DIR;
62     static const std::string REMINDER_DB_NAME;
63     static const std::string REMINDER_DB_TABLE;
64 
65 private:
66     /**
67      * @brief Inits the data in database when system boot on or proxy process reboot on.
68      *
69      * 1. Deletes all the reminders which IS_EXPIRED is true.
70      * 2. Sets all the value of STATE to ReminderRequest::REMINDER_STATUS_INACTIVE
71      *
72      * @return int32_t result code.
73      */
74     int32_t InitData();
75     int32_t DeleteBase(const std::string& deleteCondition);
76     int32_t Delete(const std::string& baseCondition, const std::string& assoConditon);
77     int32_t Insert(const sptr<ReminderRequest>& reminder, const sptr<NotificationBundleOption>& bundleOption);
78     int32_t Update(const sptr<ReminderRequest>& reminder, const sptr<NotificationBundleOption>& bundleOption);
79 
80     bool IsReminderExist(const sptr<ReminderRequest>& reminder);
81     std::vector<sptr<ReminderRequest>> GetReminders(const std::string& queryCondition);
82     sptr<ReminderRequest> BuildReminder(const std::shared_ptr<NativeRdb::ResultSet>& resultBase);
83 
84     std::shared_ptr<NativeRdb::ResultSet> Query(const std::string& tableName, const std::string& colums,
85         const int32_t reminderId);
86     std::shared_ptr<NativeRdb::ResultSet> Query(const std::string& queryCondition) const;
87 
88 private:
89     std::shared_ptr<NativeRdb::RdbStore> rdbStore_ = nullptr;
90 
91 private:
92 class ReminderStoreDataCallBack : public NativeRdb::RdbOpenCallback {
93 public:
94     int32_t OnCreate(NativeRdb::RdbStore& store) override;
95     int32_t OnUpgrade(NativeRdb::RdbStore& store, int32_t oldVersion, int32_t newVersion) override;
96     int32_t OnDowngrade(NativeRdb::RdbStore& store, int32_t currentVersion, int32_t targetVersion) override;
97 
98 private:
99     int32_t CreateTable(NativeRdb::RdbStore& store);
100     int32_t CopyData(NativeRdb::RdbStore& store);
101     std::vector<sptr<ReminderRequest>> GetOldReminders(NativeRdb::RdbStore& store);
102     void InsertNewReminders(NativeRdb::RdbStore& store, const std::vector<sptr<ReminderRequest>>& reminders);
103     void AddRdbColum(NativeRdb::RdbStore& store, const std::string& tableName,
104         const std::string& columnName, const std::string& columnType, const std::string& defValue);
105 };
106 };
107 }  // namespace Notification
108 }  // namespace OHOS
109 #endif  // BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_INTERFACES_INNER_API_REMINDER_STORE_H