[Contact] remove TODO part 2
authorLukasz Bardeli <l.bardeli@samsung.com>
Thu, 8 Oct 2015 07:03:24 +0000 (09:03 +0200)
committerTomasz Marciniak <t.marciniak@samsung.com>
Thu, 8 Oct 2015 12:09:04 +0000 (14:09 +0200)
[verification] Code compiles without error. Passrate 100%

Change-Id: I97e97fd7a79fce9b25d700547d3631e5f089953d
Signed-off-by: Lukasz Bardeli <l.bardeli@samsung.com>
src/contact/addressbook.cc
src/contact/contact_manager.cc
src/contact/contact_search_engine.cc
src/contact/contact_util.cc
src/contact/js/address_book.js
src/contact/js/common.js
src/contact/js/contact_manager.js
src/contact/js/person.js
src/contact/person.cc

index 570fff2077bdfa6ee436d5aba7f1723e1b48943d..2a2031929a5b24a9a6105367dfbfeab77163d616 100755 (executable)
@@ -85,6 +85,7 @@ PlatformResult AddressBookAdd(const JsonObject& args, JsonObject& out) {
   if (status.IsError()) return status;
 
   const JsonObject& contact = FromJson<JsonObject>(args, "contact");
+  long addressBookId = common::stol(FromJson<JsonString>(args, "addressBookId"));
 
   if (!IsNull(contact, "id")) {
     LoggerW("Contact already exists");
@@ -107,6 +108,10 @@ PlatformResult AddressBookAdd(const JsonObject& args, JsonObject& out) {
       ContactUtil::ExportContactToContactsRecord(*contacts_record_ptr, contact);
   if (status.IsError()) return status;
 
+  status = ContactUtil::SetIntInRecord(
+      contacts_record, _contacts_contact.address_book_id, addressBookId);
+  if (status.IsError()) return status;
+
   int id = -1;
   err = contacts_db_insert_record(*contacts_record_ptr, &id);
   if (CONTACTS_ERROR_NONE != err) {
@@ -139,7 +144,6 @@ PlatformResult AddressBookUpdate(const JsonObject& args, JsonObject& out) {
   if (status.IsError()) return status;
 
   const JsonObject& contact = FromJson<JsonObject>(args, "contact");
-  const JsonObject& addressbook = FromJson<JsonObject>(args, "addressBook");
   long contactId = common::stol(FromJson<JsonString>(contact, "id"));
 
   if (IsNull(contact, "id")) {
@@ -147,12 +151,6 @@ PlatformResult AddressBookUpdate(const JsonObject& args, JsonObject& out) {
     return PlatformResult(ErrorCode::UNKNOWN_ERR, "Contact doesn't exist");
   }
 
-  if (IsNull(addressbook, "id")) {
-    LoggerE("Contact is not saved in database");
-    return PlatformResult(ErrorCode::INVALID_VALUES_ERR,
-                          "Contact is not saved in database");
-  }
-
   contacts_record_h to_update = nullptr;
   int err = contacts_db_get_record(_contacts_contact._uri, contactId, &to_update);
   if (CONTACTS_ERROR_NONE != err) {
@@ -306,12 +304,6 @@ PlatformResult AddressBookUpdateBatch(const JsonObject& args, JsonArray& out) {
   if (status.IsError()) return status;
 
   const JsonArray& batch_args = FromJson<JsonArray>(args, "batchArgs");
-  const JsonObject& addressBook = FromJson<JsonObject>(args, "addressBook");
-  if (IsNull(addressBook, "id")) {
-    LoggerE("Contact is not saved in database");
-    return PlatformResult(ErrorCode::INVALID_VALUES_ERR,
-                          "Contact is not saved in database");
-  }
   contacts_list_h contacts_list = NULL;
   int err = 0;
   err = contacts_list_create(&contacts_list);
@@ -378,12 +370,6 @@ PlatformResult AddressBookRemoveBatch(const JsonObject& args) {
   PlatformResult status = ContactUtil::CheckDBConnection();
   if (status.IsError()) return status;
   const JsonArray& batch_args = FromJson<JsonArray>(args, "batchArgs");
-  const JsonObject& addressBook = FromJson<JsonObject>(args, "addressBook");
-  if (IsNull(addressBook, "id")) {
-    LoggerE("Contact is not saved in database");
-    return PlatformResult(ErrorCode::INVALID_VALUES_ERR,
-                          "Contact is not saved in database");
-  }
   int length = static_cast<int>(batch_args.size());
   int ids[length], i=0;
   for (auto& item : batch_args) {
@@ -497,9 +483,7 @@ PlatformResult AddressBookGetGroup(const JsonObject& args, JsonObject& out) {
 
   ContactUtil::ContactsRecordHPtr record(&contacts_record,
                                          ContactUtil::ContactsDeleter);
-
-  long addressbook_id =
-      common::stol(FromJson<JsonString>(args, "addressBook", "id"));
+  long addressbook_id = common::stol(FromJson<std::string>(args, "addressBookId"));
   if (IsUnified(addressbook_id)) {
     int address_book_id = 0;
     status = ContactUtil::GetIntFromRecord(
@@ -593,7 +577,7 @@ PlatformResult AddressBookRemoveGroup(const JsonObject& args, JsonObject&) {
   }
 
   int err;
-  long addressbook_id = AddressBookId(args);
+  long addressbook_id = common::stol(FromJson<std::string>(args, "addressBookId"));
   if (!IsUnified(addressbook_id)) {
     contacts_record_h contacts_record = nullptr;
     err = contacts_db_get_record(_contacts_group._uri, id, &contacts_record);
index 98d4e2f820fb64a1c5c9774fec90f5fd25e57f82..971d99b020b63ca0a85a2477d593b012753df21e 100755 (executable)
@@ -800,6 +800,26 @@ PlatformResult ContactManagerFind(const JsonObject& args, JsonArray& out) {
     if (status.IsError()) return status;
   }
 
+  const auto sort_mode_it = args.find("sortMode");
+  if (args.end() != sort_mode_it) {
+    if (!sort_mode_it->second.is<picojson::object>()) {
+      LoggerD("Failed to set sort mode.");
+      return PlatformResult(ErrorCode::TYPE_MISMATCH_ERR, "Failed to set sort mode");
+    }
+    const auto sort_mode = sort_mode_it->second;
+    std::string attribute = sort_mode.get("attributeName").to_str();
+
+    Person::PersonProperty property;
+    status = Person::PersonPropertyFromString(attribute, &property);
+    if (status.IsError()) return status;
+
+    bool is_asc = sort_mode.get("order").to_str() == "ASC";
+    error_code = contacts_query_set_sort(contacts_query, property.propertyId, is_asc);
+    status = ContactUtil::ErrorChecker(error_code,
+                                       "Failed contacts_query_set_sort");
+    if (status.IsError()) return status;
+  }
+
   contacts_list_h person_list = nullptr;
   error_code =
       contacts_db_get_records_with_query(contacts_query, 0, 0, &person_list);
index d084e2f83ca223e431853f13cfa25d6eb7b65737..68faf00386e501c82ec70c0e15f56a300d74d34d 100644 (file)
@@ -127,9 +127,6 @@ ContactSearchEngine::PropertiesMap ContactSearchEngine::s_properties_map_ = {
 };
 
 // implementation ported from wrt-plugins-tizen
-// TODO: instead of executing multiple queries and combining the results,
-//       create multiple filters, combine them into one, add sorting and
-//       execute a single query
 
 ContactSearchEngine::ContactSearchEngine()
     : addressbook_id_(0),
@@ -224,7 +221,7 @@ PlatformResult ContactSearchEngine::SetSortMode(const picojson::value& sort_mode
 
   is_sort_mode_set_ = true;
   sort_mode_attribute_ = attribute;
-  is_sort_mode_asc_ = sort_mode.get("attributeName").to_str() == "ASC";
+  is_sort_mode_asc_ = sort_mode.get("order").to_str() == "ASC";
 
   return PlatformResult(ErrorCode::NO_ERROR);
 }
@@ -1052,7 +1049,6 @@ PlatformResult ContactSearchEngine::QueryAttributeRangeString(
     ContactUtil::ContactsFilterPtr sub_filter_ptr(sub_filter,
                                                   ContactUtil::ContactsFilterDeleter);
 
-    // TODO To be supported: start
     error_code = contacts_filter_add_str(sub_filter, property_id,
                                          CONTACTS_MATCH_STARTSWITH,
                                          initial_value);
index b3ed9e448230fc240762be98d644f0fc106b59af..4697fcaee19af1ac2396bff8c9a35e8c444c3a25 100755 (executable)
@@ -627,8 +627,6 @@ PlatformResult ExportContactNameToContactsRecord(
     nickname_ptr.release();
   }
 
-  // TODO update displayName in JS!
-
   return PlatformResult(ErrorCode::NO_ERROR);
 }
 
index 6cd5d578e62dff8389dd886ef42919e38b24860f..ab61ae4e216d44d113d4107970ba81288c84a483 100755 (executable)
@@ -177,8 +177,6 @@ AddressBook.prototype.get = function() {
   }
 
   var result = native_.callSync('AddressBook_get', {
-    // TODO move to only sending the address book id (in all functions)
-    addressBook: this,
     id: args.id
   });
 
@@ -224,8 +222,7 @@ AddressBook.prototype.add = function() {
   ]);
 
   var result = native_.callSync('AddressBook_add', {
-    // TODO move to only sending the address book id (in all functions)
-    addressBook: this,
+    addressBookId: this.id,
     contact: _toJsonObject(args.contact)
   });
 
@@ -323,8 +320,11 @@ AddressBook.prototype.update = function() {
     }
   ]);
 
+  if (args.contact.addressBookId !== this.id && UNIFIED_ADDRESSBOOK_ID !== this.id) {
+    throw new WebAPIException(WebAPIException.INVALID_VALUES_ERR);
+  }
+
   var result = native_.callSync('AddressBook_update', {
-    addressBook: this,
     contact: _toJsonObject(args.contact)
   });
 
@@ -387,8 +387,20 @@ AddressBook.prototype.updateBatch = function() {
     native_.callIfPossible(args.successCallback);
   };
 
+  var thatId = this.id;
+  args.contacts.forEach(function(c) {
+    if (c.addressBookId !== thatId && UNIFIED_ADDRESSBOOK_ID !== thatId) {
+      setTimeout(function() {
+        native_.callIfPossible(args.errorCallback, new WebAPIException(
+        WebAPIException.INVALID_VALUES_ERR,
+        'Contact is not saved in database'));
+      }, 0);
+
+      return;
+    }
+  });
+
   native_.call('AddressBook_updateBatch', {
-    addressBook: this,
     batchArgs: _toJsonObject(args.contacts)
   }, callback);
 };
@@ -409,7 +421,6 @@ AddressBook.prototype.remove = function() {
   }
 
   var result = native_.callSync('AddressBook_remove', {
-    addressBook: this,
     id: args.id
   });
 
@@ -453,7 +464,6 @@ AddressBook.prototype.removeBatch = function(ids, successCallback, errorCallback
   };
 
   native_.call('AddressBook_removeBatch', {
-    addressBook: this,
     batchArgs: args.ids
   }, callback);
 };
@@ -619,7 +629,7 @@ AddressBook.prototype.getGroup = function() {
   }
 
   var result = native_.callSync('AddressBook_getGroup', {
-    addressBook: this,
+    addressBookId: this.id,
     id: args.groupId
   });
   if (native_.isFailure(result)) {
@@ -693,7 +703,7 @@ AddressBook.prototype.removeGroup = function() {
   }
 
   var result = native_.callSync('AddressBook_removeGroup',
-      {addressBook: this, id: args.groupId});
+      {addressBookId: this.id, id: args.groupId});
   if (native_.isFailure(result)) {
     throw native_.getErrorObject(result);
   }
index 5f25a91f2238928d8a2214b29073fb4b7e096476..1681cdc619505d5da1f5017d34381f1b54a129c1 100755 (executable)
@@ -119,47 +119,4 @@ EditGuard.prototype.isEditEnabled = function() {
   return _canEdit > 0;
 };
 
-var _editGuard = new EditGuard();
-
-//TODO: Move sorting and filtering to native code
-var Common = function() {};
-Common.prototype.sort = function(arr, sortMode) {
-  var _getSortProperty = function(obj, props) {
-    for (var i = 0; i < props.length; ++i) {
-      if (!obj.hasOwnProperty(props[i])) {
-        return null;
-      }
-      obj = obj[props[i]];
-    }
-    return obj;
-  };
-
-  if (sortMode instanceof tizen.SortMode) {
-    var props = sortMode.attributeName.split('.');
-    arr.sort(function(a, b) {
-      var aValue = _getSortProperty(a, props);
-      var bValue = _getSortProperty(b, props);
-
-      if (sortMode.order === 'DESC') {
-        return aValue < bValue;
-      }
-      return bValue < aValue;
-    });
-  }
-  return arr;
-};
-
-Common.prototype.filter = function(arr, filter) {
-  if (type_.isNullOrUndefined(arr))
-    return arr;
-  if (filter instanceof tizen.AttributeFilter ||
-      filter instanceof tizen.AttributeRangeFilter ||
-      filter instanceof tizen.CompositeFilter) {
-    arr = arr.filter(function(element) {
-      return filter._filter(element);
-    });
-  }
-  return arr;
-};
-
-var C = new Common();
+var _editGuard = new EditGuard();
\ No newline at end of file
index 11ea6fdc1582ff4c05a411befcaffa8082886d39..d24257cf4a2b890b8f3417985c683c1506711580 100755 (executable)
@@ -396,7 +396,6 @@ ContactManager.prototype.find = function() {
     }
   ]);
 
-  // TODO implement contact filtering/sorting.
   var data = {
     filter: utils_.repackFilter(args.filter),
     sortMode: args.sortMode
@@ -411,8 +410,6 @@ ContactManager.prototype.find = function() {
       for (var i = 0; i < _result.length; ++i) {
         retval.push(self.get(String(_result[i])));
       }
-      //TODO: Move sorting to native code
-      retval = C.sort(retval, args.sortMode);
       args.successCallback(retval);
     } else {
       native_.callIfPossible(args.errorCallback, native_.getErrorObject(result));
index 6161f7bfe34038bb19fc22c49968b60d1b88b351..0185d90079a09e05debe504e4cfef2565270612a 100755 (executable)
@@ -152,8 +152,7 @@ Person.prototype.link = function() {
   }
 
   var result = native_.callSync('Person_link', {
-    // TODO move to only sending the person id (in all functions)
-    person: this,
+    personId: this.id,
     id: args.personId
   });
   if (native_.isFailure(result)) {
@@ -182,8 +181,7 @@ Person.prototype.unlink = function(contactId) {
   }
 
   var result = native_.callSync('Person_unlink', {
-    // TODO move to only sending the person id (in all functions)
-    person: this,
+    personId: this.id,
     id: args.contactId
   });
   if (native_.isFailure(result)) {
index 1490c2cbd3bd77ddaa54041e4d38d03f4052d0c9..7166be4493be72a469546c39ecc2468d3f53b9c9 100755 (executable)
@@ -44,7 +44,7 @@ PlatformResult PersonLink(const JsonObject& args, JsonObject&) {
   if (status.IsError()) return status;
 
   long id = common::stol(FromJson<JsonString>(args, "id"));
-  long person_id = common::stol(FromJson<JsonString>(args, "person", "id"));
+  long person_id = common::stol(FromJson<JsonString>(args, "personId"));
 
   contacts_record_h contacts_record = nullptr;
 
@@ -94,7 +94,7 @@ PlatformResult PersonUnlink(const JsonObject& args, JsonObject& out) {
                                      "Contact is not a member of person");
   if (status.IsError()) return status;
 
-  long person_id = common::stol(FromJson<JsonString>(args, "person", "id"));
+  long person_id = common::stol(FromJson<JsonString>(args, "personId"));
   if (contacts_person_id != person_id) {
     LoggerW("Contact is not a member of person (wrong id's)");
     return PlatformResult(ErrorCode::INVALID_VALUES_ERR,