From: Somin Kim Date: Mon, 11 Jan 2016 09:38:19 +0000 (+0900) Subject: Modified to handle get template request X-Git-Tag: submit/tizen/20160118.014643^2~8 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=7b22a2319615e4db514ece3b6917e3871c3899da;p=platform%2Fcore%2Fcontext%2Fcontext-service.git Modified to handle get template request - Added template manager Change-Id: I1f820c30a02dcb4a5e7f71fcc317e455f54ecc2b Signed-off-by: Somin Kim --- diff --git a/src/context_trigger/context_monitor.cpp b/src/context_trigger/context_monitor.cpp index e1b79c9..d70b20f 100644 --- a/src/context_trigger/context_monitor.cpp +++ b/src/context_trigger/context_monitor.cpp @@ -184,11 +184,6 @@ bool ctx::context_monitor::is_allowed(const char *client, const char *subject) return true; } -bool ctx::context_monitor::get_fact_definition(std::string &subject, int &operation, ctx::json &attributes, ctx::json &options) -{ - return _context_mgr->pop_trigger_item(subject, operation, attributes, options); -} - int ctx::context_monitor::find_sub(request_type type, const char* subject, json* option) { // @return request id diff --git a/src/context_trigger/context_monitor.h b/src/context_trigger/context_monitor.h index 19de83a..53b2884 100644 --- a/src/context_trigger/context_monitor.h +++ b/src/context_trigger/context_monitor.h @@ -43,8 +43,6 @@ namespace ctx { void reply_result(int req_id, int error, const char *subject, ctx::json *option, ctx::json *fact); void publish_fact(int req_id, int error, const char *subject, ctx::json *option, ctx::json *fact); - bool get_fact_definition(std::string &subject, int &operation, ctx::json &attributes, ctx::json &options); - private: context_monitor(); context_monitor(const context_monitor& other); diff --git a/src/context_trigger/rule_manager.cpp b/src/context_trigger/rule_manager.cpp index dc1c373..f76c03f 100644 --- a/src/context_trigger/rule_manager.cpp +++ b/src/context_trigger/rule_manager.cpp @@ -21,6 +21,7 @@ #include #include "rule_manager.h" #include "context_monitor.h" +#include "template_manager.h" #include "rule.h" #include "timer.h" @@ -45,7 +46,8 @@ static std::string int_to_string(int i) return str; } -ctx::rule_manager::rule_manager() +ctx::rule_manager::rule_manager(ctx::template_manager* tmpl_mgr) +: _tmpl_mgr(tmpl_mgr) { } @@ -58,7 +60,7 @@ bool ctx::rule_manager::init() bool ret; int error; - // Create tables into db (rule, event, condition, action, template) + // Create tables into db (rule, template) std::string q1 = std::string("enabled INTEGER DEFAULT 0 NOT NULL, creator TEXT DEFAULT '' NOT NULL,") + "creator_app_id TEXT DEFAULT '' NOT NULL, description TEXT DEFAULT ''," + "details TEXT DEFAULT '' NOT NULL"; @@ -71,7 +73,8 @@ bool ctx::rule_manager::init() ret = db_manager::execute(2, q2.c_str(), NULL); IF_FAIL_RETURN_TAG(ret, false, _E, "Create template table failed"); - apply_templates(); + // Apply templates + _tmpl_mgr->apply_templates(); // Before re-enable rules, handle uninstalled app's rules if (get_uninstalled_app() > 0) { @@ -83,39 +86,6 @@ bool ctx::rule_manager::init() return ret; } -void ctx::rule_manager::apply_templates(void) -{ - std::string subject; - int operation; - ctx::json attributes; - ctx::json options; - std::string q_update; - std::string q_insert = "INSERT OR IGNORE INTO context_trigger_template (name, operation, attributes, options) VALUES"; - - ctx::context_monitor* ctx_monitor = ctx::context_monitor::get_instance(); - IF_FAIL_VOID_TAG(ctx_monitor, _E, "Memory allocation failed"); - - while (ctx_monitor->get_fact_definition(subject, operation, attributes, options)) { - _D("Subject: %s, Ops: %d", subject.c_str(), operation); - _J("Attr", attributes); - _J("Opt", options); - - q_update += "UPDATE context_trigger_template SET operation=" + int_to_string(operation) - + ", attributes='" + attributes.str() + "', options='" + options.str() + "' WHERE name='" + subject + "';"; - - q_insert += " ('" + subject + "', " + int_to_string(operation) + ", '" + attributes.str() + "', '" + options.str() + "'),"; - } - - q_insert.erase(q_insert.end() - 1, q_insert.end()); - q_insert += ";"; - - bool ret = db_manager::execute(5, q_update.c_str(), NULL); - IF_FAIL_VOID_TAG(ret, _E, "Update item definition failed"); - - ret = db_manager::execute(6, q_insert.c_str(), NULL); - IF_FAIL_VOID_TAG(ret, _E, "Insert item definition failed"); -} - int ctx::rule_manager::get_uninstalled_app(void) { // Return number of uninstalled apps diff --git a/src/context_trigger/rule_manager.h b/src/context_trigger/rule_manager.h index ad2b450..a73a149 100644 --- a/src/context_trigger/rule_manager.h +++ b/src/context_trigger/rule_manager.h @@ -24,13 +24,14 @@ namespace ctx { class json; class context_trigger; - class context_manager_impl; + class template_manager; class trigger_rule; class rule_manager { public: - rule_manager(); + rule_manager(ctx::template_manager* tmpl_mgr); ~rule_manager(); + bool init(); int add_rule(std::string creator, const char* app_id, ctx::json rule, ctx::json* rule_id); int remove_rule(int rule_id); @@ -44,7 +45,8 @@ namespace ctx { static bool is_uninstalled_package(std::string app_id); private: - void apply_templates(void); + ctx::template_manager* _tmpl_mgr; + bool reenable_rule(void); int verify_rule(ctx::json& rule, const char* app_id); int64_t get_duplicated_rule_id(std::string creator, ctx::json& rule); diff --git a/src/context_trigger/template_manager.cpp b/src/context_trigger/template_manager.cpp new file mode 100644 index 0000000..e5e6500 --- /dev/null +++ b/src/context_trigger/template_manager.cpp @@ -0,0 +1,107 @@ +/* + * Copyright (c) 2015 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include +#include +#include "../context_mgr_impl.h" +#include "template_manager.h" + +static std::string int_to_string(int i) +{ + std::ostringstream convert; + convert << i; + std::string str = convert.str(); + return str; +} + +ctx::template_manager::template_manager(ctx::context_manager_impl* ctx_mgr) +: _context_mgr(ctx_mgr) +{ +} + +ctx::template_manager::~template_manager() +{ +} + +bool ctx::template_manager::get_fact_definition(std::string &subject, int &operation, ctx::json &attributes, ctx::json &options) +{ + return _context_mgr->pop_trigger_item(subject, operation, attributes, options); +} + +void ctx::template_manager::apply_templates(void) +{ + // TODO remove templates if needed + std::string subject; + int operation; + ctx::json attributes; + ctx::json options; + std::string q_update; + std::string q_insert = "INSERT OR IGNORE INTO context_trigger_template (name, operation, attributes, options) VALUES"; + int cnt = 0; + + while (get_fact_definition(subject, operation, attributes, options)) { + _D("Subject: %s, Ops: %d", subject.c_str(), operation); + _J("Attr", attributes); + _J("Opt", options); + + q_update += "UPDATE context_trigger_template SET operation=" + int_to_string(operation) + + ", attributes='" + attributes.str() + "', options='" + options.str() + "' WHERE name='" + subject + "';"; + + q_insert += " ('" + subject + "', " + int_to_string(operation) + ", '" + attributes.str() + "', '" + options.str() + "'),"; + cnt++; + } + IF_FAIL_VOID(cnt > 0); + + q_insert.erase(q_insert.end() - 1, q_insert.end()); + q_insert += ";"; + + bool ret = db_manager::execute(1, q_update.c_str(), NULL); + IF_FAIL_VOID_TAG(ret, _E, "Update item definition failed"); + + ret = db_manager::execute(2, q_insert.c_str(), NULL); + IF_FAIL_VOID_TAG(ret, _E, "Insert item definition failed"); +} + +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 record; + bool ret = db_manager::execute_sync(q.c_str(), &record); + IF_FAIL_RETURN_TAG(ret, ERR_OPERATION_FAILED, _E, "Query template failed"); + IF_FAIL_RETURN_TAG(record.size() > 0, ERR_NOT_SUPPORTED, _E, "Template(%s) not found", subject.c_str()); + IF_FAIL_RETURN_TAG(record.size() == 1, ERR_OPERATION_FAILED, _E, "Tepmlate duplicated"); + + (*tmpl) = *record.begin(); + + std::string opt_str; + std::string attr_str; + tmpl->get(NULL, TYPE_OPTION_STR, &opt_str); + tmpl->get(NULL, TYPE_ATTR_STR, &attr_str); + + ctx::json opt = opt_str; + ctx::json attr = attr_str; + + tmpl->set(NULL, TYPE_OPTION_STR, opt); + tmpl->set(NULL, TYPE_ATTR_STR, attr); + + return ERR_NONE; +} diff --git a/src/context_trigger/template_manager.h b/src/context_trigger/template_manager.h new file mode 100644 index 0000000..fb8addc --- /dev/null +++ b/src/context_trigger/template_manager.h @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2015 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef __TEMPLATE_MANAGER_H__ +#define __TEMPLATE_MANAGER_H__ + +#include + +namespace ctx { + + class context_manager_impl; + + class template_manager { + public: + template_manager(ctx::context_manager_impl* ctx_mgr); + ~template_manager(); + + void apply_templates(void); + bool get_fact_definition(std::string &subject, int &operation, ctx::json &attributes, ctx::json &options); + int get_template(std::string &subject, ctx::json* tmpl); + + private: + context_manager_impl *_context_mgr; + + }; /* class template_manager */ + +} /* namespace ctx */ + +#endif /* End of __TEMPLATE_MANAGER_H__ */ diff --git a/src/context_trigger/trigger.cpp b/src/context_trigger/trigger.cpp index ded892b..84a6e79 100644 --- a/src/context_trigger/trigger.cpp +++ b/src/context_trigger/trigger.cpp @@ -19,6 +19,7 @@ #include "trigger.h" #include "rule_manager.h" #include "context_monitor.h" +#include "template_manager.h" ctx::context_trigger::context_trigger() { @@ -46,6 +47,10 @@ void ctx::context_trigger::release() delete rule_mgr; rule_mgr = NULL; + _D("Template Manager Release"); + delete tmpl_mgr; + tmpl_mgr = NULL; + ctx::context_monitor::destroy(); } @@ -54,7 +59,8 @@ bool ctx::context_trigger::assign_request(ctx::request_info* request) std::string subject = request->get_subject(); if (subject != CONTEXT_TRIGGER_SUBJECT_ADD && subject != CONTEXT_TRIGGER_SUBJECT_REMOVE && subject != CONTEXT_TRIGGER_SUBJECT_ENABLE && subject != CONTEXT_TRIGGER_SUBJECT_DISABLE && - subject != CONTEXT_TRIGGER_SUBJECT_GET && subject != CONTEXT_TRIGGER_SUBJECT_GET_RULE_IDS) { + subject != CONTEXT_TRIGGER_SUBJECT_GET && subject != CONTEXT_TRIGGER_SUBJECT_GET_RULE_IDS && + subject != CONTEXT_TRIGGER_SUBJECT_GET_TEMPLATE) { return false; } @@ -81,6 +87,8 @@ void ctx::context_trigger::process_request(ctx::request_info* request) get_rule_by_id(request); } else if (subject == CONTEXT_TRIGGER_SUBJECT_GET_RULE_IDS) { get_rule_ids(request); + } else if (subject == CONTEXT_TRIGGER_SUBJECT_GET_TEMPLATE) { + get_template(request); } else { _E("Invalid request"); } @@ -90,7 +98,10 @@ void ctx::context_trigger::process_initialize(ctx::context_manager_impl* mgr) { ctx::context_monitor::set_context_manager(mgr); - rule_mgr = new(std::nothrow) rule_manager(); + tmpl_mgr = new(std::nothrow) template_manager(mgr); + IF_FAIL_VOID_TAG(tmpl_mgr, _E, "Memory allocation failed"); + + rule_mgr = new(std::nothrow) rule_manager(tmpl_mgr); IF_FAIL_VOID_TAG(rule_mgr, _E, "Memory allocation failed"); bool ret = rule_mgr->init(); @@ -248,3 +259,18 @@ void ctx::context_trigger::get_rule_ids(ctx::request_info* request) ctx::json dummy; request->reply(error, dummy, read_data); } + +void ctx::context_trigger::get_template(ctx::request_info* request) +{ + int error; + + ctx::json option = request->get_description(); + std::string name; + option.get(NULL, SUBJECT_STR, &name); + + ctx::json tmpl; + error = tmpl_mgr->get_template(name, &tmpl); + + ctx::json dummy; + request->reply(error, dummy, tmpl); +} diff --git a/src/context_trigger/trigger.h b/src/context_trigger/trigger.h index 09fbd68..8484b39 100644 --- a/src/context_trigger/trigger.h +++ b/src/context_trigger/trigger.h @@ -22,6 +22,7 @@ namespace ctx { class rule_manager; + class template_manager; class client_request; class context_manager_impl; class context_trigger { @@ -44,8 +45,10 @@ namespace ctx { void disable_rule(ctx::request_info* request); void get_rule_by_id(ctx::request_info* request); void get_rule_ids(ctx::request_info* request); + void get_template(ctx::request_info* request); ctx::rule_manager* rule_mgr; + ctx::template_manager* tmpl_mgr; }; } /* namespace ctx */