38e9d922d90248ee67f069ca1a2bb602ce0db2aa
[framework/web/wrt-installer.git] / src / jobs / widget_install / task_new_db_insert.cpp
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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  * @file    task_new_db_insert.cpp
18  * @author  Lukasz Wrzosek(l.wrzosek@samsung.com)
19  * @author  Soyoung kim(sy037.kim@samsung.com)
20  * @version 1.0
21  * @brief   Implementation file for installer task database updating for widget update
22  */
23 #include <time.h>
24 #include <sys/stat.h>
25 #include <widget_install/task_new_db_insert.h>
26 #include <widget_install/job_widget_install.h>
27 #include <widget_install/widget_install_errors.h>
28 #include <widget_install/widget_install_context.h>
29 //#include <dpl/wrt-dao-ro/config_parser_data.h>
30 #include <dpl/wrt-dao-rw/widget_dao.h>
31 #include <dpl/foreach.h>
32 #include <dpl/log/log.h>
33 #include <dpl/assert.h>
34 //#include <dpl/wrt-dao-ro/global_config.h>
35 #include <string>
36 #include <sstream>
37
38 using namespace WrtDB;
39
40 namespace Jobs {
41 namespace WidgetInstall {
42 TaskNewDbInsert::TaskNewDbInsert(InstallerContext& context) :
43     DPL::TaskDecl<TaskNewDbInsert>(this),
44     m_context(context)
45 {
46     AddStep(&TaskNewDbInsert::StepDBInsert);
47
48     AddAbortStep(&TaskNewDbInsert::StepAbortDBInsert);
49 }
50
51 void TaskNewDbInsert::StepDBInsert()
52 {
53     Try
54     {
55         /* Set install Time */
56         time(&m_context.widgetConfig.installedTime);
57
58         LogInfo("Registering widget...");
59
60         WidgetDAO::registerWidget(
61                 *(m_context.widgetHandle),
62                 m_context.widgetConfig,
63                 m_context.wacSecurity);
64
65         FOREACH (cap, m_context.staticPermittedDevCaps) {
66             LogInfo("staticPermittedDevCaps : " << cap->first
67                     << " smack status: " << cap->second);
68         }
69
70         Assert(!!m_context.widgetConfig.pkgname
71                && "pkgName should be initialized");
72
73         WrtDB::WidgetDAO widgetDao(*m_context.widgetHandle);
74         widgetDao.setPkgName(m_context.widgetConfig.pkgname);
75
76         LogInfo("Widget registered");
77     }
78     Catch(WidgetDAO::Exception::DatabaseError)
79     {
80         LogWarning("Database failure!");
81         ReThrowMsg(Exceptions::InsertNewWidgetFailed, "Database failure!");
82     }
83     Catch(DPL::DB::SqlConnection::Exception::Base)
84     {
85         LogDebug("Database failure!");
86         ReThrowMsg(Exceptions::InsertNewWidgetFailed, "Database failure!");
87     }
88
89     m_context.job->UpdateProgress(
90         InstallerContext::INSTALL_NEW_DB_INSERT,
91         "New Widget DB UPDATE Finished");
92 }
93
94 void TaskNewDbInsert::StepAbortDBInsert()
95 {
96     LogWarning("[DB Update Task] Aborting... (DB Clean)");
97     Assert(!!m_context.widgetHandle);
98     Try
99     {
100         WidgetDAO::unregisterWidget(*m_context.widgetHandle);
101
102         LogDebug("Cleaning DB successful!");
103     }
104     Catch(DPL::DB::SqlConnection::Exception::Base)
105     {
106         LogError("Failed to handle StepAbortDBClean!");
107     }
108 }
109
110 } //namespace WidgetInstall
111 } //namespace Jobs