From: Wojciech Kosowicz Date: Fri, 19 Dec 2014 11:57:39 +0000 (+0100) Subject: [Calendar] Added implementation of asynchrouous methods X-Git-Tag: submit/tizen_tv/20150603.064601~1^2~745 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=1b2fcb2c354b6f268494b764378f5993b5763d57;p=platform%2Fcore%2Fapi%2Fwebapi-plugins.git [Calendar] Added implementation of asynchrouous methods Change-Id: I3ac21f41d08c0a313f78f511424977438be857ef Signed-off-by: Wojciech Kosowicz --- diff --git a/src/calendar/calendar.cc b/src/calendar/calendar.cc index fc9b1246..78f9affb 100644 --- a/src/calendar/calendar.cc +++ b/src/calendar/calendar.cc @@ -117,10 +117,9 @@ void Calendar::Add(const picojson::object& args, picojson::object& out) { } void Calendar::AddBatch(const picojson::object& args, - picojson::object& out) { + picojson::array& array) { LoggerD("enter"); - if (!CalendarManager::GetInstance().IsConnected()) { throw UnknownException("DB Connection failed."); } @@ -132,79 +131,58 @@ void Calendar::AddBatch(const picojson::object& args, int type = CalendarRecord::TypeToInt(FromJson(args, "type")); const char* view_uri = CalendarRecord::TypeToUri(type); + calendar_list_h list = NULL; + if (CALENDAR_ERROR_NONE != calendar_list_create(&list)) { + LoggerE("Could not create list for batch operation"); + throw UnknownException("Could not create list for batch operation"); + } + CalendarListPtr list_ptr = + CalendarListPtr(list, CalendarRecord::ListDeleter); - auto batch_func = [=](const JsonValuePtr & response)->void { - picojson::object& response_obj = response->get(); - - try { - calendar_list_h list = NULL; - if (CALENDAR_ERROR_NONE != calendar_list_create(&list)) { - LoggerE("Could not create list for batch operation"); - throw UnknownException("Could not create list for batch operation"); - } - CalendarListPtr list_ptr = - CalendarListPtr(list, CalendarRecord::ListDeleter); - - int ret; - calendar_record_h record; - - for (auto& item : items) { - ret = calendar_record_create(view_uri, &record); - if (CALENDAR_ERROR_NONE != ret) { - LoggerW("Can't create platform record %d", ret); - throw UnknownException("Can't create platform record"); - } - CalendarItem::FromJson(type, record, item.get()); - - if (CALENDAR_ERROR_NONE != calendar_list_add(list_ptr.get(), record)) { - LoggerE("Could not add record to list events"); - throw InvalidValuesException("Could not add record to list"); - } - } - - int* ids; - int count; - ret = calendar_db_insert_records(list_ptr.get(), &ids, &count); - if (CALENDAR_ERROR_NONE != ret) { - LoggerE("calendar_db_insert_records failed."); - if (CALENDAR_ERROR_INVALID_PARAMETER == ret) { - LoggerE("CALENDAR_ERROR_INVALID_PARAMETER."); - throw InvalidValuesException("Parameter is invalid"); - } else { - LoggerE("CALENDAR_ERROR_DB_FAILED"); - throw UnknownException("CALENDAR_ERROR_DB_FAILED occurred"); - } - } + int ret; + calendar_record_h record; - picojson::value result = picojson::value(picojson::array()); - picojson::array& array = result.get(); + for (auto& item : items) { + ret = calendar_record_create(view_uri, &record); + if (CALENDAR_ERROR_NONE != ret) { + LoggerW("Can't create platform record %d", ret); + throw UnknownException("Can't create platform record"); + } + CalendarItem::FromJson(type, record, item.get()); - for (int i = 0; i < count; i++) { - picojson::value id = picojson::value(picojson::object()); - picojson::object& id_obj = id.get(); + if (CALENDAR_ERROR_NONE != calendar_list_add(list_ptr.get(), record)) { + LoggerE("Could not add record to list events"); + throw InvalidValuesException("Could not add record to list"); + } + } - id_obj.insert(std::make_pair("uid", std::to_string(ids[i]))); + int* ids; + int count; + ret = calendar_db_insert_records(list_ptr.get(), &ids, &count); + if (CALENDAR_ERROR_NONE != ret) { + LoggerE("calendar_db_insert_records failed."); + if (CALENDAR_ERROR_INVALID_PARAMETER == ret) { + LoggerE("CALENDAR_ERROR_INVALID_PARAMETER."); + throw InvalidValuesException("Parameter is invalid"); + } else { + LoggerE("CALENDAR_ERROR_DB_FAILED"); + throw UnknownException("CALENDAR_ERROR_DB_FAILED occurred"); + } + } - if (type == CALENDAR_BOOK_TYPE_EVENT) { - id_obj.insert(std::make_pair("rid", picojson::value())); - } + for (int i = 0; i < count; i++) { + picojson::value id = picojson::value(picojson::object()); + picojson::object& id_obj = id.get(); - array.push_back(id); - } - free(ids); + id_obj.insert(std::make_pair("uid", std::to_string(ids[i]))); + if (type == CALENDAR_BOOK_TYPE_EVENT) { + id_obj.insert(std::make_pair("rid", picojson::value())); } - catch(...) {//(const BasePlatformException& e) { - } - }; - int callback_handle;// = NativePlugin::GetAsyncCallbackHandle(args); - auto after_batch_func = [callback_handle](const JsonValuePtr& response) { - }; - - TaskQueue::GetInstance().Queue( - batch_func, after_batch_func, - JsonValuePtr(new picojson::value(picojson::object()))); + array.push_back(id); + } + free(ids); } void Calendar::Update(const picojson::object& args, @@ -266,7 +244,7 @@ void Calendar::Update(const picojson::object& args, } void Calendar::UpdateBatch(const picojson::object& args, - picojson::object& out) { + picojson::array& array) { LoggerD("enter"); if (!CalendarManager::GetInstance().IsConnected()) { @@ -285,65 +263,47 @@ void Calendar::UpdateBatch(const picojson::object& args, int type = CalendarRecord::TypeToInt(FromJson(args, "type")); const char* view_uri = CalendarRecord::TypeToUri(type); + calendar_list_h list = NULL; + if (CALENDAR_ERROR_NONE != calendar_list_create(&list)) { + LoggerE("Could not create list for batch operation"); + throw UnknownException("Could not create list for batch operation"); + } + CalendarListPtr list_ptr = + CalendarListPtr(list, CalendarRecord::ListDeleter); - auto batch_func = [=](const JsonValuePtr & response)->void { - picojson::object& response_obj = response->get(); - - try { - calendar_list_h list = NULL; - if (CALENDAR_ERROR_NONE != calendar_list_create(&list)) { - LoggerE("Could not create list for batch operation"); - throw UnknownException("Could not create list for batch operation"); - } - CalendarListPtr list_ptr = - CalendarListPtr(list, CalendarRecord::ListDeleter); - - int ret, id; - calendar_record_h record; - - for (auto& item : items) { - const picojson::object& item_obj = item.get(); - if (type == CALENDAR_BOOK_TYPE_EVENT) { - id = common::stol(FromJson(item_obj, "id", "uid")); - } else { - id = common::stol(FromJson(item_obj, "id")); - } - - ret = calendar_db_get_record(view_uri, id, &record); - if (CALENDAR_ERROR_NONE != ret) { - LoggerW("Can't get platform record %d", ret); - throw UnknownException("Can't get platform record"); - } - CalendarItem::FromJson(type, record, item.get()); - - if (CALENDAR_ERROR_NONE != calendar_list_add(list_ptr.get(), record)) { - LoggerE("Could not add record to list events"); - throw InvalidValuesException("Could not add record to list"); - } - } - - if (type == CALENDAR_BOOK_TYPE_TODO || update_all) { - if (CALENDAR_ERROR_NONE != calendar_db_update_records(list_ptr.get())) { - LoggerE("Can't update calendar items"); - throw UnknownException("Can't update calendar items"); - } - } else { - // @todo update the exdate for a recurring parent event and add a new - // child event - } + int ret, id; + calendar_record_h record; + for (auto& item : items) { + const picojson::object& item_obj = item.get(); + if (type == CALENDAR_BOOK_TYPE_EVENT) { + id = common::stol(FromJson(item_obj, "id", "uid")); + } else { + id = common::stol(FromJson(item_obj, "id")); } - catch (...) {//const BasePlatformException& e) { + + ret = calendar_db_get_record(view_uri, id, &record); + if (CALENDAR_ERROR_NONE != ret) { + LoggerW("Can't get platform record %d", ret); + throw UnknownException("Can't get platform record"); } - }; + CalendarItem::FromJson(type, record, item.get()); - int callback_handle; //= NativePlugin::GetAsyncCallbackHandle(args); - auto after_batch_func = [callback_handle](const JsonValuePtr& response) { - }; + if (CALENDAR_ERROR_NONE != calendar_list_add(list_ptr.get(), record)) { + LoggerE("Could not add record to list events"); + throw InvalidValuesException("Could not add record to list"); + } + } - TaskQueue::GetInstance().Queue( - batch_func, after_batch_func, - JsonValuePtr(new picojson::value(picojson::object()))); + if (type == CALENDAR_BOOK_TYPE_TODO || update_all) { + if (CALENDAR_ERROR_NONE != calendar_db_update_records(list_ptr.get())) { + LoggerE("Can't update calendar items"); + throw UnknownException("Can't update calendar items"); + } + } else { + // @todo update the exdate for a recurring parent event and add a new + // child event + } } void Calendar::Remove(const picojson::object& args, @@ -366,7 +326,7 @@ void Calendar::Remove(const picojson::object& args, CalendarItem::Remove(type, id); } -void Calendar::Find(const picojson::object& args, picojson::object& out) { +void Calendar::Find(const picojson::object& args, picojson::array& array) { LoggerD("enter"); if (!CalendarManager::GetInstance().IsConnected()) { @@ -375,90 +335,67 @@ void Calendar::Find(const picojson::object& args, picojson::object& out) { // TODO implement calendar filter and sorting in native code. int calendar_id = common::stol(FromJson(args, "calendarId")); - int callback_handle; //=NativePlugin::GetAsyncCallbackHandle(args); - - auto get = [calendar_id](const std::shared_ptr & response) - ->void { - LoggerD("Entered"); - try { - int error_code = 0; - CalendarRecordPtr calendar_ptr = - CalendarRecord::GetById(calendar_id, _calendar_book._uri); - int type = - CalendarRecord::GetInt(calendar_ptr.get(), _calendar_book.store_type); - calendar_query_h calendar_query = nullptr; - if (type == CALENDAR_BOOK_TYPE_EVENT) { - error_code = - calendar_query_create(_calendar_event._uri, &calendar_query); - } else { - error_code = - calendar_query_create(_calendar_todo._uri, &calendar_query); - } - if (CALENDAR_ERROR_NONE != error_code) { - throw UnknownException("calendar_query_create failed"); - } + int error_code = 0; + CalendarRecordPtr calendar_ptr = + CalendarRecord::GetById(calendar_id, _calendar_book._uri); + int type = + CalendarRecord::GetInt(calendar_ptr.get(), _calendar_book.store_type); + calendar_query_h calendar_query = nullptr; + if (type == CALENDAR_BOOK_TYPE_EVENT) { + error_code = + calendar_query_create(_calendar_event._uri, &calendar_query); + } else { + error_code = + calendar_query_create(_calendar_todo._uri, &calendar_query); + } + if (CALENDAR_ERROR_NONE != error_code) { + throw UnknownException("calendar_query_create failed"); + } - CalendarQueryPtr calendar_query_ptr(calendar_query, - CalendarRecord::QueryDeleter); + CalendarQueryPtr calendar_query_ptr(calendar_query, + CalendarRecord::QueryDeleter); - calendar_list_h record_list = nullptr; - error_code = calendar_db_get_records_with_query(calendar_query, 0, 0, - &record_list); - if (CALENDAR_ERROR_NONE != error_code) { - throw UnknownException("calendar_db_get_records_with_query failed"); - } - CalendarListPtr record_list_ptr(record_list, CalendarRecord::ListDeleter); + calendar_list_h record_list = nullptr; + error_code = calendar_db_get_records_with_query(calendar_query, 0, 0, + &record_list); + if (CALENDAR_ERROR_NONE != error_code) { + throw UnknownException("calendar_db_get_records_with_query failed"); + } + CalendarListPtr record_list_ptr(record_list, CalendarRecord::ListDeleter); - int record_count = 0; - error_code = calendar_list_get_count(record_list, &record_count); - if (CALENDAR_ERROR_NONE != error_code) { - throw UnknownException("calendar_list_get_count failed"); - } - error_code = calendar_list_first(record_list); - if (CALENDAR_ERROR_NONE != error_code) { - throw UnknownException("calendar_list_first failed"); - } + int record_count = 0; + error_code = calendar_list_get_count(record_list, &record_count); + if (CALENDAR_ERROR_NONE != error_code) { + throw UnknownException("calendar_list_get_count failed"); + } + error_code = calendar_list_first(record_list); + if (CALENDAR_ERROR_NONE != error_code) { + throw UnknownException("calendar_list_first failed"); + } - picojson::value result{picojson::array{}}; - picojson::array& calendarItems = result.get(); - calendarItems.reserve(record_count); - for (int i = 0; i < record_count; ++i) { - calendar_record_h current_record = NULL; - error_code = - calendar_list_get_current_record_p(record_list, ¤t_record); - if (CALENDAR_ERROR_NONE != error_code) { - throw UnknownException("calendar_list_get_current_record_p failed"); - } - picojson::value record_obj = picojson::value(picojson::object()); - CalendarItem::ToJson(type, current_record, - &record_obj.get()); - calendarItems.push_back(record_obj); - - error_code = calendar_list_next(record_list); - if (CALENDAR_ERROR_NONE != error_code) { - LoggerE("calendar_list_next failed (%i/%i)", i, record_count); - break; - } - } + array.reserve(record_count); + for (int i = 0; i < record_count; ++i) { + calendar_record_h current_record = NULL; + error_code = + calendar_list_get_current_record_p(record_list, ¤t_record); + if (CALENDAR_ERROR_NONE != error_code) { + throw UnknownException("calendar_list_get_current_record_p failed"); } - catch (...) {//const BasePlatformException& e) { - // LoggerE("error: %s: %s", e.name().c_str(), e.message().c_str()); + picojson::value record_obj = picojson::value(picojson::object()); + CalendarItem::ToJson(type, current_record, + &record_obj.get()); + array.push_back(record_obj); + + error_code = calendar_list_next(record_list); + if (CALENDAR_ERROR_NONE != error_code) { + LoggerE("calendar_list_next failed (%i/%i)", i, record_count); + break; } - }; - - auto get_response = [callback_handle](const std::shared_ptr & - response)->void { - }; - - TaskQueue::GetInstance().Queue( - get, get_response, - std::shared_ptr(new picojson::value(picojson::object()))); - -// NativePlugin::ReportSuccess(out); + } } void Calendar::RemoveBatch(const picojson::object& args, - picojson::object& out) { + picojson::array& array) { LoggerD("enter"); if (!CalendarManager::GetInstance().IsConnected()) { @@ -473,64 +410,47 @@ void Calendar::RemoveBatch(const picojson::object& args, int type = CalendarRecord::TypeToInt(FromJson(args, "type")); const char* view_uri = CalendarRecord::TypeToUri(type); - auto batch_func = [=](const JsonValuePtr & response)->void { - picojson::object& response_obj = response->get(); + std::vector ids_to_remove; + int id; + for (int i = 0, size = ids.size(); i < size; i++) { + if (type == CALENDAR_BOOK_TYPE_EVENT) { + id = common::stol( + FromJson(ids.at(i).get(), "uid")); + } else { + id = common::stol(ids.at(i).get()); + } - try { - std::vector ids_to_remove; - int id; - for (int i = 0, size = ids.size(); i < size; i++) { - if (type == CALENDAR_BOOK_TYPE_EVENT) { - id = common::stol( - FromJson(ids.at(i).get(), "uid")); - } else { - id = common::stol(ids.at(i).get()); - } - - CalendarRecordPtr record_ptr = CalendarItem::GetById(id, view_uri); - - if (type == CALENDAR_BOOK_TYPE_EVENT) { - const std::string& rid = CalendarRecord::GetString( - record_ptr.get(), _calendar_event.recurrence_id); - if (rid.empty()) { - ids_to_remove.push_back(id); - } else { - // @todo handle recurrence_id - } - } else { - ids_to_remove.push_back(id); - } - } + CalendarRecordPtr record_ptr = CalendarItem::GetById(id, view_uri); - int ret; - if (ids_to_remove.size() > 0) { - ret = calendar_db_delete_records(view_uri, &ids_to_remove[0], - ids_to_remove.size()); - - if (CALENDAR_ERROR_NONE != ret) { - LoggerE("calendar_db_delete_records failed."); - if (CALENDAR_ERROR_INVALID_PARAMETER == ret) { - LoggerE("CALENDAR_ERROR_INVALID_PARAMETER"); - throw InvalidValuesException("Parameter is invalid"); - } else { - LoggerE("CALENDAR_ERROR_DB_FAILED"); - throw UnknownException("UnknownError"); - } - } + if (type == CALENDAR_BOOK_TYPE_EVENT) { + const std::string& rid = CalendarRecord::GetString( + record_ptr.get(), _calendar_event.recurrence_id); + if (rid.empty()) { + ids_to_remove.push_back(id); + } else { + // @todo handle recurrence_id } + } else { + ids_to_remove.push_back(id); } - catch(...) { //(const BasePlatformException& e) { - } - }; - - int callback_handle;// = NativePlugin::GetAsyncCallbackHandle(args); - auto after_batch_func = [callback_handle](const JsonValuePtr& response) { - }; + } - TaskQueue::GetInstance().Queue( - batch_func, after_batch_func, - JsonValuePtr(new picojson::value(picojson::object()))); + int ret; + if (ids_to_remove.size() > 0) { + ret = calendar_db_delete_records(view_uri, &ids_to_remove[0], + ids_to_remove.size()); + if (CALENDAR_ERROR_NONE != ret) { + LoggerE("calendar_db_delete_records failed."); + if (CALENDAR_ERROR_INVALID_PARAMETER == ret) { + LoggerE("CALENDAR_ERROR_INVALID_PARAMETER"); + throw InvalidValuesException("Parameter is invalid"); + } else { + LoggerE("CALENDAR_ERROR_DB_FAILED"); + throw UnknownException("UnknownError"); + } + } + } } void Calendar::AddChangeListener(const picojson::object& args, picojson::object& out) { @@ -690,10 +610,6 @@ void Calendar::ChangeCallback(const char* view_uri, void*) { throw UnknownException("Can't get new version"); } current_db_version_ = updated_version; - -// wrt::common::NativeContext::GetInstance()->FireEvent( -// listeners_registered_[CalendarRecord::TypeToString(type)], -// response.serialize()); } } // namespace calendar diff --git a/src/calendar/calendar.h b/src/calendar/calendar.h index bcd713d4..6b911e6a 100644 --- a/src/calendar/calendar.h +++ b/src/calendar/calendar.h @@ -71,7 +71,7 @@ class Calendar { * {status: 'success', result: items} * @endcode */ - void AddBatch(const JsonObject& args, JsonObject& out); + void AddBatch(const JsonObject& args, JsonArray& array); /** * Signature: @code void update(item, updateAllInstances); @endcode @@ -99,7 +99,7 @@ class Calendar { * {status: 'success'} * @endcode */ - void UpdateBatch(const JsonObject& args, JsonObject& out); + void UpdateBatch(const JsonObject& args, JsonArray& array); /** * Signature: @code void remove(item); @endcode @@ -125,7 +125,7 @@ class Calendar { * {status: 'success'} * @endcode */ - void RemoveBatch(const JsonObject& args, JsonObject& out); + void RemoveBatch(const JsonObject& args, JsonArray& array); /** * Signature: @code void find(successCallback, errorCallback, filter, @@ -144,7 +144,7 @@ class Calendar { * {status: 'success', result: {calendarItemsArray}} * @endcode */ - void Find(const JsonObject& args, JsonObject& out); + void Find(const JsonObject& args, JsonArray& array); /** * Signature: @code void addChangeListener(successCallback); @endcode diff --git a/src/calendar/calendar_instance.cc b/src/calendar/calendar_instance.cc index 316385ea..85f9e092 100644 --- a/src/calendar/calendar_instance.cc +++ b/src/calendar/calendar_instance.cc @@ -29,12 +29,8 @@ CalendarInstance::CalendarInstance() { // Calendar REGISTER_SYNC("Calendar_get", Calendar_get); REGISTER_SYNC("Calendar_add", Calendar_add); - REGISTER_SYNC("Calendar_addBatch", Calendar_addBatch); REGISTER_SYNC("Calendar_update", Calendar_update); - REGISTER_SYNC("Calendar_updateBatch", Calendar_updateBatch); REGISTER_SYNC("Calendar_remove", Calendar_remove); - REGISTER_SYNC("Calendar_removeBatch", Calendar_removeBatch); - REGISTER_SYNC("Calendar_find", Calendar_find); REGISTER_SYNC("Calendar_addChangeListener", Calendar_addChangeListener); REGISTER_SYNC("Calendar_removeChangeListener", Calendar_removeChangeListener); @@ -49,7 +45,12 @@ CalendarInstance::CalendarInstance() { #undef REGISTER_SYNC #define REGISTER_ASYNC(c, x) \ RegisterHandler(c, std::bind(&CalendarInstance::x, this, _1, _2)); + REGISTER_ASYNC("Calendar_addBatch", Calendar_addBatch); + REGISTER_ASYNC("Calendar_updateBatch", Calendar_updateBatch); + REGISTER_ASYNC("Calendar_removeBatch", Calendar_removeBatch); + REGISTER_ASYNC("Calendar_updateBatch", Calendar_updateBatch); REGISTER_ASYNC("CalendarManager_getCalendars", CalendarManager_getCalendars); + REGISTER_ASYNC("Calendar_find", Calendar_find); #undef REGISTER_ASYNC } @@ -71,10 +72,29 @@ void CalendarInstance::Calendar_add(const JsonValue& args, JsonObject& out) { void CalendarInstance::Calendar_addBatch(const JsonValue& args, JsonObject& out) { - JsonValue val{JsonObject{}}; - Calendar::GetInstance().AddBatch(common::JsonCast(args), - val.get()); - ReportSuccess(val, out); + const double callback_id = args.get("callbackId").get(); + auto get = [=](const std::shared_ptr& response) -> void { + try { + JsonValue result = JsonValue(JsonArray()); + Calendar::GetInstance().AddBatch(common::JsonCast(args), + result.get()); + ReportSuccess(result, response->get()); + } catch (const PlatformException& e) { + ReportError(e, response->get()); + } + }; + + auto get_response = + [callback_id, this](const std::shared_ptr& response) -> void { + picojson::object& obj = response->get(); + obj.insert(std::make_pair("callbackId", callback_id)); + LoggerD("callback is %s", response->serialize().c_str()); + PostMessage(response->serialize().c_str()); + }; + + TaskQueue::GetInstance().Queue( + get, get_response, + std::shared_ptr(new JsonValue(JsonObject()))); } void CalendarInstance::Calendar_update(const JsonValue& args, JsonObject& out) { @@ -86,10 +106,29 @@ void CalendarInstance::Calendar_update(const JsonValue& args, JsonObject& out) { void CalendarInstance::Calendar_updateBatch(const JsonValue& args, JsonObject& out) { - JsonValue val{JsonObject{}}; - Calendar::GetInstance().UpdateBatch(common::JsonCast(args), - val.get()); - ReportSuccess(val, out); + const double callback_id = args.get("callbackId").get(); + auto get = [=](const std::shared_ptr& response) -> void { + try { + JsonValue result = JsonValue(JsonArray()); + Calendar::GetInstance().UpdateBatch(common::JsonCast(args), + result.get()); + ReportSuccess(result, response->get()); + } catch (const PlatformException& e) { + ReportError(e, response->get()); + } + }; + + auto get_response = + [callback_id, this](const std::shared_ptr& response) -> void { + picojson::object& obj = response->get(); + obj.insert(std::make_pair("callbackId", callback_id)); + LoggerD("callback is %s", response->serialize().c_str()); + PostMessage(response->serialize().c_str()); + }; + + TaskQueue::GetInstance().Queue( + get, get_response, + std::shared_ptr(new JsonValue(JsonObject()))); } void CalendarInstance::Calendar_remove(const JsonValue& args, JsonObject& out) { @@ -101,17 +140,55 @@ void CalendarInstance::Calendar_remove(const JsonValue& args, JsonObject& out) { void CalendarInstance::Calendar_removeBatch(const JsonValue& args, JsonObject& out) { - JsonValue val{JsonObject{}}; - Calendar::GetInstance().RemoveBatch(common::JsonCast(args), - val.get()); - ReportSuccess(out); + const double callback_id = args.get("callbackId").get(); + auto get = [=](const std::shared_ptr& response) -> void { + try { + JsonValue result = JsonValue(JsonArray()); + Calendar::GetInstance().RemoveBatch(common::JsonCast(args), + result.get()); + ReportSuccess(result, response->get()); + } catch (const PlatformException& e) { + ReportError(e, response->get()); + } + }; + + auto get_response = + [callback_id, this](const std::shared_ptr& response) -> void { + picojson::object& obj = response->get(); + obj.insert(std::make_pair("callbackId", callback_id)); + LoggerD("callback is %s", response->serialize().c_str()); + PostMessage(response->serialize().c_str()); + }; + + TaskQueue::GetInstance().Queue( + get, get_response, + std::shared_ptr(new JsonValue(JsonObject()))); } void CalendarInstance::Calendar_find(const JsonValue& args, JsonObject& out) { - JsonValue val{JsonObject{}}; - Calendar::GetInstance().Find(common::JsonCast(args), - val.get()); - ReportSuccess(out); + const double callback_id = args.get("callbackId").get(); + auto get = [=](const std::shared_ptr& response) -> void { + try { + JsonValue result = JsonValue(JsonArray()); + Calendar::GetInstance().Find(common::JsonCast(args), + result.get()); + ReportSuccess(result, response->get()); + } catch (const PlatformException& e) { + ReportError(e, response->get()); + } + }; + + auto get_response = + [callback_id, this](const std::shared_ptr& response) -> void { + picojson::object& obj = response->get(); + obj.insert(std::make_pair("callbackId", callback_id)); + LoggerD("callback isssssss %s", response->serialize().c_str()); + PostMessage(response->serialize().c_str()); + }; + + TaskQueue::GetInstance().Queue( + get, get_response, + std::shared_ptr(new JsonValue(JsonObject()))); } void CalendarInstance::Calendar_addChangeListener(const JsonValue& args, @@ -165,7 +242,7 @@ void CalendarInstance::CalendarManager_getCalendars(const JsonValue& args, [callback_id, this](const std::shared_ptr& response) -> void { picojson::object& obj = response->get(); obj.insert(std::make_pair("callbackId", callback_id)); - LoggerD("callback isssssss %s", response->serialize().c_str()); + LoggerD("callback is %s", response->serialize().c_str()); PostMessage(response->serialize().c_str()); };