Salome HOME
FileService agr/api_cpp
authorCédric Aguerre <cedric.aguerre@edf.fr>
Mon, 31 Aug 2015 16:40:07 +0000 (18:40 +0200)
committerCédric Aguerre <cedric.aguerre@edf.fr>
Mon, 31 Aug 2015 16:40:07 +0000 (18:40 +0200)
projects/GDE_API_CPP/api/src/CMakeLists.txt
projects/GDE_API_CPP/api/src/File.hpp [new file with mode: 0644]
projects/GDE_API_CPP/api/src/FileService.cpp [new file with mode: 0644]
projects/GDE_API_CPP/api/src/FileService.hpp [new file with mode: 0644]
projects/GDE_API_CPP/api/src/GDESession.cpp
projects/GDE_API_CPP/api/src/GDESession.hpp
projects/GDE_API_CPP/api/tests/FileTest.cpp [new file with mode: 0644]
projects/GDE_API_CPP/api/tests/FileTest.hpp [new file with mode: 0644]

index e80baf66a161e7940a6312fcd1651ec219f120d6..fe254b2e6e8b5014c0fd2a7d618ba0bbe702f26f 100644 (file)
@@ -21,6 +21,7 @@ SET(gde_api_cpp_HEADERS
   CommandResultTO.hpp
   CommandTO.hpp
   Credentials.hpp
+  File.hpp
   GDESession.hpp
   Group.hpp
   HttpConnection.hpp
@@ -39,6 +40,7 @@ SET(gde_api_cpp_SOURCES
   CommandResultTO.cpp
   CommandTO.cpp
   CommandTO.tpp
+  FileService.cpp
   GDESession.cpp
   HttpConnection.cpp
   JsonFormatter.cpp
diff --git a/projects/GDE_API_CPP/api/src/File.hpp b/projects/GDE_API_CPP/api/src/File.hpp
new file mode 100644 (file)
index 0000000..23b036c
--- /dev/null
@@ -0,0 +1,89 @@
+#ifndef GDE_FILE_HPP
+#define GDE_FILE_HPP
+
+#include <Poco/Timestamp.h>
+
+#include <string>
+
+namespace gde {
+
+  class File {
+    friend class FileService;
+    friend class GDESession;
+
+  public:
+    ~File() {}
+
+    inline int getId() const { return _id; }
+    inline void setId(int id) { this->_id = id; }
+
+    inline std::string getName() const { return _name; }
+    inline void setName(const std::string& name) { this->_name = name; }
+
+    inline int getLength() const { return _length; }
+    inline void setLength(int length) { this->_length = length; }
+
+    inline std::string getChecksum() const { return _checksum; }
+    inline void setChecksum(const std::string& checksum) { this->_checksum = checksum; }
+
+    inline Poco::Timestamp getCreationDate() const { return _creationDate; }
+    inline void setCreationDate(const Poco::Timestamp& creationDate) { this->_creationDate = creationDate; }
+
+    inline Poco::Timestamp getUpdateDate() const { return _updateDate; }
+    inline void setUpdateDate(const Poco::Timestamp& updateDate) { this->_updateDate = updateDate; }
+
+    inline bool getValid() const { return _valid; }
+    inline void setValid(bool valid) { this->_valid = valid; }
+
+    inline bool getDeleted() const { return _deleted; }
+    inline void setDeleted(bool deleted) { this->_deleted = deleted; }
+
+    inline Poco::Timestamp getDeletionDate() const { return _deletionDate; }
+    inline void setDeletionDate(const Poco::Timestamp& deletionDate) { this->_deletionDate = deletionDate; }
+
+    inline int getAttributeGroupId() const { return _attributeGroupId; }
+    inline void setAttributeGroupId(int attributeGroupId) { this->_attributeGroupId = attributeGroupId; }
+
+    inline std::string getData() const { return _data; }
+    inline void setData(const std::string& data) { this->_data = data; }
+
+    inline int getDataProfileId() const { return _dataProfileId; }
+    inline void setDataProfileId(int dataProfileId) { this->_dataProfileId = dataProfileId; }
+
+  private:
+    File(int id=-1,
+         const std::string& name="",
+         int length=-1,
+         const std::string& checksum="",
+         const Poco::Timestamp& creationDate=Poco::Timestamp(),
+         const Poco::Timestamp& updateDate=Poco::Timestamp(),
+         bool valid = false,
+         bool deleted = false,
+         const Poco::Timestamp& deletionDate=Poco::Timestamp(),
+         int attributeGroupId=-1,
+         const std::string& data="",
+         int dataProfileId=-1
+         )
+      : _id(id), _name(name), _length(length), _checksum(checksum), _creationDate(creationDate), _updateDate(updateDate), _valid(valid), _deleted(deleted), _deletionDate(deletionDate), _attributeGroupId(attributeGroupId), _data(data), _dataProfileId(dataProfileId)
+    {}
+    //File(const File&); // non copyable
+    //File& operator=(const File&); // non copyable
+
+  private:
+    int _id;
+    std::string _name;
+    int _length;
+    std::string _checksum;
+    Poco::Timestamp _creationDate;
+    Poco::Timestamp _updateDate;
+    bool _valid;
+    bool _deleted;
+    Poco::Timestamp _deletionDate;
+    int _attributeGroupId;
+    std::string _data;
+    int _dataProfileId;
+  };
+
+};
+
+#endif
diff --git a/projects/GDE_API_CPP/api/src/FileService.cpp b/projects/GDE_API_CPP/api/src/FileService.cpp
new file mode 100644 (file)
index 0000000..8af9267
--- /dev/null
@@ -0,0 +1,175 @@
+#include "FileService.hpp"
+#include "HttpConnection.hpp"
+#include "CommandTO.hpp"
+#include "CommandResultTO.hpp"
+#include "JsonFormatter.hpp"
+#include "Credentials.hpp"
+
+#include <Poco/DateTimeFormatter.h>
+#include <Poco/Timestamp.h>
+#include <Poco/DateTimeFormat.h>
+
+/*
+ * WARNING: keep enum values synchronized to WEB API (FileService)
+ */
+enum {
+  CREATE_FILE = 1,
+  DELETE_FILE,
+  UPDATE_FILE,
+  READ_FILE,
+  FIND_BY_NAME
+};
+
+std::string gde::FileService::_servletName = "FileService";
+
+const gde::File
+gde::FileService::createFile(const std::string& name)
+{
+  // build JSON string for CommandTO data
+  Poco::JSON::Object obj;
+  obj.set("name", Poco::Dynamic::Var(name));
+  std::string creationDate = Poco::DateTimeFormatter::format(Poco::Timestamp(), Poco::DateTimeFormat::SORTABLE_FORMAT);
+  obj.set("creationDate", Poco::Dynamic::Var(creationDate));
+  std::string data = JsonFormatter::stringify(obj);
+
+  CommandTO cto(CREATE_FILE, data);
+  const Credentials& credentials = _session.getCredentials();
+  std::string uri = _session.getServiceURI(_servletName);
+  CommandResultTO crto = gde::HttpConnection(Poco::URI(uri), credentials).doPost(cto);
+
+  // build File 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");
+    int length = JsonFormatter::extract<int>(object, "length");
+    std::string checksum = JsonFormatter::extract<std::string>(object, "checksum");
+    Poco::Timestamp creationDate = JsonFormatter::extract(object, "creationDate");
+    Poco::Timestamp updateDate = JsonFormatter::extract(object, "updateDate");
+    bool valid = JsonFormatter::extract<bool>(object, "valid");
+    bool deleted = JsonFormatter::extract<bool>(object, "deleted");
+    Poco::Timestamp deletionDate = JsonFormatter::extract(object, "deletionDate");
+    int attributeGroupId = JsonFormatter::extract<int>(object, "attributeGroupId");
+    std::string data = JsonFormatter::extract<std::string>(object, "data");
+    int dataProfileId = JsonFormatter::extract<int>(object, "dataProfileId");
+
+    return gde::File(id, name, length, checksum, creationDate, updateDate, valid, deleted, deletionDate, attributeGroupId, data, dataProfileId);
+  }
+}
+
+bool
+gde::FileService::deleteFile(const File& file)
+{
+  CommandTO cto(DELETE_FILE);
+  cto.setParameter("fileId", file.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::File
+gde::FileService::updateFile(const File& file)
+{
+  // build JSON string for CommandTO data
+  Poco::JSON::Object obj;
+  obj.set("id", Poco::Dynamic::Var(file.getId()));
+  obj.set("name", Poco::Dynamic::Var(file.getName()));
+  obj.set("length", Poco::Dynamic::Var(file.getLength()));
+  obj.set("checksum", Poco::Dynamic::Var(file.getChecksum()));
+  obj.set("creationDate", Poco::Dynamic::Var(file.getCreationDate()));
+  obj.set("valid", Poco::Dynamic::Var(file.getValid()));
+  obj.set("deleted", Poco::Dynamic::Var(file.getDeleted()));
+  obj.set("deletionDate", Poco::Dynamic::Var(file.getDeletionDate()));
+  obj.set("attributeGroupId", Poco::Dynamic::Var(file.getAttributeGroupId()));
+  obj.set("data", Poco::Dynamic::Var(file.getData()));
+  obj.set("dataProfileId", Poco::Dynamic::Var(file.getDataProfileId()));
+  std::string updateDate = Poco::DateTimeFormatter::format(Poco::Timestamp(), Poco::DateTimeFormat::SORTABLE_FORMAT);
+  obj.set("updateDate", Poco::Dynamic::Var(updateDate));
+  std::string data = JsonFormatter::stringify(obj);
+
+  CommandTO cto(UPDATE_FILE, data);
+  const Credentials& credentials = _session.getCredentials();
+  std::string uri = _session.getServiceURI(_servletName);
+  CommandResultTO crto = gde::HttpConnection(Poco::URI(uri), credentials).doPost(cto);
+
+  // build File 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");
+    int length = JsonFormatter::extract<int>(object, "length");
+    std::string checksum = JsonFormatter::extract<std::string>(object, "checksum");
+    Poco::Timestamp creationDate = JsonFormatter::extract(object, "creationDate");
+    Poco::Timestamp updateDate = JsonFormatter::extract(object, "updateDate");
+    bool valid = JsonFormatter::extract<bool>(object, "valid");
+    bool deleted = JsonFormatter::extract<bool>(object, "deleted");
+    Poco::Timestamp deletionDate = JsonFormatter::extract(object, "deletionDate");
+    int attributeGroupId = JsonFormatter::extract<int>(object, "attributeGroupId");
+    std::string data = JsonFormatter::extract<std::string>(object, "data");
+    int dataProfileId = JsonFormatter::extract<int>(object, "dataProfileId");
+
+    return gde::File(id, name, length, checksum, creationDate, updateDate, valid, deleted, deletionDate, attributeGroupId, data, dataProfileId);
+  }
+}
+
+const gde::File
+gde::FileService::readFile(int fileId)
+{
+  CommandTO cto(READ_FILE);
+  cto.setParameter("fileId", fileId);
+
+  const Credentials& credentials = _session.getCredentials();
+  std::string uri = _session.getServiceURI(_servletName);
+  CommandResultTO crto = gde::HttpConnection(Poco::URI(uri), credentials).doPost(cto);
+
+  // build File 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");
+    int length = JsonFormatter::extract<int>(object, "length");
+    std::string checksum = JsonFormatter::extract<std::string>(object, "checksum");
+    Poco::Timestamp creationDate = JsonFormatter::extract(object, "creationDate");
+    Poco::Timestamp updateDate = JsonFormatter::extract(object, "updateDate");
+    bool valid = JsonFormatter::extract<bool>(object, "valid");
+    bool deleted = JsonFormatter::extract<bool>(object, "deleted");
+    Poco::Timestamp deletionDate = JsonFormatter::extract(object, "deletionDate");
+    int attributeGroupId = JsonFormatter::extract<int>(object, "attributeGroupId");
+    std::string data = JsonFormatter::extract<std::string>(object, "data");
+    int dataProfileId = JsonFormatter::extract<int>(object, "dataProfileId");
+
+    return gde::File(id, name, length, checksum, creationDate, updateDate, valid, deleted, deletionDate, attributeGroupId, data, dataProfileId);
+  }
+}
+
+const gde::File
+gde::FileService::findByName(const std::string& name)
+{
+  CommandTO cto(FIND_BY_NAME);
+  cto.setParameter("fileName", name);
+
+  const Credentials& credentials = _session.getCredentials();
+  std::string uri = _session.getServiceURI(_servletName);
+  CommandResultTO crto = gde::HttpConnection(Poco::URI(uri), credentials).doPost(cto);
+
+  // build File 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");
+    int length = JsonFormatter::extract<int>(object, "length");
+    std::string checksum = JsonFormatter::extract<std::string>(object, "checksum");
+    Poco::Timestamp creationDate = JsonFormatter::extract(object, "creationDate");
+    Poco::Timestamp updateDate = JsonFormatter::extract(object, "updateDate");
+    bool valid = JsonFormatter::extract<bool>(object, "valid");
+    bool deleted = JsonFormatter::extract<bool>(object, "deleted");
+    Poco::Timestamp deletionDate = JsonFormatter::extract(object, "deletionDate");
+    int attributeGroupId = JsonFormatter::extract<int>(object, "attributeGroupId");
+    std::string data = JsonFormatter::extract<std::string>(object, "data");
+    int dataProfileId = JsonFormatter::extract<int>(object, "dataProfileId");
+
+    return gde::File(id, name, length, checksum, creationDate, updateDate, valid, deleted, deletionDate, attributeGroupId, data, dataProfileId);
+  }
+}
diff --git a/projects/GDE_API_CPP/api/src/FileService.hpp b/projects/GDE_API_CPP/api/src/FileService.hpp
new file mode 100644 (file)
index 0000000..3b8eb6d
--- /dev/null
@@ -0,0 +1,35 @@
+#ifndef GDE_FILE_SERVICE_HPP
+#define GDE_FILE_SERVICE_HPP
+
+#include "File.hpp"
+#include "GDESession.hpp"
+
+#include <string>
+
+namespace gde {
+
+  class FileService {
+    friend class GDESession;
+
+  public:
+    const File createFile(const std::string& name);
+    bool deleteFile(const File&);
+    const File updateFile(const File&);
+    const File readFile(int fileId);
+    const File findByName(const std::string& name);
+
+  private:
+    FileService(const GDESession& session) : _session(session) {}
+    ~FileService() {}
+    FileService(const FileService&); // non copyable
+    FileService& operator=(const FileService&); // non copyable
+
+  private:
+    static std::string _servletName;
+    const GDESession& _session;
+
+  };
+
+};
+
+#endif
index 7abdf9dc2f9aae3b4e2a343944225b8dfa3d5e0d..a86850a92dcaa257abb9045de78fe83d1167f94f 100644 (file)
@@ -3,6 +3,7 @@
 #include "StudyService.hpp"
 #include "AttributesService.hpp"
 #include "ProfilesService.hpp"
