1 /*
2 * Copyright (c) 2023 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 LOG_TAG "UnifiedRecord"
16 #include "unified_record.h"
17
18 #include "getter_system.h"
19 #include "logger.h"
20
21 namespace OHOS {
22 namespace UDMF {
UnifiedRecord()23 UnifiedRecord::UnifiedRecord()
24 {
25 dataType_ = UD_BUTT;
26 }
27
UnifiedRecord(UDType type)28 UnifiedRecord::UnifiedRecord(UDType type)
29 {
30 dataType_ = type;
31 utdId_ = UtdUtils::GetUtdIdFromUtdEnum(type);
32 }
33
UnifiedRecord(UDType type,ValueType value)34 UnifiedRecord::UnifiedRecord(UDType type, ValueType value)
35 {
36 dataType_ = type;
37 utdId_ = UtdUtils::GetUtdIdFromUtdEnum(type);
38 value_ = value;
39 if (std::holds_alternative<std::shared_ptr<Object>>(value_)) {
40 hasObject_ = true;
41 }
42 }
43
GetType() const44 UDType UnifiedRecord::GetType() const
45 {
46 return this->dataType_;
47 }
48
SetType(const UDType & type)49 void UnifiedRecord::SetType(const UDType &type)
50 {
51 dataType_ = type;
52 utdId_ = UtdUtils::GetUtdIdFromUtdEnum(type);
53 }
54
GetSize()55 int64_t UnifiedRecord::GetSize()
56 {
57 return 0;
58 }
59
GetUid() const60 std::string UnifiedRecord::GetUid() const
61 {
62 return this->uid_;
63 }
64
SetUid(const std::string & id)65 void UnifiedRecord::SetUid(const std::string &id)
66 {
67 this->uid_ = id;
68 }
69
GetValue()70 ValueType UnifiedRecord::GetValue()
71 {
72 return value_;
73 }
74
SetValue(const ValueType & value)75 void UnifiedRecord::SetValue(const ValueType &value)
76 {
77 value_ = value;
78 }
79
GetOriginValue() const80 ValueType UnifiedRecord::GetOriginValue() const
81 {
82 return value_;
83 }
84
HasType(const std::string & utdId) const85 bool UnifiedRecord::HasType(const std::string &utdId) const
86 {
87 if (entries_->find(utdId) != entries_->end()) {
88 return true;
89 }
90 return utdId == utdId_;
91 }
92
AddEntry(const std::string & utdId,ValueType && value)93 void UnifiedRecord::AddEntry(const std::string &utdId, ValueType &&value)
94 {
95 if (utdId == utdId_ || utdId_.empty()) {
96 utdId_ = utdId;
97 value_ = std::move(value);
98 auto udType = static_cast<UDType>(UtdUtils::GetUtdEnumFromUtdId(utdId_));
99 if (udType != UD_BUTT) {
100 dataType_ = udType;
101 } else {
102 dataType_ = APPLICATION_DEFINED_RECORD;
103 }
104 } else {
105 entries_->insert_or_assign(utdId, std::move(value));
106 }
107 }
108
GetEntry(const std::string & utdId)109 ValueType UnifiedRecord::GetEntry(const std::string &utdId)
110 {
111 if (utdId_ == utdId && !(std::holds_alternative<std::monostate>(value_))) {
112 return value_;
113 }
114 auto it = entries_->find(utdId);
115 if (it != entries_->end() && !(std::holds_alternative<std::monostate>(it->second))) {
116 return it->second;
117 }
118 auto getter = GetterSystem::GetInstance().GetGetter(channelName_);
119 if (getter != nullptr && (utdId_ == utdId || it != entries_->end())) {
120 auto value = getter->GetValueByType(dataId_, recordId_, utdId);
121 AddEntry(utdId, ValueType(value));
122 return value;
123 }
124 return std::monostate();
125 }
126
GetEntries() const127 std::shared_ptr<std::map<std::string, ValueType>> UnifiedRecord::GetEntries() const
128 {
129 auto res = std::make_shared<std::map<std::string, ValueType>>(*entries_);
130 if (!utdId_.empty()) {
131 res->insert_or_assign(utdId_, value_);
132 }
133 return res;
134 }
135
GetUtdIds() const136 std::set<std::string> UnifiedRecord::GetUtdIds() const
137 {
138 std::set<std::string> utdIds;
139 if (!utdId_.empty()) {
140 utdIds.emplace(utdId_);
141 }
142 for (const auto& [key, value] : *entries_) {
143 utdIds.emplace(key);
144 }
145 return utdIds;
146 }
147
SetUtdId(const std::string & utdId)148 void UnifiedRecord::SetUtdId(const std::string& utdId)
149 {
150 utdId_ = utdId;
151 }
152
GetUtdId() const153 std::string UnifiedRecord::GetUtdId() const
154 {
155 return utdId_;
156 }
157
SetDataId(uint32_t dataId)158 void UnifiedRecord::SetDataId(uint32_t dataId)
159 {
160 dataId_ = dataId;
161 }
162
GetDataId() const163 uint32_t UnifiedRecord::GetDataId() const
164 {
165 return dataId_;
166 }
167
SetRecordId(uint32_t recordId)168 void UnifiedRecord::SetRecordId(uint32_t recordId)
169 {
170 recordId_ = recordId;
171 }
172
GetRecordId() const173 uint32_t UnifiedRecord::GetRecordId() const
174 {
175 return recordId_;
176 }
177
SetEntryGetter(const std::vector<std::string> & utdIds,const std::shared_ptr<EntryGetter> & entryGetter)178 void UnifiedRecord::SetEntryGetter(
179 const std::vector<std::string> &utdIds,
180 const std::shared_ptr<EntryGetter> &entryGetter)
181 {
182 for (auto const &utdId : utdIds) {
183 if (HasType(utdId)) {
184 LOG_WARN(UDMF_FRAMEWORK, "already has the utdId: %{public}s", utdId.c_str());
185 continue;
186 }
187 AddEntry(utdId, ValueType());
188 }
189 entryGetter_ = entryGetter;
190 }
191
GetEntryGetter()192 std::shared_ptr<EntryGetter> UnifiedRecord::GetEntryGetter()
193 {
194 return entryGetter_;
195 }
196
SetChannelName(const std::string & channelName)197 void UnifiedRecord::SetChannelName(const std::string &channelName)
198 {
199 channelName_ = channelName;
200 }
201
InitObject()202 void UnifiedRecord::InitObject()
203 {
204 if (!std::holds_alternative<std::shared_ptr<Object>>(value_)) {
205 auto value = value_;
206 value_ = std::make_shared<Object>();
207 auto object = std::get<std::shared_ptr<Object>>(value_);
208 object->value_["VALUE_TYPE"] = value;
209 }
210 }
211
HasObject()212 bool UnifiedRecord::HasObject()
213 {
214 return hasObject_;
215 }
216
217 } // namespace UDMF
218 } // namespace OHOS