1 #include "ProfilesService.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)
19 CREATE_PROFILE_ATTRIBUTE,
20 DELETE_PROFILE_ATTRIBUTE,
21 READ_PROFILE_ATTRIBUTE,
22 UPDATE_PROFILE_ATTRIBUTE
25 std::string gde::ProfilesService::_servletName = "ProfilesService";
28 _buildAttributesJsonString(const gde::Profile& profile)
30 // Solution here is not very smart. Have tried using Poco to stringify attributes,
31 // but stringification is too recursive, for example it produces:
32 // "attributes":"[ { \"id\" : 0, \"name\" : \"myAttribute\",...
34 // "attributes":[{"id":0,"name":"myAttribute",...
35 const std::vector<gde::ProfileAttribute>& attributes = profile.getAttributes();
36 std::string data = "{";
37 data += gde::JsonFormatter::format("\"id\":", profile.getId());
38 data += gde::JsonFormatter::format(", \"name\":", profile.getName());
39 data += ", \"attributes\":[";
40 for (std::vector<gde::ProfileAttribute>::const_iterator itr = attributes.begin(); itr != attributes.end(); ++itr) {
41 if (itr != attributes.begin())
43 data += itr->toJson();
50 gde::ProfilesService::createProfile(const gde::Profile& profile)
52 std::string data = _buildAttributesJsonString(profile);
53 CommandTO cto(CREATE_PROFILE, data);
55 const Credentials& credentials = _session.getCredentials();
56 std::string uri = _session.getServiceURI(_servletName);
57 CommandResultTO crto = gde::HttpConnection(Poco::URI(uri), credentials).doPost(cto);
59 // build Profile object from CommandResultTO data (json format)
61 Poco::JSON::Object::Ptr object = JsonFormatter::parse(crto.getData());
62 int id = JsonFormatter::extract<int>(object, "id");
63 std::string name = JsonFormatter::extract<std::string>(object, "name");
64 std::vector<gde::ProfileAttribute> attributes = JsonFormatter::extractVector<gde::ProfileAttribute>(object, "attributes");
65 return gde::Profile(id, name, attributes);
70 gde::ProfilesService::deleteProfile(const gde::Profile& profile)
72 CommandTO cto(DELETE_PROFILE);
73 cto.setParameter("profileId", profile.getId());
75 const Credentials& credentials = _session.getCredentials();
76 std::string uri = _session.getServiceURI(_servletName);
77 CommandResultTO crto = gde::HttpConnection(Poco::URI(uri), credentials).doPost(cto);
78 return crto.getCode() == CommandResultTO::OK;
82 gde::ProfilesService::readProfile(int profileId)
84 CommandTO cto(READ_PROFILE);
85 cto.setParameter("profileId", profileId);
87 const Credentials& credentials = _session.getCredentials();
88 std::string uri = _session.getServiceURI(_servletName);
89 CommandResultTO crto = gde::HttpConnection(Poco::URI(uri), credentials).doPost(cto);
91 // build Profile object from CommandResultTO data (json format)
93 Poco::JSON::Object::Ptr object = JsonFormatter::parse(crto.getData());
94 int id = JsonFormatter::extract<int>(object, "id");
95 std::string name = JsonFormatter::extract<std::string>(object, "name");
96 std::vector<gde::ProfileAttribute> attributes = JsonFormatter::extractVector<gde::ProfileAttribute>(object, "attributes");
97 return gde::Profile(id, name, attributes);
102 gde::ProfilesService::updateProfile(const gde::Profile& profile)
104 std::string data = _buildAttributesJsonString(profile);
105 CommandTO cto(UPDATE_PROFILE, data);
107 const Credentials& credentials = _session.getCredentials();
108 std::string uri = _session.getServiceURI(_servletName);
109 CommandResultTO crto = gde::HttpConnection(Poco::URI(uri), credentials).doPost(cto);
111 // build Profile object from CommandResultTO data (json format)
113 Poco::JSON::Object::Ptr object = JsonFormatter::parse(crto.getData());
114 int id = JsonFormatter::extract<int>(object, "id");
115 std::string name = JsonFormatter::extract<std::string>(object, "name");
116 std::vector<gde::ProfileAttribute> attributes = JsonFormatter::extractVector<gde::ProfileAttribute>(object, "attributes");
117 return gde::Profile(id, name, attributes);
121 const gde::ProfileAttribute
122 gde::ProfilesService::createProfileAttribute(const std::string& name, const std::string& type, int profileId, bool mandatory)
124 // build JSON string for CommandTO data
125 Poco::JSON::Object obj;
126 obj.set("name", Poco::Dynamic::Var(name));
127 obj.set("type", Poco::Dynamic::Var(type));
128 obj.set("profileId", Poco::Dynamic::Var(profileId));
129 obj.set("mandatory", Poco::Dynamic::Var(mandatory));
130 std::string data = JsonFormatter::stringify(obj);
132 CommandTO cto(CREATE_PROFILE_ATTRIBUTE, data);
133 const Credentials& credentials = _session.getCredentials();
134 std::string uri = _session.getServiceURI(_servletName);
135 CommandResultTO crto = gde::HttpConnection(Poco::URI(uri), credentials).doPost(cto);
137 // build ProfileAttribute object from CommandResultTO data (json format)
139 Poco::JSON::Object::Ptr object = JsonFormatter::parse(crto.getData());
140 int id = JsonFormatter::extract<int>(object, "id");
141 std::string name = JsonFormatter::extract<std::string>(object, "name");
142 std::string type = JsonFormatter::extract<std::string>(object, "type");
143 int profileId = JsonFormatter::extract<int>(object, "profileId");
144 bool mandatory = JsonFormatter::extract<bool>(object, "mandatory");
145 return gde::ProfileAttribute(id, name, type, profileId, mandatory);
150 gde::ProfilesService::deleteProfileAttribute(const gde::ProfileAttribute& attribute)
152 CommandTO cto(DELETE_PROFILE_ATTRIBUTE);
153 cto.setParameter("profileAttributeId", attribute.getId());
155 const Credentials& credentials = _session.getCredentials();
156 std::string uri = _session.getServiceURI(_servletName);
157 CommandResultTO crto = gde::HttpConnection(Poco::URI(uri), credentials).doPost(cto);
158 return crto.getCode() == CommandResultTO::OK;
161 const gde::ProfileAttribute
162 gde::ProfilesService::readProfileAttribute(int attributeId)
164 CommandTO cto(READ_PROFILE_ATTRIBUTE);
165 cto.setParameter("profileAttributeId", attributeId);
167 const Credentials& credentials = _session.getCredentials();
168 std::string uri = _session.getServiceURI(_servletName);
169 CommandResultTO crto = gde::HttpConnection(Poco::URI(uri), credentials).doPost(cto);
171 // build ProfileAttribute object from CommandResultTO data (json format)
173 Poco::JSON::Object::Ptr object = JsonFormatter::parse(crto.getData());
174 int id = JsonFormatter::extract<int>(object, "id");
175 std::string name = JsonFormatter::extract<std::string>(object, "name");
176 std::string type = JsonFormatter::extract<std::string>(object, "type");
177 int profileId = JsonFormatter::extract<int>(object, "profileId");
178 bool mandatory = JsonFormatter::extract<bool>(object, "mandatory");
179 return gde::ProfileAttribute(id, name, type, profileId, mandatory);
183 const gde::ProfileAttribute
184 gde::ProfilesService::updateProfileAttribute(const gde::ProfileAttribute& attribute)
186 // build JSON string for CommandTO data
187 Poco::JSON::Object obj;
188 obj.set("id", Poco::Dynamic::Var(attribute.getId()));
189 obj.set("name", Poco::Dynamic::Var(attribute.getName()));
190 obj.set("type", Poco::Dynamic::Var(attribute.getType()));
191 obj.set("profileId", Poco::Dynamic::Var(attribute.getProfileId()));
192 obj.set("mandatory", Poco::Dynamic::Var(attribute.getMandatory()));
193 std::string data = JsonFormatter::stringify(obj);
194 CommandTO cto(UPDATE_PROFILE_ATTRIBUTE, data);
196 const Credentials& credentials = _session.getCredentials();
197 std::string uri = _session.getServiceURI(_servletName);
198 CommandResultTO crto = gde::HttpConnection(Poco::URI(uri), credentials).doPost(cto);
200 // build ProfileAttribute object from CommandResultTO data (json format)
202 Poco::JSON::Object::Ptr object = JsonFormatter::parse(crto.getData());
203 int id = JsonFormatter::extract<int>(object, "id");
204 std::string name = JsonFormatter::extract<std::string>(object, "name");
205 std::string type = JsonFormatter::extract<std::string>(object, "type");
206 int profileId = JsonFormatter::extract<int>(object, "profileId");
207 bool mandatory = JsonFormatter::extract<bool>(object, "mandatory");
208 return gde::ProfileAttribute(id, name, type, profileId, mandatory);