]> SALOME platform Git repositories - modules/gde.git/blob - projects/GDE_API_CPP/api/src/ProfilesService.cpp
Salome HOME
FileService
[modules/gde.git] / projects / GDE_API_CPP / api / src / ProfilesService.cpp
1 #include "ProfilesService.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_PROFILE = 1,
16   DELETE_PROFILE,
17   READ_PROFILE,
18   UPDATE_PROFILE,
19   CREATE_PROFILE_ATTRIBUTE,
20   DELETE_PROFILE_ATTRIBUTE,
21   READ_PROFILE_ATTRIBUTE,
22   UPDATE_PROFILE_ATTRIBUTE
23 };
24
25 std::string gde::ProfilesService::_servletName = "ProfilesService";
26
27 std::string
28 _buildAttributesJsonString(const gde::Profile& profile)
29 {
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\",...
33   // instead of:
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())
42       data += ",";
43     data += itr->toJson();
44   }
45   data += "]}";
46   return data;
47 }
48
49 const gde::Profile
50 gde::ProfilesService::createProfile(const gde::Profile& profile)
51 {
52   std::string data = _buildAttributesJsonString(profile);
53   CommandTO cto(CREATE_PROFILE, data);
54
55   const Credentials& credentials = _session.getCredentials();
56   std::string uri = _session.getServiceURI(_servletName);
57   CommandResultTO crto = gde::HttpConnection(Poco::URI(uri), credentials).doPost(cto);
58
59   // build Profile object from CommandResultTO data (json format)
60   {
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);
66   }
67 }
68
69 bool
70 gde::ProfilesService::deleteProfile(const gde::Profile& profile)
71 {
72   CommandTO cto(DELETE_PROFILE);
73   cto.setParameter("profileId", profile.getId());
74
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;
79 }
80
81 const gde::Profile
82 gde::ProfilesService::readProfile(int profileId)
83 {
84   CommandTO cto(READ_PROFILE);
85   cto.setParameter("profileId", profileId);
86
87   const Credentials& credentials = _session.getCredentials();
88   std::string uri = _session.getServiceURI(_servletName);
89   CommandResultTO crto = gde::HttpConnection(Poco::URI(uri), credentials).doPost(cto);
90
91   // build Profile object from CommandResultTO data (json format)
92   {
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);
98   }
99 }
100
101 const gde::Profile
102 gde::ProfilesService::updateProfile(const gde::Profile& profile)
103 {
104   std::string data = _buildAttributesJsonString(profile);
105   CommandTO cto(UPDATE_PROFILE, data);
106
107   const Credentials& credentials = _session.getCredentials();
108   std::string uri = _session.getServiceURI(_servletName);
109   CommandResultTO crto = gde::HttpConnection(Poco::URI(uri), credentials).doPost(cto);
110
111   // build Profile object from CommandResultTO data (json format)
112   {
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);
118   }
119 }
120
121 const gde::ProfileAttribute
122 gde::ProfilesService::createProfileAttribute(const std::string& name, const std::string& type, int profileId, bool mandatory)
123 {
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);
131
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);
136
137   // build ProfileAttribute object from CommandResultTO data (json format)
138   {
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);
146   }
147 }
148
149 bool
150 gde::ProfilesService::deleteProfileAttribute(const gde::ProfileAttribute& attribute)
151 {
152   CommandTO cto(DELETE_PROFILE_ATTRIBUTE);
153   cto.setParameter("profileAttributeId", attribute.getId());
154
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;
159 }
160
161 const gde::ProfileAttribute
162 gde::ProfilesService::readProfileAttribute(int attributeId)
163 {
164   CommandTO cto(READ_PROFILE_ATTRIBUTE);
165   cto.setParameter("profileAttributeId", attributeId);
166
167   const Credentials& credentials = _session.getCredentials();
168   std::string uri = _session.getServiceURI(_servletName);
169   CommandResultTO crto = gde::HttpConnection(Poco::URI(uri), credentials).doPost(cto);
170
171   // build ProfileAttribute object from CommandResultTO data (json format)
172   {
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);
180   }
181 }
182
183 const gde::ProfileAttribute
184 gde::ProfilesService::updateProfileAttribute(const gde::ProfileAttribute& attribute)
185 {
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);
195
196   const Credentials& credentials = _session.getCredentials();
197   std::string uri = _session.getServiceURI(_servletName);
198   CommandResultTO crto = gde::HttpConnection(Poco::URI(uri), credentials).doPost(cto);
199
200   // build ProfileAttribute object from CommandResultTO data (json format)
201   {
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);
209   }
210 }