2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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.
17 * @file global_dao.cpp
18 * @author Pawel Sikorski (p.sikorski@samsung.com)
20 * @brief This file contains the definition of global dao class.
23 #include <dpl/wrt-dao-rw/global_dao.h>
24 #include <dpl/log/log.h>
25 #include <dpl/string.h>
26 #include <dpl/db/orm.h>
27 #include <orm_generator_wrt.h>
28 #include <dpl/wrt-dao-ro/webruntime_database.h>
29 #include <dpl/wrt-dao-ro/WrtDatabase.h>
33 void GlobalDAO::AddAdultBlackListElement(const DPL::String &url)
36 using namespace DPL::DB::ORM;
37 using namespace DPL::DB::ORM::wrt;
39 ScopedTransaction transaction(&WrtDatabase::interface());
40 if (IsElementOnAdultBlackList(url)) {
45 WRT_DB_INSERT(insert, ChildProtectionBlacklist, &WrtDatabase::interface())
46 ChildProtectionBlacklist::Row row;
52 } Catch(DPL::DB::SqlConnection::Exception::Base) {
53 ReThrowMsg(GlobalDAO::Exception::DatabaseError,
54 "Failed to add element to AdultBlackList");
58 void GlobalDAO::RemoveAdultBlackListElement(const DPL::String &url)
61 using namespace DPL::DB::ORM;
62 using namespace DPL::DB::ORM::wrt;
63 WRT_DB_DELETE(deleteFrom, ChildProtectionBlacklist, &WrtDatabase::interface())
65 deleteFrom->Where(Equals<ChildProtectionBlacklist::url>(url));
66 deleteFrom->Execute();
67 } Catch(DPL::DB::SqlConnection::Exception::Base) {
68 ReThrowMsg(GlobalDAO::Exception::DatabaseError,
69 "Failed to removed element from AdultBlackList");
73 void GlobalDAO::UpdateAdultBlackList(const DPL::String &oldUrl,
74 const DPL::String &newUrl)
77 using namespace DPL::DB::ORM;
78 using namespace DPL::DB::ORM::wrt;
80 ScopedTransaction transaction(&WrtDatabase::interface());
81 if (IsElementOnAdultBlackList(newUrl)) {
85 WRT_DB_UPDATE(update, ChildProtectionBlacklist, &WrtDatabase::interface())
86 ChildProtectionBlacklist::Row row;
89 update->Where(Equals<ChildProtectionBlacklist::url>(oldUrl));
93 } Catch(DPL::DB::SqlConnection::Exception::Base) {
94 ReThrowMsg(GlobalDAO::Exception::DatabaseError,
95 "Failed to update an element in AdultBlackList");
99 void GlobalDAO::SetDeveloperMode(bool mode)
101 LogDebug("updating Developer mode to:" << mode);
103 using namespace DPL::DB::ORM;
104 using namespace DPL::DB::ORM::wrt;
105 GlobalProperties::Row row;
106 row.Set_developer_mode(mode);
108 WRT_DB_UPDATE(update, GlobalProperties, &WrtDatabase::interface())
112 Catch(DPL::DB::SqlConnection::Exception::Base){
113 ReThrowMsg(GlobalDAO::Exception::DatabaseError,
114 "Failed to update Developer Mode");
118 void GlobalDAO::AddDefferedWidgetPackageInstallation(const DPL::String &path)
120 LogDebug("Adding widget package as defered. Path: " << path);
122 using namespace DPL::DB::ORM;
123 using namespace DPL::DB::ORM::wrt;
124 DefferedWidgetPackageInstallation::Row row;
127 WRT_DB_INSERT(insert, DefferedWidgetPackageInstallation, &WrtDatabase::interface())
131 Catch(DPL::DB::SqlConnection::Exception::Base){
132 ReThrowMsg(GlobalDAO::Exception::DatabaseError,
133 "Failed to add defered widget package");
137 void GlobalDAO::RemoveDefferedWidgetPackageInstallation(const DPL::String &path)
139 LogDebug("Remove widget package from differed list. Path: " << path);
141 using namespace DPL::DB::ORM;
142 using namespace DPL::DB::ORM::wrt;
143 WRT_DB_DELETE(del, DefferedWidgetPackageInstallation, &WrtDatabase::interface())
144 del->Where(Equals<DefferedWidgetPackageInstallation::path>(path));
147 Catch(DPL::DB::SqlConnection::Exception::Base){
148 ReThrowMsg(GlobalDAO::Exception::DatabaseError,
149 "Failed to remove defered widget package");
153 void GlobalDAO::SetParentalMode(bool parental_status)
156 using namespace DPL::DB::ORM;
157 using namespace DPL::DB::ORM::wrt;
158 GlobalProperties::Row row;
159 row.Set_parental_mode(parental_status);
160 WRT_DB_UPDATE(update, GlobalProperties, &WrtDatabase::interface())
164 } Catch(DPL::DB::SqlConnection::Exception::Base) {
165 ReThrowMsg(GlobalDAO::Exception::DatabaseError,
166 "Failed to update Parental Mode");
170 void GlobalDAO::SetParentalAllowedAge(const DPL::OptionalInt& age)
172 using namespace DPL::DB::ORM;
173 using namespace DPL::DB::ORM::wrt;
175 typedef wrt::GlobalProperties::Row ParentalAllowedAgeRow;
177 ParentalAllowedAgeRow row;
178 row.Set_parental_allowed_age(age);
179 WRT_DB_UPDATE(update, GlobalProperties, &WrtDatabase::interface())
182 } Catch(DPL::DB::SqlConnection::Exception::Base) {
183 ReThrowMsg(GlobalDAO::Exception::DatabaseError,
184 "Failed to update Parental Mode");
188 void GlobalDAO::SetSecureByDefault(bool secure)
190 //If secure == true -> widget does not need to be signed
192 using namespace DPL::DB::ORM;
193 using namespace DPL::DB::ORM::wrt;
194 GlobalProperties::Row row;
195 row.Set_secure_by_default(secure);
196 WRT_DB_UPDATE(update, GlobalProperties, &WrtDatabase::interface())
200 Catch(DPL::DB::SqlConnection::Exception::Base) {
201 ReThrowMsg(GlobalDAO::Exception::DatabaseError,
202 "Failed to update secureByDefault");
206 void GlobalDAO::setComplianceMode(bool mode)
208 LogDebug("Updating compliance mode to:" << mode);
210 using namespace DPL::DB::ORM; using namespace DPL::DB::ORM::wrt;
211 GlobalProperties::Row row;
212 row.Set_compliance_mode(mode);
214 WRT_DB_UPDATE(update, GlobalProperties, &WrtDatabase::interface())
218 Catch (DPL::DB::SqlConnection::Exception::Base){
219 ReThrowMsg(GlobalDAO::Exception::DatabaseError,
220 "Failed to update compliance mode");
224 void GlobalDAO::setComplianceFakeImei(const std::string &imei)
226 LogDebug("Setting compliance fake IMEI: " << imei);
228 using namespace DPL::DB::ORM; using namespace DPL::DB::ORM::wrt;
229 GlobalProperties::Row row;
230 row.Set_compliance_fake_imei(DPL::FromASCIIString(imei));
232 WRT_DB_UPDATE(update, GlobalProperties, &WrtDatabase::interface())
236 Catch (DPL::DB::SqlConnection::Exception::Base){
237 ReThrowMsg(GlobalDAO::Exception::DatabaseError,
238 "Failed to update compliance fake IMEI");
242 void GlobalDAO::setComplianceFakeMeid(const std::string &meid)
244 LogDebug("Setting compliance fake MEID: " << meid);
246 using namespace DPL::DB::ORM; using namespace DPL::DB::ORM::wrt;
247 GlobalProperties::Row row;
248 row.Set_compliance_fake_meid(DPL::FromASCIIString(meid));
250 WRT_DB_UPDATE(update, GlobalProperties, &WrtDatabase::interface())
254 Catch (DPL::DB::SqlConnection::Exception::Base){
255 ReThrowMsg(GlobalDAO::Exception::DatabaseError,
256 "Failed to update compliance fake MEID");
260 void GlobalDAO::AddCategoryRule(const ChildProtection::PowderRules::
261 CategoryRule& powder)
264 using namespace DPL::DB::ORM;
265 using namespace DPL::DB::ORM::wrt;
266 PowderRules::Row row;
267 row.Set_category(powder.category);
268 row.Set_level(powder.level);
269 row.Set_context(powder.context);
271 wrt::ScopedTransaction transaction(&WrtDatabase::interface());
272 if (IsPowderRulePresent(powder)) {
273 transaction.Commit();
276 WRT_DB_INSERT(insert, PowderRules, &WrtDatabase::interface())
280 transaction.Commit();
281 } Catch(DPL::DB::SqlConnection::Exception::Base) {
282 ReThrowMsg(GlobalDAO::Exception::DatabaseError,
283 "Failed to add POWDER rules");
287 void GlobalDAO::RemoveCategoryRule(const ChildProtection::PowderRules::
288 CategoryRule& powder)
291 using namespace DPL::DB::ORM;
292 using namespace DPL::DB::ORM::wrt;
294 if (!powder.context.IsNull()) {
295 WRT_DB_DELETE(delWithContext, PowderRules, &WrtDatabase::interface())
297 delWithContext->Where(
298 And(Equals<PowderRules::category>(powder.category),
299 And(Equals<PowderRules::level>(powder.level),
300 Equals<PowderRules::context>(powder.context))));
301 delWithContext->Execute();
303 WRT_DB_DELETE(delWithoutContext, PowderRules, &WrtDatabase::interface())
304 delWithoutContext->Where(
305 And(Equals<PowderRules::category>(powder.category),
306 And(Equals<PowderRules::level>(powder.level),
307 Is<PowderRules::context>(powder.context))));
308 delWithoutContext->Execute();
310 } Catch(DPL::DB::SqlConnection::Exception::Base) {
311 ReThrowMsg(GlobalDAO::Exception::DatabaseError,
312 "Failed to remove POWDER rules");
316 void GlobalDAO::UpdateCategoryRule(
317 const ChildProtection::PowderRules::CategoryRule& oldRule,
318 const ChildProtection::PowderRules::CategoryRule& newRule)
321 using namespace DPL::DB::ORM;
322 using namespace DPL::DB::ORM::wrt;
323 PowderRules::Row row;
324 row.Set_category(newRule.category);
325 row.Set_level(newRule.level);
326 row.Set_context(newRule.context);
328 wrt::ScopedTransaction transaction(&WrtDatabase::interface());
330 if (IsPowderRulePresent(newRule)) {
331 transaction.Commit();
335 if (!oldRule.context.IsNull()) {
336 WRT_DB_UPDATE(updateWithContext, PowderRules, &WrtDatabase::interface())
337 updateWithContext->Where(
338 And(Equals<PowderRules::category>(oldRule.category),
339 And(Equals<PowderRules::level>(oldRule.level),
340 Equals<PowderRules::context>(oldRule.context))));
341 updateWithContext->Values(row);
342 updateWithContext->Execute();
344 WRT_DB_UPDATE(updateWithoutContext, PowderRules, &WrtDatabase::interface())
345 updateWithoutContext->Where(
346 And(Equals<PowderRules::category>(oldRule.category),
347 And(Equals<PowderRules::level>(oldRule.level),
348 Is<PowderRules::context>(oldRule.context))));
349 updateWithoutContext->Values(row);
350 updateWithoutContext->Execute();
352 transaction.Commit();
353 } Catch(DPL::DB::SqlConnection::Exception::Base) {
354 ReThrowMsg(GlobalDAO::Exception::DatabaseError,
355 "Failed to update POWDER rules");
359 void GlobalDAO::SetHomeNetworkDataUsage(GlobalDAO::NetworkAccessMode newMode)
361 LogDebug("updating home network data usage to:" << newMode);
363 using namespace DPL::DB::ORM;
364 using namespace DPL::DB::ORM::wrt;
365 GlobalProperties::Row row;
366 row.Set_home_network_data_usage(static_cast<int>(newMode));
368 WRT_DB_UPDATE(update, GlobalProperties, &WrtDatabase::interface())
372 Catch(DPL::DB::SqlConnection::Exception::Base){
373 ReThrowMsg(GlobalDAO::Exception::DatabaseError,
374 "Failed to update home network data usage");
378 void GlobalDAO::SetRoamingDataUsage(GlobalDAO::NetworkAccessMode newMode)
380 LogDebug("updating roaming network data usage to:" << newMode);
382 using namespace DPL::DB::ORM;
383 using namespace DPL::DB::ORM::wrt;
384 GlobalProperties::Row row;
385 row.Set_roaming_data_usage(static_cast<int>(newMode));
387 WRT_DB_UPDATE(update, GlobalProperties, &WrtDatabase::interface())
391 Catch(DPL::DB::SqlConnection::Exception::Base){
392 ReThrowMsg(GlobalDAO::Exception::DatabaseError,
393 "Failed to update roaming network data usage");
397 void GlobalDAO::SetAutoSaveIdPasswd(const DPL::String &url,
398 const AutoSaveData &saveData)
401 using namespace DPL::DB::ORM;
402 using namespace DPL::DB::ORM::wrt;
403 ScopedTransaction transaction(&WrtDatabase::interface());
404 AutoSaveIdPasswd::Row row;
406 row.Set_address(url);
407 row.Set_userId(saveData.userId);
408 row.Set_passwd(saveData.passwd);
410 DPL::Optional<GlobalDAO::AutoSaveData> savedData =
411 GetAutoSaveIdPasswd(url);
413 if (!savedData.IsNull()) {
414 WRT_DB_UPDATE(update, AutoSaveIdPasswd, &WrtDatabase::interface())
416 update->Where(Equals<AutoSaveIdPasswd::address>(url));
420 WRT_DB_INSERT(insert, AutoSaveIdPasswd, &WrtDatabase::interface());
425 transaction.Commit();
426 } Catch(DPL::DB::SqlConnection::Exception::Base) {
427 ReThrowMsg(GlobalDAO::Exception::DatabaseError,
428 "Fail to register id, passwd for autosave");
432 void GlobalDAO::AddWhiteURI(const std::string &uri, bool subDomain)
434 LogDebug("Add White URI : " << uri);
436 using namespace DPL::DB::ORM;
437 using namespace DPL::DB::ORM::wrt;
438 WidgetWhiteURIList::Row row;
439 row.Set_uri(DPL::FromASCIIString(uri));
440 row.Set_subdomain_access(static_cast<int>(subDomain));
441 wrt::ScopedTransaction transaction(&WrtDatabase::interface());
443 WRT_DB_INSERT(insert, WidgetWhiteURIList, &WrtDatabase::interface())
447 transaction.Commit();
449 Catch(DPL::DB::SqlConnection::Exception::Base){
450 ReThrowMsg(GlobalDAO::Exception::DatabaseError,
451 "Failed to add white URI");
455 void GlobalDAO::RemoveWhiteURI(const std::string &uri)
457 LogDebug("Remove White URI : " << uri);
459 using namespace DPL::DB::ORM;
460 using namespace DPL::DB::ORM::wrt;
462 WRT_DB_DELETE(deleteFrom, WidgetWhiteURIList, &WrtDatabase::interface())
463 deleteFrom->Where(Equals<WidgetWhiteURIList::uri>(DPL::FromASCIIString(uri)));
464 deleteFrom->Execute();
465 } Catch(DPL::DB::SqlConnection::Exception::Base) {
466 ReThrowMsg(GlobalDAO::Exception::DatabaseError,
467 "Failed to removed white URI from AdultBlackList");