+#include "FileService.hpp"
 
 std::string
 gde::GDESession::getServiceURI(const std::string& serviceName) const
@@ -187,3 +188,35 @@ gde::GDESession::updateProfileAttribute(const gde::ProfileAttribute& attribute)
 {
   return gde::ProfilesService(*this).updateProfileAttribute(attribute);
 }
+
+/* FileService */
+
+const gde::File
+gde::GDESession::createFile(const std::string& name)
+{
+  return gde::FileService(*this).createFile(name);
+}
+
+bool
+gde::GDESession::deleteFile(const gde::File& file)
+{
+  return gde::FileService(*this).deleteFile(file);
+}
+
+const gde::File
+gde::GDESession::updateFile(const gde::File& file)
+{
+  return gde::FileService(*this).updateFile(file);
+}
+
+const gde::File
+gde::GDESession::readFile(int fileId)
+{
+  return gde::FileService(*this).readFile(fileId);
+}
+
+const gde::File
+gde::GDESession::findByName(const std::string& name)
+{
+  return gde::FileService(*this).findByName(name);
+}
index 0ed8c347c61e0ba84a34d0a5325241ea70bc12fd..dcfee98f6d718a0d63276f735f91c151178bb008 100644 (file)
@@ -9,6 +9,7 @@
 #include "AttributeGroup.hpp"
 #include "Profile.hpp"
 #include "ProfileAttribute.hpp"
