From: Tomasz Marciniak Date: Thu, 7 Jan 2016 13:15:09 +0000 (+0100) Subject: [Contact] Added timeUsed attribute for sort mode of find() function. X-Git-Tag: submit/tizen/20160119.035442^2~3^2 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=3227b37a8a88d6064a6805666454a5ee3e8878e1;p=platform%2Fcore%2Fapi%2Fwebapi-plugins.git [Contact] Added timeUsed attribute for sort mode of find() function. [Verification] Code compiles. Unable to test. Change-Id: I07cffdefce294e05ae30ac1de648b6d3aecabc10 Signed-off-by: Tomasz Marciniak --- diff --git a/src/contact/contact_manager.cc b/src/contact/contact_manager.cc index a1dce5ab..d90788c7 100755 --- a/src/contact/contact_manager.cc +++ b/src/contact/contact_manager.cc @@ -177,13 +177,30 @@ PlatformResult ContactManagerGetInternal(int person_id, JsonObject* out) { ("Person with id: %d, not found, error: %d", person_id, error_code)); } - ContactUtil::ContactsRecordHPtr contacts_record_ptr( - &contacts_record, ContactUtil::ContactsDeleter); - PlatformResult status = ContactUtil::ImportPersonFromContactsRecord(contacts_record, out); + + if (CONTACTS_ERROR_NONE != contacts_record_destroy(contacts_record, true)) { + LoggerE("failed to destroy contacts_record_h"); + } + if (status.IsError()) return status; + //get information from view _contacts_person_usage + error_code = contacts_db_get_record(_contacts_person_usage._uri, person_id, + &contacts_record); + if (CONTACTS_ERROR_NONE != error_code) { + LoggerE("Person with id: %d, not found, error: %d", person_id, error_code); + return PlatformResult(ErrorCode::NOT_FOUND_ERR, "Person not found"); + } + + status = ContactUtil::ImportPersonFromContactsUsageRecord(contacts_record, out); + + if (CONTACTS_ERROR_NONE != contacts_record_destroy(contacts_record, true)) { + LoggerE("failed to destroy contacts_record_h"); + } + + if (status.IsError()) return status; return PlatformResult(ErrorCode::NO_ERROR); } } @@ -419,15 +436,47 @@ PlatformResult ContactManagerFind(const JsonObject& args, JsonArray& out) { if (status.IsError()) return status; contacts_query_h contacts_query = nullptr; - int error_code = - contacts_query_create(_contacts_person._uri, &contacts_query); - status = - ContactUtil::ErrorChecker(error_code, "Failed contacts_query_create"); - if (status.IsError()) return status; - ContactUtil::ContactsQueryHPtr contacts_query_ptr( &contacts_query, ContactUtil::ContactsQueryDeleter); + int error_code = CONTACTS_ERROR_NONE; + + const auto sort_mode_it = args.find("sortMode"); + if (args.end() != sort_mode_it && !IsNull(args, "sortMode")) { + if (!sort_mode_it->second.is()) { + 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; + + //As neither _contacts_person_usage nor _contacts_person cover all filter attributes + //which result can be sorted it has to be checked which view can be used here. + if ("timesUsed" == attribute) { + error_code = contacts_query_create(_contacts_person_usage._uri, &contacts_query); + } else { + error_code = contacts_query_create(_contacts_person._uri, &contacts_query); + } + + status = ContactUtil::ErrorChecker(error_code, "Failed contacts_query_create"); + 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; + } else { + error_code = contacts_query_create(_contacts_person._uri, &contacts_query); + + status = ContactUtil::ErrorChecker(error_code, "Failed contacts_query_create"); + if (status.IsError()) return status; + } + // Add filter to query std::vector> intermediate_filters( 1); @@ -442,8 +491,13 @@ PlatformResult ContactManagerFind(const JsonObject& args, JsonArray& out) { if (status.IsError()) return status; contacts_filter_h contacts_filter = nullptr; - int error_code = - contacts_filter_create(_contacts_person._uri, &contacts_filter); + int error_code = CONTACTS_ERROR_NONE; + + if ("timesUsed" != name) { + error_code = contacts_filter_create(_contacts_person._uri, &contacts_filter); + } else { + error_code = contacts_filter_create(_contacts_person_usage._uri, &contacts_filter); + } status = ContactUtil::ErrorChecker(error_code, "Failed contacts_query_set_filter"); if (status.IsError()) return status; @@ -532,8 +586,13 @@ PlatformResult ContactManagerFind(const JsonObject& args, JsonArray& out) { if (status.IsError()) return status; contacts_filter_h contacts_filter = nullptr; - int error_code = - contacts_filter_create(_contacts_person._uri, &contacts_filter); + int error_code = CONTACTS_ERROR_NONE; + + if ("timesUsed" != name) { + error_code = contacts_filter_create(_contacts_person._uri, &contacts_filter); + } else { + error_code = contacts_filter_create(_contacts_person_usage._uri, &contacts_filter); + } status = ContactUtil::ErrorChecker(error_code, "Failed contacts_query_set_filter"); if (status.IsError()) return status; @@ -595,8 +654,11 @@ PlatformResult ContactManagerFind(const JsonObject& args, JsonArray& out) { if (initial_value_exists && end_value_exists) { contacts_filter_h sub_filter = NULL; - error_code = - contacts_filter_create(_contacts_person._uri, &sub_filter); + if ("timesUsed" != name) { + error_code = contacts_filter_create(_contacts_person._uri, &sub_filter); + } else { + error_code = contacts_filter_create(_contacts_person_usage._uri, &sub_filter); + } status = ContactUtil::ErrorChecker(error_code, "Failed contacts_filter_add_str"); if (status.IsError()) return status; @@ -669,8 +731,11 @@ PlatformResult ContactManagerFind(const JsonObject& args, JsonArray& out) { if (initial_value_exists && end_value_exists) { contacts_filter_h sub_filter = NULL; - error_code = - contacts_filter_create(_contacts_person._uri, &sub_filter); + if ("timesUsed" != name) { + error_code = contacts_filter_create(_contacts_person._uri, &sub_filter); + } else { + error_code = contacts_filter_create(_contacts_person_usage._uri, &sub_filter); + } status = ContactUtil::ErrorChecker(error_code, "Failed contacts_filter_add_bool"); if (status.IsError()) return status; @@ -797,25 +862,6 @@ 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()) { - return LogAndCreateResult(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); diff --git a/src/contact/contact_util.cc b/src/contact/contact_util.cc index 3ce66d0b..b3af4255 100755 --- a/src/contact/contact_util.cc +++ b/src/contact/contact_util.cc @@ -2802,6 +2802,31 @@ PlatformResult ImportPersonFromContactsRecord(contacts_record_h record, return PlatformResult(ErrorCode::NO_ERROR); } +PlatformResult ImportPersonFromContactsUsageRecord(contacts_record_h record, + JsonObject* out_ptr) { + if (nullptr == record) { + LoggerW("Platform person record did not set"); + return PlatformResult(ErrorCode::INVALID_VALUES_ERR, "Platform person record did not set"); + } + + JsonObject& arguments_obj = *out_ptr; + + int int_value = 0; + // timesUsed + PlatformResult status = ContactUtil::GetIntFromRecord( + record, _contacts_person_usage.times_used, &int_value); + + if (status.IsError()) { + LoggerE("Error: %s", status.message().c_str()); + return status; + } + + arguments_obj.insert( + std::make_pair("timesUsed", JsonValue(static_cast(int_value)))); + + return PlatformResult(ErrorCode::NO_ERROR); +} + /** * @brief Updates contacts_record_h with values from Person object * @param[out] contacts_record_h Record which is updated diff --git a/src/contact/contact_util.h b/src/contact/contact_util.h index ed806575..438dcbe8 100644 --- a/src/contact/contact_util.h +++ b/src/contact/contact_util.h @@ -141,6 +141,8 @@ common::PlatformResult ExportContactGroupToContactsRecord( contacts_record_h contacts_record, const JsonObject &in); common::PlatformResult ImportPersonFromContactsRecord( contacts_record_h contacts_record, JsonObject *out); +common::PlatformResult ImportPersonFromContactsUsageRecord( + contacts_record_h contacts_record, JsonObject *out); common::PlatformResult CheckDBConnection(); diff --git a/src/contact/js/person.js b/src/contact/js/person.js index e812c027..98839c9d 100755 --- a/src/contact/js/person.js +++ b/src/contact/js/person.js @@ -24,6 +24,7 @@ var Person = function(data) { var _hasEmail = false; var _isFavorite = false; var _displayContactId = ''; + var _timesUsed = 0; if (data.hasOwnProperty('id') && type_.isString(data.id)) { _id = data.id; @@ -46,6 +47,9 @@ var Person = function(data) { if (data.hasOwnProperty('isFavorite') && type_.isBoolean(data.isFavorite)) { _isFavorite = data.isFavorite; } + if (data.hasOwnProperty('timesUsed') && type_.isNumber(data.timesUsed)) { + _timesUsed = data.timesUsed; + } Object.defineProperties(this, { id: { @@ -131,6 +135,17 @@ var Person = function(data) { _displayContactId = converter_.toString(v, false); }, enumerable: true + }, + timesUsed: { + get: function() { + return _timesUsed; + }, + set: function(v) { + if (_editGuard.isEditEnabled()) { + _timesUsed = converter_.toLong(v, false); + } + }, + enumerable: true } }); }; diff --git a/src/contact/person.cc b/src/contact/person.cc index 8c847186..579da2f4 100755 --- a/src/contact/person.cc +++ b/src/contact/person.cc @@ -28,14 +28,13 @@ static const PersonPropertyMap personPropertyMap = { {"id", {_contacts_person.id, kPrimitiveTypeId}}, {"displayName", {_contacts_person.display_name, kPrimitiveTypeString}}, {"contactCount", {_contacts_person.link_count, kPrimitiveTypeLong}}, - {"hasPhoneNumber", - {_contacts_person.has_phonenumber, kPrimitiveTypeBoolean}}, + {"hasPhoneNumber", {_contacts_person.has_phonenumber, kPrimitiveTypeBoolean}}, {"hasEmail", {_contacts_person.has_email, kPrimitiveTypeBoolean}}, {"isFavorite", {_contacts_person.is_favorite, kPrimitiveTypeBoolean}}, {"photoURI", {_contacts_person.image_thumbnail_path, kPrimitiveTypeString}}, {"ringtoneURI", {_contacts_person.ringtone_path, kPrimitiveTypeString}}, - {"displayContactId", - {_contacts_person.display_contact_id, kPrimitiveTypeId}}, + {"displayContactId", {_contacts_person.display_contact_id, kPrimitiveTypeId}}, + {"timesUsed", {_contacts_person_usage.times_used, kPrimitiveTypeId}}, }; PlatformResult PersonLink(const JsonObject& args, JsonObject&) {