[Contact] Added handling of groupIds in Contact
authorPiotr Kosko <p.kosko@samsung.com>
Fri, 27 Nov 2015 09:24:21 +0000 (10:24 +0100)
committerPiotr Kosko <p.kosko@samsung.com>
Tue, 12 Apr 2016 10:27:17 +0000 (19:27 +0900)
[Feature] Gathering and updating groupIds of Contact object is added.

[Verification] Code compiles without errors. TCT passrate is 100%.
// Code below works
var addressbook, c;
function getAll() {
  // Defines the error callback.
  function errorCB(err) { console.log( 'The following error occurred: ' +  err.name); }
  function contactsFoundCB(contacts) {
    c = contacts;
    for (var i =0; i < contacts.length; ++i) {
      var message = "Found contact " + contacts[i].name.displayName + " is in groups: [ ";
      console.log(contacts[i].groupIds.length);
      for (var j = 0; j < contacts[i].groupIds.length; ++j) {
        message += addressbook.getGroup(contacts[i].groupIds[j]).name + " ";
      }
      message += " ]";
      console.log(message);
    }
  }

  addressbook = tizen.contact.getDefaultAddressBook();
  try {
    addressbook.find(contactsFoundCB, errorCB);
  } catch (err) {
    console.log( 'The following error occurred while finding: ' +  err.name);
  }
}

function updateGroup(groups) {
  c[0].groupIds = groups;
  addressbook.update(c[0])
}

//Step 1
  // see contacts
  getAll();
//Step 2
  // update group of first contact
  updateGroup([1]);
  // see if contact changed
  getAll();
//Step 3
  // again update group of first contact
  updateGroup(['1', '2']);
  // see if contact changed
  getAll();

Change-Id: Ie17aa6c9d9c3b357074fef416afd71a0a3f04e8e
Signed-off-by: Piotr Kosko <p.kosko@samsung.com>
src/contact/contact_util.cc
src/contact/contact_util.h
src/contact/js/contact.js

index 6e91ef002ccac52343b29f7482c0580679f92025..cdc51a53baecf44f978918393fe36eb51c20f9ed 100755 (executable)
@@ -2367,6 +2367,86 @@ PlatformResult ExportNotesToContactsRecord(contacts_record_h contacts_record,
   return PlatformResult(ErrorCode::NO_ERROR);
 }
 
+PlatformResult ImportGroupIdsFromContactsRecord(
+    contacts_record_h contacts_record, unsigned int index, JsonValue* val) {
+
+  LoggerD("Enter");
+  // contacts_record is protected by unique_ptr and its ownership is not passed
+  // here
+  if (!contacts_record) {
+    LoggerE("Contacts record is null");
+    return PlatformResult(ErrorCode::UNKNOWN_ERR, "Contacts record is null");
+  }
+
+  contacts_record_h record = nullptr;
+  int err = contacts_record_get_child_record_at_p(
+      contacts_record, _contacts_contact.group_relation, index, &record);
+  if (CONTACTS_ERROR_NONE != err && CONTACTS_ERROR_NO_DATA != err) {
+    // ignoring this record
+    LoggerW("Skipping record with index %i. error code: %i", index, err);
+    return PlatformResult(ErrorCode::NO_ERROR);
+  }
+
+  int group_id = -1;
+  PlatformResult status =
+      ContactUtil::GetIntFromRecord(record, _contacts_group_relation.id, &group_id);
+  if (status.IsError()) {
+    LoggerE("Error: %s", status.message().c_str());
+    return status;
+  }
+
+  if (-1 == group_id) {
+    return PlatformResult(ErrorCode::UNKNOWN_ERR, "Fetching groupId failed");
+  }
+  *val = JsonValue{std::to_string(group_id)};
+
+  return PlatformResult(ErrorCode::NO_ERROR);
+}
+
+PlatformResult ExportGroupIdsToContactsRecord(contacts_record_h contacts_record,
+                                           int value) {
+
+  LoggerD("Enter");
+  contacts_record_h notes_record = nullptr;
+  // contacts_record is protected by unique_ptr and its ownership is not passed
+  // here
+  if (!contacts_record) {
+    LoggerE("Contacts record is null");
+    return PlatformResult(ErrorCode::UNKNOWN_ERR, "Contacts record is null");
+  }
+
+  int err = contacts_record_create(_contacts_group_relation._uri, &notes_record);
+  PlatformResult status =
+      ContactUtil::ErrorChecker(err, "Fail to create note record in database");
+  if (status.IsError()) {
+    LoggerE("Error: %s", status.message().c_str());
+    return status;
+  }
+
+  ContactsRecordHPtr record(&notes_record, ContactsDeleter);
+
+  status = ContactUtil::SetIntInRecord(notes_record, _contacts_group_relation.group_id,
+                                       value);
+  if (status.IsError()) {
+    LoggerE("Error: %s", status.message().c_str());
+    return status;
+  }
+
+  err = contacts_record_add_child_record(contacts_record,
+                                         _contacts_contact.group_relation, notes_record);
+  status =
+      ContactUtil::ErrorChecker(err, "Fail to save note record in database");
+  if (status.IsError()) {
+    LoggerE("Error: %s", status.message().c_str());
+    return status;
+  }
+
+  // Do not delete record, it is passed to the platform
+  record.release();
+
+  return PlatformResult(ErrorCode::NO_ERROR);
+}
+
 PlatformResult ImportContactFromContactsRecord(
     contacts_record_h contacts_record, JsonObject* out_ptr) {
 
@@ -2593,6 +2673,29 @@ PlatformResult ImportContactFromContactsRecord(
             "vibrationURI",
             value ? JsonValue{ConvertPathToUri(value)} : JsonValue{}));
     value = nullptr;
