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 #ifndef BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_SERVICES_ANS_INCLUDE_REMINDER_STORE_STRATEGY_H
16 #define BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_SERVICES_ANS_INCLUDE_REMINDER_STORE_STRATEGY_H
17 
18 #include <vector>
19 #include <unordered_map>
20 
21 #include "reminder_request.h"
22 #include "rdb_store.h"
23 
24 namespace OHOS {
25 namespace Notification {
26 class ReminderStrategy {
27 public:
28     /**
29      * @brief Gets the value from rdb result.
30      *
31      * @param resultSet the rdb result.
32      * @param name the column name in rdb.
33      * @param value the column value in rdb.
34      */
35     template<typename T>
36     static void GetRdbValue(const std::shared_ptr<NativeRdb::ResultSet>& resultSet,
37         const std::string& name, T& value);
38 
39 public:
40     /**
41      * @brief Persist the reminder to the database.
42      */
43     static void AppendValuesBucket(const sptr<ReminderRequest>& reminder,
44         NativeRdb::ValuesBucket &values, const bool oldVersion = false);
45 
46     /**
47      * @brief Restore the reminder from the database(old version rdb).
48      */
49     static void RecoverFromOldVersion(sptr<ReminderRequest>& reminder,
50         const std::shared_ptr<NativeRdb::ResultSet>& resultSet);
51 
52     /**
53      * @brief Restore the reminder from the database.
54      */
55     static void RecoverFromDb(sptr<ReminderRequest>& reminder, const std::shared_ptr<NativeRdb::ResultSet>& resultSet);
56 
57 private:
58     /**
59      * @brief Recovery time related fields from the database(old version rdb).
60      */
61     static void RecoverTimeFromOldVersion(sptr<ReminderRequest>& reminder,
62         const std::shared_ptr<NativeRdb::ResultSet>& resultSet);
63     /**
64      * @brief Recovery id related fields from the database(old version rdb).
65      */
66     static void RecoverIdFromOldVersion(sptr<ReminderRequest>& reminder,
67         const std::shared_ptr<NativeRdb::ResultSet>& resultSet);
68     /**
69      * @brief Recovery context related from the database(old version rdb).
70      */
71     static void RecoverContextFromOldVersion(sptr<ReminderRequest>& reminder,
72         const std::shared_ptr<NativeRdb::ResultSet>& resultSet);
73 
74     /**
75      * @brief Recovery time related fields from the database.
76      */
77     static void RecoverTimeFromDb(sptr<ReminderRequest>& reminder,
78         const std::shared_ptr<NativeRdb::ResultSet>& resultSet);
79     /**
80      * @brief Recovery id related fields from the database.
81      */
82     static void RecoverIdFromDb(sptr<ReminderRequest>& reminder,
83         const std::shared_ptr<NativeRdb::ResultSet>& resultSet);
84     /**
85      * @brief Recovery context related from the database.
86      */
87     static void RecoverContextFromDb(sptr<ReminderRequest>& reminder,
88         const std::shared_ptr<NativeRdb::ResultSet>& resultSet);
89 };
90 
91 class ReminderTimerStrategy {
92 public:
93     /**
94      * @brief Persist the reminder to the database.
95      */
96     static void AppendValuesBucket(const sptr<ReminderRequest>& reminder,
97         NativeRdb::ValuesBucket& values);
98 
99     /**
100      * @brief Restore the reminder from the database(old version rdb).
101      */
102     static void RecoverFromOldVersion(sptr<ReminderRequest>& reminder,
103         const std::shared_ptr<NativeRdb::ResultSet>& resultSet);
104 
105     /**
106      * @brief Restore the reminder from the database.
107      */
108     static void RecoverFromDb(sptr<ReminderRequest>& reminder, const std::shared_ptr<NativeRdb::ResultSet>& baseResult,
109         const std::shared_ptr<NativeRdb::ResultSet>& resultSet);
110 };
111 
112 class ReminderAlarmStrategy {
113 public:
114     /**
115      * @brief Persist the reminder to the database.
116      */
117     static void AppendValuesBucket(const sptr<ReminderRequest> &reminder, NativeRdb::ValuesBucket &values);
118 
119     /**
120      * @brief Restore the reminder from the database(old version rdb).
121      */
122     static void RecoverFromOldVersion(sptr<ReminderRequest>& reminder,
123         const std::shared_ptr<NativeRdb::ResultSet>& resultSet);
124 
125     /**
126      * @brief Restore the reminder from the database.
127      */
128     static void RecoverFromDb(sptr<ReminderRequest>& reminder, const std::shared_ptr<NativeRdb::ResultSet>& baseResult,
129         const std::shared_ptr<NativeRdb::ResultSet>& resultSet);
130 };
131 
132 class ReminderCalendarStrategy {
133 public:
134     /**
135      * @brief Persist the reminder to the database.
136      */
137     static void AppendValuesBucket(const sptr<ReminderRequest> &reminder, NativeRdb::ValuesBucket &values);
138 
139     /**
140      * @brief Restore the reminder from the database(old version rdb).
141      */
142     static void RecoverFromOldVersion(sptr<ReminderRequest>& reminder,
143         const std::shared_ptr<NativeRdb::ResultSet>& resultSet);
144 
145     /**
146      * @brief Restore the reminder from the database.
147      */
148     static void RecoverFromDb(sptr<ReminderRequest>& reminder, const std::shared_ptr<NativeRdb::ResultSet>& baseResult,
149         const std::shared_ptr<NativeRdb::ResultSet>& resultSet);
150 
151 private:
152     static void RecoverTime(sptr<ReminderRequest>& reminder, const std::shared_ptr<NativeRdb::ResultSet>& resultSet);
153 };
154 
155 template<typename T>
GetRdbValue(const std::shared_ptr<NativeRdb::ResultSet> & resultSet,const std::string & name,T & value)156 void ReminderStrategy::GetRdbValue(const std::shared_ptr<NativeRdb::ResultSet>& resultSet,
157     const std::string& name, T& value)
158 {
159     value = T();
160     int32_t columnIndex = -1;
161     resultSet->GetColumnIndex(name, columnIndex);
162     if (columnIndex == -1) {
163         ANSR_LOGE("the column %{public}s does not exsit.", name.c_str());
164         return;
165     }
166 
167     if constexpr (std::is_same_v<T, std::string>) {
168         resultSet->GetString(columnIndex, value);
169     } else if constexpr (std::is_same_v<T, int64_t>) {
170         resultSet->GetLong(columnIndex, value);
171     } else if constexpr (std::is_same_v<T, uint64_t>) {
172         int64_t t = 0;
173         resultSet->GetLong(columnIndex, t);
174         value = static_cast<uint64_t>(t);
175     } else if constexpr (std::is_same_v<T, int32_t>) {
176         resultSet->GetInt(columnIndex, value);
177     } else if constexpr (std::is_same_v<T, uint32_t>) {
178         int32_t t = 0;
179         resultSet->GetInt(columnIndex, t);
180         value = static_cast<uint32_t>(t);
181     } else if constexpr (std::is_same_v<T, uint16_t>) {
182         int32_t t = 0;
183         resultSet->GetInt(columnIndex, t);
184         value = static_cast<uint16_t>(t);
185     } else if constexpr (std::is_same_v<T, uint8_t>) {
186         int32_t t = 0;
187         resultSet->GetInt(columnIndex, t);
188         value = static_cast<uint8_t>(t);
189     }
190 }
191 }  // namespace Notification
192 }  // namespace OHOS
193 #endif  // BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_SERVICES_ANS_INCLUDE_REMINDER_STORE_STRATEGY_H