Modified to add/remove a template synchronously after initialization 08/63308/2
authorSomin Kim <somin926.kim@samsung.com>
Wed, 23 Mar 2016 05:05:25 +0000 (14:05 +0900)
committerSomin Kim <somin926.kim@samsung.com>
Thu, 24 Mar 2016 06:25:19 +0000 (15:25 +0900)
Change-Id: I8187258c689b890df5a754d8f7c35a2c6835f496
Signed-off-by: Somin Kim <somin926.kim@samsung.com>
src/context_mgr_impl.cpp
src/context_mgr_impl.h
src/context_trigger/rule_manager.cpp
src/context_trigger/rule_manager.h
src/context_trigger/template_manager.cpp
src/context_trigger/template_manager.h

index 00aabfe..3a287f8 100644 (file)
@@ -26,6 +26,7 @@
 #include "request.h"
 #include "provider.h"
 #include "context_mgr_impl.h"
+#include "context_trigger/template_manager.h"
 
 /* Context Providers */
 #include <internal/device_context_provider.h>
@@ -53,6 +54,7 @@ struct trigger_item_format_s {
 };
 
 static std::list<trigger_item_format_s> __trigger_item_list;
+bool ctx::context_manager_impl::initialized = false;
 
 ctx::context_manager_impl::context_manager_impl()
 {
@@ -79,6 +81,8 @@ bool ctx::context_manager_impl::init()
        ret = init_custom_context_provider();
        IF_FAIL_RETURN_TAG(ret, false, _E, "Initialization failed: custom-context-provider");
 
+       initialized = true;
+
        return true;
 }
 
@@ -130,7 +134,14 @@ bool ctx::context_manager_impl::unregister_provider(const char *subject)
 bool ctx::context_manager_impl::register_trigger_item(const char *subject, int operation, ctx::Json attributes, ctx::Json options, const char* owner)
 {
        IF_FAIL_RETURN_TAG(subject, false, _E, "Invalid parameter");
-       __trigger_item_list.push_back(trigger_item_format_s(subject, operation, attributes, options, (owner)? owner : ""));
+
+       if (!initialized) {
+               __trigger_item_list.push_back(trigger_item_format_s(subject, operation, attributes, options, (owner)? owner : ""));
+       } else {
+               ctx::template_manager* tmpl_mgr = ctx::template_manager::get_instance();
+               IF_FAIL_RETURN_TAG(tmpl_mgr, false, _E, "Memory allocation failed");
+               tmpl_mgr->register_template(subject, operation, attributes, options, owner);
+       }
 
        return true;
 }
