1 #include "AttributesService.hpp"
2 #include "HttpConnection.hpp"
3 #include "CommandTO.hpp"
4 #include "CommandResultTO.hpp"
5 #include "JsonFormatter.hpp"
6 #include "Credentials.hpp"
12 * WARNING: keep enum values synchronized to WEB API (AttributesService)
18 CREATE_ATTRIBUTE_GROUP,
19 DELETE_ATTRIBUTE_GROUP,
20 UPDATE_ATTRIBUTE_GROUP,
24 std::string gde::AttributesService::_servletName = "AttributesService";
27 gde::AttributesService::createAttribute(const std::string& name, const std::string& type, const std::string& value, int groupId, bool mandatory)
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);
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);
43 // build Attribute object from CommandResultTO data (json format)
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);
57 gde::AttributesService::deleteAttribute(const Attribute& attribute)
59 CommandTO cto(DELETE_ATTRIBUTE);
60 cto.setParameter("attributeId", attribute.getId());
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;
69 gde::AttributesService::readAttribute(int attributeId)
71 CommandTO cto(READ_ATTRIBUTE);
72 cto.setParameter("attributeId", attributeId);
74 const Credentials& credentials = _session.getCredentials();
75 std::string uri = _session.getServiceURI(_servletName);
76 CommandResultTO crto = gde::HttpConnection(Poco::URI(uri), credentials).doPost(cto);
78 // build Attribute object from CommandResultTO data (json format)
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);
92 _buildAttributesJsonString(const gde::AttributeGroup& attributeGroup)
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\",...
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())
106 data += itr->toJson();
112 const gde::AttributeGroup
113 gde::AttributesService::createAttributeGroup(const AttributeGroup& attributeGroup)
115 std::string data = _buildAttributesJsonString(attributeGroup);
116 CommandTO cto(CREATE_ATTRIBUTE_GROUP, data);
118 const Credentials& credentials = _session.getCredentials();
119 std::string uri = _session.getServiceURI(_servletName);
120 CommandResultTO crto = gde::HttpConnection(Poco::URI(uri), credentials).doPost(cto);
122 // build AttributeGroup object from CommandResultTO data (json format)
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);
132 gde::AttributesService::deleteAttributeGroup(const gde::AttributeGroup& attributeGroup)
134 CommandTO cto(DELETE_ATTRIBUTE_GROUP);
135 cto.setParameter("attributeGroupId", attributeGroup.getId());
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;
143 const gde::AttributeGroup
144 gde::AttributesService::updateAttributeGroup(const gde::AttributeGroup& attributeGroup)
146 std::string data = _buildAttributesJsonString(attributeGroup);
147 CommandTO cto(UPDATE_ATTRIBUTE_GROUP, data);
149 const Credentials& credentials = _session.getCredentials();
150 std::string uri = _session.getServiceURI(_servletName);
151 CommandResultTO crto = gde::HttpConnection(Poco::URI(uri), credentials).doPost(cto);
153 // build AttributeGroup object from CommandResultTO data (json format)
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);
162 const gde::AttributeGroup
163 gde::AttributesService::readAttributeGroup(int attributeGroupId)
165 CommandTO cto(READ_ATTRIBUTE_GROUP);
166 cto.setParameter("attributeGroupId", attributeGroupId);
168 const Credentials& credentials = _session.getCredentials();
169 std::string uri = _session.getServiceURI(_servletName);
170 CommandResultTO crto = gde::HttpConnection(Poco::URI(uri), credentials).doPost(cto);
172 // build AttributeGroup object from CommandResultTO data (json format)
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);