#include <db_mgr.h>
#include <app_manager.h>
#include "rule_manager.h"
+#include "template_manager.h"
#include "context_monitor.h"
#include "rule.h"
#include "timer.h"
_D(YELLOW("Re-enable rule started"));
std::string q_rowid;
+ q_rowid.clear();
std::vector<json>::iterator vec_end = record.end();
for (std::vector<json>::iterator vec_pos = record.begin(); vec_pos != vec_end; ++vec_pos) {
ctx::json elem = *vec_pos;
_E("Re-enable rule%d failed(%d)", row_id, error);
}
}
+ IF_FAIL_RETURN(!q_rowid.empty(), true);
q_rowid = q_rowid.substr(0, q_rowid.length() - 4);
// For rules which is failed to re-enable
int ctx::rule_manager::add_rule(std::string creator, const char* app_id, ctx::json rule, ctx::json* rule_id)
{
+ apply_templates();
bool ret;
int64_t rid;
int ctx::rule_manager::remove_rule(int rule_id)
{
+ apply_templates();
bool ret;
// Delete rule from DB
int ctx::rule_manager::enable_rule(int rule_id)
{
+ apply_templates();
int error;
std::string query;
std::vector<json> rule_record;
int ctx::rule_manager::disable_rule(int rule_id)
{
+ apply_templates();
bool ret;
int error;
int ctx::rule_manager::get_rule_by_id(std::string creator, int rule_id, ctx::json* request_result)
{
+ apply_templates();
std::string q = "SELECT description FROM context_trigger_rule WHERE (creator = '";
q += creator;
q += "') and (row_id = ";
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();
+}
bool rule_equals(ctx::json& lrule, ctx::json& rrule);
int get_uninstalled_app(void);
int clear_rule_of_uninstalled_app(bool is_init = false);
+ void apply_templates(void);
std::set<std::string> uninstalled_apps;
#include "rule_manager.h"
#include "template_manager.h"
+ctx::template_manager *ctx::template_manager::_instance = NULL;
+ctx::context_manager_impl *ctx::template_manager::_context_mgr = NULL;
+ctx::rule_manager *ctx::template_manager::_rule_mgr = NULL;
+
static std::string int_to_string(int i)
{
std::ostringstream convert;
return str;
}
-ctx::template_manager::template_manager(ctx::context_manager_impl* ctx_mgr, ctx::rule_manager* rule_mgr)
-: _context_mgr(ctx_mgr), _rule_mgr(rule_mgr)
+ctx::template_manager::template_manager()
{
}
ctx::template_manager::~template_manager()
{
- apply_templates();
+}
+
+void ctx::template_manager::set_manager(ctx::context_manager_impl* ctx_mgr, ctx::rule_manager* rule_mgr)
+{
+ _context_mgr = ctx_mgr;
+ _rule_mgr = rule_mgr;
+}
+
+ctx::template_manager* ctx::template_manager::get_instance()
+{
+ IF_FAIL_RETURN_TAG(_context_mgr, NULL, _E, "Context manager is needed");
+ IF_FAIL_RETURN_TAG(_rule_mgr, NULL, _E, "Rule manager is needed");
+
+ IF_FAIL_RETURN(!_instance, _instance);
+
+ _instance = new(std::nothrow) template_manager();
+ IF_FAIL_RETURN_TAG(_instance, NULL, _E, "Memory alllocation failed");
+
+ return _instance;
+}
+
+void ctx::template_manager::destroy()
+{
+ _instance->apply_templates();
+
+ if (_instance) {
+ delete _instance;
+ _instance = NULL;
+ }
}
bool ctx::template_manager::init()
class rule_manager;
class template_manager {
public:
- template_manager(ctx::context_manager_impl* ctx_mgr, ctx::rule_manager* rule_mgr);
- ~template_manager();
+ static template_manager* get_instance();
+ static void set_manager(ctx::context_manager_impl* ctx_mgr, ctx::rule_manager* rule_mgr);
+ static void destroy();
bool init();
void apply_templates();
int get_template(std::string &subject, ctx::json* tmpl);
private:
- context_manager_impl *_context_mgr;
- rule_manager *_rule_mgr;
+ template_manager();
+ template_manager(const template_manager& other);
+ ~template_manager();
+
+ static template_manager *_instance;
+ static context_manager_impl *_context_mgr;
+ static rule_manager *_rule_mgr;
std::string add_template(std::string &subject, int &operation, ctx::json &attributes, ctx::json &options, std::string &owner);
std::string remove_template(std::string &subject);
ctx::context_trigger::context_trigger()
: rule_mgr(NULL)
- , tmpl_mgr(NULL)
{
}
{
// Release the occupied resources.
// This function is called from the main thread during the service termination process.
- _D("Template Manager Release");
- delete tmpl_mgr;
- tmpl_mgr = NULL;
+
+ _D("Template Manager Destroy");
+ ctx::template_manager::destroy();
_D("Rule Manager Release");
delete rule_mgr;
rule_mgr = NULL;
+ _D("Context Monitor Destroy");
ctx::context_monitor::destroy();
}
IF_FAIL_VOID_TAG(rule_mgr, _E, "Memory allocation failed");
// Template Manager
- tmpl_mgr = new(std::nothrow) template_manager(mgr, rule_mgr);
+ ctx::template_manager::set_manager(mgr, rule_mgr);
+ ctx::template_manager* tmpl_mgr = ctx::template_manager::get_instance();
IF_FAIL_VOID_TAG(tmpl_mgr, _E, "Memory allocation failed");
// Initialization
std::string name;
option.get(NULL, SUBJECT_STR, &name);
+ ctx::template_manager* tmpl_mgr = ctx::template_manager::get_instance();
+ if (!tmpl_mgr) {
+ _E("Memory allocation failed");
+ request->reply(ERR_OUT_OF_MEMORY);
+ return;
+ }
+
ctx::json tmpl;
error = tmpl_mgr->get_template(name, &tmpl);
namespace ctx {
- class template_manager;
class rule_manager;
class client_request;
class context_manager_impl;
void get_template(ctx::request_info* request);
ctx::rule_manager* rule_mgr;
- ctx::template_manager* tmpl_mgr;
};
} /* namespace ctx */