From: Andrzej Popowski Date: Mon, 18 Jan 2016 14:27:43 +0000 (+0100) Subject: [Contact] - fixing problem with timeUsed attribute X-Git-Tag: submit/tizen/20160119.035442^2~2^2 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=68888c701e04e874f5fccb1a3d48c547195214fb;p=platform%2Fcore%2Fapi%2Fwebapi-plugins.git [Contact] - fixing problem with timeUsed attribute Change-Id: Ia0b50368bf65fd5defd270cdff8246a9ae884124 Signed-off-by: Andrzej Popowski --- diff --git a/src/contact/contact_manager.cc b/src/contact/contact_manager.cc index d90788c7..5a6181a6 100755 --- a/src/contact/contact_manager.cc +++ b/src/contact/contact_manager.cc @@ -28,6 +28,7 @@ #include #include "contact/contact_instance.h" #include "contact/person.h" +#include "contact/contact_search_engine.h" namespace extension { namespace contact { @@ -180,27 +181,21 @@ PlatformResult ContactManagerGetInternal(int person_id, JsonObject* out) { 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()) { + if (CONTACTS_ERROR_NONE != contacts_record_destroy(contacts_record, true)) { + LoggerE("failed to destroy contacts_record_h"); + } + return status; } - 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); - + status = ContactSearchEngine::GetPersonUsage(person_id, out); if (CONTACTS_ERROR_NONE != contacts_record_destroy(contacts_record, true)) { LoggerE("failed to destroy contacts_record_h"); } - - if (status.IsError()) return status; + if (!status) { + return status; + } return PlatformResult(ErrorCode::NO_ERROR); } } diff --git a/src/contact/contact_search_engine.cc b/src/contact/contact_search_engine.cc index f1297fdf..bc11b0be 100644 --- a/src/contact/contact_search_engine.cc +++ b/src/contact/contact_search_engine.cc @@ -633,6 +633,81 @@ PlatformResult ContactSearchEngine::GetContacts(Iterator begin, Iterator end, return PlatformResult(ErrorCode::NO_ERROR); } +PlatformResult ContactSearchEngine::GetPersonUsage(int person_id, JsonObject* out_ptr) { + LoggerD("Entered"); + + contacts_query_h query = nullptr; + contacts_filter_h filter = nullptr; + contacts_list_h list = nullptr; + PlatformResult status(ErrorCode::NO_ERROR); + + int error_code = contacts_query_create(_contacts_person_usage._uri, &query); + status = ContactUtil::ErrorChecker(error_code, "Failed contacts_query_create"); + if (!status) { + return status; + } + ContactUtil::ContactsQueryHPtr query_ptr(&query, ContactUtil::ContactsQueryDeleter); + + error_code = contacts_filter_create(_contacts_person_usage._uri, &filter); + status = ContactUtil::ErrorChecker(error_code, "Failed contacts_filter_create"); + if (!status) { + return status; + } + ContactUtil::ContactsFilterPtr filter_ptr(filter, ContactUtil::ContactsFilterDeleter); + + error_code = contacts_filter_add_int(filter, + _contacts_person_usage.person_id, + CONTACTS_MATCH_EQUAL, + person_id); + status = ContactUtil::ErrorChecker(error_code, "Failed contacts_filter_add_int"); + if (!status) { + return status; + } + + error_code = contacts_query_set_filter(query, filter); + status = ContactUtil::ErrorChecker(error_code, "Failed contacts_query_set_filter"); + if (!status) { + return status; + } + + error_code = contacts_db_get_records_with_query(query, 0, 0, &list); + status = ContactUtil::ErrorChecker(error_code, "Failed contacts_db_get_records_with_query"); + if (!status) { + return status; + } + ContactUtil::ContactsListHPtr list_ptr(&list, ContactUtil::ContactsListDeleter); + + int record_count = 0; + error_code = contacts_list_get_count(list, &record_count); + status = ContactUtil::ErrorChecker(error_code, "Failed contacts_list_get_count"); + if (!status) { + return status; + } + + if (record_count == 1) { + contacts_record_h record = nullptr; + + contacts_list_first(list); + error_code = contacts_list_get_current_record_p(list, &record); + status = ContactUtil::ErrorChecker(error_code, "Failed contacts_list_get_current_record_p"); + if (!status) { + return status; + } + + int times_used = 0; + error_code = contacts_record_get_int(record, _contacts_person_usage.times_used, ×_used); + status = ContactUtil::ErrorChecker(error_code, "Failed contacts_record_get_int"); + if (!status) { + return status; + } + out_ptr->insert(std::make_pair("timesUsed", JsonValue(static_cast(times_used)))); + } else { + out_ptr->insert(std::make_pair("timesUsed", JsonValue(static_cast(0)))); + } + + return PlatformResult(ErrorCode::NO_ERROR); +} + common::PlatformResult ContactSearchEngine::GetQueryResults( contacts_query_h query, contacts_filter_h filter, unsigned int property_id, LongSetPtr result) { diff --git a/src/contact/contact_search_engine.h b/src/contact/contact_search_engine.h index 7cc91ed4..f859b36f 100644 --- a/src/contact/contact_search_engine.h +++ b/src/contact/contact_search_engine.h @@ -27,6 +27,7 @@ #include "common/filter-utils.h" #include "common/picojson.h" #include "common/platform_result.h" +#include "contact/contact_util.h" namespace extension { namespace contact { @@ -41,6 +42,8 @@ class ContactSearchEngine { common::PlatformResult Find(picojson::array* out); + static common::PlatformResult GetPersonUsage(int person_id, JsonObject* out_ptr); + private: typedef std::vector LongVector; typedef std::shared_ptr LongVectorPtr; @@ -85,7 +88,6 @@ class ContactSearchEngine { template common::PlatformResult GetContacts(Iterator begin, Iterator end, picojson::array* out); - common::PlatformResult QueryAttributeBool( const FilterPropertyStruct& attribute_properties, LongSetPtr result, bool match_value);