@@ -138,7 +149,14 @@ bool ctx::context_manager_impl::register_trigger_item(const char *subject, int o
 bool ctx::context_manager_impl::unregister_trigger_item(const char *subject)
 {
        IF_FAIL_RETURN_TAG(subject, false, _E, "Invalid parameter");
-       __trigger_item_list.push_back(trigger_item_format_s(subject));
+
+       if (!initialized) {
+               __trigger_item_list.push_back(trigger_item_format_s(subject));
+       } else {
+               ctx::template_manager* tmpl_mgr = ctx::template_manager::get_instance();
+               IF_FAIL_RETURN_TAG(tmpl_mgr, false, _E, "Memory allocation failed");
+               tmpl_mgr->unregister_template(subject);
+       }
 
        return true;
 }
index 3eb2d36..b66dd0e 100644 (file)
@@ -53,6 +53,7 @@ namespace ctx {
 
        private:
                std::map<std::string, context_provider_handler*> provider_handle_map;
+               static bool initialized;
 
                static gboolean thread_switcher(gpointer data);
                void _publish(const char *subject, ctx::Json &option, int error, ctx::Json &data_updated);
index c29a1d4..e9462c3 100644 (file)
@@ -20,7 +20,6 @@
 #include <db_mgr.h>
 #include <package_manager.h>
 #include "rule_manager.h"
-#include "template_manager.h"
 #include "context_monitor.h"
 #include "rule.h"
 #include "timer.h"
@@ -502,7 +501,6 @@ int ctx::rule_manager::verify_rule(ctx::Json& rule, const char* creator)
 
 int ctx::rule_manager::add_rule(std::string creator, const char* pkg_id, ctx::Json rule, ctx::Json* rule_id)
 {
-       apply_templates();
        bool ret;
        int64_t rid;
 
@@ -547,7 +545,6 @@ int ctx::rule_manager::add_rule(std::string creator, const char* pkg_id, ctx::Js
 
 int ctx::rule_manager::remove_rule(int rule_id)
 {
-       apply_templates();
        bool ret;
 
        // Delete rule from DB
@@ -565,7 +562,6 @@ int ctx::rule_manager::remove_rule(int rule_id)
 
 int ctx::rule_manager::enable_rule(int rule_id)
 {
-       apply_templates();
        int error;
        std::string query;
        std::vector<Json> rule_record;
@@ -616,7 +612,6 @@ CATCH:
 
 int ctx::rule_manager::disable_rule(int rule_id)
 {
-       apply_templates();
        bool ret;
        int error;
 
@@ -716,7 +711,6 @@ bool ctx::rule_manager::is_rule_enabled(int rule_id)
 
 int ctx::rule_manager::get_rule_by_id(std::string pkg_id, int rule_id, ctx::Json* request_result)
 {
-       apply_templates();
        std::string q = "SELECT description FROM context_trigger_rule WHERE (package_id = '";
        q += pkg_id;
        q += "') and (row_id = ";
@@ -772,10 +766,3 @@ int ctx::rule_manager::get_rule_ids(std::string pkg_id, ctx::Json* request_resul
 
        return ERR_NONE;
 }
-
-void ctx::rule_manager::apply_templates()
-{
-       ctx::template_manager* tmpl_mgr = ctx::template_manager::get_instance();
-       IF_FAIL_VOID_TAG(tmpl_mgr, _E, "Memory allocation failed");
-       tmpl_mgr->apply_templates();
-}
index 1862fcd..29bb933 100644 (file)
@@ -56,7 +56,6 @@ namespace ctx {
                        bool rule_equals(ctx::Json& lrule, ctx::Json& rrule);
                        int get_uninstalled_app(void);
                        int clear_rule_of_uninstalled_package(bool is_init = false);
-                       void apply_templates(void);
 
                        std::set<std::string> uninstalled_packages;
 
index ae9606e..bb88721 100644 (file)
@@ -100,22 +100,49 @@ void ctx::template_manager::apply_templates()
 
        while(_context_mgr->pop_trigger_item(subject, operation, attributes, options, owner, unregister)) {
                if (unregister) {
-                       query += remove_template(subject);
-                       _rule_mgr->pause_rule_with_item(subject);
+                       unregister_template(subject);
                } else {
-                       query += add_template(subject, operation, attributes, options, owner);
-                       if (!owner.empty()) {
-                               _rule_mgr->resume_rule_with_item(subject);
-                       }
+                       register_template(subject, operation, attributes, options, owner);
                }
        }
-       IF_FAIL_VOID(!query.empty());
+}
+
+void ctx::template_manager::register_template(std::string subject, int operation, ctx::Json attributes, ctx::Json options, std::string owner)
+{
+       _D("[Add template] Subject: %s, Ops: %d, Owner: %s", subject.c_str(), operation, owner.c_str());
+       _J("Attr", attributes);
+       _J("Opt", options);
+
+       std::string query = "UPDATE context_trigger_template SET operation=" + int_to_string(operation)
+                       + ", attributes='" + attributes.str() + "', options='" + options.str() + "', owner='" + owner
+                       + "' WHERE name='" + subject + "'; ";
+
+       query += "INSERT OR IGNORE INTO context_trigger_template (name, operation, attributes, options, owner) VALUES ('"
+                       + subject + "', " + int_to_string(operation) + ", '" + attributes.str() + "', '" + options.str() + "', '"
+                       + owner + "'); ";
 
        std::vector<Json> record;
        bool ret = db_manager::execute_sync(query.c_str(), &record);
        IF_FAIL_VOID_TAG(ret, _E, "Update template db failed");
+
+       if (!owner.empty()) {
+               _rule_mgr->resume_rule_with_item(subject);
+       }
+}
+
+void ctx::template_manager::unregister_template(std::string subject)
+{
+       _D("[Remove template] Subject: %s", subject.c_str());
+       std::string query = "DELETE FROM context_trigger_template WHERE name = '" + subject + "'; ";
+
+       std::vector<Json> record;
+       bool ret = db_manager::execute_sync(query.c_str(), &record);
+       IF_FAIL_VOID_TAG(ret, _E, "Update template db failed");
+
+       _rule_mgr->pause_rule_with_item(subject);
 }
 
+
 std::string ctx::template_manager::add_template(std::string &subject, int &operation, ctx::Json &attributes, ctx::Json &options, std::string &owner)
 {
        _D("[Add template] Subject: %s, Ops: %d, Owner: %s", subject.c_str(), operation, owner.c_str());
@@ -144,8 +171,6 @@ std::string ctx::template_manager::remove_template(std::string &subject)
 int ctx::template_manager::get_template(std::string &subject, ctx::Json* tmpl)
 {
        // Update latest template information
-       apply_templates();
-
        std::string q = "SELECT * FROM context_trigger_template WHERE name = '" + subject + "'";
 
        std::vector<Json> record;
index 3f84a24..ae04693 100644 (file)
@@ -32,6 +32,8 @@ namespace ctx {
                bool init();
                void apply_templates();
                int get_template(std::string &subject, ctx::Json* tmpl);
+               void register_template(std::string subject, int operation, ctx::Json attributes, ctx::Json options, std::string owner);
+               void unregister_template(std::string subject);
 
        private:
                template_manager();