]> SALOME platform Git repositories - modules/gde.git/blob - projects/GDE_API_CPP/api/src/AttributesService.cpp
Salome HOME
FileService
[modules/gde.git] / projects / GDE_API_CPP / api / src / AttributesService.cpp
1 #include "AttributesService.hpp"
2 #include "HttpConnection.hpp"
3 #include "CommandTO.hpp"
4 #include "CommandResultTO.hpp"
5 #include "JsonFormatter.hpp"
6 #include "Credentials.hpp"
7
8 #include <vector>
9 #include <typeinfo>
10
11 /*
12  * WARNING: keep enum values synchronized to WEB API (AttributesService)
13  */
14 enum {
15   CREATE_ATTRIBUTE = 1,
16   DELETE_ATTRIBUTE,
17   READ_ATTRIBUTE,
18   CREATE_ATTRIBUTE_GROUP,
19   DELETE_ATTRIBUTE_GROUP,
20   UPDATE_ATTRIBUTE_GROUP,
21   READ_ATTRIBUTE_GROUP
22 };
23
24 std::string gde::AttributesService::_servletName = "AttributesService";
25
26 const gde::Attribute
27 gde::AttributesService::createAttribute(const std::string& name, const std::string& type, const std::string& value, int groupId, bool mandatory)
28 {
29   // build JSON string for CommandTO data
30   Poco::JSON::Object obj;
31   obj.set("name", Poco::Dynamic::Var(name));
32   obj.set("type", Poco::Dynamic::Var(type));
33   obj.set("value", Poco::Dynamic::Var(value));
34   obj.set("groupId", Poco::Dynamic::Var(groupId));
35   obj.set("mandatory", Poco::Dynamic::Var(mandatory));
36   std::string data = JsonFormatter::stringify(obj);
37
38   CommandTO cto(CREATE_ATTRIBUTE, data);
39   const Credentials& credentials = _session.getCredentials();
40   std::string uri = _session.getServiceURI(_servletName);
41   CommandResultTO crto = gde::HttpConnection(Poco::URI(uri), credentials).doPost(cto);
42
43   // build Attribute object from CommandResultTO data (json format)
44   {
45     Poco::JSON::Object::Ptr object = JsonFormatter::parse(crto.getData());
46     int id = JsonFormatter::extract<int>(object, "id");
47     std::string name = JsonFormatter::extract<std::string>(object, "name");
48     std::string type = JsonFormatter::extract<std::string>(object, "type");
49     std::string value = JsonFormatter::extract<std::string>(object, "value");
50     int groupId = JsonFormatter::extract<int>(object, "groupId");
51     bool mandatory = JsonFormatter::extract<bool>(object, "mandatory");
52     return gde::Attribute(id, name, type, value, groupId, mandatory);
53   }
54 }
55
56 bool
57 gde::AttributesService::deleteAttribute(const Attribute& attribute)
58 {
59   CommandTO cto(DELETE_ATTRIBUTE);
60   cto.setParameter("attributeId", attribute.getId());
61
62   const Credentials& credentials = _session.getCredentials();
63   std::string uri = _session.getServiceURI(_servletName);
64   CommandResultTO crto = gde::HttpConnection(Poco::URI(uri), credentials).doPost(cto);
65   return crto.getCode() == CommandResultTO::OK;
66 }
67
68 const gde::Attribute
69 gde::AttributesService::readAttribute(int attributeId)
70 {
71   CommandTO cto(READ_ATTRIBUTE);
72   cto.setParameter("attributeId", attributeId);
73
74   const Credentials& credentials = _session.getCredentials();
75   std::string uri = _session.getServiceURI(_servletName);
76   CommandResultTO crto = gde::HttpConnection(Poco::URI(uri), credentials).doPost(cto);
77
78   // build Attribute object from CommandResultTO data (json format)
79   {
80     Poco::JSON::Object::Ptr object = JsonFormatter::parse(crto.getData());
81     int id = JsonFormatter::extract<int>(object, "id");
82     std::string name = JsonFormatter::extract<std::string>(object, "name");
83     std::string type = JsonFormatter::extract<std::string>(object, "type");
84     std::string value = JsonFormatter::extract<std::string>(object, "value");
85     int groupId = JsonFormatter::extract<int>(object, "groupId");
86     bool mandatory = JsonFormatter::extract<bool>(object, "mandatory");
87     return gde::Attribute(id, name, type, value, groupId, mandatory);
88   }
89 }
90
91 std::string
92 _buildAttributesJsonString(const gde::AttributeGroup& attributeGroup)
93 {
94   // Solution here is not very smart. Have tried using Poco to stringify attributes,
95   // but stringification is too recursive, for example it produces:
96   // "attributeCollection":"[ { \"id\" : 0, \"name\" : \"myAttribute\",...
97   // instead of:
98   // "attributeCollection":[{"id":0,"name":"myAttribute",...
99   const std::vector<gde::Attribute>& attributes = attributeGroup.getAttributes();
100   std::string data = "{";
101   data += gde::JsonFormatter::format("\"id\":", attributeGroup.getId());
102   data += ", \"attributeCollection\":[";
103   for (std::vector<gde::Attribute>::const_iterator itr = attributes.begin(); itr != attributes.end(); ++itr) {
104     if (itr != attributes.begin())
105       data += ",";
106     data += itr->toJson();
107   }
108   data += "]}";
109   return data;
110 }
111
112 const gde::AttributeGroup
113 gde::AttributesService::createAttributeGroup(const AttributeGroup& attributeGroup)
114 {
115   std::string data = _buildAttributesJsonString(attributeGroup);
116   CommandTO cto(CREATE_ATTRIBUTE_GROUP, data);
117
118   const Credentials& credentials = _session.getCredentials();
119   std::string uri = _session.getServiceURI(_servletName);
120   CommandResultTO crto = gde::HttpConnection(Poco::URI(uri), credentials).doPost(cto);
121
122   // build AttributeGroup object from CommandResultTO data (json format)
123   {
124     Poco::JSON::Object::Ptr object = JsonFormatter::parse(crto.getData());
125     int id = JsonFormatter::extract<int>(object, "id");
126     std::vector<gde::Attribute> attributes = JsonFormatter::extractVector<gde::Attribute>(object, "attributeCollection");
127     return gde::AttributeGroup(id, attributes);
128   }
129 }
130
131 bool
132 gde::AttributesService::deleteAttributeGroup(const gde::AttributeGroup& attributeGroup)
133 {
134   CommandTO cto(DELETE_ATTRIBUTE_GROUP);
135   cto.setParameter("attributeGroupId", attributeGroup.getId());
136
137   const Credentials& credentials = _session.getCredentials();
138   std::string uri = _session.getServiceURI(_servletName);
139   CommandResultTO crto = gde::HttpConnection(Poco::URI(uri), credentials).doPost(cto);
140   return crto.getCode() == CommandResultTO::OK;
141 }
142
143 const gde::AttributeGroup
144 gde::AttributesService::updateAttributeGroup(const gde::AttributeGroup& attributeGroup)
145 {
146   std::string data = _buildAttributesJsonString(attributeGroup);
147   CommandTO cto(UPDATE_ATTRIBUTE_GROUP, data);
148
149   const Credentials& credentials = _session.getCredentials();
150   std::string uri = _session.getServiceURI(_servletName);
151   CommandResultTO crto = gde::HttpConnection(Poco::URI(uri), credentials).doPost(cto);
152
153   // build AttributeGroup object from CommandResultTO data (json format)
154   {
155     Poco::JSON::Object::Ptr object = JsonFormatter::parse(crto.getData());
156     int id = JsonFormatter::extract<int>(object, "id");
157     std::vector<Attribute> attributes = JsonFormatter::extractVector<gde::Attribute>(object, "attributeCollection");
158     return gde::AttributeGroup(id, attributes);
159   }
160 }
161
162 const gde::AttributeGroup
163 gde::AttributesService::readAttributeGroup(int attributeGroupId)
164 {
165   CommandTO cto(READ_ATTRIBUTE_GROUP);
166   cto.setParameter("attributeGroupId", attributeGroupId);
167
168   const Credentials& credentials = _session.getCredentials();
169   std::string uri = _session.getServiceURI(_servletName);
170   CommandResultTO crto = gde::HttpConnection(Poco::URI(uri), credentials).doPost(cto);
171
172   // build AttributeGroup object from CommandResultTO data (json format)
173   {
174     Poco::JSON::Object::Ptr object = JsonFormatter::parse(crto.getData());
175     int id = JsonFormatter::extract<int>(object, "id");
176     std::vector<Attribute> attributes = JsonFormatter::extractVector<gde::Attribute>(object, "attributeCollection");
177     return gde::AttributeGroup(id, attributes);
178   }
179 }