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
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);
#include <app_manager.h>
#include "rule_manager.h"
#include "context_monitor.h"
+#include "template_manager.h"
#include "rule.h"
#include "timer.h"
return str;
}
-ctx::rule_manager::rule_manager()
+ctx::rule_manager::rule_manager(ctx::template_manager* tmpl_mgr)
+: _tmpl_mgr(tmpl_mgr)
{
}
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";
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) {
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
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);
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);
--- /dev/null
+/*
+ * 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 <sstream>
+#include <types_internal.h>
+#include <context_trigger_types_internal.h>
+#include <db_mgr.h>
+#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<json> 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;
+}
--- /dev/null
+/*
+ * 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 <json.h>
+
+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__ */
#include "trigger.h"
#include "rule_manager.h"
#include "context_monitor.h"
+#include "template_manager.h"
ctx::context_trigger::context_trigger()
{
delete rule_mgr;
rule_mgr = NULL;
+ _D("Template Manager Release");
+ delete tmpl_mgr;
+ tmpl_mgr = NULL;
+
ctx::context_monitor::destroy();
}
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;
}
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");
}
{
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();
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);
+}
namespace ctx {
class rule_manager;
+ class template_manager;
class client_request;
class context_manager_impl;
class context_trigger {
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 */