+
+    //### groupIds: ###
+    JsonArray& group_ids = out.insert(std::make_pair(std::string("groupIds"),
+        picojson::value(JsonArray()))).first->second.get<JsonArray>();
+
+    status = ContactUtil::GetNumberOfChildRecord(
+        contacts_record, _contacts_contact.group_relation, &child_rec_count);
+    if (status.IsError()) {
+      LoggerE("Error: %s", status.message().c_str());
+      return status;
+    }
+
+    for (int i = 0; i < child_rec_count; ++i) {
+      JsonValue val{JsonObject{}};
+
+      status = ImportGroupIdsFromContactsRecord(contacts_record, static_cast<unsigned int>(i), &val);
+      if (status.IsError()) {
+        LoggerE("Error: %s", status.message().c_str());
+        return status;
+      }
+
+      group_ids.push_back(val);
+    }
   }
 
   return PlatformResult(ErrorCode::NO_ERROR);
@@ -2782,6 +2885,26 @@ PlatformResult ExportContactToContactsRecord(contacts_record_h contacts_record,
     }
   }
 
+  {
+    //### groupIds: ###
+    PlatformResult status = ContactUtil::ClearAllContactRecord(
+        contacts_record, _contacts_contact.group_relation);
+    if (status.IsError()) {
+      LoggerE("Error: %s", status.message().c_str());
+      return status;
+    }
+
+    const JsonArray& elements = FromJson<JsonArray>(in, "groupIds");
+    for (auto& element : elements) {
+      auto& str = JsonCast<JsonString>(element);
+      if (str.empty()) {
+        continue;
+      }
+      ExportGroupIdsToContactsRecord(contacts_record, atoi(str.c_str()));
+    }
+  }
+
+
   return PlatformResult(ErrorCode::NO_ERROR);
 }
 
index 9e7b9854d08a3a9361bec7e9338a33e8df94e8d6..471d124827990833385fa4a28d5231205fa84b31 100644 (file)
@@ -132,6 +132,10 @@ common::PlatformResult ImportContactNotesFromContactsRecord(
     contacts_record_h contacts_record, unsigned int index, JsonValue *val);
 common::PlatformResult ExportNotesToContactsRecord(
     contacts_record_h contacts_record, const std::string &value);
+common::PlatformResult ImportGroupIdsFromContactsRecord(
+    contacts_record_h contacts_record, unsigned int index, JsonValue *val);
+common::PlatformResult ExportGroupIdsToContactsRecord(
+    contacts_record_h contacts_record, int value);
 common::PlatformResult ImportContactFromContactsRecord(
     contacts_record_h contacts_record, JsonObject *out);
 common::PlatformResult ExportPersonToContactsRecord(contacts_record_h record,
index 1db49ffd5936b84aef58c2d8dd9a91d4c7e426c9..bb2f1685524c0f97298159e24b5f70a1463494d8 100755 (executable)
@@ -70,9 +70,7 @@ var Contact = function(data) {
     }
     for (var i = 0; i < arr.length; ++i) {
       if (type_.isString(type)) {
-        if (!type_.isString(arr[i])) {
-          return previousValue;
-        }
+        arr[i] = converter_.toString(arr[i]);
       } else if (_editGuard.isEditEnabled()) {
         arr[i] = new type(arr[i]);
       } else if (!(arr[i] instanceof type)) {