2 * Copyright (c) 2015 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include <TriggerTypes.h>
20 #include "../ContextManager.h"
21 #include "RuleManager.h"
22 #include "TemplateManager.h"
25 using namespace ctx::trigger;
27 TemplateManager *TemplateManager::__instance = NULL;
28 ContextManager *TemplateManager::__contextMgr = NULL;
29 RuleManager *TemplateManager::__ruleMgr = NULL;
31 static std::string __intToString(int i)
33 std::ostringstream convert;
35 std::string str = convert.str();
39 TemplateManager::TemplateManager()
43 TemplateManager::~TemplateManager()
47 void TemplateManager::setManager(ContextManager* ctxMgr, RuleManager* ruleMgr)
49 __contextMgr = ctxMgr;
53 TemplateManager* TemplateManager::getInstance()
55 IF_FAIL_RETURN_TAG(__contextMgr, NULL, _E, "Context manager is needed");
56 IF_FAIL_RETURN_TAG(__ruleMgr, NULL, _E, "Rule manager is needed");
58 IF_FAIL_RETURN(!__instance, __instance);
60 __instance = new(std::nothrow) TemplateManager();
61 IF_FAIL_RETURN_TAG(__instance, NULL, _E, "Memory alllocation failed");
66 void TemplateManager::destroy()
68 IF_FAIL_VOID(__instance);
70 __instance->applyTemplates();
76 bool TemplateManager::init()
78 std::string q = std::string("CREATE TABLE IF NOT EXISTS ContextTriggerTemplate ")
79 + "(name TEXT DEFAULT '' NOT NULL PRIMARY KEY, operation INTEGER DEFAULT 3 NOT NULL, "
80 + "attributes TEXT DEFAULT '' NOT NULL, options TEXT DEFAULT '' NOT NULL, owner TEXT DEFAULT '' NOT NULL)";
82 std::vector<CtxJson1> record;
83 bool ret = __dbManager.executeSync(q.c_str(), &record);
84 IF_FAIL_RETURN_TAG(ret, false, _E, "Create template table failed");
92 void TemplateManager::applyTemplates()
102 while (__contextMgr->popTriggerTemplate(subject, operation, attributes, options)) {
103 registerTemplate(subject, operation, attributes, options, "");
107 void TemplateManager::registerTemplate(std::string subject, int operation, CtxJson1 attributes, CtxJson1 options, std::string owner)
109 _D("[Add template] Subject: %s, Ops: %d, Owner: %s", subject.c_str(), operation, owner.c_str());
110 _J("Attr", attributes);
113 std::string query = "UPDATE ContextTriggerTemplate SET operation=" + __intToString(operation)
114 + ", attributes='" + attributes.str() + "', options='" + options.str() + "', owner='" + owner
115 + "' WHERE name='" + subject + "'; ";
117 query += "INSERT OR IGNORE INTO ContextTriggerTemplate (name, operation, attributes, options, owner) VALUES ('"
118 + subject + "', " + __intToString(operation) + ", '" + attributes.str() + "', '" + options.str() + "', '"
121 std::vector<CtxJson1> record;
122 bool ret = __dbManager.executeSync(query.c_str(), &record);
123 IF_FAIL_VOID_TAG(ret, _E, "Update template db failed");
125 if (!owner.empty()) {
126 __ruleMgr->resumeRuleWithItem(subject);
130 void TemplateManager::unregisterTemplate(std::string subject)
132 _D("[Remove template] Subject: %s", subject.c_str());
133 std::string query = "DELETE FROM ContextTriggerTemplate WHERE name = '" + subject + "'; ";
135 std::vector<CtxJson1> record;
136 bool ret = __dbManager.executeSync(query.c_str(), &record);
137 IF_FAIL_VOID_TAG(ret, _E, "Update template db failed");
139 __ruleMgr->pauseRuleWithItem(subject);
143 std::string TemplateManager::__addTemplate(std::string &subject, int &operation, CtxJson1 &attributes, CtxJson1 &options, std::string &owner)
145 _D("[Add template] Subject: %s, Ops: %d, Owner: %s", subject.c_str(), operation, owner.c_str());
146 _J("Attr", attributes);
149 std::string query = "UPDATE ContextTriggerTemplate SET operation=" + __intToString(operation)
150 + ", attributes='" + attributes.str() + "', options='" + options.str() + "', owner='" + owner
151 + "' WHERE name='" + subject + "'; ";
153 query += "INSERT OR IGNORE INTO ContextTriggerTemplate (name, operation, attributes, options, owner) VALUES ('"
154 + subject + "', " + __intToString(operation) + ", '" + attributes.str() + "', '" + options.str() + "', '"
160 std::string TemplateManager::__removeTemplate(std::string &subject)
162 _D("[Remove template] Subject: %s", subject.c_str());
163 std::string query = "DELETE FROM ContextTriggerTemplate WHERE name = '" + subject + "'; ";
168 int TemplateManager::getTemplate(std::string &subject, CtxJson1* tmpl)
170 if (!__contextMgr->isSupported(subject.c_str()))
171 return ERR_NOT_SUPPORTED;
173 // Update latest template information
174 std::string q = "SELECT * FROM ContextTriggerTemplate WHERE name = '" + subject + "'";
176 std::vector<CtxJson1> record;
177 bool ret = __dbManager.executeSync(q.c_str(), &record);
178 IF_FAIL_RETURN_TAG(ret, ERR_OPERATION_FAILED, _E, "Query template failed");
179 IF_FAIL_RETURN_TAG(record.size() > 0, ERR_NOT_SUPPORTED, _E, "Template(%s) not found", subject.c_str());
180 IF_FAIL_RETURN_TAG(record.size() == 1, ERR_OPERATION_FAILED, _E, "Tepmlate duplicated");
182 (*tmpl) = *record.begin();
186 tmpl->get(NULL, TRIG_TMPL_KEY_OPTION, &optStr);
187 tmpl->get(NULL, TRIG_TMPL_KEY_ATTRIBUTE, &attrStr);
189 CtxJson1 opt = optStr;
190 CtxJson1 attr = attrStr;
192 tmpl->set(NULL, TRIG_TMPL_KEY_OPTION, opt);
193 tmpl->set(NULL, TRIG_TMPL_KEY_ATTRIBUTE, attr);