Salome HOME
FileService
[modules/gde.git] / projects / GDE_API_CPP / api / src / StudyService.cpp
1 #include "StudyService.hpp"
2 #include "HttpConnection.hpp"
3 #include "CommandTO.hpp"
4 #include "CommandResultTO.hpp"
5 #include "JsonFormatter.hpp"
6 #include "Credentials.hpp"
7
8 #include <Poco/DateTimeFormatter.h>
9 #include <Poco/Timestamp.h>
10 #include <Poco/DateTimeFormat.h>
11
12 /*
13  * WARNING: keep enum values synchronized to WEB API (StudyService)
14  */
15 enum {
16   CREATE_STUDY = 1,
17   SET_STUDY_SATE,
18   READ_STUDY,
19   DELETE_STUDY
20 };
21
22 std::string gde::StudyService::_servletName = "StudyService";
23
24 const gde::Study
25 gde::StudyService::createStudy(const std::string& name)
26 {
27   // build JSON string for CommandTO data
28   Poco::JSON::Object obj;
29   obj.set("name", Poco::Dynamic::Var(name));
30   std::string creationDate = Poco::DateTimeFormatter::format(Poco::Timestamp(), Poco::DateTimeFormat::SORTABLE_FORMAT);
31   obj.set("creationDate", Poco::Dynamic::Var(creationDate));
32   std::string data = JsonFormatter::stringify(obj);
33
34   CommandTO cto(CREATE_STUDY, data);
35   const Credentials& credentials = _session.getCredentials();
36   std::string uri = _session.getServiceURI(_servletName);
37   CommandResultTO crto = gde::HttpConnection(Poco::URI(uri), credentials).doPost(cto);
38
39   // build Study object from CommandResultTO data (json format)
40   {
41     Poco::JSON::Object::Ptr object = JsonFormatter::parse(crto.getData());
42     int id = JsonFormatter::extract<int>(object, "id");
43     std::string name = JsonFormatter::extract<std::string>(object, "name");
44     Poco::Timestamp creationDate = JsonFormatter::extract(object, "creationDate");
45     Poco::Timestamp updateDate = JsonFormatter::extract(object, "updateDate");
46     bool valid = JsonFormatter::extract<bool>(object, "valid");
47     bool deleted = JsonFormatter::extract<bool>(object, "deleted");
48     Poco::Timestamp deletionDate = JsonFormatter::extract(object, "deletionDate");
49     int attributeGroupId = JsonFormatter::extract<int>(object, "attributeGroupId");
50     int profileId = JsonFormatter::extract<int>(object, "profileId");
51     bool locked = JsonFormatter::extract<bool>(object, "locked");
52     int lockOwner = JsonFormatter::extract<int>(object, "lockOwner");
53
54     return gde::Study(id, name, creationDate, updateDate, valid, deleted, deletionDate, attributeGroupId, profileId, locked, lockOwner);
55   }
56 }
57
58 bool
59 gde::StudyService::deleteStudy(const Study& study)
60 {
61   CommandTO cto(DELETE_STUDY);
62   cto.setParameter("studyId", study.getId());
63
64   const Credentials& credentials = _session.getCredentials();
65   std::string uri = _session.getServiceURI(_servletName);
66   CommandResultTO crto = gde::HttpConnection(Poco::URI(uri), credentials).doPost(cto);
67   return crto.getCode() == CommandResultTO::OK;
68 }
69
70 bool
71 gde::StudyService::setStudyState(const Study& study, int lock)
72 {
73   CommandTO cto(SET_STUDY_SATE);
74   cto.setParameter("studyId", study.getId());
75   cto.setParameter("lock", lock);
76
77   const Credentials& credentials = _session.getCredentials();
78   std::string uri = _session.getServiceURI(_servletName);
79   CommandResultTO crto = gde::HttpConnection(Poco::URI(uri), credentials).doPost(cto);
80   return crto.getCode() == CommandResultTO::OK;
81 }
82
83 const gde::Study
84 gde::StudyService::readStudy(int studyId)
85 {
86   CommandTO cto(READ_STUDY);
87   cto.setParameter("studyId", studyId);
88
89   const Credentials& credentials = _session.getCredentials();
90   std::string uri = _session.getServiceURI(_servletName);
91   CommandResultTO crto = gde::HttpConnection(Poco::URI(uri), credentials).doPost(cto);
92
93   // build Study object from CommandResultTO data (json format)
94   {
95     Poco::JSON::Object::Ptr object = JsonFormatter::parse(crto.getData());
96     int id = JsonFormatter::extract<int>(object, "id");
97     std::string name = JsonFormatter::extract<std::string>(object, "name");
98     Poco::Timestamp creationDate = JsonFormatter::extract(object, "creationDate");
99     Poco::Timestamp updateDate = JsonFormatter::extract(object, "updateDate");
100     bool valid = JsonFormatter::extract<bool>(object, "valid");
101     bool deleted = JsonFormatter::extract<bool>(object, "deleted");
102     Poco::Timestamp deletionDate = JsonFormatter::extract(object, "deletionDate");
103     int attributeGroupId = JsonFormatter::extract<int>(object, "attributeGroupId");
104     int profileId = JsonFormatter::extract<int>(object, "profileId");
105     bool locked = JsonFormatter::extract<bool>(object, "locked");
106     int lockOwner = JsonFormatter::extract<int>(object, "lockOwner");
107
108     return gde::Study(id, name, creationDate, updateDate, valid, deleted, deletionDate, attributeGroupId, profileId, locked, lockOwner);
109   }
110 }