Salome HOME
AttributesService
[modules/gde.git] / projects / GDE_API_CPP / api / src / AttributesService.cpp
diff --git a/projects/GDE_API_CPP/api/src/AttributesService.cpp b/projects/GDE_API_CPP/api/src/AttributesService.cpp
new file mode 100644 (file)
index 0000000..882d2d8
--- /dev/null
@@ -0,0 +1,178 @@
+#include "AttributesService.hpp"
+#include "HttpConnection.hpp"
+#include "CommandTO.hpp"
+#include "CommandResultTO.hpp"
+#include "JsonFormatter.hpp"
+#include "Credentials.hpp"
+
+#include <vector>
+#include <typeinfo>
+
+/*
+ * WARNING: keep enum values synchronized to WEB API (AttributesService)
+ */
+enum {
+  CREATE_ATTRIBUTE = 1,
+  DELETE_ATTRIBUTE,
+  READ_ATTRIBUTE,
+  CREATE_ATTRIBUTE_GROUP,
+  DELETE_ATTRIBUTE_GROUP,
+  UPDATE_ATTRIBUTE_GROUP,
+  READ_ATTRIBUTE_GROUP
+};
+
+std::string gde::AttributesService::_servletName = "AttributesService";
+
+const gde::Attribute
+gde::AttributesService::createAttribute(const std::string& name, const std::string& type, const std::string& value, int groupId, bool mandatory)
+{
+  // build JSON string for CommandTO data
+  Poco::JSON::Object obj;
+  obj.set("name", Poco::Dynamic::Var(name));
+  obj.set("type", Poco::Dynamic::Var(type));
+  obj.set("value", Poco::Dynamic::Var(value));
+  obj.set("groupId", Poco::Dynamic::Var(groupId));
+  obj.set("mandatory", Poco::Dynamic::Var(mandatory));
+  std::string data = JsonFormatter::stringify(obj);
+
+  CommandTO cto(CREATE_ATTRIBUTE, data);
+  const Credentials& credentials = _session.getCredentials();
+  std::string uri = _session.getServiceURI(_servletName);
+  CommandResultTO crto = gde::HttpConnection(Poco::URI(uri), credentials).doPost(cto);
+
+  // build Attribute object from CommandResultTO data (json format)
+  {
+    Poco::JSON::Object::Ptr object = JsonFormatter::parse(crto.getData());
+    int id = JsonFormatter::extract<int>(object, "id");
+    std::string name = JsonFormatter::extract<std::string>(object, "name");
+    std::string type = JsonFormatter::extract<std::string>(object, "type");
+    std::string value = JsonFormatter::extract<std::string>(object, "value");
+    int groupId = JsonFormatter::extract<int>(object, "groupId");
+    bool mandatory = JsonFormatter::extract<bool>(object, "mandatory");
+    return gde::Attribute(id, name, type, value, groupId, mandatory);
+  }
+}
+
+bool
+gde::AttributesService::deleteAttribute(const Attribute& attribute)
+{
+  CommandTO cto(DELETE_ATTRIBUTE);
+  cto.setParameter("attributeId", attribute.getId());
+
+  const Credentials& credentials = _session.getCredentials();
+  std::string uri = _session.getServiceURI(_servletName);
+  CommandResultTO crto = gde::HttpConnection(Poco::URI(uri), credentials).doPost(cto);
+  return crto.getCode() == CommandResultTO::OK;
+}
+
+const gde::Attribute
+gde::AttributesService::readAttribute(int attributeId)
+{
+  CommandTO cto(READ_ATTRIBUTE);
+  cto.setParameter("attributeId", attributeId);
+
+  const Credentials& credentials = _session.getCredentials();
+  std::string uri = _session.getServiceURI(_servletName);
+  CommandResultTO crto = gde::HttpConnection(Poco::URI(uri), credentials).doPost(cto);
+
+  // build Attribute object from CommandResultTO data (json format)
+  {
+    Poco::JSON::Object::Ptr object = JsonFormatter::parse(crto.getData());
+    int id = JsonFormatter::extract<int>(object, "id");
+    std::string name = JsonFormatter::extract<std::string>(object, "name");
+    std::string type = JsonFormatter::extract<std::string>(object, "type");
+    std::string value = JsonFormatter::extract<std::string>(object, "value");
+    int groupId = JsonFormatter::extract<int>(object, "groupId");
+    bool mandatory = JsonFormatter::extract<bool>(object, "mandatory");
+    return gde::Attribute(id, name, type, value, groupId, mandatory);
+  }
+}
+
+std::string
+_buildAttributesJsonString(int attributeGroupId, const std::vector<gde::Attribute>& attributes)
+{
+  // Solution here is not very smart. Have tried using Poco to stringify attributes,
+  // but stringification is too recursive, for example it produces:
+  // "attributeCollection":"[ { \"id\" : 0, \"name\" : \"myAttribute\",...
+  // instead of:
+  // "attributeCollection":[{"id":0,"name":"myAttribute",...
+  std::string data = "{";
+  data += gde::JsonFormatter::format("\"id\":", attributeGroupId);
+  data += ", \"attributeCollection\":[";
+  for (std::vector<gde::Attribute>::const_iterator itr = attributes.begin(); itr != attributes.end(); ++itr) {
+    if (itr != attributes.begin())
+      data += ",";
+    data += itr->toJson();
+  }
+  data += "]}";
+  return data;
+}
+
+const gde::AttributeGroup
+gde::AttributesService::createAttributeGroup(const AttributeGroup& attributeGroup)
+{
+  std::string data = _buildAttributesJsonString(attributeGroup.getId(), attributeGroup.getAttributes());
+  CommandTO cto(CREATE_ATTRIBUTE_GROUP, data);
+
+  const Credentials& credentials = _session.getCredentials();
+  std::string uri = _session.getServiceURI(_servletName);
+  CommandResultTO crto = gde::HttpConnection(Poco::URI(uri), credentials).doPost(cto);
+
+  // build AttributeGroup object from CommandResultTO data (json format)
+  {
+    Poco::JSON::Object::Ptr object = JsonFormatter::parse(crto.getData());
+    int id = JsonFormatter::extract<int>(object, "id");
+    std::vector<gde::Attribute> attributes = JsonFormatter::extractVector(object, "attributeCollection");
+    return gde::AttributeGroup(id, attributes);
+  }
+}
+
+bool
+gde::AttributesService::deleteAttributeGroup(const gde::AttributeGroup& attributeGroup)
+{
+  CommandTO cto(DELETE_ATTRIBUTE_GROUP);
+  cto.setParameter("attributeGroupId", attributeGroup.getId());
+
+  const Credentials& credentials = _session.getCredentials();
+  std::string uri = _session.getServiceURI(_servletName);
+  CommandResultTO crto = gde::HttpConnection(Poco::URI(uri), credentials).doPost(cto);
+  return crto.getCode() == CommandResultTO::OK;
+}
+
+const gde::AttributeGroup
+gde::AttributesService::updateAttributeGroup(const gde::AttributeGroup& attributeGroup, const std::vector<Attribute>& attributes)
+{
+  std::string data = _buildAttributesJsonString(attributeGroup.getId(), attributes);
+  CommandTO cto(UPDATE_ATTRIBUTE_GROUP, data);
+
+  const Credentials& credentials = _session.getCredentials();
+  std::string uri = _session.getServiceURI(_servletName);
+  CommandResultTO crto = gde::HttpConnection(Poco::URI(uri), credentials).doPost(cto);
+
+  // build AttributeGroup object from CommandResultTO data (json format)
+  {
+    Poco::JSON::Object::Ptr object = JsonFormatter::parse(crto.getData());
+    int id = JsonFormatter::extract<int>(object, "id");
+    std::vector<Attribute> attributes = JsonFormatter::extractVector(object, "attributeCollection");
+    return gde::AttributeGroup(id, attributes);
+  }
+}
+
+const gde::AttributeGroup
+gde::AttributesService::readAttributeGroup(int attributeGroupId)
+{
+  CommandTO cto(READ_ATTRIBUTE_GROUP);
+  cto.setParameter("attributeGroupId", attributeGroupId);
+
+  const Credentials& credentials = _session.getCredentials();
+  std::string uri = _session.getServiceURI(_servletName);
+  CommandResultTO crto = gde::HttpConnection(Poco::URI(uri), credentials).doPost(cto);
+
+  // build AttributeGroup object from CommandResultTO data (json format)
+  {
+    Poco::JSON::Object::Ptr object = JsonFormatter::parse(crto.getData());
+    int id = JsonFormatter::extract<int>(object, "id");
+    std::vector<Attribute> attributes = JsonFormatter::extractVector(object, "attributeCollection");
+    return gde::AttributeGroup(id, attributes);
+  }
+}