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 OHOS_FORM_FWK_FORM_INFO_RDB_STORAGE_MGR_H
17 #define OHOS_FORM_FWK_FORM_INFO_RDB_STORAGE_MGR_H
18 
19 #include <singleton.h>
20 #include <string>
21 #include "form_db_info.h"
22 #include "form_rdb_data_mgr.h"
23 
24 namespace OHOS {
25 namespace AppExecFwk {
26 
27 /**
28  * @class FormInfoRdbStorageMgr
29  * Form info storage.
30  */
31 class FormInfoRdbStorageMgr final : public DelayedRefSingleton<FormInfoRdbStorageMgr> {
32 DECLARE_DELAYED_REF_SINGLETON(FormInfoRdbStorageMgr)
33 public:
34     DISALLOW_COPY_AND_MOVE(FormInfoRdbStorageMgr);
35 
36     /**
37      * @brief Load all form info from DB to formInfoStorages.
38      * @param formInfoStorages Storage all form info.
39      * @return Returns ERR_OK on success, others on failure.
40      */
41     ErrCode LoadFormInfos(std::vector<std::pair<std::string, std::string>> &formInfoStorages);
42 
43     /**
44      * @brief Delete the form info in DB.
45      * @param bundleName The form info bundleName.
46      * @return Returns ERR_OK on success, others on failure.
47      */
48     ErrCode RemoveBundleFormInfos(const std::string &bundleName);
49 
50     /**
51      * @brief Save or update the form info in DB.
52      * @param bundleName The form info bundleName @param formInfoStorages The form info.
53      * @return Returns ERR_OK on success, others on failure.
54      */
55     ErrCode UpdateBundleFormInfos(const std::string &bundleName, const std::string &formInfoStorages);
56 
57     /**
58      * @brief Load all form data from DB to innerFormInfos.
59      * @param innerFormInfos Storage all form data.
60      * @return Returns ERR_OK on success, others on failure.
61      */
62     ErrCode LoadFormData(std::vector<InnerFormInfo> &innerFormInfos);
63 
64     /**
65      * @brief Save or update the form data in DB.
66      * @param innerFormInfo Indicates the InnerFormInfo object to be save.
67      * @return Returns ERR_OK on success, others on failure.
68      */
69     ErrCode SaveStorageFormData(const InnerFormInfo &innerFormInfo);
70 
71     /**
72      * @brief Modify the form data in DB.
73      * @param innerFormInfo Indicates the InnerFormInfo object to be Modify.
74      * @return Returns ERR_OK on success, others on failure.
75      */
76     ErrCode ModifyStorageFormData(const InnerFormInfo &innerFormInfo);
77 
78     /**
79      * @brief Delete the form data in DB.
80      * @param formId The form data Id.
81      * @return Returns ERR_OK on success, others on failure.
82      */
83     ErrCode DeleteStorageFormData(const std::string &formId);
84 
85     /**
86      * @brief Load status data of form from DB.
87      * @param formId The form data Id.
88      * @param statusData Status data of form.
89      * @return Returns ERR_OK on success, others on failure.
90      */
91     ErrCode LoadStatusData(const std::string &formId, std::string &statusData);
92 
93     /**
94      * @brief Save or update the status data of form in DB.
95      * @param formId The form data Id.
96      * @param statusData Status data of form.
97      * @return Returns ERR_OK on success, others on failure.
98      */
99     ErrCode UpdateStatusData(const std::string &formId, const std::string &statusData);
100 private:
101     void SaveEntries(const std::unordered_map<std::string, std::string> &value,
102         std::vector<InnerFormInfo> &innerFormInfos);
103 
104     mutable std::mutex rdbStorePtrMutex_;
105 };
106 }  // namespace AppExecFwk
107 }  // namespace OHOS
108 
109 #endif // OHOS_FORM_FWK_FORM_INFO_RDB_STORAGE_MGR_H
110