+#include "File.hpp"
 
 #include <string>
 #include <vector>
@@ -79,6 +80,14 @@ namespace gde {
     const ProfileAttribute readProfileAttribute(int);
     const ProfileAttribute updateProfileAttribute(const ProfileAttribute&);
 
+    /* FileService */
+
+    const File createFile(const std::string& name);
+    bool deleteFile(const File&);
+    const File updateFile(const File&);
+    const File readFile(int fileId);
+    const File findByName(const std::string& name);
+
   private:
     std::string _serverAddress;
     Credentials _credentials;
diff --git a/projects/GDE_API_CPP/api/tests/FileTest.cpp b/projects/GDE_API_CPP/api/tests/FileTest.cpp
new file mode 100644 (file)
index 0000000..67e4588
--- /dev/null
@@ -0,0 +1,77 @@
+#include <iostream>
+
+#include <TestUtilities.hpp>
+#include <File.hpp>
+#include <GDESession.hpp>
+#include <FileTest.hpp>
+
+void
+FileTest::testCreateDeleteFile()
+{
+  gde::GDESession session(serverAddress, credentials);
+
+  const gde::File& file = session.createFile("myFile.txt");
+  CPPUNIT_ASSERT(file.getName() == "myFile.txt");
+  CPPUNIT_ASSERT(file.getId() > 0);
+
+  CPPUNIT_ASSERT(session.deleteFile(file));
+}
+
+void
+FileTest::testReadFile()
+{
+  gde::GDESession session(serverAddress, credentials);
+
+  const gde::File& file = session.createFile("myFile.txt");
+  CPPUNIT_ASSERT(file.getName() == "myFile.txt");
+  CPPUNIT_ASSERT(file.getId() > 0);
+
+  {
+    const gde::File& f = session.readFile(file.getId());
+    CPPUNIT_ASSERT(f.getName() == file.getName());
+    CPPUNIT_ASSERT(f.getId() == file.getId());
+  }
+
+  CPPUNIT_ASSERT(session.deleteFile(file));
+}
+
+void
+FileTest::testUpdateFile()
+{
+  gde::GDESession session(serverAddress, credentials);
+
+  const gde::File& file = session.createFile("myFile.txt");
+  CPPUNIT_ASSERT(file.getName() == "myFile.txt");
+  CPPUNIT_ASSERT(file.getId() > 0);
+
+  gde::File file2(file);
+  file2.setName("myFile_update.txt");
+  CPPUNIT_ASSERT(file2.getName() == "myFile_update.txt");
+  CPPUNIT_ASSERT(file.getId() == file2.getId());
+
+  {
+    const gde::File& f = session.updateFile(file2);
+    CPPUNIT_ASSERT(f.getName() == "myFile_update.txt");
+    CPPUNIT_ASSERT(f.getId() == file2.getId());
+  }
+
+  CPPUNIT_ASSERT(session.deleteFile(file));
+}
+
+void
+FileTest::testFindByName()
+{
+  gde::GDESession session(serverAddress, credentials);
+
+  const gde::File& file = session.createFile("myFile.txt");
+  CPPUNIT_ASSERT(file.getName() == "myFile.txt");
+  CPPUNIT_ASSERT(file.getId() > 0);
+
+  {
+    const gde::File& f = session.findByName("myFile.txt");
+    CPPUNIT_ASSERT(f.getName() == "myFile.txt");
+    CPPUNIT_ASSERT(f.getId() == file.getId());
+  }
+
+  CPPUNIT_ASSERT(session.deleteFile(file));
+}
diff --git a/projects/GDE_API_CPP/api/tests/FileTest.hpp b/projects/GDE_API_CPP/api/tests/FileTest.hpp
new file mode 100644 (file)
index 0000000..e516c14
--- /dev/null
@@ -0,0 +1,24 @@
+#ifndef GDE_FILE_TEST_HPP
+#define GDE_FILE_TEST_HPP
+
+#include <cppunit/extensions/HelperMacros.h>
+
+class FileTest : public CppUnit::TestFixture
+{
+  CPPUNIT_TEST_SUITE(FileTest);
+  CPPUNIT_TEST(testCreateDeleteFile);
+  CPPUNIT_TEST(testReadFile);
+  CPPUNIT_TEST(testUpdateFile);
+  CPPUNIT_TEST(testFindByName);
+  CPPUNIT_TEST_SUITE_END();
+
+public:
+  void testCreateDeleteFile();
+  void testReadFile();
+  void testUpdateFile();
+  void testFindByName();
+};
+
+CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(FileTest, "FileTest");
+
+#endif