summaryrefslogtreecommitdiffstats
path: root/reader/src/database/networkdb
diff options
context:
space:
mode:
authorMichele Calgaro <michele.calgaro@yahoo.it>2024-06-07 23:30:05 +0900
committerMichele Calgaro <michele.calgaro@yahoo.it>2024-06-07 23:30:05 +0900
commit17b259df9cb6b28779d4881b2b6c805ee2e48eea (patch)
tree5ed61937459cb7081089111b0242c01ec178f1f3 /reader/src/database/networkdb
parent1cba8bce178eb2d6719c6f7f21e2c9352c5513a6 (diff)
downloadtde-ebook-reader-17b259df9cb6b28779d4881b2b6c805ee2e48eea.tar.gz
tde-ebook-reader-17b259df9cb6b28779d4881b2b6c805ee2e48eea.zip
Rename to tde-ebook-reader
Signed-off-by: Michele Calgaro <michele.calgaro@yahoo.it>
Diffstat (limited to 'reader/src/database/networkdb')
-rw-r--r--reader/src/database/networkdb/DBRunnables.h81
-rw-r--r--reader/src/database/networkdb/NetworkDB.cpp152
-rw-r--r--reader/src/database/networkdb/NetworkDB.h76
-rw-r--r--reader/src/database/networkdb/NetworkDBQuery.cpp103
-rw-r--r--reader/src/database/networkdb/NetworkDBQuery.h48
-rw-r--r--reader/src/database/networkdb/runnables/ClearNetworkDBRunnable.cpp39
-rw-r--r--reader/src/database/networkdb/runnables/InitNetworkDBRunnable.cpp30
-rw-r--r--reader/src/database/networkdb/runnables/SaveNetworkLinkRunnable.cpp138
8 files changed, 667 insertions, 0 deletions
diff --git a/reader/src/database/networkdb/DBRunnables.h b/reader/src/database/networkdb/DBRunnables.h
new file mode 100644
index 0000000..e9633f6
--- /dev/null
+++ b/reader/src/database/networkdb/DBRunnables.h
@@ -0,0 +1,81 @@
+/*
+ * Copyright (C) 2004-2012 Geometer Plus <contact@geometerplus.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301, USA.
+ */
+
+#ifndef __DBRUNNABLES_H__
+#define __DBRUNNABLES_H__
+
+#include "../sqldb/DBConnection.h"
+#include "../sqldb/DBCommand.h"
+#include "../sqldb/DBRunnable.h"
+
+#include "NetworkDBQuery.h"
+
+#include "../../network/NetworkLink.h"
+#include "../../network/opds/OPDSLink.h"
+
+class InitNetworkDBRunnable : public DBRunnable {
+
+public:
+ InitNetworkDBRunnable(DBConnection &connection);
+ bool run();
+
+private:
+ DBConnection &myConnection;
+};
+
+class ClearNetworkDBRunnable : public DBRunnable {
+
+public:
+ ClearNetworkDBRunnable(DBConnection &connection);
+ bool run();
+
+private:
+ DBConnection &myConnection;
+};
+
+class SaveNetworkLinkRunnable : public DBRunnable {
+
+public:
+ SaveNetworkLinkRunnable(DBConnection &connection);
+ bool run();
+ void setNetworkLink(shared_ptr<NetworkLink> link);
+
+private:
+ bool addNetworkLink();
+ bool updateNetworkLink(int linkId);
+ bool updateNetworkLinkUrls(int linkId);
+
+private:
+ shared_ptr<NetworkLink> myNetworkLink;
+
+ shared_ptr<DBCommand> myFindNetworkLinkId;
+ shared_ptr<DBCommand> myAddNetworkLink;
+ shared_ptr<DBCommand> myUpdateNetworkLink;
+
+ shared_ptr<DBCommand> myFindNetworkLinkUrls;
+ shared_ptr<DBCommand> myAddNetworkLinkUrl;
+ shared_ptr<DBCommand> myUpdateNetworkLinkUrl;
+ shared_ptr<DBCommand> myDeleteNetworkLinkUrl;
+
+};
+
+inline InitNetworkDBRunnable::InitNetworkDBRunnable(DBConnection &connection) : myConnection(connection) {}
+inline ClearNetworkDBRunnable::ClearNetworkDBRunnable(DBConnection &connection) : myConnection(connection) {}
+
+#endif /* __DBRUNNABLES_H__ */
diff --git a/reader/src/database/networkdb/NetworkDB.cpp b/reader/src/database/networkdb/NetworkDB.cpp
new file mode 100644
index 0000000..f422643
--- /dev/null
+++ b/reader/src/database/networkdb/NetworkDB.cpp
@@ -0,0 +1,152 @@
+/*
+ * Copyright (C) 2004-2012 Geometer Plus <contact@geometerplus.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301, USA.
+ */
+
+#include <ZLibrary.h>
+#include <ZLDir.h>
+
+#include "NetworkDB.h"
+
+shared_ptr<NetworkDB> NetworkDB::ourInstance = 0;
+
+const std::string NetworkDB::DATABASE_NAME = "network.db";
+
+NetworkDB &NetworkDB::Instance() {
+ if (ourInstance.isNull()) {
+ ZLFile dir(databaseDirName());
+ dir.directory(true);
+ ZLFile file(databaseDirName() + ZLibrary::FileNameDelimiter + DATABASE_NAME);
+ ourInstance = new NetworkDB(file.physicalFilePath());
+ ourInstance->initDatabase();
+ }
+ return *ourInstance;
+}
+
+NetworkDB::NetworkDB(const std::string &path) : SQLiteDataBase(path), myInitialized(false) {
+ initCommands();
+}
+
+NetworkDB::~NetworkDB() {
+}
+
+bool NetworkDB::initDatabase() {
+ if (isInitialized()) {
+ return true;
+ }
+
+ if (!open()) {
+ return false;
+ }
+
+ myInitialized = true;
+
+ shared_ptr<DBRunnable> runnable = new InitNetworkDBRunnable(connection());
+ if (!executeAsTransaction(*runnable)) {
+ myInitialized = false;
+ close();
+ return false;
+ }
+
+ return true;
+}
+
+void NetworkDB::initCommands() {
+ myLoadNetworkLinks = SQLiteFactory::createCommand(NetworkDBQuery::LOAD_NETWORK_LINKS, connection());
+ myLoadNetworkLinkUrls = SQLiteFactory::createCommand(NetworkDBQuery::LOAD_NETWORK_LINKURLS, connection(), "@link_id", DBValue::DBINT);
+ myFindNetworkLinkId = SQLiteFactory::createCommand(NetworkDBQuery::FIND_NETWORK_LINK_ID, connection(), "@site_name", DBValue::DBTEXT);
+ myDeleteNetworkLinkUrls = SQLiteFactory::createCommand(NetworkDBQuery::DELETE_NETWORK_LINKURLS, connection(), "@link_id", DBValue::DBINT);
+ myDeleteNetworkLink = SQLiteFactory::createCommand(NetworkDBQuery::DELETE_NETWORK_LINK, connection(), "@link_id", DBValue::DBINT);
+
+ mySaveNetworkLink = new SaveNetworkLinkRunnable(connection());
+}
+
+bool NetworkDB::clearDatabase() {
+ if (!isInitialized()) {
+ return false;
+ }
+ shared_ptr<DBRunnable> runnable = new ClearNetworkDBRunnable(connection());
+ return executeAsTransaction(*runnable);
+}
+
+
+bool NetworkDB::saveNetworkLink(shared_ptr<NetworkLink> link) {
+ if (!isInitialized()) {
+ return false;
+ }
+ mySaveNetworkLink->setNetworkLink(link);
+ bool result = executeAsTransaction(*mySaveNetworkLink);
+ return result;
+}
+
+bool NetworkDB::loadNetworkLinks(std::vector<shared_ptr<NetworkLink> >& links) {
+ shared_ptr<DBDataReader> reader = myLoadNetworkLinks->executeReader();
+
+ links.clear();
+
+ while (reader->next()) {
+ if (reader->type(0) != DBValue::DBINT) {/* link_id */
+ return false;
+ }
+ std::map<std::string,std::string> linkUrls;
+ ((DBIntValue &) *myLoadNetworkLinkUrls->parameter("@link_id").value()) = reader->intValue(0);
+ shared_ptr<DBDataReader> urlreader = myLoadNetworkLinkUrls->executeReader();
+ long t = 0;
+ while (urlreader->next()) {
+ linkUrls[urlreader->textValue(0, std::string())] = urlreader->textValue(1, std::string());
+ t = urlreader->intValue(2);
+ }
+ shared_ptr<ATOMUpdated> atomUpdated = new ATOMUpdated();
+ atomUpdated->setLongSeconds_stupid(t);
+ std::string iconUrl;
+ if (linkUrls .count("icon") != 0) {
+ iconUrl = linkUrls["icon"];
+ linkUrls.erase("icon");
+ }
+ std::string siteName = reader->textValue(2, std::string());
+ std::string predId = reader->textValue(5, std::string());
+ std::string title = reader->textValue(1, std::string());
+ std::string summary = reader->textValue(3, std::string());
+ std::string language = reader->textValue(4, std::string());
+ bool isEnabled = reader->intValue(6) == 1;
+
+ shared_ptr<NetworkLink> link = new OPDSLink(siteName);
+ link->setTitle(title);
+ link->setSummary(summary);
+ link->setLanguage(language);
+ link->setIcon(iconUrl);
+ link->setLinks(linkUrls);
+ link->setPredefinedId(predId);
+ link->setEnabled(isEnabled);
+ link->setUpdated(atomUpdated);
+ links.push_back(link);
+ }
+ return true;
+}
+
+bool NetworkDB::deleteNetworkLink(const std::string &siteName){
+ ((DBTextValue &) *myFindNetworkLinkId->parameter("@site_name").value()) = siteName;
+ shared_ptr<DBDataReader> reader = myFindNetworkLinkId->executeReader();
+ if (reader.isNull() || !reader->next()) {
+ return false;
+ }
+ int linkId = reader->intValue(0);
+ ((DBIntValue &) *myDeleteNetworkLink->parameter("@link_id").value()) = linkId;
+ ((DBIntValue &) *myDeleteNetworkLinkUrls->parameter("@link_id").value()) = linkId;
+ return myDeleteNetworkLinkUrls->execute() && myDeleteNetworkLink->execute();
+
+}
diff --git a/reader/src/database/networkdb/NetworkDB.h b/reader/src/database/networkdb/NetworkDB.h
new file mode 100644
index 0000000..d4761a6
--- /dev/null
+++ b/reader/src/database/networkdb/NetworkDB.h
@@ -0,0 +1,76 @@
+/*
+ * Copyright (C) 2004-2012 Geometer Plus <contact@geometerplus.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301, USA.
+ */
+
+#ifndef __NETWORKDB_H__
+#define __NETWORKDB_H__
+
+#include <set>
+#include <map>
+#include <deque>
+
+#include "../sqldb/implsqlite/SQLiteDataBase.h"
+#include "DBRunnables.h"
+
+class NetworkDB : public SQLiteDataBase {
+
+public:
+ static const std::string DATABASE_NAME;
+
+ static NetworkDB &Instance();
+
+private:
+ static shared_ptr<NetworkDB> ourInstance;
+
+ NetworkDB(const std::string &path);
+
+public:
+ virtual ~NetworkDB();
+
+public:
+ bool initDatabase();
+ bool isInitialized() const;
+ bool clearDatabase();
+
+ bool saveNetworkLink(shared_ptr<NetworkLink> link);
+ bool loadNetworkLinks(std::vector<shared_ptr<NetworkLink> >& links);
+ bool deleteNetworkLink(const std::string &siteName);
+
+private:
+ void initCommands();
+
+private:
+ bool myInitialized;
+
+ shared_ptr<DBCommand> myLoadNetworkLinks;
+ shared_ptr<DBCommand> myFindNetworkLinkId;
+ shared_ptr<DBCommand> myDeleteNetworkLink;
+ shared_ptr<DBCommand> myDeleteNetworkLinkUrls;
+ shared_ptr<DBCommand> myLoadNetworkLinkUrls;
+
+ shared_ptr<SaveNetworkLinkRunnable> mySaveNetworkLink;
+
+private: // disable copying
+ NetworkDB(const NetworkDB &);
+ const NetworkDB &operator = (const NetworkDB &);
+};
+
+
+inline bool NetworkDB::isInitialized() const { return myInitialized; }
+
+#endif /* __NETWORKDB_H__ */
diff --git a/reader/src/database/networkdb/NetworkDBQuery.cpp b/reader/src/database/networkdb/NetworkDBQuery.cpp
new file mode 100644
index 0000000..a8771cb
--- /dev/null
+++ b/reader/src/database/networkdb/NetworkDBQuery.cpp
@@ -0,0 +1,103 @@
+/*
+ * Copyright (C) 2004-2012 Geometer Plus <contact@geometerplus.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301, USA.
+ */
+
+#include "NetworkDBQuery.h"
+
+const std::string NetworkDBQuery::INIT_DATABASE = \
+ " CREATE TABLE IF NOT EXISTS Links( " \
+ " link_id INTEGER PRIMARY KEY, " \
+ " title TEXT NOT NULL, " \
+ " site_name TEXT NOT NULL, " \
+ " summary TEXT, " \
+ " language TEXT, " \
+ " predefined_id TEXT, " \
+ " is_enabled INTEGER " \
+ "); " \
+ " " \
+ "CREATE TABLE IF NOT EXISTS LinkUrls( " \
+ " key TEXT NOT NULL, " \
+ " link_id INTEGER NOT NULL REFERENCES Links(link_id), " \
+ " url TEXT, " \
+ " update_time INTEGER, " \
+ " CONSTRAINT LinkUrls_PK PRIMARY KEY (key, link_id) " \
+ "); ";
+
+const std::string NetworkDBQuery::CLEAR_DATABASE = \
+ "DROP TABLE Links; " \
+ "DROP TABLE LinkUrls; ";
+
+
+const std::string NetworkDBQuery::FIND_NETWORK_LINK_ID = "SELECT link_id, predefined_id FROM Links WHERE site_name = @site_name; ";
+const std::string NetworkDBQuery::ADD_NETWORK_LINK = \
+ "INSERT INTO Links (title, site_name, summary, language, predefined_id, is_enabled) " \
+ " VALUES ( " \
+ " @title, " \
+ " @site_name, " \
+ " @summary, " \
+ " nullif(@language,\"\"), " \
+ " nullif(@predefined_id,\"\"), " \
+ " @is_enabled " \
+ " ); " \
+ " " \
+ "SELECT last_insert_rowid() AS link_id; ";
+
+const std::string NetworkDBQuery::DELETE_NETWORK_LINK = \
+ "DELETE FROM Links WHERE link_id = @link_id; ";
+
+const std::string NetworkDBQuery::UPDATE_NETWORK_LINK = \
+ "UPDATE Links SET " \
+ " title = @title, " \
+ " summary = @summary, " \
+ " language = nullif(@language,\"\"), " \
+ " predefined_id = nullif(@predefined_id,\"\"), " \
+ " is_enabled = @is_enabled " \
+ "WHERE " \
+ " link_id = @link_id; ";
+
+const std::string NetworkDBQuery::ADD_NETWORK_LINKURL = \
+ "INSERT INTO LinkUrls (key, link_id, url, update_time) " \
+ " VALUES ( " \
+ " @key, " \
+ " @link_id, " \
+ " @url, " \
+ " @update_time " \
+ " ); ";
+
+const std::string NetworkDBQuery::FIND_NETWORK_LINKURLS = "SELECT key, url, update_time FROM LinkUrls WHERE link_id = @link_id; ";
+
+const std::string NetworkDBQuery::UPDATE_NETWORK_LINKURL = \
+ "UPDATE LinkUrls SET " \
+ " url = @url, " \
+ " update_time = @update_time " \
+ "WHERE " \
+ " link_id = @link_id AND key = @key; ";
+
+const std::string NetworkDBQuery::DELETE_NETWORK_LINKURLS = \
+ "DELETE FROM LinkUrls " \
+ "WHERE " \
+ " link_id = @link_id; ";
+
+const std::string NetworkDBQuery::DELETE_NETWORK_LINKURL = \
+ "DELETE FROM LinkUrls " \
+ "WHERE " \
+ " link_id = @link_id AND key = @key; ";
+
+const std::string NetworkDBQuery::LOAD_NETWORK_LINKS = "SELECT link_id, title, site_name, summary, language, predefined_id, is_enabled FROM Links; ";
+
+const std::string NetworkDBQuery::LOAD_NETWORK_LINKURLS = "SELECT key, url, update_time FROM LinkUrls WHERE link_id = @link_id; ";
diff --git a/reader/src/database/networkdb/NetworkDBQuery.h b/reader/src/database/networkdb/NetworkDBQuery.h
new file mode 100644
index 0000000..931705e
--- /dev/null
+++ b/reader/src/database/networkdb/NetworkDBQuery.h
@@ -0,0 +1,48 @@
+/*
+ * Copyright (C) 2004-2012 Geometer Plus <contact@geometerplus.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301, USA.
+ */
+
+#ifndef __NETWORKDBQUERY_H__
+#define __NETWORKDBQUERY_H__
+
+#include <string>
+
+class NetworkDBQuery {
+
+public:
+ static const std::string INIT_DATABASE;
+ static const std::string CLEAR_DATABASE;
+
+ static const std::string FIND_NETWORK_LINK_ID;
+ static const std::string ADD_NETWORK_LINK;
+ static const std::string UPDATE_NETWORK_LINK;
+ static const std::string DELETE_NETWORK_LINK;
+
+ static const std::string ADD_NETWORK_LINKURL;
+ static const std::string DELETE_NETWORK_LINKURLS;
+ static const std::string FIND_NETWORK_LINKURLS;
+ static const std::string DELETE_NETWORK_LINKURL;
+ static const std::string UPDATE_NETWORK_LINKURL;
+
+ static const std::string LOAD_NETWORK_LINKS;
+ static const std::string LOAD_NETWORK_LINKURLS;
+
+private: // disable creation Instances
+ NetworkDBQuery();
+};
+#endif /* __NETWORKDBQUERY_H__ */
diff --git a/reader/src/database/networkdb/runnables/ClearNetworkDBRunnable.cpp b/reader/src/database/networkdb/runnables/ClearNetworkDBRunnable.cpp
new file mode 100644
index 0000000..fe79ed3
--- /dev/null
+++ b/reader/src/database/networkdb/runnables/ClearNetworkDBRunnable.cpp
@@ -0,0 +1,39 @@
+/*
+ * Copyright (C) 2009-2012 Geometer Plus <contact@geometerplus.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301, USA.
+ */
+
+#include "../DBRunnables.h"
+#include "../../sqldb/implsqlite/SQLiteFactory.h"
+
+bool ClearNetworkDBRunnable::run() {
+ shared_ptr<DBCommand> cmd;
+
+ cmd = SQLiteFactory::createCommand(NetworkDBQuery::CLEAR_DATABASE, myConnection);
+ if (!cmd->execute()) {
+ return false;
+ }
+
+ cmd = SQLiteFactory::createCommand(NetworkDBQuery::INIT_DATABASE, myConnection);
+ if (!cmd->execute()) {
+ return false;
+ }
+
+ return true;
+}
+
+
diff --git a/reader/src/database/networkdb/runnables/InitNetworkDBRunnable.cpp b/reader/src/database/networkdb/runnables/InitNetworkDBRunnable.cpp
new file mode 100644
index 0000000..d0730b7
--- /dev/null
+++ b/reader/src/database/networkdb/runnables/InitNetworkDBRunnable.cpp
@@ -0,0 +1,30 @@
+/*
+ * Copyright (C) 2009-2012 Geometer Plus <contact@geometerplus.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301, USA.
+ */
+
+#include "../DBRunnables.h"
+#include "../../sqldb/implsqlite/SQLiteFactory.h"
+
+bool InitNetworkDBRunnable::run() {
+ shared_ptr<DBCommand> cmd;
+ cmd = SQLiteFactory::createCommand(NetworkDBQuery::INIT_DATABASE, myConnection);
+ if (!cmd->execute()) {
+ return false;
+ }
+ return true;
+}
diff --git a/reader/src/database/networkdb/runnables/SaveNetworkLinkRunnable.cpp b/reader/src/database/networkdb/runnables/SaveNetworkLinkRunnable.cpp
new file mode 100644
index 0000000..4c80499
--- /dev/null
+++ b/reader/src/database/networkdb/runnables/SaveNetworkLinkRunnable.cpp
@@ -0,0 +1,138 @@
+/*
+ * Copyright (C) 2009-2012 Geometer Plus <contact@geometerplus.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301, USA.
+ */
+
+#include "../DBRunnables.h"
+#include "../../../network/NetworkLink.h"
+#include "../../sqldb/implsqlite/SQLiteFactory.h"
+
+SaveNetworkLinkRunnable::SaveNetworkLinkRunnable(DBConnection &connection) {
+ myFindNetworkLinkId = SQLiteFactory::createCommand(NetworkDBQuery::FIND_NETWORK_LINK_ID, connection, "@site_name", DBValue::DBTEXT);
+ myAddNetworkLink = SQLiteFactory::createCommand(NetworkDBQuery::ADD_NETWORK_LINK, connection, "@title", DBValue::DBTEXT, "@site_name", DBValue::DBTEXT, "@summary", DBValue::DBTEXT, "@language", DBValue::DBTEXT, "@predefined_id", DBValue::DBTEXT, "@is_enabled", DBValue::DBINT);
+ myUpdateNetworkLink = SQLiteFactory::createCommand(NetworkDBQuery::UPDATE_NETWORK_LINK, connection, "@title", DBValue::DBTEXT, "@summary", DBValue::DBTEXT, "@language", DBValue::DBTEXT, "@predefined_id", DBValue::DBTEXT, "@is_enabled", DBValue::DBINT, "@link_id", DBValue::DBINT);
+
+ myFindNetworkLinkUrls = SQLiteFactory::createCommand(NetworkDBQuery::FIND_NETWORK_LINKURLS, connection, "@link_id", DBValue::DBINT);
+ myAddNetworkLinkUrl = SQLiteFactory::createCommand(NetworkDBQuery::ADD_NETWORK_LINKURL, connection, "@key", DBValue::DBTEXT, "@link_id", DBValue::DBINT, "@url", DBValue::DBTEXT, "@update_time", DBValue::DBINT);
+ myUpdateNetworkLinkUrl = SQLiteFactory::createCommand(NetworkDBQuery::UPDATE_NETWORK_LINKURL, connection, "@key", DBValue::DBTEXT, "@link_id", DBValue::DBINT, "@url", DBValue::DBTEXT, "@update_time", DBValue::DBINT);
+ myDeleteNetworkLinkUrl = SQLiteFactory::createCommand(NetworkDBQuery::DELETE_NETWORK_LINKURL, connection, "@key", DBValue::DBTEXT, "@link_id", DBValue::DBINT);
+}
+
+bool SaveNetworkLinkRunnable::run() {
+ if (myNetworkLink.isNull()) {
+ return false;
+ }
+ ((DBTextValue &) *myFindNetworkLinkId->parameter("@site_name").value()) = myNetworkLink->getSiteName();
+ shared_ptr<DBDataReader> reader = myFindNetworkLinkId->executeReader();
+ if (reader.isNull() || !reader->next()) {
+ return addNetworkLink();
+ } else if (myNetworkLink->isPredefined()) {
+ return updateNetworkLink(reader->intValue(0)) && updateNetworkLinkUrls(reader->intValue(0));
+ } else {
+ //TODO implement for custom links
+ return false;
+ }
+ return false;
+}
+
+bool SaveNetworkLinkRunnable::addNetworkLink() {
+ ((DBTextValue &) *myAddNetworkLink->parameter("@title").value()) = myNetworkLink->getTitle();
+ ((DBTextValue &) *myAddNetworkLink->parameter("@site_name").value()) = myNetworkLink->getSiteName();
+ ((DBTextValue &) *myAddNetworkLink->parameter("@summary").value()) = myNetworkLink->getSummary();
+ ((DBTextValue &) *myAddNetworkLink->parameter("@language").value()) = myNetworkLink->getLanguage();
+ ((DBTextValue &) *myAddNetworkLink->parameter("@predefined_id").value()) = myNetworkLink->getPredefinedId();
+ ((DBIntValue &) *myAddNetworkLink->parameter("@is_enabled").value()) = myNetworkLink->isEnabled();
+ shared_ptr<DBValue> dbLinkId = myAddNetworkLink->executeScalar();
+ if (dbLinkId.isNull() || dbLinkId->type() != DBValue::DBINT || ((DBIntValue &) *dbLinkId).value() == 0) {
+ return false;
+ }
+
+ bool allExecuted = true;
+ std::map<std::string,std::string> tempLinks = myNetworkLink->getLinks();
+ if (myNetworkLink->getIcon() != std::string()) {
+ tempLinks["icon"] = myNetworkLink->getIcon();
+ }
+ long t = 0;
+ if (myNetworkLink->getUpdated() != 0) {
+ t = myNetworkLink->getUpdated()->getLongSeconds_stupid();
+ }
+ for (std::map<std::string,std::string>::iterator it = tempLinks.begin(); it != tempLinks.end(); ++it) {
+ ((DBTextValue &) *myAddNetworkLinkUrl->parameter("@key").value()) = it->first;
+ ((DBTextValue &) *myAddNetworkLinkUrl->parameter("@url").value()) = it->second;
+ ((DBIntValue &) *myAddNetworkLinkUrl->parameter("@link_id").value()) = ((DBIntValue &) *dbLinkId).value();
+ ((DBIntValue &) *myAddNetworkLinkUrl->parameter("@update_time").value()) = t;
+ allExecuted = allExecuted && myAddNetworkLinkUrl->execute();
+ }
+ return allExecuted;
+}
+
+bool SaveNetworkLinkRunnable::updateNetworkLink(int linkId) {
+ ((DBTextValue &) *myUpdateNetworkLink->parameter("@title").value()) = myNetworkLink->getTitle();
+ ((DBTextValue &) *myUpdateNetworkLink->parameter("@summary").value()) = myNetworkLink->getSummary();
+ ((DBTextValue &) *myUpdateNetworkLink->parameter("@language").value()) = myNetworkLink->getLanguage();
+ ((DBTextValue &) *myUpdateNetworkLink->parameter("@predefined_id").value()) = myNetworkLink->getPredefinedId();
+ ((DBIntValue &) *myUpdateNetworkLink->parameter("@is_enabled").value()) = myNetworkLink->isEnabled();
+ ((DBIntValue &) *myUpdateNetworkLink->parameter("@link_id").value()) = linkId;
+
+ return myUpdateNetworkLink->execute();
+}
+
+bool SaveNetworkLinkRunnable::updateNetworkLinkUrls(int linkId) {
+ bool allExecuted = true;
+ ((DBIntValue &) *myFindNetworkLinkUrls->parameter("@link_id").value()) = linkId;
+ shared_ptr<DBDataReader> reader = myFindNetworkLinkUrls->executeReader();
+ std::map<std::string,std::string> linksToCheck = myNetworkLink->getLinks();
+ if (!myNetworkLink->getIcon().empty()) {
+ linksToCheck["icon"] = myNetworkLink->getIcon();
+ }
+ long t = 0;
+ if (!myNetworkLink->getUpdated().isNull()) {
+ t = myNetworkLink->getUpdated()->getLongSeconds_stupid();
+ }
+ while (reader->next()) {
+ if (reader->type(0) != DBValue::DBTEXT || reader->type(1) != DBValue::DBTEXT) {
+ return false;
+ }
+ std::string key = reader->textValue(0, std::string());
+// std::string url = reader->textValue(1, std::string());
+ if (linksToCheck.count(key) == 0) {
+ ((DBTextValue &) *myDeleteNetworkLinkUrl->parameter("@key").value()) = key;
+ ((DBIntValue &) *myDeleteNetworkLinkUrl->parameter("@link_id").value()) = linkId;
+ allExecuted = allExecuted && myDeleteNetworkLinkUrl->execute();
+ } else {
+ ((DBTextValue &) *myUpdateNetworkLinkUrl->parameter("@key").value()) = key;
+ ((DBTextValue &) *myUpdateNetworkLinkUrl->parameter("@url").value()) = linksToCheck[key];
+ ((DBIntValue &) *myUpdateNetworkLinkUrl->parameter("@link_id").value()) = linkId;
+ ((DBIntValue &) *myUpdateNetworkLinkUrl->parameter("@update_time").value()) = t;
+ linksToCheck.erase(key);
+ allExecuted = allExecuted && myUpdateNetworkLinkUrl->execute();
+ }
+ }
+
+ for (std::map<std::string,std::string>::iterator it = linksToCheck.begin(); it != linksToCheck.end(); ++it) {
+ ((DBTextValue &) *myAddNetworkLinkUrl->parameter("@key").value()) = it->first;
+ ((DBTextValue &) *myAddNetworkLinkUrl->parameter("@url").value()) = it->second;
+ ((DBIntValue &) *myAddNetworkLinkUrl->parameter("@link_id").value()) = linkId;
+ ((DBIntValue &) *myAddNetworkLinkUrl->parameter("@update_time").value()) = t;
+ allExecuted = allExecuted && myAddNetworkLinkUrl->execute();
+ }
+ return allExecuted;
+}
+
+void SaveNetworkLinkRunnable::setNetworkLink(shared_ptr<NetworkLink> link) {
+ myNetworkLink = link;
+}