Beta merge 2
[profile/ivi/wrt-plugins-tizen.git] / src / platform / Tizen / Account / old / AccountService.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
18 /**
19  * @file        Account.cpp
20  * @author      Jihwa Park (jh7979.park@samsung.com)
21  * @author      Sangtai Kim
22  * @version     0.1
23  */
24
25 #include <dpl/log/log.h>
26 #include <dpl/scoped_ptr.h>
27
28 #include "AccountManager.h"
29 #include "API/Account/OnAddEventsChanged.h"
30 #include "API/Account/OnUpdateEventsChanged.h"
31 #include "API/Account/OnDeleteEventsChanged.h"
32 #include "account-svc-db.h"
33 #include "AccountService.h"
34 using namespace TizenApis::Api::Account;
35 using namespace WrtDeviceApis::Commons;
36 //using namespace WrtDeviceApis::CommonsJavaScript;
37
38 namespace TizenApis {
39 namespace Platform {
40 namespace Account{
41
42 AccountService::AccountService()
43 {
44     LogDebug("entered");
45 }
46
47 AccountService::~AccountService()
48 {
49     LogDebug("entered");
50 }
51
52 //TODO: implmeting Eventwrapper
53
54
55 void AccountService::OnRequestReceived(const IEventDeleteAccountPtr &account)
56 {
57     LogDebug("entered");
58 #if 1
59
60     Try
61     {
62         if (!account->getEvent()) {
63             ThrowMsg(NullPointerException, "event parameter is NULL");
64         }
65
66         //TODO: check if ID is valid
67          /*
68         if (!account->getEvent()->getIdIsSet()) {
69             ThrowMsg(Commons::InvalidArgumentException,
70                      "Cannot delete non-existing event.");
71         }
72         */
73         DPL::ScopedPtr<AccountWrapper> accountWrapper(new AccountWrapper(
74                                                       account->getEvent()));
75         accountWrapper->convertAbstractAccountToPlatformAccount();
76         if (account->checkCancelled()) {
77             account->setCancelAllowed(true);
78             account->setResult(true);
79             return;
80         }
81         accountWrapper->deleteAccount();
82         account->setResult(true);
83     }
84     catch (const NotFoundException &ex)
85     {
86         LogError("event doesn't exist");
87         account->setResult(false);
88         account->setExceptionCode(ExceptionCodes::NotFoundException);
89     }
90     catch (const Exception &ex)
91     {
92         LogError("Error during deleting event " << ex.DumpToString());
93         account->setResult(false);
94     }
95     account->setCancelAllowed(false);
96 #endif
97 }
98
99 void AccountService::OnRequestReceived(const IEventUpdateAccountPtr &account)
100 {
101     LogDebug("entered");
102 #if 1
103
104     Try
105     {
106         if (!account->getEvent()) {
107             ThrowMsg(NullPointerException, "event parameter is NULL");
108         }
109   /*      if (!account->getEvent()->getIdIsSet()) {
110             ThrowMsg(
111                 Commons::InvalidArgumentException,
112                 "Cannot update non-existing event. Event needs adding or ID is wrong");
113         }*/
114         DPL::ScopedPtr<AccountWrapper> accountWrapper(new AccountWrapper(
115                                                       account->getEvent()));
116         accountWrapper->convertAbstractAccountToPlatformAccount();
117         if (account->checkCancelled()) {
118             account->setCancelAllowed(true);
119             account->setResult(true);
120             return;
121         }
122         accountWrapper->saveAccount();
123         account->setResult(true);
124     }
125     catch (const Exception &ex)
126     {
127         LogError("Error during updating event " << ex.DumpToString());
128         account->setResult(false);
129     }
130     account->setCancelAllowed(false);
131 #endif
132 }
133
134 void AccountService::OnRequestReceived(const IEventFindAccountsPtr &event)
135 {
136     LogDebug("entered");
137
138     const AccountFilterPtr &filter = event->getFilter();
139     char* handle = NULL;
140     int error_code = ACCOUNT_OPERATION_SUCCESS;
141
142         try {
143
144         if (NULL != filter)
145         {
146             if(filter->getIdIsSet()) {
147                 std::istringstream istream(filter->getIdFilter());
148                 int id;
149                 istream>>id;
150                                 LogDebug("id : " << id);
151             }
152         }
153
154                 if (ACCOUNT_OPERATION_SUCCESS != account_svc_new(ACCOUNT_CATEGORY, &handle)) {
155                         ThrowMsg(PlatformException, "Can't create handle");
156                 }
157                 if (ACCOUNT_OPERATION_SUCCESS != account_svc_get_account_list(handle, 0)) {
158                         ThrowMsg(PlatformException, "Can't get all  records");
159                 }
160
161                 while(error_code== ACCOUNT_OPERATION_SUCCESS)
162                 {
163                     event->tryCancelled();
164                     int accountId = account_svc_get_value_int(handle, ACCOUNT_ID, &error_code);
165
166                     if(error_code!=ACCOUNT_OPERATION_SUCCESS)   {
167                                 ThrowMsg(PlatformException, "Can't get handle");
168                         }
169
170                         DPL::ScopedPtr<AccountWrapper> accountWrapper(new AccountWrapper());
171                         accountWrapper->loadAccount(accountId);
172                         event->addEvent(accountWrapper->getAbstractAccount());
173                         error_code = account_svc_get_next_val(handle);
174                 }
175                 event->setResult(true);
176                 account_svc_get_finish(handle);
177     }
178     catch (const Exception &ex)
179     {
180         LogError("Exception: " << ex.DumpToString());
181         event->setResult(false);
182     }
183
184     event->setCancelAllowed(true);
185 }
186
187 void AccountService::OnRequestReceived(const IEventCreateAccountPtr &event)
188 {
189     LogDebug("entered");
190 }
191
192 void AccountService::OnRequestReceived(const IEventAddAccountPtr &account)
193 {
194     LogDebug("entered");
195     Try
196     {
197         if (!account->getEvent()) {
198             ThrowMsg(NullPointerException, "event parameter is NULL");
199         }
200        /* if (account->getEvent()->getIdIsSet()) {
201             LogWarning("adding event that is already added");
202             account->getEvent()->resetId();
203         }*/
204         DPL::ScopedPtr<AccountWrapper> accountWrapper(new AccountWrapper(
205                                                       account->getEvent()));
206         accountWrapper->convertAbstractAccountToPlatformAccount();
207         if (account->checkCancelled()) {
208             account->setCancelAllowed(true);
209             account->setResult(true);
210             return;
211         }
212         accountWrapper->saveAccount();
213         account->setResult(true);
214     }
215     catch (const Exception &ex)
216     {
217         LogError("Error during adding event" << ex.DumpToString());
218         account->setResult(false);
219     }
220     account->setCancelAllowed(false);
221 }
222
223 }
224 }
225 }