Store the dbus connection while launching the service
[platform/core/context/context-service.git] / src / agent / legacy / trigger / TemplateManager.cpp
1 /*
2  * Copyright (c) 2015 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include <sstream>
18 #include <Types.h>
19 #include <TriggerTypes.h>
20 #include "../ContextManager.h"
21 #include "RuleManager.h"
22 #include "TemplateManager.h"
23
24 using namespace ctx;
25 using namespace ctx::trigger;
26
27 TemplateManager *TemplateManager::__instance = NULL;
28 ContextManager *TemplateManager::__contextMgr = NULL;
29 RuleManager *TemplateManager::__ruleMgr = NULL;
30
31 static std::string __intToString(int i)
32 {
33         std::ostringstream convert;
34         convert << i;
35         std::string str = convert.str();
36         return str;
37 }
38
39 TemplateManager::TemplateManager()
40 {
41 }
42
43 TemplateManager::~TemplateManager()
44 {
45 }
46
47 void TemplateManager::setManager(ContextManager* ctxMgr, RuleManager* ruleMgr)
48 {
49         __contextMgr = ctxMgr;
50         __ruleMgr = ruleMgr;
51 }
52
53 TemplateManager* TemplateManager::getInstance()
54 {
55         IF_FAIL_RETURN_TAG(__contextMgr, NULL, _E, "Context manager is needed");
56         IF_FAIL_RETURN_TAG(__ruleMgr, NULL, _E, "Rule manager is needed");
57
58         IF_FAIL_RETURN(!__instance, __instance);
59
60         __instance = new(std::nothrow) TemplateManager();
61         IF_FAIL_RETURN_TAG(__instance, NULL, _E, "Memory alllocation failed");
62
63         return __instance;
64 }
65
66 void TemplateManager::destroy()
67 {
68         IF_FAIL_VOID(__instance);
69
70         __instance->applyTemplates();
71
72         delete __instance;
73         __instance = NULL;
74 }
75
76 bool TemplateManager::init()
77 {
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)";
81
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");
85
86         // Apply templates
87         applyTemplates();
88
89         return true;
90 }
91
92 void TemplateManager::applyTemplates()
93 {
94         std::string subject;
95         int operation;
96         CtxJson1 attributes;
97         CtxJson1 options;
98         std::string owner;
99         std::string query;
100         query.clear();
101
102         while (__contextMgr->popTriggerTemplate(subject, operation, attributes, options)) {
103                 registerTemplate(subject, operation, attributes, options, "");
104         }
105 }
106
107 void TemplateManager::registerTemplate(std::string subject, int operation, CtxJson1 attributes, CtxJson1 options, std::string owner)
108 {
109         _D("[Add template] Subject: %s, Ops: %d, Owner: %s", subject.c_str(), operation, owner.c_str());
110         _J("Attr", attributes);
111         _J("Opt", options);
112
113         std::string query = "UPDATE ContextTriggerTemplate SET operation=" + __intToString(operation)
114                         + ", attributes='" + attributes.str() + "', options='" + options.str() + "', owner='" + owner
115                         + "' WHERE name='" + subject + "'; ";
116
117         query += "INSERT OR IGNORE INTO ContextTriggerTemplate (name, operation, attributes, options, owner) VALUES ('"
118                         + subject + "', " + __intToString(operation) + ", '" + attributes.str() + "', '" + options.str() + "', '"
119                         + owner + "'); ";
120
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");
124
125         if (!owner.empty()) {
126                 __ruleMgr->resumeRuleWithItem(subject);
127         }
128 }
129
130 void TemplateManager::unregisterTemplate(std::string subject)
131 {
132         _D("[Remove template] Subject: %s", subject.c_str());
133         std::string query = "DELETE FROM ContextTriggerTemplate WHERE name = '" + subject + "'; ";
134
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");
138
139         __ruleMgr->pauseRuleWithItem(subject);
140 }
141
142
143 std::string TemplateManager::__addTemplate(std::string &subject, int &operation, CtxJson1 &attributes, CtxJson1 &options, std::string &owner)
144 {
145         _D("[Add template] Subject: %s, Ops: %d, Owner: %s", subject.c_str(), operation, owner.c_str());
146         _J("Attr", attributes);
147         _J("Opt", options);
148
149         std::string query = "UPDATE ContextTriggerTemplate SET operation=" + __intToString(operation)
150                         + ", attributes='" + attributes.str() + "', options='" + options.str() + "', owner='" + owner
151                         + "' WHERE name='" + subject + "'; ";
152
153         query += "INSERT OR IGNORE INTO ContextTriggerTemplate (name, operation, attributes, options, owner) VALUES ('"
154                         + subject + "', " + __intToString(operation) + ", '" + attributes.str() + "', '" + options.str() + "', '"
155                         + owner + "'); ";
156
157         return query;
158 }
159
160 std::string TemplateManager::__removeTemplate(std::string &subject)
161 {
162         _D("[Remove template] Subject: %s", subject.c_str());
163         std::string query = "DELETE FROM ContextTriggerTemplate WHERE name = '" + subject + "'; ";
164
165         return query;
166 }
167
168 int TemplateManager::getTemplate(std::string &subject, CtxJson1* tmpl)
169 {
170         if (!__contextMgr->isSupported(subject.c_str()))
171                 return ERR_NOT_SUPPORTED;
172
173         // Update latest template information
174         std::string q = "SELECT * FROM ContextTriggerTemplate WHERE name = '" + subject + "'";
175
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");
181
182         (*tmpl) = *record.begin();
183
184         std::string optStr;
185         std::string attrStr;
186         tmpl->get(NULL, TRIG_TMPL_KEY_OPTION, &optStr);
187         tmpl->get(NULL, TRIG_TMPL_KEY_ATTRIBUTE, &attrStr);
188
189         CtxJson1 opt = optStr;
190         CtxJson1 attr = attrStr;
191
192         tmpl->set(NULL, TRIG_TMPL_KEY_OPTION, opt);
193         tmpl->set(NULL, TRIG_TMPL_KEY_ATTRIBUTE, attr);
194
195         return ERR_NONE;
196 }