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 
16 #include "system_defined_record.h"
17 
18 namespace OHOS {
19 namespace UDMF {
SystemDefinedRecord()20 SystemDefinedRecord::SystemDefinedRecord() : UnifiedRecord(SYSTEM_DEFINED_RECORD)
21 {
22 }
23 
GetSize()24 int64_t SystemDefinedRecord::GetSize()
25 {
26     return UnifiedDataUtils::GetDetailsSize(this->details_);
27 }
28 
SystemDefinedRecord(UDType type,ValueType value)29 SystemDefinedRecord::SystemDefinedRecord(UDType type, ValueType value) : UnifiedRecord(type, value)
30 {
31     this->dataType_ = SYSTEM_DEFINED_RECORD;
32     if (std::holds_alternative<std::shared_ptr<Object>>(value)) {
33         auto object = std::get<std::shared_ptr<Object>>(value);
34         std::shared_ptr<Object> detailObj = nullptr;
35         if (object->GetValue(DETAILS, detailObj)) {
36             details_ = ObjectUtils::ConvertToUDDetails(detailObj);
37         }
38         hasObject_ = true;
39     }
40 }
41 
AddProperty(const std::string & property,UDVariant & value)42 void SystemDefinedRecord::AddProperty(const std::string &property, UDVariant &value)
43 {
44     auto it = details_.find(property);
45     if (it == details_.end()) {
46         details_.insert(std::make_pair(property, value));
47     } else {
48         details_[property] = value;
49     }
50     if (std::holds_alternative<std::shared_ptr<Object>>(value_)) {
51         std::get<std::shared_ptr<Object>>(value_)->value_[DETAILS] = ObjectUtils::ConvertToObject(details_);
52     }
53 }
54 
GetPropertyByName(const std::string & property) const55 UDVariant SystemDefinedRecord::GetPropertyByName(const std::string &property) const
56 {
57     auto it = details_.find(property);
58     if (it == details_.end()) {
59         return nullptr;
60     }
61     return it->second;
62 }
63 
SetDetails(UDDetails & details)64 void SystemDefinedRecord::SetDetails(UDDetails &details)
65 {
66     this->details_ = details;
67     if (std::holds_alternative<std::shared_ptr<Object>>(value_)) {
68         std::get<std::shared_ptr<Object>>(value_)->value_[DETAILS] = ObjectUtils::ConvertToObject(details_);
69     }
70 }
71 
GetDetails() const72 UDDetails SystemDefinedRecord::GetDetails() const
73 {
74     return this->details_;
75 }
76 
InitObject()77 void SystemDefinedRecord::InitObject()
78 {
79     if (!std::holds_alternative<std::shared_ptr<Object>>(value_)) {
80         auto value = value_;
81         value_ = std::make_shared<Object>();
82         auto object = std::get<std::shared_ptr<Object>>(value_);
83         object->value_[DETAILS] = ObjectUtils::ConvertToObject(details_);
84         object->value_["VALUE_TYPE"] = value;
85     }
86 }
87 } // namespace UDMF
88 } // namespace OHOS