summaryrefslogtreecommitdiffstats
path: root/veslibrary/ves_cpplibrary/src/lib/encode/XNotificationFieldsImp.cpp
blob: 143411439ccb02957b08458325ce42a4e335b514 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include "XNotificationFieldsImp.h"

const std::string XEnumNotificationFieldsNotificationFieldsVersionMapping[] = {
    "2.0"
};

XNotificationFieldsImp::XNotificationFieldsImp(std::shared_ptr<XCommonEventHeader> header,
    const XString& changeIdentifier,
    const XString& changeType
    ):
        header_(header),
        changeIdentifier_(changeIdentifier),
        changeType_(changeType),
        notificationFieldsVersion_(XEnumNotificationFieldsNotificationFieldsVersion2_0)
{

}

std::string XNotificationFieldsImp::toString()
{
    try
    {
        auto js = toJson();
        return js.dump();
    }
    catch (json::exception& e)
    {
        SPDLOG_ERROR("Fail to dump XNotificationFields to json string:{}.", e.what());
        return "";
    }
}

void XNotificationFieldsImp::setNewState(const XString& newState)
{
    comVals_["newState"] = newState;
}
            
void XNotificationFieldsImp::setChangeType(const XString& changeType)
{
    comVals_["changeType"] = changeType;
}
            
void XNotificationFieldsImp::setArrayOfNamedHashMap(const XArrayOfNamedHashMap& arrayOfNamedHashMap)
{
    arrayOfNamedHashMap_ = arrayOfNamedHashMap;
}
            
void XNotificationFieldsImp::setChangeContact(const XString& changeContact)
{
    comVals_["changeContact"] = changeContact;
}
            
void XNotificationFieldsImp::setChangeIdentifier(const XString& changeIdentifier)
{
    comVals_["changeIdentifier"] = changeIdentifier;
}
            
void XNotificationFieldsImp::setAdditionalFields(const XHashMap& additionalFields)
{
    additionalFields_ = additionalFields;
}
            
void XNotificationFieldsImp::setOldState(const XString& oldState)
{
    comVals_["oldState"] = oldState;
}
            
void XNotificationFieldsImp::setStateInterface(const XString& stateInterface)
{
    comVals_["stateInterface"] = stateInterface;
}

json XNotificationFieldsImp::toJson()
{
    try
    {
        json field = {
            {"notificationFieldsVersion", XEnumNotificationFieldsNotificationFieldsVersionMapping[notificationFieldsVersion_]},
            {"changeType", changeType_},
            {"changeIdentifier", changeIdentifier_}
        };

        //optional val
        if (!arrayOfNamedHashMap_.empty())
        {
            field["arrayOfNamedHashMap"] = fromArray(arrayOfNamedHashMap_);
        }

        if (!additionalFields_.empty())
        {
            field["additionalFields"] = additionalFields_;
        }

        field = mergeCommonValues(field, comVals_);

        json jsEvent;
        header_->setDomain(XEnumCommonEventHeaderDomainNotification);
        jsEvent["event"]["commonEventHeader"] = dynamic_pointer_cast<XJsonable>(header_)->toJson();
        jsEvent["event"]["notificationFields"] = field;

        return jsEvent;
    }
    catch (json::exception& e)
    {
        SPDLOG_ERROR("Fail to build XNotificationFields to json object:{}.", e.what());
        return json();
    }
}

std::shared_ptr<XCommonEventHeader> XNotificationFieldsImp::getCommonHeader()
{
    return header_;
}