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.
20 * @author Jihwa Park (jh7979.park@samsung.com)
25 #include <dpl/log/log.h>
26 #include <dpl/scoped_ptr.h>
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;
42 AccountService::AccountService()
47 AccountService::~AccountService()
52 //TODO: implmeting Eventwrapper
55 void AccountService::OnRequestReceived(const IEventDeleteAccountPtr &account)
62 if (!account->getEvent()) {
63 ThrowMsg(NullPointerException, "event parameter is NULL");
66 //TODO: check if ID is valid
68 if (!account->getEvent()->getIdIsSet()) {
69 ThrowMsg(Commons::InvalidArgumentException,
70 "Cannot delete non-existing event.");
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);
81 accountWrapper->deleteAccount();
82 account->setResult(true);
84 catch (const NotFoundException &ex)
86 LogError("event doesn't exist");
87 account->setResult(false);
88 account->setExceptionCode(ExceptionCodes::NotFoundException);
90 catch (const Exception &ex)
92 LogError("Error during deleting event " << ex.DumpToString());
93 account->setResult(false);
95 account->setCancelAllowed(false);
99 void AccountService::OnRequestReceived(const IEventUpdateAccountPtr &account)
106 if (!account->getEvent()) {
107 ThrowMsg(NullPointerException, "event parameter is NULL");
109 /* if (!account->getEvent()->getIdIsSet()) {
111 Commons::InvalidArgumentException,
112 "Cannot update non-existing event. Event needs adding or ID is wrong");
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);
122 accountWrapper->saveAccount();
123 account->setResult(true);
125 catch (const Exception &ex)
127 LogError("Error during updating event " << ex.DumpToString());
128 account->setResult(false);
130 account->setCancelAllowed(false);
134 void AccountService::OnRequestReceived(const IEventFindAccountsPtr &event)
138 const AccountFilterPtr &filter = event->getFilter();
140 int error_code = ACCOUNT_OPERATION_SUCCESS;
146 if(filter->getIdIsSet()) {
147 std::istringstream istream(filter->getIdFilter());
150 LogDebug("id : " << id);
154 if (ACCOUNT_OPERATION_SUCCESS != account_svc_new(ACCOUNT_CATEGORY, &handle)) {
155 ThrowMsg(PlatformException, "Can't create handle");
157 if (ACCOUNT_OPERATION_SUCCESS != account_svc_get_account_list(handle, 0)) {
158 ThrowMsg(PlatformException, "Can't get all records");
161 while(error_code== ACCOUNT_OPERATION_SUCCESS)
163 event->tryCancelled();
164 int accountId = account_svc_get_value_int(handle, ACCOUNT_ID, &error_code);
166 if(error_code!=ACCOUNT_OPERATION_SUCCESS) {
167 ThrowMsg(PlatformException, "Can't get handle");
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);
175 event->setResult(true);
176 account_svc_get_finish(handle);
178 catch (const Exception &ex)
180 LogError("Exception: " << ex.DumpToString());
181 event->setResult(false);
184 event->setCancelAllowed(true);
187 void AccountService::OnRequestReceived(const IEventCreateAccountPtr &event)
192 void AccountService::OnRequestReceived(const IEventAddAccountPtr &account)
197 if (!account->getEvent()) {
198 ThrowMsg(NullPointerException, "event parameter is NULL");
200 /* if (account->getEvent()->getIdIsSet()) {
201 LogWarning("adding event that is already added");
202 account->getEvent()->resetId();
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);
212 accountWrapper->saveAccount();
213 account->setResult(true);
215 catch (const Exception &ex)
217 LogError("Error during adding event" << ex.DumpToString());
218 account->setResult(false);
220 account->setCancelAllowed(false);