2 // Tizen Web Device API
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
20 * @author Kisub Song (kisubs.song@samsung.com)
26 #include <dpl/exception.h>
27 #include <Commons/Exception.h>
28 #include <Commons/Regex.h>
29 #include "ContactUtility.h"
30 #include "ContactsSvcObjectConverter.h"
36 using namespace WrtDeviceApis::Commons;
45 Person::Person(contacts_record_h record)
55 void Person::OnRequestReceived(const EventPersonLinkPtr &event)
60 contacts_record_h contacts_record = NULL;
67 if(!event->getPersonIdIsSet())
68 ThrowMsg(InvalidArgumentException, "person id were not set.");
70 string personIdStr = event->getPersonId();
72 if(!ContactUtility::checkStrIsUInt(personIdStr))
73 ThrowMsg(InvalidArgumentException, "Id is wrong (" << personIdStr << ")" );
76 personId = ContactUtility::strToInt(personIdStr);
77 selfPersonId = ContactUtility::strToInt(m_id);
79 ThrowMsg(InvalidArgumentException, "Id is wrong (" << personIdStr << ")" );
82 errorCode = contacts_db_get_record(_contacts_person._uri, personId, &contacts_record);
83 if(errorCode != CONTACTS_ERROR_NONE)
85 contacts_record_destroy(contacts_record, true);
86 ThrowMsg(NotFoundException, "No person (id:" << personId << ")");
88 contacts_record_destroy(contacts_record, true);
89 contacts_record = NULL;
91 LoggerD("Linking person : " << personId);
93 errorCode = contacts_person_link_person(selfPersonId, personId);
94 if(errorCode == CONTACTS_ERROR_INVALID_PARAMETER)
95 ThrowMsg(NotFoundException, "Error during executing contacts_db_delete_record()");
96 else if (errorCode != CONTACTS_ERROR_NONE)
97 ThrowMsg(PlatformException, "Error during executing contacts_db_delete_record()");
99 errorCode = contacts_db_get_record(_contacts_person._uri, selfPersonId, &contacts_record);
100 if(errorCode != CONTACTS_ERROR_NONE)
102 contacts_record_destroy(contacts_record, true);
103 ThrowMsg(NotFoundException, "No person (id:" << selfPersonId << ")");
105 PersonPtr person = PersonPtr(new Person());
106 ContactsSvcObjectConverter::convertToAbstract(contacts_record, person);
110 contacts_record_destroy(contacts_record, true);
111 contacts_record = NULL;
113 event->setResult(true);
114 event->setExceptionCode(ExceptionCodes::None);
116 Catch (InvalidArgumentException)
118 LoggerE("Invalid person id : " << _rethrown_exception.GetMessage());
119 event->setResult(false);
120 event->setExceptionCode(ExceptionCodes::InvalidArgumentException);
123 Catch (NotFoundException)
125 LoggerE("Person doesn't exist : " << _rethrown_exception.GetMessage());
126 event->setResult(false);
127 event->setExceptionCode(ExceptionCodes::NotFoundException);
130 Catch (PlatformException) {
131 LoggerE("Error during linking person : " << _rethrown_exception.GetMessage());
132 event->setResult(false);
133 event->setExceptionCode(ExceptionCodes::PlatformException);
137 LoggerE("Error during linking person : " << _rethrown_exception.GetMessage());
138 event->setResult(false);
139 event->setExceptionCode(ExceptionCodes::PlatformException);
144 void Person::OnRequestReceived(const EventPersonUnlinkPtr &event)
151 contacts_record_h contacts_record = NULL;
155 if(!event->getContactIdIsSet())
156 ThrowMsg(InvalidArgumentException, "contact id were not set.");
158 string contactIdStr = event->getContactId();
160 if(!ContactUtility::checkStrIsUInt(contactIdStr))
161 ThrowMsg(InvalidArgumentException, "Id is wrong (" << contactIdStr << ")" );
164 selfPersonId = ContactUtility::strToInt(m_id);
165 contactId = ContactUtility::strToInt(contactIdStr);
167 ThrowMsg(InvalidArgumentException, "Id is wrong (" << contactIdStr << ")" );
170 errorCode = contacts_db_get_record(_contacts_simple_contact._uri, contactId, &contacts_record);
171 if(errorCode != CONTACTS_ERROR_NONE)
173 contacts_record_destroy(contacts_record, true);
174 ThrowMsg(NotFoundException, "No contact (id:" << contactId << ")");
177 int contactsPersonId = 0;
178 errorCode = contacts_record_get_int(contacts_record, _contacts_simple_contact.person_id, &contactsPersonId);
179 if(errorCode != CONTACTS_ERROR_NONE)
181 contacts_record_destroy(contacts_record, true);
182 ThrowMsg(NotFoundException, "Contact (id:" << contactId << ") is not a member of person");
184 contacts_record_destroy(contacts_record, true);
185 contacts_record = NULL;
187 if(contactsPersonId != selfPersonId)
189 ThrowMsg(OutOfRangeException, "Contact (id:" << contactId << ") is not a member of person");
192 LoggerD("Unlinking contact : " << contactId);
195 int errorCode = contacts_person_unlink_contact(selfPersonId, contactId, &newPersonId);
196 if(errorCode == CONTACTS_ERROR_INVALID_PARAMETER)
197 ThrowMsg(NotFoundException, "Error during executing contacts_person_unlink_contact()");
198 else if (errorCode != CONTACTS_ERROR_NONE)
199 ThrowMsg(PlatformException, "Error during executing contacts_person_unlink_contact()");
200 LoggerD("newPersonId : " << newPersonId);
202 errorCode = contacts_db_get_record(_contacts_person._uri, newPersonId, &contacts_record);
203 if(errorCode != CONTACTS_ERROR_NONE)
205 ThrowMsg(NotFoundException, "No person (id:" << newPersonId << ")");
207 PersonPtr person = PersonPtr(new Person());
208 ContactsSvcObjectConverter::convertToAbstract(contacts_record, person);
209 event->setPerson(person);
210 contacts_record_destroy(contacts_record, true);
211 contacts_record = NULL;
213 errorCode = contacts_db_get_record(_contacts_person._uri, selfPersonId, &contacts_record);
214 if(errorCode != CONTACTS_ERROR_NONE)
216 contacts_record_destroy(contacts_record, true);
217 ThrowMsg(NotFoundException, "No person (id:" << selfPersonId << ")");
219 person = PersonPtr(new Person());
220 ContactsSvcObjectConverter::convertToAbstract(contacts_record, person);
224 contacts_record_destroy(contacts_record, true);
225 contacts_record = NULL;
227 event->setResult(true);
228 event->setExceptionCode(ExceptionCodes::None);
230 Catch (InvalidArgumentException)
232 LoggerE("Invalid person id : " << _rethrown_exception.GetMessage());
233 event->setResult(false);
234 event->setExceptionCode(ExceptionCodes::InvalidArgumentException);
237 Catch (NotFoundException)
239 LoggerE("Contact doesn't exist : " << _rethrown_exception.GetMessage());
240 event->setResult(false);
241 event->setExceptionCode(ExceptionCodes::NotFoundException);
244 Catch (OutOfRangeException)
246 LoggerE("Contact doesn't exist : " << _rethrown_exception.GetMessage());
247 event->setResult(false);
248 event->setExceptionCode(ExceptionCodes::OutOfRangeException);
251 Catch (PlatformException) {
252 LoggerE("Error during linking person : " << _rethrown_exception.GetMessage());
253 event->setResult(false);
254 event->setExceptionCode(ExceptionCodes::PlatformException);
258 LoggerE("Error during linking person : " << _rethrown_exception.GetMessage());
259 event->setResult(false);
260 event->setExceptionCode(ExceptionCodes::PlatformException);
264 if(contacts_record != NULL)
265 contacts_record_destroy(contacts_record, true);