wrt-plugins-tizen_0.4.23
[framework/web/wrt-plugins-tizen.git] / src / Contact / Person.cpp
1 //
2 // Tizen Web Device API
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
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
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
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.
16 //
17
18 /**
19  * @file        Person.cpp
20  * @author      Kisub Song (kisubs.song@samsung.com)
21  * @version     0.1
22  * @brief
23  */
24
25 #include "Person.h"
26 #include <dpl/exception.h>
27 #include <Commons/Exception.h>
28 #include <Commons/Regex.h>
29 #include "ContactUtility.h"
30 #include "ContactsSvcObjectConverter.h"
31 #include <Logger.h>
32
33 namespace DeviceAPI {
34 namespace Contact {
35
36 using namespace WrtDeviceApis::Commons;
37 using namespace std;
38
39 Person::Person()
40         : IPerson()
41 {
42         LoggerD("entered");
43 }
44
45 Person::Person(contacts_record_h record)
46         : IPerson()
47 {
48         LoggerD("entered");
49 }
50
51 Person::~Person()
52 {
53 }
54
55 void Person::OnRequestReceived(const EventPersonLinkPtr &event)
56 {
57         LoggerD("entered");
58
59         int errorCode = 0;
60         contacts_record_h contacts_record = NULL;
61
62         int selfPersonId = 0;
63         int personId = 0;
64
65         Try
66         {
67                 if(!event->getPersonIdIsSet())
68                         ThrowMsg(InvalidArgumentException, "person id were not set.");
69
70                 string personIdStr = event->getPersonId();
71
72                 if(!ContactUtility::checkStrIsUInt(personIdStr))
73                         ThrowMsg(InvalidArgumentException, "Id is wrong (" << personIdStr << ")" );
74
75                 try {
76                         personId = ContactUtility::strToInt(personIdStr);
77                         selfPersonId = ContactUtility::strToInt(m_id);
78                 } catch (...) {
79                         ThrowMsg(InvalidArgumentException, "Id is wrong (" << personIdStr << ")" );
80                 }
81
82                 errorCode = contacts_db_get_record(_contacts_person._uri, personId, &contacts_record);
83                 if(errorCode != CONTACTS_ERROR_NONE)
84                 {
85                         contacts_record_destroy(contacts_record, true);
86                         ThrowMsg(NotFoundException, "No person (id:" << personId << ")");
87                 }
88                 contacts_record_destroy(contacts_record, true);
89                 contacts_record = NULL;
90
91                 LoggerD("Linking person : " << personId);
92
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()");
98
99                 errorCode = contacts_db_get_record(_contacts_person._uri, selfPersonId, &contacts_record);
100                 if(errorCode != CONTACTS_ERROR_NONE)
101                 {
102                         contacts_record_destroy(contacts_record, true);
103                         ThrowMsg(NotFoundException, "No person (id:" << selfPersonId << ")");
104                 }
105                 PersonPtr person = PersonPtr(new Person());
106                 ContactsSvcObjectConverter::convertToAbstract(contacts_record, person);
107
108                 copy(person);
109
110                 contacts_record_destroy(contacts_record, true);
111                 contacts_record = NULL;
112
113                 event->setResult(true);
114                 event->setExceptionCode(ExceptionCodes::None);
115         }
116         Catch (InvalidArgumentException)
117         {
118                 LoggerE("Invalid person id : " << _rethrown_exception.GetMessage());
119                 event->setResult(false);
120                 event->setExceptionCode(ExceptionCodes::InvalidArgumentException);
121                 return;
122         }
123         Catch (NotFoundException)
124         {
125                 LoggerE("Person doesn't exist : " << _rethrown_exception.GetMessage());
126                 event->setResult(false);
127                 event->setExceptionCode(ExceptionCodes::NotFoundException);
128                 return;
129         }
130         Catch (PlatformException) {
131                 LoggerE("Error during linking person : " << _rethrown_exception.GetMessage());
132                 event->setResult(false);
133                 event->setExceptionCode(ExceptionCodes::PlatformException);
134                 return;
135         }
136         Catch (Exception) {
137                 LoggerE("Error during linking person : " << _rethrown_exception.GetMessage());
138                 event->setResult(false);
139                 event->setExceptionCode(ExceptionCodes::PlatformException);
140                 return;
141         }
142 }
143
144 void Person::OnRequestReceived(const EventPersonUnlinkPtr &event)
145 {
146         LoggerD("entered");
147
148         int errorCode = 0;
149         int selfPersonId;
150         int contactId;
151         contacts_record_h contacts_record = NULL;
152
153         Try
154         {
155                 if(!event->getContactIdIsSet())
156                         ThrowMsg(InvalidArgumentException, "contact id were not set.");
157
158                 string contactIdStr = event->getContactId();
159
160                 if(!ContactUtility::checkStrIsUInt(contactIdStr))
161                         ThrowMsg(InvalidArgumentException, "Id is wrong (" << contactIdStr << ")" );
162
163                 try {
164                         selfPersonId = ContactUtility::strToInt(m_id);
165                         contactId = ContactUtility::strToInt(contactIdStr);
166                 } catch (...) {
167                         ThrowMsg(InvalidArgumentException, "Id is wrong (" << contactIdStr << ")" );
168                 }
169
170                 errorCode = contacts_db_get_record(_contacts_simple_contact._uri, contactId, &contacts_record);
171                 if(errorCode != CONTACTS_ERROR_NONE)
172                 {
173                         contacts_record_destroy(contacts_record, true);
174                         ThrowMsg(NotFoundException, "No contact (id:" << contactId << ")");
175                 }
176
177                 int contactsPersonId = 0;
178                 errorCode = contacts_record_get_int(contacts_record, _contacts_simple_contact.person_id, &contactsPersonId);
179                 if(errorCode != CONTACTS_ERROR_NONE)
180                 {
181                         contacts_record_destroy(contacts_record, true);
182                         ThrowMsg(NotFoundException, "Contact (id:" << contactId << ") is not a member of person");
183                 }
184                 contacts_record_destroy(contacts_record, true);
185                 contacts_record = NULL;
186
187                 if(contactsPersonId != selfPersonId)
188                 {
189                         ThrowMsg(OutOfRangeException, "Contact (id:" << contactId << ") is not a member of person");
190                 }
191
192                 LoggerD("Unlinking contact : " << contactId);
193
194                 int newPersonId = 0;
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);
201
202                 errorCode = contacts_db_get_record(_contacts_person._uri, newPersonId, &contacts_record);
203                 if(errorCode != CONTACTS_ERROR_NONE)
204                 {
205                         ThrowMsg(NotFoundException, "No person (id:" << newPersonId << ")");
206                 }
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;
212
213                 errorCode = contacts_db_get_record(_contacts_person._uri, selfPersonId, &contacts_record);
214                 if(errorCode != CONTACTS_ERROR_NONE)
215                 {
216                         contacts_record_destroy(contacts_record, true);
217                         ThrowMsg(NotFoundException, "No person (id:" << selfPersonId << ")");
218                 }
219                 person = PersonPtr(new Person());
220                 ContactsSvcObjectConverter::convertToAbstract(contacts_record, person);
221
222                 copy(person);
223
224                 contacts_record_destroy(contacts_record, true);
225                 contacts_record = NULL;
226
227                 event->setResult(true);
228                 event->setExceptionCode(ExceptionCodes::None);
229         }
230         Catch (InvalidArgumentException)
231         {
232                 LoggerE("Invalid person id : " << _rethrown_exception.GetMessage());
233                 event->setResult(false);
234                 event->setExceptionCode(ExceptionCodes::InvalidArgumentException);
235                 return;
236         }
237         Catch (NotFoundException)
238         {
239                 LoggerE("Contact doesn't exist : " << _rethrown_exception.GetMessage());
240                 event->setResult(false);
241                 event->setExceptionCode(ExceptionCodes::NotFoundException);
242                 return;
243         }
244         Catch (OutOfRangeException)
245         {
246                 LoggerE("Contact doesn't exist : " << _rethrown_exception.GetMessage());
247                 event->setResult(false);
248                 event->setExceptionCode(ExceptionCodes::OutOfRangeException);
249                 return;
250         }
251         Catch (PlatformException) {
252                 LoggerE("Error during linking person : " << _rethrown_exception.GetMessage());
253                 event->setResult(false);
254                 event->setExceptionCode(ExceptionCodes::PlatformException);
255                 return;
256         }
257         Catch (Exception) {
258                 LoggerE("Error during linking person : " << _rethrown_exception.GetMessage());
259                 event->setResult(false);
260                 event->setExceptionCode(ExceptionCodes::PlatformException);
261                 return;
262         }
263
264         if(contacts_record != NULL)
265                 contacts_record_destroy(contacts_record, true);
266 }
267
268 } // Contact
269 } // DeviceAPI