From: Cédric Aguerre Date: Mon, 31 Aug 2015 16:40:07 +0000 (+0200) Subject: FileService X-Git-Tag: gde-v0.1~9 X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;ds=sidebyside;h=refs%2Fheads%2Fagr%2Fapi_cpp;p=modules%2Fgde.git FileService --- diff --git a/projects/GDE_API_CPP/api/src/CMakeLists.txt b/projects/GDE_API_CPP/api/src/CMakeLists.txt index e80baf6..fe254b2 100644 --- a/projects/GDE_API_CPP/api/src/CMakeLists.txt +++ b/projects/GDE_API_CPP/api/src/CMakeLists.txt @@ -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 index 0000000..23b036c --- /dev/null +++ b/projects/GDE_API_CPP/api/src/File.hpp @@ -0,0 +1,89 @@ +#ifndef GDE_FILE_HPP +#define GDE_FILE_HPP + +#include + +#include + +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 index 0000000..8af9267 --- /dev/null +++ b/projects/GDE_API_CPP/api/src/FileService.cpp @@ -0,0 +1,175 @@ +#include "FileService.hpp" +#include "HttpConnection.hpp" +#include "CommandTO.hpp" +#include "CommandResultTO.hpp" +#include "JsonFormatter.hpp" +#include "Credentials.hpp" + +#include +#include +#include + +/* + * 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(object, "id"); + std::string name = JsonFormatter::extract(object, "name"); + int length = JsonFormatter::extract(object, "length"); + std::string checksum = JsonFormatter::extract(object, "checksum"); + Poco::Timestamp creationDate = JsonFormatter::extract(object, "creationDate"); + Poco::Timestamp updateDate = JsonFormatter::extract(object, "updateDate"); + bool valid = JsonFormatter::extract(object, "valid"); + bool deleted = JsonFormatter::extract(object, "deleted"); + Poco::Timestamp deletionDate = JsonFormatter::extract(object, "deletionDate"); + int attributeGroupId = JsonFormatter::extract(object, "attributeGroupId"); + std::string data = JsonFormatter::extract(object, "data"); + int dataProfileId = JsonFormatter::extract(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(object, "id"); + std::string name = JsonFormatter::extract(object, "name"); + int length = JsonFormatter::extract(object, "length"); + std::string checksum = JsonFormatter::extract(object, "checksum"); + Poco::Timestamp creationDate = JsonFormatter::extract(object, "creationDate"); + Poco::Timestamp updateDate = JsonFormatter::extract(object, "updateDate"); + bool valid = JsonFormatter::extract(object, "valid"); + bool deleted = JsonFormatter::extract(object, "deleted"); + Poco::Timestamp deletionDate = JsonFormatter::extract(object, "deletionDate"); + int attributeGroupId = JsonFormatter::extract(object, "attributeGroupId"); + std::string data = JsonFormatter::extract(object, "data"); + int dataProfileId = JsonFormatter::extract(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(object, "id"); + std::string name = JsonFormatter::extract(object, "name"); + int length = JsonFormatter::extract(object, "length"); + std::string checksum = JsonFormatter::extract(object, "checksum"); + Poco::Timestamp creationDate = JsonFormatter::extract(object, "creationDate"); + Poco::Timestamp updateDate = JsonFormatter::extract(object, "updateDate"); + bool valid = JsonFormatter::extract(object, "valid"); + bool deleted = JsonFormatter::extract(object, "deleted"); + Poco::Timestamp deletionDate = JsonFormatter::extract(object, "deletionDate"); + int attributeGroupId = JsonFormatter::extract(object, "attributeGroupId"); + std::string data = JsonFormatter::extract(object, "data"); + int dataProfileId = JsonFormatter::extract(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(object, "id"); + std::string name = JsonFormatter::extract(object, "name"); + int length = JsonFormatter::extract(object, "length"); + std::string checksum = JsonFormatter::extract(object, "checksum"); + Poco::Timestamp creationDate = JsonFormatter::extract(object, "creationDate"); + Poco::Timestamp updateDate = JsonFormatter::extract(object, "updateDate"); + bool valid = JsonFormatter::extract(object, "valid"); + bool deleted = JsonFormatter::extract(object, "deleted"); + Poco::Timestamp deletionDate = JsonFormatter::extract(object, "deletionDate"); + int attributeGroupId = JsonFormatter::extract(object, "attributeGroupId"); + std::string data = JsonFormatter::extract(object, "data"); + int dataProfileId = JsonFormatter::extract(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 index 0000000..3b8eb6d --- /dev/null +++ b/projects/GDE_API_CPP/api/src/FileService.hpp @@ -0,0 +1,35 @@ +#ifndef GDE_FILE_SERVICE_HPP +#define GDE_FILE_SERVICE_HPP + +#include "File.hpp" +#include "GDESession.hpp" + +#include + +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 diff --git a/projects/GDE_API_CPP/api/src/GDESession.cpp b/projects/GDE_API_CPP/api/src/GDESession.cpp index 7abdf9d..a86850a 100644 --- a/projects/GDE_API_CPP/api/src/GDESession.cpp +++ b/projects/GDE_API_CPP/api/src/GDESession.cpp @@ -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); +} diff --git a/projects/GDE_API_CPP/api/src/GDESession.hpp b/projects/GDE_API_CPP/api/src/GDESession.hpp index 0ed8c34..dcfee98 100644 --- a/projects/GDE_API_CPP/api/src/GDESession.hpp +++ b/projects/GDE_API_CPP/api/src/GDESession.hpp @@ -9,6 +9,7 @@ #include "AttributeGroup.hpp" #include "Profile.hpp" #include "ProfileAttribute.hpp" +#include "File.hpp" #include #include @@ -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 index 0000000..67e4588 --- /dev/null +++ b/projects/GDE_API_CPP/api/tests/FileTest.cpp @@ -0,0 +1,77 @@ +#include + +#include +#include +#include +#include + +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 index 0000000..e516c14 --- /dev/null +++ b/projects/GDE_API_CPP/api/tests/FileTest.hpp @@ -0,0 +1,24 @@ +#ifndef GDE_FILE_TEST_HPP +#define GDE_FILE_TEST_HPP + +#include + +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