}
}
-void Calendar::Get(const picojson::object& args, picojson::object& out) {
- LoggerD("enter");
-
+PlatformResult Calendar::Get(const picojson::object& args, picojson::object& out) {
if (!CalendarManager::GetInstance().IsConnected()) {
- throw UnknownException("DB Connection failed.");
+ return PlatformResult(ErrorCode::UNKNOWN_ERR, "DB Connection failed.");
}
int calendar_id = common::stol(FromJson<std::string>(args, "calendarId"));
+
+ calendar_record_h handle = nullptr;
+ PlatformResult status =
+ CalendarRecord::GetById(calendar_id, _calendar_book._uri, &handle);
+ if (status.IsError()) return status;
+
CalendarRecordPtr calendar_ptr =
- CalendarRecord::GetById(calendar_id, _calendar_book._uri);
- int type =
- CalendarRecord::GetInt(calendar_ptr.get(), _calendar_book.store_type);
+ CalendarRecordPtr(handle, CalendarRecord::Deleter);
+
+ int type;
+ status = CalendarRecord::GetInt(calendar_ptr.get(), _calendar_book.store_type,
+ &type);
+ if (status.IsError()) return status;
int id;
if (type == CALENDAR_BOOK_TYPE_EVENT) {
id = common::stol(FromJson<std::string>(args, "id"));
}
+ std::string uri;
+ status = CalendarRecord::TypeToUri(type, &uri);
+ if (status.IsError()) return status;
+
+ calendar_record_h handle_uri = nullptr;
+ status = CalendarRecord::GetById(id, uri.c_str(), &handle_uri);
+ if (status.IsError()) return status;
+
CalendarRecordPtr record_ptr =
- CalendarRecord::GetById(id, CalendarRecord::TypeToUri(type));
- picojson::value record_obj = picojson::value(picojson::object());
- CalendarItem::ToJson(type, record_ptr.get(), &out);
+ CalendarRecordPtr(handle_uri, CalendarRecord::Deleter);
+
+ status = CalendarItem::ToJson(type, record_ptr.get(), &out);
+ if (status.IsError()) return status;
+
+ return PlatformResult(ErrorCode::NO_ERROR);
}
-void Calendar::Add(const picojson::object& args, picojson::object& out) {
- LoggerD("enter");
+PlatformResult Calendar::Add(const picojson::object& args,
+ picojson::object& out) {
if (!CalendarManager::GetInstance().IsConnected()) {
- throw UnknownException("DB Connection failed.");
+ return PlatformResult(ErrorCode::UNKNOWN_ERR, "DB Connection failed.");
}
const auto& item = FromJson<picojson::object>(args, "item");
int type = CalendarRecord::TypeToInt(FromJson<std::string>(args, "type"));
- CalendarRecordPtr item_ptr = CalendarItem::Create(type);
- CalendarItem::FromJson(type, item_ptr.get(), item);
- int record_id = CalendarRecord::Insert(item_ptr.get());
+ calendar_record_h handle = nullptr;
+ PlatformResult status = CalendarItem::Create(type, &handle);
+ if (status.IsError()) return status;
+
+ CalendarRecordPtr item_ptr =
+ CalendarRecordPtr(handle, CalendarRecord::Deleter);
+
+ status = CalendarItem::FromJson(type, item_ptr.get(), item);
+ if (status.IsError()) return status;
+
+ int record_id;
+ status = CalendarRecord::Insert(item_ptr.get(), &record_id);
+ if (status.IsError()) return status;
+
out.insert(std::make_pair("uid", std::to_string(record_id)));
if (type == CALENDAR_BOOK_TYPE_EVENT) {
- std::string rid = CalendarRecord::GetString(item_ptr.get(),
- _calendar_event.recurrence_id);
+ std::string rid;
+ PlatformResult status = CalendarRecord::GetString(
+ item_ptr.get(), _calendar_event.recurrence_id, &rid);
+ if (status.IsError()) return status;
+
if (!rid.empty()) {
out["rid"] = picojson::value(rid);
} else {
out["rid"] = picojson::value();
}
}
-}
-void Calendar::AddBatch(const picojson::object& args, picojson::array& array) {
- LoggerD("enter");
+ return PlatformResult(ErrorCode::NO_ERROR);
+}
+PlatformResult Calendar::AddBatch(const picojson::object& args,
+ picojson::array& array) {
if (!CalendarManager::GetInstance().IsConnected()) {
- throw UnknownException("DB Connection failed.");
+ return PlatformResult(ErrorCode::UNKNOWN_ERR, "DB Connection failed.");
}
auto& items = FromJson<picojson::array>(args, "items");
if (items.empty()) {
- throw InvalidValuesException("No items");
+ return PlatformResult(ErrorCode::INVALID_VALUES_ERR, "No items");
}
int type = CalendarRecord::TypeToInt(FromJson<std::string>(args, "type"));
- const char* view_uri = CalendarRecord::TypeToUri(type);
+ std::string view_uri;
+ PlatformResult status = CalendarRecord::TypeToUri(type, &view_uri);
+ if (status.IsError()) return status;
+
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");
+ return PlatformResult(ErrorCode::UNKNOWN_ERR,
+ "Could not create list for batch operation");
}
CalendarListPtr list_ptr = CalendarListPtr(list, CalendarRecord::ListDeleter);
calendar_record_h record;
for (auto& item : items) {
- ret = calendar_record_create(view_uri, &record);
+ ret = calendar_record_create(view_uri.c_str(), &record);
if (CALENDAR_ERROR_NONE != ret) {
LoggerW("Can't create platform record %d", ret);
- throw UnknownException("Can't create platform record");
+ return PlatformResult(ErrorCode::UNKNOWN_ERR,
+ "Can't create platform record");
}
- CalendarItem::FromJson(type, record, item.get<picojson::object>());
+ PlatformResult status =
+ CalendarItem::FromJson(type, record, item.get<picojson::object>());
+ if (status.IsError()) return status;
+
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");
+ return PlatformResult(ErrorCode::INVALID_VALUES_ERR,
+ "Could not add record to list");
}
}
LoggerE("calendar_db_insert_records failed.");
if (CALENDAR_ERROR_INVALID_PARAMETER == ret) {
LoggerE("CALENDAR_ERROR_INVALID_PARAMETER.");
- throw InvalidValuesException("Parameter is invalid");
+ return PlatformResult(ErrorCode::INVALID_VALUES_ERR,
+ "Parameter is invalid");
} else {
LoggerE("CALENDAR_ERROR_DB_FAILED");
- throw UnknownException("CALENDAR_ERROR_DB_FAILED occurred");
+ return PlatformResult(ErrorCode::UNKNOWN_ERR,
+ "CALENDAR_ERROR_DB_FAILED occurred");
}
}
array.push_back(id);
}
free(ids);
-}
-void Calendar::Update(const picojson::object& args, picojson::object& /*out*/) {
- LoggerD("enter");
+ return PlatformResult(ErrorCode::NO_ERROR);
+}
+PlatformResult Calendar::Update(const picojson::object& args,
+ picojson::object& /*out*/) {
if (!CalendarManager::GetInstance().IsConnected()) {
- throw UnknownException("DB Connection failed.");
+ return PlatformResult(ErrorCode::UNKNOWN_ERR, "DB Connection failed.");
}
const auto& item = FromJson<picojson::object>(args, "item");
id = common::stol(FromJson<std::string>(item, "id"));
}
+ std::string value_str;
+ PlatformResult status = CalendarRecord::TypeToUri(type, &value_str);
+ if (status.IsError()) return status;
+
+ calendar_record_h handle = nullptr;
+ status = CalendarRecord::GetById(id, value_str.c_str(), &handle);
+ if (status.IsError()) return status;
+
CalendarRecordPtr record_ptr =
- CalendarRecord::GetById(id, CalendarRecord::TypeToUri(type));
- CalendarItem::FromJson(type, record_ptr.get(), item);
+ CalendarRecordPtr(handle, CalendarRecord::Deleter);
+
+ status = CalendarItem::FromJson(type, record_ptr.get(), item);
+ if (status.IsError()) return status;
if (type == CALENDAR_BOOK_TYPE_TODO || update_all ||
common::IsNull(item, "recurrenceRule")) {
if (CALENDAR_ERROR_NONE != calendar_db_update_record(record_ptr.get())) {
LoggerE("Can't update calendar item");
- throw UnknownException("Can't update calendar item");
+ return PlatformResult(ErrorCode::UNKNOWN_ERR,
+ "Can't update calendar item");
}
} else {
// first update the parent event
"rid")) {
exdate.append(common::FromJson<std::string>(item, "id", "rid"));
}
- CalendarRecord::SetString(record_ptr.get(), _calendar_event.exdate, exdate);
+ PlatformResult status = CalendarRecord::SetString(
+ record_ptr.get(), _calendar_event.exdate, exdate);
+ if (status.IsError()) return status;
// don't set the recurrence id for the parent event
- CalendarRecord::SetString(record_ptr.get(), _calendar_event.recurrence_id,
- "");
+ status = CalendarRecord::SetString(record_ptr.get(),
+ _calendar_event.recurrence_id, "");
+ if (status.IsError()) return status;
if (CALENDAR_ERROR_NONE != calendar_db_update_record(record_ptr.get())) {
LoggerE("Can't update calendar item");
- throw UnknownException("Can't update calendar item");
+ return PlatformResult(ErrorCode::UNKNOWN_ERR,
+ "Can't update calendar item");
}
// now add the detached child event
- CalendarRecordPtr item_ptr = CalendarItem::Create(type);
- CalendarItem::FromJson(type, item_ptr.get(), item);
- CalendarRecord::Insert(item_ptr.get());
+ calendar_record_h handle_new = nullptr;
+ status = CalendarItem::Create(type, &handle_new);
+ if (status.IsError()) return status;
+
+ CalendarRecordPtr item_ptr =
+ CalendarRecordPtr(handle_new, CalendarRecord::Deleter);
+
+ status = CalendarItem::FromJson(type, item_ptr.get(), item);
+ if (status.IsError()) return status;
+
+ int record_id;
+ status = CalendarRecord::Insert(item_ptr.get(), &record_id);
+ if (status.IsError()) return status;
}
-}
-void Calendar::UpdateBatch(const picojson::object& args,
- picojson::array& array) {
- LoggerD("enter");
+ return PlatformResult(ErrorCode::NO_ERROR);
+}
+PlatformResult Calendar::UpdateBatch(const picojson::object& args,
+ picojson::array& array) {
if (!CalendarManager::GetInstance().IsConnected()) {
- throw UnknownException("DB Connection failed.");
+ return PlatformResult(ErrorCode::UNKNOWN_ERR, "DB Connection failed.");
}
auto& items = FromJson<picojson::array>(args, "items");
if (items.empty()) {
- throw InvalidValuesException("No items");
+ return PlatformResult(ErrorCode::INVALID_VALUES_ERR, "No items");
}
bool update_all = true;
}
int type = CalendarRecord::TypeToInt(FromJson<std::string>(args, "type"));
- const char* view_uri = CalendarRecord::TypeToUri(type);
+ std::string view_uri;
+ PlatformResult status = CalendarRecord::TypeToUri(type, &view_uri);
+ if (status.IsError()) return status;
+
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");
+ return PlatformResult(ErrorCode::UNKNOWN_ERR,
+ "Could not create list for batch operation");
}
CalendarListPtr list_ptr = CalendarListPtr(list, CalendarRecord::ListDeleter);
id = common::stol(FromJson<std::string>(item_obj, "id"));
}
- ret = calendar_db_get_record(view_uri, id, &record);
+ ret = calendar_db_get_record(view_uri.c_str(), id, &record);
if (CALENDAR_ERROR_NONE != ret) {
LoggerW("Can't get platform record %d", ret);
- throw UnknownException("Can't get platform record");
+ return PlatformResult(ErrorCode::UNKNOWN_ERR,
+ "Can't get platform record");
}
- CalendarItem::FromJson(type, record, item.get<picojson::object>());
+ PlatformResult status =
+ CalendarItem::FromJson(type, record, item.get<picojson::object>());
+ if (status.IsError()) return status;
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");
+ return PlatformResult(ErrorCode::INVALID_VALUES_ERR,
+ "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");
+ return PlatformResult(ErrorCode::UNKNOWN_ERR,
+ "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, picojson::object& out) {
- LoggerD("enter");
+ return PlatformResult(ErrorCode::NO_ERROR);
+}
+PlatformResult Calendar::Remove(const picojson::object& args,
+ picojson::object& out) {
if (!CalendarManager::GetInstance().IsConnected()) {
- throw UnknownException("DB Connection failed.");
+ return PlatformResult(ErrorCode::UNKNOWN_ERR, "DB Connection failed.");
}
int type = CalendarRecord::TypeToInt(FromJson<std::string>(args, "type"));
id = common::stol(FromJson<std::string>(args, "id"));
}
- CalendarItem::Remove(type, id);
+ return CalendarItem::Remove(type, id);
}
-void Calendar::Find(const picojson::object& args, picojson::array& array) {
- LoggerD("enter");
-
+PlatformResult Calendar::Find(const picojson::object& args, picojson::array& array) {
if (!CalendarManager::GetInstance().IsConnected()) {
- throw UnknownException("DB Connection failed.");
+ return PlatformResult(ErrorCode::UNKNOWN_ERR, "DB Connection failed.");
}
int calendar_id = common::stol(FromJson<std::string>(args, "calendarId"));
int error_code = 0;
+ calendar_record_h handle = nullptr;
+ PlatformResult status =
+ CalendarRecord::GetById(calendar_id, _calendar_book._uri, &handle);
+ if (status.IsError()) return status;
+
CalendarRecordPtr calendar_ptr =
- CalendarRecord::GetById(calendar_id, _calendar_book._uri);
- int type =
- CalendarRecord::GetInt(calendar_ptr.get(), _calendar_book.store_type);
+ CalendarRecordPtr(handle, CalendarRecord::Deleter);
+
+ int type;
+ status = CalendarRecord::GetInt(calendar_ptr.get(), _calendar_book.store_type,
+ &type);
+ if (status.IsError()) return status;
+
calendar_query_h calendar_query = nullptr;
if (type == CALENDAR_BOOK_TYPE_EVENT) {
error_code = calendar_query_create(_calendar_event._uri, &calendar_query);
- ErrorChecker(error_code);
+ if ((status = ErrorChecker(error_code)).IsError()) return status;
} else {
error_code = calendar_query_create(_calendar_todo._uri, &calendar_query);
- ErrorChecker(error_code);
+ if ((status = ErrorChecker(error_code)).IsError()) return status;
}
if (CALENDAR_ERROR_NONE != error_code) {
- throw UnknownException("calendar_query_create failed");
+ return PlatformResult(ErrorCode::UNKNOWN_ERR,
+ "calendar_query_create failed");
}
std::vector<std::vector<CalendarFilterPtr>> intermediate_filters(1);
if (!IsNull(args, "filter")) {
if (type == CALENDAR_BOOK_TYPE_EVENT) {
error_code =
calendar_filter_create(_calendar_event._uri, &calendar_filter);
- ErrorChecker(error_code);
+ if ((status = ErrorChecker(error_code)).IsError()) return status;
} else {
error_code =
calendar_filter_create(_calendar_todo._uri, &calendar_filter);
- ErrorChecker(error_code);
+ if ((status = ErrorChecker(error_code)).IsError()) return status;
}
CalendarFilterPtr calendar_filter_ptr(calendar_filter,
CalendarFilterDeleter);
unsigned int propertyId = 0;
- if (name == "startDate" || name == "endDate" || name == "dueDate")
- propertyId = CalendarItem::GetPlatformProperty(type, name + "_time");
- else
- propertyId = CalendarItem::GetPlatformProperty(type, name);
+ if (name == "startDate" || name == "endDate" || name == "dueDate") {
+ PlatformResult status = CalendarItem::GetPlatformProperty(
+ type, name + "_time", &propertyId);
+ if (status.IsError()) return status;
+ } else {
+ PlatformResult status =
+ CalendarItem::GetPlatformProperty(type, name, &propertyId);
+ if (status.IsError()) return status;
+ }
+
if (name == "id" || name == "id.uid") {
if (type == CALENDAR_BOOK_TYPE_EVENT && name == "id") {
value = common::stol(
value = common::stol(JsonCast<std::string>(match_value));
}
if (value < 0) {
- throw InvalidValuesException("Match value cannot be less than 0");
+ return PlatformResult(ErrorCode::INVALID_VALUES_ERR,
+ "Match value cannot be less than 0");
}
calendar_match_int_flag_e flag;
if (AttributeMatchFlag::kExists == match_flag) {
}
error_code =
calendar_filter_add_int(calendar_filter, propertyId, flag, value);
- ErrorChecker(error_code);
+ if ((status = ErrorChecker(error_code)).IsError()) return status;
} else if (name == "startDate" || name == "endDate" ||
name == "dueDate") {
calendar_match_int_flag_e flag;
error_code = calendar_filter_add_caltime(
calendar_filter, propertyId, flag,
CalendarItem::DateToPlatform(dateTofilter, false));
- ErrorChecker(error_code);
+ if ((status = ErrorChecker(error_code)).IsError()) return status;
} else {
std::string value = JsonCast<std::string>(match_value);
calendar_match_str_flag_e flag = CALENDAR_MATCH_EXISTS;
visitor.SetOnCompositeFilterEnd([&](CompositeFilterType calType) {
if (intermediate_filters.size() == 0) {
- throw UnknownException("Reached stack size equal to 0!");
+ return PlatformResult(ErrorCode::UNKNOWN_ERR,
+ "Reached stack size equal to 0!");
}
calendar_filter_h merged_filter = nullptr;
if (type == CALENDAR_BOOK_TYPE_EVENT) {
error_code =
calendar_filter_create(_calendar_event._uri, &merged_filter);
- ErrorChecker(error_code);
+ if ((status = ErrorChecker(error_code)).IsError()) return status;
} else {
error_code =
calendar_filter_create(_calendar_todo._uri, &merged_filter);
- ErrorChecker(error_code);
+ if ((status = ErrorChecker(error_code)).IsError()) return status;
}
CalendarFilterPtr merged_filter_ptr(merged_filter, CalendarFilterDeleter);
for (std::size_t i = 0; i < intermediate_filters.back().size(); ++i) {
error_code = calendar_filter_add_filter(
merged_filter, intermediate_filters.back().at(i).get());
- ErrorChecker(error_code);
+ if ((status = ErrorChecker(error_code)).IsError()) return status;
if (CompositeFilterType::kIntersection == calType) {
error_code = calendar_filter_add_operator(
merged_filter, CALENDAR_FILTER_OPERATOR_AND);
- ErrorChecker(error_code);
+ if ((status = ErrorChecker(error_code)).IsError()) return status;
} else if (CompositeFilterType::kUnion == calType) {
error_code = calendar_filter_add_operator(
merged_filter, CALENDAR_FILTER_OPERATOR_OR);
- ErrorChecker(error_code);
+ if ((status = ErrorChecker(error_code)).IsError()) return status;
} else {
- throw InvalidValuesException("Invalid union type!");
+ return PlatformResult(ErrorCode::INVALID_VALUES_ERR,
+ "Invalid union type!");
}
}
intermediate_filters.pop_back();
const JsonValue& end_value) {
unsigned int propertyId = 0;
if (name == "startDate" || name == "endDate" || name == "dueDate") {
- propertyId = CalendarItem::GetPlatformProperty(type, name + "_time");
-
+ PlatformResult status = CalendarItem::GetPlatformProperty(
+ type, name + "_time", &propertyId);
+ if (status.IsError()) return status;
} else {
- propertyId = CalendarItem::GetPlatformProperty(type, name);
+ PlatformResult status =
+ CalendarItem::GetPlatformProperty(type, name, &propertyId);
+ if (status.IsError()) return status;
}
+
calendar_filter_h calendar_filter = nullptr;
int error_code = 0;
if (type == CALENDAR_BOOK_TYPE_EVENT) {
error_code =
calendar_filter_create(_calendar_event._uri, &calendar_filter);
- ErrorChecker(error_code);
+ if ((status = ErrorChecker(error_code)).IsError()) return status;
} else {
error_code =
calendar_filter_create(_calendar_todo._uri, &calendar_filter);
- ErrorChecker(error_code);
+ if ((status = ErrorChecker(error_code)).IsError()) return status;
}
CalendarFilterPtr calendar_filter_ptr(calendar_filter,
CalendarFilterDeleter);
if (type == CALENDAR_BOOK_TYPE_EVENT) {
error_code =
calendar_filter_create(_calendar_event._uri, &sub_filter);
- ErrorChecker(error_code);
+ if ((status = ErrorChecker(error_code)).IsError()) return status;
} else {
error_code =
calendar_filter_create(_calendar_todo._uri, &sub_filter);
- ErrorChecker(error_code);
+ if ((status = ErrorChecker(error_code)).IsError()) return status;
}
CalendarFilterPtr sub_filter_ptr(sub_filter, CalendarFilterDeleter);
end_value_date);
error_code = calendar_filter_add_filter(calendar_filter, sub_filter);
- ErrorChecker(error_code);
+ if ((status = ErrorChecker(error_code)).IsError()) return status;
} else if (initial_value_exists) {
error_code = calendar_filter_add_int(
calendar_filter, propertyId, CALENDAR_MATCH_GREATER_THAN_OR_EQUAL,
initial_value_date);
- ErrorChecker(error_code);
+ if ((status = ErrorChecker(error_code)).IsError()) return status;
} else if (end_value_exists) {
error_code = calendar_filter_add_int(
calendar_filter, propertyId, CALENDAR_MATCH_LESS_THAN_OR_EQUAL,
end_value_date);
- ErrorChecker(error_code);
+ if ((status = ErrorChecker(error_code)).IsError()) return status;
}
} else if (name == "startDate" || name == "dueDate" ||
name == "endDate") {
if (type == CALENDAR_BOOK_TYPE_EVENT) {
error_code =
calendar_filter_create(_calendar_event._uri, &sub_filter);
- ErrorChecker(error_code);
+ if ((status = ErrorChecker(error_code)).IsError()) return status;
} else {
error_code =
calendar_filter_create(_calendar_todo._uri, &sub_filter);
- ErrorChecker(error_code);
+ if ((status = ErrorChecker(error_code)).IsError()) return status;
}
CalendarFilterPtr sub_filter_ptr(sub_filter, CalendarFilterDeleter);
error_code = calendar_filter_add_caltime(
sub_filter, propertyId, CALENDAR_MATCH_GREATER_THAN_OR_EQUAL,
CalendarItem::DateToPlatform(initial_value_date, false));
- ErrorChecker(error_code);
+ if ((status = ErrorChecker(error_code)).IsError()) return status;
error_code = calendar_filter_add_operator(
sub_filter, CALENDAR_FILTER_OPERATOR_AND);
- ErrorChecker(error_code);
+ if ((status = ErrorChecker(error_code)).IsError()) return status;
error_code = calendar_filter_add_caltime(
sub_filter, propertyId, CALENDAR_MATCH_LESS_THAN_OR_EQUAL,
CalendarItem::DateToPlatform(end_value_date, false));
- ErrorChecker(error_code);
+ if ((status = ErrorChecker(error_code)).IsError()) return status;
error_code = calendar_filter_add_filter(calendar_filter, sub_filter);
- ErrorChecker(error_code);
+ if ((status = ErrorChecker(error_code)).IsError()) return status;
} else if (initial_value_exists) {
error_code = calendar_filter_add_caltime(
calendar_filter, propertyId, CALENDAR_MATCH_GREATER_THAN_OR_EQUAL,
CalendarItem::DateToPlatform(initial_value_date, false));
- ErrorChecker(error_code);
+ if ((status = ErrorChecker(error_code)).IsError()) return status;
} else if (end_value_exists) {
error_code = calendar_filter_add_caltime(
calendar_filter, propertyId, CALENDAR_MATCH_LESS_THAN_OR_EQUAL,
CalendarItem::DateToPlatform(end_value_date, false));
- ErrorChecker(error_code);
+ if ((status = ErrorChecker(error_code)).IsError()) return status;
}
} else {
std::string initial_value_str;
if (type == CALENDAR_BOOK_TYPE_EVENT) {
error_code =
calendar_filter_create(_calendar_event._uri, &sub_filter);
- ErrorChecker(error_code);
+ if ((status = ErrorChecker(error_code)).IsError()) return status;
} else {
error_code =
calendar_filter_create(_calendar_todo._uri, &sub_filter);
- ErrorChecker(error_code);
+ if ((status = ErrorChecker(error_code)).IsError()) return status;
}
CalendarFilterPtr sub_filter_ptr(sub_filter, CalendarFilterDeleter);
error_code = calendar_filter_add_str(sub_filter, propertyId,
CALENDAR_MATCH_STARTSWITH,
initial_value_str.c_str());
- ErrorChecker(error_code);
+ if ((status = ErrorChecker(error_code)).IsError()) return status;
error_code = calendar_filter_add_operator(
sub_filter, CALENDAR_FILTER_OPERATOR_AND);
- ErrorChecker(error_code);
+ if ((status = ErrorChecker(error_code)).IsError()) return status;
error_code = calendar_filter_add_str(sub_filter, propertyId,
CALENDAR_MATCH_ENDSWITH,
end_value_str.c_str());
- ErrorChecker(error_code);
+ if ((status = ErrorChecker(error_code)).IsError()) return status;
error_code = calendar_filter_add_filter(calendar_filter, sub_filter);
- ErrorChecker(error_code);
+ if ((status = ErrorChecker(error_code)).IsError()) return status;
} else if (initial_value_exists) {
error_code = calendar_filter_add_str(calendar_filter, propertyId,
CALENDAR_MATCH_STARTSWITH,
initial_value_str.c_str());
- ErrorChecker(error_code);
+ if ((status = ErrorChecker(error_code)).IsError()) return status;
} else if (end_value_exists) {
error_code = calendar_filter_add_str(calendar_filter, propertyId,
CALENDAR_MATCH_ENDSWITH,
end_value_str.c_str());
- ErrorChecker(error_code);
+ if ((status = ErrorChecker(error_code)).IsError()) return status;
}
}
intermediate_filters[intermediate_filters.size() - 1].push_back(
if ((intermediate_filters.size() != 1) ||
(intermediate_filters[0].size() != 1)) {
LoggerE("Bad filter evaluation!");
- throw UnknownException("Bad filter evaluation!");
+ return PlatformResult(ErrorCode::UNKNOWN_ERR, "Bad filter evaluation!");
}
error_code = calendar_query_set_filter(calendar_query,
intermediate_filters[0][0].get());
- ErrorChecker(error_code);
+ if ((status = ErrorChecker(error_code)).IsError()) return status;
}
if (!IsNull(args, "sortMode")) {
FromJson<std::string>(sortModeObject, "attributeName");
std::string order = FromJson<std::string>(sortModeObject, "order");
if (attributeName == "startDate" || attributeName == "dueDate" ||
- attributeName == "endDate")
- propertyId =
- CalendarItem::GetPlatformProperty(type, attributeName + "_time");
- else
- propertyId = CalendarItem::GetPlatformProperty(type, attributeName);
+ attributeName == "endDate") {
+ PlatformResult status = CalendarItem::GetPlatformProperty(
+ type, attributeName + "_time", &propertyId);
+ if (status.IsError()) return status;
+ } else {
+ PlatformResult status =
+ CalendarItem::GetPlatformProperty(type, attributeName, &propertyId);
+ if (status.IsError()) return status;
+ }
+
if (order.empty() || order == "ASC") {
error_code = calendar_query_set_sort(calendar_query, propertyId, true);
- ErrorChecker(error_code);
+ if ((status = ErrorChecker(error_code)).IsError()) return status;
} else if (order == "DESC") {
error_code = calendar_query_set_sort(calendar_query, propertyId, false);
- ErrorChecker(error_code);
+ if ((status = ErrorChecker(error_code)).IsError()) return status;
}
}
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");
+ return PlatformResult(ErrorCode::UNKNOWN_ERR,
+ "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");
+ return PlatformResult(ErrorCode::UNKNOWN_ERR,
+ "calendar_list_get_count failed");
}
error_code = calendar_list_first(record_list);
if (CALENDAR_ERROR_NONE != error_code) {
- throw UnknownException("calendar_list_first failed");
+ return PlatformResult(ErrorCode::UNKNOWN_ERR, "calendar_list_first failed");
}
array.reserve(record_count);
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");
+ return PlatformResult(ErrorCode::UNKNOWN_ERR,
+ "calendar_list_get_current_record_p failed");
}
picojson::value record_obj = picojson::value(picojson::object());
- CalendarItem::ToJson(type, current_record,
- &record_obj.get<picojson::object>());
+ PlatformResult status = CalendarItem::ToJson(
+ type, current_record, &record_obj.get<picojson::object>());
+ if (status.IsError()) return status;
+
array.push_back(record_obj);
error_code = calendar_list_next(record_list);
break;
}
}
-}
-void Calendar::RemoveBatch(const picojson::object& args,
- picojson::array& array) {
- LoggerD("enter");
+ return PlatformResult(ErrorCode::NO_ERROR);
+}
+PlatformResult Calendar::RemoveBatch(const picojson::object& args,
+ picojson::array& array) {
if (!CalendarManager::GetInstance().IsConnected()) {
- throw UnknownException("DB Connection failed.");
+ return PlatformResult(ErrorCode::UNKNOWN_ERR, "DB Connection failed.");
}
auto& ids = FromJson<picojson::array>(args, "ids");
if (ids.empty()) {
- throw InvalidValuesException("No items");
+ return PlatformResult(ErrorCode::INVALID_VALUES_ERR, "No items");
}
int type = CalendarRecord::TypeToInt(FromJson<std::string>(args, "type"));
- const char* view_uri = CalendarRecord::TypeToUri(type);
+ std::string view_uri;
+ PlatformResult status = CalendarRecord::TypeToUri(type, &view_uri);
+ if (status.IsError()) return status;
std::vector<int> ids_to_remove;
int id;
id = common::stol(ids.at(i).get<std::string>());
}
- CalendarRecordPtr record_ptr = CalendarItem::GetById(id, view_uri);
+ calendar_record_h handle = nullptr;
+ PlatformResult status =
+ CalendarItem::GetById(id, view_uri.c_str(), &handle);
+ if (status.IsError()) return status;
+
+ CalendarRecordPtr record_ptr =
+ CalendarRecordPtr(handle, CalendarRecord::Deleter);
if (type == CALENDAR_BOOK_TYPE_EVENT) {
- const std::string& rid = CalendarRecord::GetString(
- record_ptr.get(), _calendar_event.recurrence_id);
+ std::string rid;
+ status = CalendarRecord::GetString(record_ptr.get(),
+ _calendar_event.recurrence_id, &rid);
+ if (status.IsError()) return status;
+
if (rid.empty()) {
ids_to_remove.push_back(id);
} else {
int ret;
if (ids_to_remove.size() > 0) {
- ret = calendar_db_delete_records(view_uri, &ids_to_remove[0],
+ ret = calendar_db_delete_records(view_uri.c_str(), &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");
+ return PlatformResult(ErrorCode::INVALID_VALUES_ERR,
+ "Parameter is invalid");
} else {
LoggerE("CALENDAR_ERROR_DB_FAILED");
- throw UnknownException("UnknownError");
+ return PlatformResult(ErrorCode::UNKNOWN_ERR, "UnknownError");
}
}
}
-}
-void Calendar::AddChangeListener(const picojson::object& args,
- picojson::object& out) {
- LoggerD("enter");
+ return PlatformResult(ErrorCode::NO_ERROR);
+}
+PlatformResult Calendar::AddChangeListener(const picojson::object& args,
+ picojson::object& out) {
if (!CalendarManager::GetInstance().IsConnected()) {
- throw UnknownException("DB Connection failed.");
+ return PlatformResult(ErrorCode::UNKNOWN_ERR, "DB Connection failed.");
}
const std::string& type = FromJson<std::string>(args, "type");
int ret;
if (listeners_registered_.find(type) == listeners_registered_.end()) {
- const char* view_uri = CalendarRecord::TypeToUri(type);
- ret = calendar_db_add_changed_cb(view_uri, ChangeCallback, nullptr);
+ std::string view_uri;
+ PlatformResult status = CalendarRecord::TypeToUri(type, &view_uri);
+ if (status.IsError()) return status;
+
+ ret = calendar_db_add_changed_cb(view_uri.c_str(), ChangeCallback, nullptr);
if (CALENDAR_ERROR_NONE != ret) {
LoggerE("Add calendar change callback error for type %s", type.c_str());
- throw UnknownException("Add calendar change callback error");
+ return PlatformResult(ErrorCode::UNKNOWN_ERR,
+ "Add calendar change callback error");
}
ret = calendar_db_get_current_version(¤t_db_version_);
if (CALENDAR_ERROR_NONE != ret) {
current_db_version_ = 0;
LoggerE("Can't get calendar db version");
- throw UnknownException("Can't get calendar db version");
+ return PlatformResult(ErrorCode::UNKNOWN_ERR,
+ "Can't get calendar db version");
}
listeners_registered_[type] = listener_id;
}
-}
-void Calendar::RemoveChangeListener(const picojson::object& args,
- picojson::object& out) {
- LoggerD("enter");
+ return PlatformResult(ErrorCode::NO_ERROR);
+}
+PlatformResult Calendar::RemoveChangeListener(const picojson::object& args,
+ picojson::object& out) {
if (!CalendarManager::GetInstance().IsConnected()) {
- throw UnknownException("DB Connection failed.");
+ return PlatformResult(ErrorCode::UNKNOWN_ERR, "DB Connection failed.");
}
const std::string& type = FromJson<std::string>(args, "type");
int ret;
if (listeners_registered_.find(type) != listeners_registered_.end()) {
- const char* view_uri = CalendarRecord::TypeToUri(type);
- ret = calendar_db_remove_changed_cb(view_uri, ChangeCallback, nullptr);
+ std::string view_uri;
+ PlatformResult status = CalendarRecord::TypeToUri(type, &view_uri);
+ if (status.IsError()) return status;
+
+ ret = calendar_db_remove_changed_cb(view_uri.c_str(), ChangeCallback,
+ nullptr);
if (CALENDAR_ERROR_NONE != ret) {
LoggerE("Remove calendar change callback error for type %s",
type.c_str());
- throw UnknownException("Remove calendar change callback error");
+ return PlatformResult(ErrorCode::UNKNOWN_ERR,
+ "Remove calendar change callback error");
}
listeners_registered_.erase(type);
}
+
+ return PlatformResult(ErrorCode::NO_ERROR);
}
void Calendar::ChangeCallback(const char* view_uri, void*) {
&updated_version);
if (CALENDAR_ERROR_NONE != ret) {
LoggerE("Can't get the changed item list");
- throw UnknownException("Can't get the changed item list");
+ return;
}
CalendarListPtr list_ptr = CalendarListPtr(list, CalendarRecord::ListDeleter);
ret = calendar_list_get_count(list_ptr.get(), &count);
if (CALENDAR_ERROR_NONE != ret) {
LoggerE("Can't get the changed item count");
- throw UnknownException("Can't get the changed item count");
+ return;
}
LoggerD("Item count: %d", count);
ret = calendar_list_get_current_record_p(list, &update_info);
if (CALENDAR_ERROR_NONE != ret) {
LoggerE("Can't get current record");
- throw UnknownException("Can't get current record");
+ return;
}
- id = CalendarRecord::GetInt(update_info, _calendar_updated_info.id);
- status = CalendarRecord::GetInt(update_info,
- _calendar_updated_info.modified_status);
+ PlatformResult result =
+ CalendarRecord::GetInt(update_info, _calendar_updated_info.id, &id);
+ if (result.IsError()) return;
+
+ result = CalendarRecord::GetInt(
+ update_info, _calendar_updated_info.modified_status, &status);
+ if (result.IsError()) return;
if (status == CALENDAR_RECORD_MODIFIED_STATUS_DELETED) {
- calendar_id = CalendarRecord::GetInt(
- update_info, _calendar_updated_info.calendar_book_id);
+ result = CalendarRecord::GetInt(
+ update_info, _calendar_updated_info.calendar_book_id, &calendar_id);
+ if (result.IsError()) return;
picojson::value removed_row = picojson::value(picojson::object());
picojson::object& removed_obj = removed_row.get<picojson::object>();
continue;
}
- try {
- CalendarRecordPtr record_ptr = CalendarRecord::GetById(id, view_uri);
- picojson::value record_obj = picojson::value(picojson::object());
- CalendarItem::ToJson(type, record_ptr.get(),
- &record_obj.get<picojson::object>());
- if (status == CALENDAR_RECORD_MODIFIED_STATUS_INSERTED) {
- added.push_back(record_obj);
- } else {
- updated.push_back(record_obj);
- }
- } catch (PlatformException& ex) {
- LoggerE("error occured");
+ calendar_record_h handle = nullptr;
+ result = CalendarRecord::GetById(id, view_uri, &handle);
+ if (result.IsError()) return;
+
+ CalendarRecordPtr record_ptr =
+ CalendarRecordPtr(handle, CalendarRecord::Deleter);
+
+ picojson::value record_obj = picojson::value(picojson::object());
+ result = CalendarItem::ToJson(type, record_ptr.get(),
+ &record_obj.get<picojson::object>());
+ if (result.IsError()) {
+ LoggerE("error occured: %s", result.message().c_str());
+ return;
+ }
+
+ if (status == CALENDAR_RECORD_MODIFIED_STATUS_INSERTED) {
+ added.push_back(record_obj);
+ } else {
+ updated.push_back(record_obj);
}
calendar_list_next(list);
ret = calendar_db_get_current_version(&updated_version);
if (CALENDAR_ERROR_NONE != ret) {
LoggerE("Can't get new version");
- throw UnknownException("Can't get new version");
+ return;
}
current_db_version_ = updated_version;
CalendarInstance::GetInstance().PostMessage(response.serialize().c_str());
}
-void Calendar::ErrorChecker(int errorCode) {
+PlatformResult Calendar::ErrorChecker(int errorCode) {
if (errorCode != CALENDAR_ERROR_NONE)
- throw UnknownException("exception occured");
+ return PlatformResult(ErrorCode::UNKNOWN_ERR, "exception occured");
+
+ return PlatformResult(ErrorCode::NO_ERROR);
}
} // namespace calendar
#include <memory>
#include "common/picojson.h"
+#include "common/platform_result.h"
namespace extension {
namespace calendar {
* {status: 'success', item: {}}
* @endcode
*/
- void Get(const JsonObject& args, JsonObject& out);
+ common::PlatformResult Get(const JsonObject& args, JsonObject& out);
/**
* Signature: @code void add(item); @endcode
* {status: 'success'}
* @endcode
*/
- void Add(const JsonObject& args, JsonObject& out);
+ common::PlatformResult Add(const JsonObject& args, JsonObject& out);
/**
* Signature: @code void addBatch(items, successCallback, errorCallback);
* {status: 'success', result: items}
* @endcode
*/
- void AddBatch(const JsonObject& args, JsonArray& array);
+ common::PlatformResult AddBatch(const JsonObject& args, JsonArray& array);
/**
* Signature: @code void update(item, updateAllInstances); @endcode
* {status: 'success'}
* @endcode
*/
- void Update(const JsonObject& args, JsonObject& out);
+ common::PlatformResult Update(const JsonObject& args, JsonObject& out);
/**
* Signature: @code void updateBatch(items, successCallback, errorCallback,
* {status: 'success'}
* @endcode
*/
- void UpdateBatch(const JsonObject& args, JsonArray& array);
+ common::PlatformResult UpdateBatch(const JsonObject& args, JsonArray& array);
/**
* Signature: @code void remove(item); @endcode
* {status: 'success'}
* @endcode
*/
- void Remove(const JsonObject& args, JsonObject& out);
+ common::PlatformResult Remove(const JsonObject& args, JsonObject& out);
/**
* Signature: @code void removeBatch(items, successCallback, errorCallback);
* {status: 'success'}
* @endcode
*/
- void RemoveBatch(const JsonObject& args, JsonArray& array);
+ common::PlatformResult RemoveBatch(const JsonObject& args, JsonArray& array);
/**
* Signature: @code void find(successCallback, errorCallback, filter,
* {status: 'success', result: {calendarItemsArray}}
* @endcode
*/
- void Find(const JsonObject& args, JsonArray& array);
+ common::PlatformResult Find(const JsonObject& args, JsonArray& array);
/**
* Signature: @code void addChangeListener(successCallback); @endcode
* {status: 'success'}
* @endcode
*/
- void AddChangeListener(const JsonObject& args,
- JsonObject& out);
+ common::PlatformResult AddChangeListener(const JsonObject& args,
+ JsonObject& out);
/**
* Signature: @code void removeChangeListener(); @endcode
* {status: 'success'}
* @endcode
*/
- void RemoveChangeListener(const JsonObject& args,
- JsonObject& out);
+ common::PlatformResult RemoveChangeListener(const JsonObject& args,
+ JsonObject& out);
static Calendar& GetInstance();
static std::map<std::string, std::string> listeners_registered_;
static int current_db_version_;
static void ChangeCallback(const char* view_uri, void* user_data);
- void ErrorChecker(int errorCode);
+ common::PlatformResult ErrorChecker(int errorCode);
};
} // namespace calendar
void CalendarInstance::CalendarGet(const JsonValue& args, JsonObject& out) {
JsonValue val{JsonObject{}};
- Calendar::GetInstance().Get(common::JsonCast<JsonObject>(args),
- val.get<JsonObject>());
- ReportSuccess(val, out);
+
+ PlatformResult status = Calendar::GetInstance().Get(
+ common::JsonCast<JsonObject>(args), val.get<JsonObject>());
+
+ if (status.IsSuccess())
+ ReportSuccess(val, out);
+ else
+ ReportError(status, &out);
}
void CalendarInstance::CalendarAdd(const JsonValue& args, JsonObject& out) {
JsonValue val{JsonObject{}};
- Calendar::GetInstance().Add(common::JsonCast<JsonObject>(args),
- val.get<JsonObject>());
- ReportSuccess(val, out);
+
+ PlatformResult status = Calendar::GetInstance().Add(
+ common::JsonCast<JsonObject>(args), val.get<JsonObject>());
+
+ if (status.IsSuccess())
+ ReportSuccess(val, out);
+ else
+ ReportError(status, &out);
}
void CalendarInstance::CalendarAddBatch(const JsonValue& args,
JsonObject& out) {
const double callback_id = args.get("callbackId").get<double>();
- auto get = [=](const std::shared_ptr<JsonValue> & response)->void {
- try {
- JsonValue result = JsonValue(JsonArray());
- Calendar::GetInstance().AddBatch(common::JsonCast<JsonObject>(args),
- result.get<JsonArray>());
+ auto get = [=](const std::shared_ptr<JsonValue>& response) -> void {
+ JsonValue result = JsonValue(JsonArray());
+ PlatformResult status = Calendar::GetInstance().AddBatch(
+ common::JsonCast<JsonObject>(args), result.get<JsonArray>());
+
+ if (status.IsSuccess())
ReportSuccess(result, response->get<picojson::object>());
- }
- catch (const PlatformException& e) {
- ReportError(e, response->get<picojson::object>());
- }
+ else
+ ReportError(status, &response->get<picojson::object>());
};
- auto get_response = [ callback_id, this ](const std::shared_ptr<JsonValue> &
- response)->void {
+ auto get_response =
+ [callback_id, this](const std::shared_ptr<JsonValue>& response) -> void {
picojson::object& obj = response->get<picojson::object>();
obj.insert(std::make_pair("callbackId", callback_id));
LoggerD("callback is %s", response->serialize().c_str());
void CalendarInstance::CalendarUpdate(const JsonValue& args, JsonObject& out) {
JsonValue val{JsonObject{}};
- Calendar::GetInstance().Update(common::JsonCast<JsonObject>(args),
- val.get<JsonObject>());
- ReportSuccess(val, out);
+
+ PlatformResult status = Calendar::GetInstance().Update(
+ common::JsonCast<JsonObject>(args), val.get<JsonObject>());
+
+ if (status.IsSuccess())
+ ReportSuccess(val, out);
+ else
+ ReportError(status, &out);
}
void CalendarInstance::CalendarUpdateBatch(const JsonValue& args,
JsonObject& out) {
const double callback_id = args.get("callbackId").get<double>();
- auto get = [=](const std::shared_ptr<JsonValue> & response)->void {
- try {
- JsonValue result = JsonValue(JsonArray());
- Calendar::GetInstance().UpdateBatch(common::JsonCast<JsonObject>(args),
- result.get<JsonArray>());
+ auto get = [=](const std::shared_ptr<JsonValue>& response) -> void {
+ JsonValue result = JsonValue(JsonArray());
+ PlatformResult status = Calendar::GetInstance().UpdateBatch(
+ common::JsonCast<JsonObject>(args), result.get<JsonArray>());
+
+ if (status.IsSuccess())
ReportSuccess(result, response->get<picojson::object>());
- }
- catch (const PlatformException& e) {
- ReportError(e, response->get<picojson::object>());
- }
+ else
+ ReportError(status, &response->get<picojson::object>());
};
- auto get_response = [ callback_id, this ](const std::shared_ptr<JsonValue> &
- response)->void {
+ auto get_response =
+ [callback_id, this](const std::shared_ptr<JsonValue>& response) -> void {
picojson::object& obj = response->get<picojson::object>();
obj.insert(std::make_pair("callbackId", callback_id));
LoggerD("callback is %s", response->serialize().c_str());
void CalendarInstance::CalendarRemove(const JsonValue& args, JsonObject& out) {
JsonValue val{JsonObject{}};
- Calendar::GetInstance().Remove(common::JsonCast<JsonObject>(args),
- val.get<JsonObject>());
- ReportSuccess(out);
+
+ PlatformResult status = Calendar::GetInstance().Remove(
+ common::JsonCast<JsonObject>(args), val.get<JsonObject>());
+
+ if (status.IsSuccess())
+ ReportSuccess(out);
+ else
+ ReportError(status, &val.get<JsonObject>());
}
void CalendarInstance::CalendarRemoveBatch(const JsonValue& args,
JsonObject& out) {
const double callback_id = args.get("callbackId").get<double>();
- auto get = [=](const std::shared_ptr<JsonValue> & response)->void {
- try {
- JsonValue result = JsonValue(JsonArray());
- Calendar::GetInstance().RemoveBatch(common::JsonCast<JsonObject>(args),
- result.get<JsonArray>());
+ auto get = [=](const std::shared_ptr<JsonValue>& response) -> void {
+ JsonValue result = JsonValue(JsonArray());
+ PlatformResult status = Calendar::GetInstance().RemoveBatch(
+ common::JsonCast<JsonObject>(args), result.get<JsonArray>());
+
+ if (status.IsSuccess())
ReportSuccess(result, response->get<picojson::object>());
- }
- catch (const PlatformException& e) {
- ReportError(e, response->get<picojson::object>());
- }
+ else
+ ReportError(status, &response->get<picojson::object>());
};
- auto get_response = [ callback_id, this ](const std::shared_ptr<JsonValue> &
- response)->void {
+ auto get_response =
+ [callback_id, this](const std::shared_ptr<JsonValue>& response) -> void {
picojson::object& obj = response->get<picojson::object>();
obj.insert(std::make_pair("callbackId", callback_id));
LoggerD("callback is %s", response->serialize().c_str());
void CalendarInstance::CalendarFind(const JsonValue& args, JsonObject& out) {
const double callback_id = args.get("callbackId").get<double>();
- auto get = [=](const std::shared_ptr<JsonValue> & response)->void {
- try {
- JsonValue result = JsonValue(JsonArray());
- Calendar::GetInstance().Find(common::JsonCast<JsonObject>(args),
- result.get<JsonArray>());
+ auto get = [=](const std::shared_ptr<JsonValue>& response) -> void {
+ JsonValue result = JsonValue(JsonArray());
+ PlatformResult status = Calendar::GetInstance().Find(
+ common::JsonCast<JsonObject>(args), result.get<JsonArray>());
+
+ if (status.IsSuccess())
ReportSuccess(result, response->get<picojson::object>());
- }
- catch (const PlatformException& e) {
- ReportError(e, response->get<picojson::object>());
- }
+ else
+ ReportError(status, &response->get<picojson::object>());
};
- auto get_response = [ callback_id, this ](const std::shared_ptr<JsonValue> &
- response)->void {
+ auto get_response =
+ [callback_id, this](const std::shared_ptr<JsonValue>& response) -> void {
picojson::object& obj = response->get<picojson::object>();
obj.insert(std::make_pair("callbackId", callback_id));
LoggerD("callback isssssss %s", response->serialize().c_str());
void CalendarInstance::CalendarAddChangeListener(const JsonValue& args,
JsonObject& out) {
JsonValue val{JsonObject{}};
- Calendar::GetInstance().AddChangeListener(common::JsonCast<JsonObject>(args),
- val.get<JsonObject>());
- ReportSuccess(out);
+
+ PlatformResult status = Calendar::GetInstance().AddChangeListener(
+ common::JsonCast<JsonObject>(args), val.get<JsonObject>());
+
+ if (status.IsSuccess())
+ ReportSuccess(out);
+ else
+ ReportError(status, &val.get<JsonObject>());
}
void CalendarInstance::CalendarRemoveChangeListener(const JsonValue& args,
JsonObject& out) {
JsonValue val{JsonObject{}};
- Calendar::GetInstance().RemoveChangeListener(
+
+ PlatformResult status = Calendar::GetInstance().RemoveChangeListener(
common::JsonCast<JsonObject>(args), val.get<JsonObject>());
- ReportSuccess(out);
+
+ if (status.IsSuccess())
+ ReportSuccess(out);
+ else
+ ReportError(status, &val.get<JsonObject>());
}
// CalendarManager
void CalendarInstance::CalendarManagerAddCalendar(const JsonValue& args,
JsonObject& out) {
JsonValue val{JsonObject{}};
- CalendarManager::GetInstance().AddCalendar(common::JsonCast<JsonObject>(args),
- val.get<JsonObject>());
- ReportSuccess(val, out);
+ PlatformResult status = CalendarManager::GetInstance().AddCalendar(
+ common::JsonCast<JsonObject>(args), val.get<JsonObject>());
+
+ if (status.IsSuccess())
+ ReportSuccess(val, out);
+ else
+ ReportError(status, &out);
}
void CalendarInstance::CalendarManagerGetCalendar(const JsonValue& args,
JsonObject& out) {
JsonValue val{JsonObject{}};
- CalendarManager::GetInstance().GetCalendar(common::JsonCast<JsonObject>(args),
+ PlatformResult status = CalendarManager::GetInstance().GetCalendar(common::JsonCast<JsonObject>(args),
val.get<JsonObject>());
- ReportSuccess(val, out);
+
+ if (status.IsSuccess())
+ ReportSuccess(val, out);
+ else
+ ReportError(status, &out);
}
void CalendarInstance::CalendarManagerGetCalendars(const JsonValue& args,
JsonObject& out) {
const double callback_id = args.get("callbackId").get<double>();
- auto get = [=](const std::shared_ptr<JsonValue> & response)->void {
- try {
- JsonValue result = JsonValue(JsonArray());
- CalendarManager::GetInstance().GetCalendars(
- common::JsonCast<JsonObject>(args), result.get<JsonArray>());
+ auto get = [=](const std::shared_ptr<JsonValue>& response) -> void {
+ JsonValue result = JsonValue(JsonArray());
+
+ PlatformResult status = CalendarManager::GetInstance().GetCalendars(
+ common::JsonCast<JsonObject>(args), result.get<JsonArray>());
+
+ if (status.IsSuccess())
ReportSuccess(result, response->get<picojson::object>());
- }
- catch (const PlatformException& e) {
- ReportError(e, response->get<picojson::object>());
- }
+ else
+ ReportError(status, &response->get<JsonObject>());
};
auto get_response = [ callback_id, this ](const std::shared_ptr<JsonValue> &
}
void CalendarInstance::CalendarManagerRemoveCalendar(const JsonValue& args,
- JsonObject& out) {
+ JsonObject& out) {
JsonValue val{JsonObject{}};
- CalendarManager::GetInstance().RemoveCalendar(
+ PlatformResult status = CalendarManager::GetInstance().RemoveCalendar(
common::JsonCast<JsonObject>(args), val.get<JsonObject>());
- ReportSuccess(val, out);
+
+ if (status.IsSuccess())
+ ReportSuccess(val, out);
+ else
+ ReportError(status, &out);
}
} // namespace calendar
{"YEARLY", CALENDAR_RECURRENCE_YEARLY}}}};
PlatformEnumReverseMap CalendarItem::platform_enum_reverse_map_ = {};
-CalendarRecordPtr CalendarItem::Create(int type) {
- LoggerD("enter");
+PlatformResult CalendarItem::Create(int type, calendar_record_h* handle) {
+ std::string value_str;
+ PlatformResult status = CalendarRecord::TypeToUri(type, &value_str);
+ if (status.IsError()) return status;
- return CalendarRecord::Create(CalendarRecord::TypeToUri(type));
+ return CalendarRecord::Create(value_str.c_str(), handle);
}
-void CalendarItem::Remove(int type, int id) {
- LoggerD("enter");
+PlatformResult CalendarItem::Remove(int type, int id) {
+ std::string view_uri;
+ PlatformResult status = CalendarRecord::TypeToUri(type, &view_uri);
+ if (status.IsError()) return status;
- const char* view_uri = CalendarRecord::TypeToUri(type);
- CalendarRecordPtr record = GetById(id, view_uri);
+ calendar_record_h handle = nullptr;
+ status = GetById(id, view_uri.c_str(), &handle);
+ if (status.IsError()) return status;
+
+ CalendarRecordPtr record = CalendarRecordPtr(handle, CalendarRecord::Deleter);
if (type == CALENDAR_BOOK_TYPE_EVENT) {
- const std::string& rid = GetString(type, record.get(), "recurrence_id");
+ std::string rid;
+ PlatformResult status =
+ GetString(type, record.get(), "recurrence_id", &rid);
+ if (status.IsError()) return status;
+
if (rid.length() > 0) {
// @todo remove all occurrences
- return;
+ return PlatformResult(ErrorCode::UNKNOWN_ERR,
+ "TODO: remove all occurrences");
}
}
- if (CALENDAR_ERROR_NONE != calendar_db_delete_record(view_uri, id)) {
+ if (CALENDAR_ERROR_NONE != calendar_db_delete_record(view_uri.c_str(), id)) {
LOGE("Calendar record delete error");
- throw common:: UnknownException("Record deletion error");
+ return PlatformResult(ErrorCode::UNKNOWN_ERR, "Record deletion error");
}
+
+ return PlatformResult(ErrorCode::NO_ERROR);
}
-unsigned int CalendarItem::GetPlatformProperty(int type,
- const std::string& property) {
+PlatformResult CalendarItem::GetPlatformProperty(int type,
+ const std::string& property,
+ unsigned int* value) {
if (platform_property_map_.find(property) == platform_property_map_.end()) {
- throw common::UnknownException(std::string("Undefined property ") + property);
+ return PlatformResult(ErrorCode::UNKNOWN_ERR,
+ std::string("Undefined property ") + property);
}
auto prop = platform_property_map_.at(property);
if (prop.find(type) == prop.end()) {
LoggerD("Property %s not defined for type %d", property.c_str(), type);
- return -1u;
+ return PlatformResult(
+ ErrorCode::INVALID_VALUES_ERR,
+ std::string("Property %s not defined for type ", property.c_str()) +
+ std::to_string(type));
}
- return prop.at(type);
+ *value = prop.at(type);
+
+ return PlatformResult(ErrorCode::NO_ERROR);
}
-int CalendarItem::StringToPlatformEnum(const std::string& field,
- const std::string& value) {
+PlatformResult CalendarItem::StringToPlatformEnum(const std::string& field,
+ const std::string& value,
+ int* platform_enum) {
auto iter = platform_enum_map_.find(field);
if (iter == platform_enum_map_.end()) {
- throw common::UnknownException(std::string("Undefined platform enum type ") +
- field);
+ return PlatformResult(ErrorCode::UNKNOWN_ERR,
+ std::string("Undefined platform enum type ") + field);
}
auto def = platform_enum_map_.at(field);
auto def_iter = def.find(value);
if (def_iter != def.end()) {
- return def_iter->second;
+ *platform_enum = def_iter->second;
+ return PlatformResult(ErrorCode::NO_ERROR);
}
// default value - if any
def_iter = def.find("_DEFAULT");
if (def_iter != def.end()) {
- return def_iter->second;
+ *platform_enum = def_iter->second;
+ return PlatformResult(ErrorCode::NO_ERROR);
}
std::string message =
"Platform enum value " + value + " not found for " + field;
- throw InvalidValuesException(message);
+ return PlatformResult(ErrorCode::INVALID_VALUES_ERR, message);
}
-std::string CalendarItem::PlatformEnumToString(const std::string& field,
- int value) {
+PlatformResult CalendarItem::PlatformEnumToString(const std::string& field,
+ int value,
+ std::string* platform_str) {
// @todo can be replaced by Boost.Bimap
if (platform_enum_reverse_map_.empty()) {
for (auto& def : platform_enum_map_) {
auto iter = platform_enum_reverse_map_.find(field);
if (iter == platform_enum_reverse_map_.end()) {
- throw common::UnknownException(std::string("Undefined platform enum type ") +
- field);
+ return PlatformResult(ErrorCode::UNKNOWN_ERR,
+ std::string("Undefined platform enum type ") + field);
}
auto def = platform_enum_reverse_map_.at(field);
auto def_iter = def.find(value);
if (def_iter != def.end()) {
- return def_iter->second;
+ *platform_str = def_iter->second;
+ return PlatformResult(ErrorCode::NO_ERROR);
}
std::string message = "Platform enum value " + std::to_string(value) +
" not found for " + field;
- throw InvalidValuesException(message);
+ return PlatformResult(ErrorCode::INVALID_VALUES_ERR, message);
}
-void CalendarItem::SetString(int type, calendar_record_h rec,
- const std::string& property,
- const picojson::object& in, bool optional) {
+PlatformResult CalendarItem::SetString(int type, calendar_record_h rec,
+ const std::string& property,
+ const picojson::object& in,
+ bool optional) {
LoggerD("set: %s", property.c_str());
if (optional && IsNull(in, property.c_str())) {
- return;
+ return PlatformResult(ErrorCode::NO_ERROR);
}
const std::string& value =
common::FromJson<std::string>(in, property.c_str());
- SetString(type, rec, property, value);
+ return SetString(type, rec, property, value);
}
-void CalendarItem::SetString(int type, calendar_record_h rec,
- const std::string& property,
- const std::string& value) {
+PlatformResult CalendarItem::SetString(int type, calendar_record_h rec,
+ const std::string& property,
+ const std::string& value) {
LoggerD("set: %s", property.c_str());
- unsigned int prop = GetPlatformProperty(type, property);
+ unsigned int prop;
+ PlatformResult status = GetPlatformProperty(type, property, &prop);
+ if (status.IsError()) return status;
if (prop != -1u) {
- CalendarRecord::SetString(rec, prop, value);
+ PlatformResult status = CalendarRecord::SetString(rec, prop, value);
+ if (status.IsError()) return status;
}
+
+ return PlatformResult(ErrorCode::NO_ERROR);
}
-std::string CalendarItem::GetString(int type, calendar_record_h rec,
- const std::string& property) {
+PlatformResult CalendarItem::GetString(int type, calendar_record_h rec,
+ const std::string& property,
+ std::string* value) {
LoggerD("get: %s", property.c_str());
- return CalendarRecord::GetString(rec, GetPlatformProperty(type, property));
+ unsigned int prop;
+ PlatformResult status = GetPlatformProperty(type, property, &prop);
+ if (status.IsError()) return status;
+
+ return CalendarRecord::GetString(rec, prop, value);
}
-void CalendarItem::SetInt(int type, calendar_record_h rec,
- const std::string& property, const picojson::object& in,
- bool optional) {
+PlatformResult CalendarItem::SetInt(int type, calendar_record_h rec,
+ const std::string& property,
+ const picojson::object& in, bool optional) {
LoggerD("set: %s", property.c_str());
if (optional && IsNull(in, property.c_str())) {
- return;
+ return PlatformResult(ErrorCode::NO_ERROR);
}
int value = common::FromJson<double>(in, property.c_str());
- SetInt(type, rec, property, value);
+ return SetInt(type, rec, property, value);
}
-void CalendarItem::SetInt(int type, calendar_record_h rec,
- const std::string& property, int value) {
+PlatformResult CalendarItem::SetInt(int type, calendar_record_h rec,
+ const std::string& property, int value) {
LoggerD("set: %s", property.c_str());
- unsigned int prop = GetPlatformProperty(type, property);
+ unsigned int prop;
+ PlatformResult status = GetPlatformProperty(type, property, &prop);
+ if (status.IsError()) return status;
- if (prop != -1u) {
- CalendarRecord::SetInt(rec, prop, value);
- }
+ return CalendarRecord::SetInt(rec, prop, value);
}
-int CalendarItem::GetInt(int type, calendar_record_h rec,
- const std::string& property) {
+PlatformResult CalendarItem::GetInt(int type, calendar_record_h rec,
+ const std::string& property, int* value) {
LoggerD("get: %s", property.c_str());
- return CalendarRecord::GetInt(rec, GetPlatformProperty(type, property));
+ unsigned int prop;
+ PlatformResult status = GetPlatformProperty(type, property, &prop);
+ if (status.IsError()) return status;
+
+ return CalendarRecord::GetInt(rec, prop, value);
}
-void CalendarItem::SetEnum(int type, calendar_record_h rec,
- const std::string& property, const picojson::object& in,
- const std::string& enum_name) {
+PlatformResult CalendarItem::SetEnum(int type, calendar_record_h rec,
+ const std::string& property,
+ const picojson::object& in,
+ const std::string& enum_name) {
std::string value = common::FromJson<std::string>(in, property.c_str());
- SetInt(type, rec, property, StringToPlatformEnum(enum_name, value));
+
+ int value_int;
+ PlatformResult status = StringToPlatformEnum(enum_name, value, &value_int);
+ if (status.IsError()) return status;
+
+ status = SetInt(type, rec, property, value_int);
+ if (status.IsError()) return status;
+
+ return PlatformResult(ErrorCode::NO_ERROR);
}
-void CalendarItem::SetEnum(calendar_record_h rec, unsigned int property,
- const std::string& enum_name,
- const std::string& value) {
- CalendarRecord::SetInt(rec, property, StringToPlatformEnum(enum_name, value));
+PlatformResult CalendarItem::SetEnum(calendar_record_h rec,
+ unsigned int property,
+ const std::string& enum_name,
+ const std::string& value) {
+ int value_int;
+ PlatformResult status = StringToPlatformEnum(enum_name, value, &value_int);
+ if (status.IsError()) return status;
+
+ status = CalendarRecord::SetInt(rec, property, value_int);
+ if (status.IsError()) return status;
+
+ return PlatformResult(ErrorCode::NO_ERROR);
}
-std::string CalendarItem::GetEnum(int type, calendar_record_h rec,
- const std::string& property,
- const std::string& enum_name) {
- return PlatformEnumToString(enum_name, GetInt(type, rec, property));
+PlatformResult CalendarItem::GetEnum(int type, calendar_record_h rec,
+ const std::string& property,
+ const std::string& enum_name,
+ std::string* enum_str) {
+ int value;
+ PlatformResult status = GetInt(type, rec, property, &value);
+ if (status.IsError()) return status;
+
+ return PlatformEnumToString(enum_name, value, enum_str);
}
-std::string CalendarItem::GetEnum(calendar_record_h rec, unsigned int property,
- const std::string& enum_name) {
- return PlatformEnumToString(enum_name, CalendarRecord::GetInt(rec, property));
+PlatformResult CalendarItem::GetEnum(calendar_record_h rec,
+ unsigned int property,
+ const std::string& enum_name,
+ std::string* enum_str) {
+ int value;
+ PlatformResult status = CalendarRecord::GetInt(rec, property, &value);
+ if (status.IsError()) return status;
+
+ return PlatformEnumToString(enum_name, value, enum_str);
}
-void CalendarItem::SetDouble(int type, calendar_record_h rec,
+PlatformResult CalendarItem::SetDouble(int type, calendar_record_h rec,
const std::string& property, double value) {
LoggerD("set: %s", property.c_str());
- unsigned int prop = GetPlatformProperty(type, property);
+ unsigned int prop;
+ PlatformResult status = GetPlatformProperty(type, property, &prop);
+ if (status.IsError()) return status;
- if (prop != -1u) {
- int ret = calendar_record_set_double(rec, prop, value);
+ int ret = calendar_record_set_double(rec, prop, value);
- if (CALENDAR_ERROR_NONE != ret) {
- LoggerW("Can't set double value to record: %d", ret);
- throw common::UnknownException("Set double to record failed.");
- }
+ if (CALENDAR_ERROR_NONE != ret) {
+ LoggerW("Can't set double value to record: %d", ret);
+ return PlatformResult(ErrorCode::UNKNOWN_ERR, "Set double to record failed.");
}
+
+ return PlatformResult(ErrorCode::NO_ERROR);
}
-double CalendarItem::GetDouble(int type, calendar_record_h rec,
- const std::string& property) {
+PlatformResult CalendarItem::GetDouble(int type, calendar_record_h rec,
+ const std::string& property,
+ double* value) {
LoggerD("get: %s", property.c_str());
- double value;
- int ret = calendar_record_get_double(rec, GetPlatformProperty(type, property),
- &value);
+ unsigned int prop;
+ PlatformResult status = GetPlatformProperty(type, property, &prop);
+ if (status.IsError()) return status;
+
+ int ret = calendar_record_get_double(rec, prop, value);
if (CALENDAR_ERROR_NONE != ret) {
LoggerW("Can't get double value form record: %d", ret);
- throw common::UnknownException("Get int from record failed.");
+ return PlatformResult(ErrorCode::UNKNOWN_ERR,
+ "Get int from record failed.");
}
- return value;
+ return PlatformResult(ErrorCode::NO_ERROR);
}
-void CalendarItem::SetCaltime(int type, calendar_record_h rec,
- const std::string& property,
- calendar_time_s value, bool throw_on_error) {
- LoggerD("enter");
+PlatformResult CalendarItem::SetCaltime(int type, calendar_record_h rec,
+ const std::string& property,
+ calendar_time_s value,
+ bool throw_on_error) {
+ unsigned int prop;
+ PlatformResult status = GetPlatformProperty(type, property, &prop);
+ if (status.IsError()) return status;
- unsigned int prop = GetPlatformProperty(type, property);
-
- if (prop != -1u) {
- SetCaltime(rec, prop, value, throw_on_error);
- }
+ return SetCaltime(rec, prop, value, throw_on_error);
}
-void CalendarItem::SetCaltime(calendar_record_h rec, unsigned int property,
- calendar_time_s value, bool throw_on_error) {
+PlatformResult CalendarItem::SetCaltime(calendar_record_h rec,
+ unsigned int property,
+ calendar_time_s value,
+ bool throw_on_error) {
int ret = calendar_record_set_caltime(rec, property, value);
if (CALENDAR_ERROR_NONE != ret) {
LoggerW("Can't set caltime value to record: %d", ret);
if (throw_on_error) {
- throw common::UnknownException("Set caltime to record failed.");
+ return PlatformResult(ErrorCode::UNKNOWN_ERR,
+ "Set caltime to record failed.");
}
}
+
+ return PlatformResult(ErrorCode::NO_ERROR);
}
-calendar_time_s CalendarItem::GetCaltime(int type, calendar_record_h rec,
- const std::string& property,
- bool throw_on_error) {
+PlatformResult CalendarItem::GetCaltime(int type, calendar_record_h rec,
+ const std::string& property,
+ calendar_time_s* cal_time,
+ bool throw_on_error) {
LoggerD("get: %s", property.c_str());
- unsigned int prop = GetPlatformProperty(type, property);
+ unsigned int prop;
+ PlatformResult status = GetPlatformProperty(type, property, &prop);
+ if (status.IsError()) return status;
- return GetCaltime(rec, prop, throw_on_error);
+ return GetCaltime(rec, prop, cal_time, throw_on_error);
}
-calendar_time_s CalendarItem::GetCaltime(calendar_record_h rec,
- unsigned int property,
- bool throw_on_error) {
- calendar_time_s cal;
-
+PlatformResult CalendarItem::GetCaltime(calendar_record_h rec,
+ unsigned int property,
+ calendar_time_s* cal_time,
+ bool throw_on_error) {
if (property != -1u) {
- int ret = calendar_record_get_caltime(rec, property, &cal);
+ int ret = calendar_record_get_caltime(rec, property, cal_time);
if (CALENDAR_ERROR_NONE != ret) {
LoggerW("Can't get calendar_time value form record: %d", ret);
if (throw_on_error) {
- throw common::UnknownException(
- "Can't get calendar_time value form record");
+ return PlatformResult(ErrorCode::UNKNOWN_ERR,
+ "Can't get calendar_time value form record");
}
}
}
- return cal;
+ return PlatformResult(ErrorCode::NO_ERROR);
}
-void CalendarItem::SetLli(calendar_record_h rec, unsigned int property,
- long long int value, bool throw_on_error) {
-
+PlatformResult CalendarItem::SetLli(calendar_record_h rec,
+ unsigned int property, long long int value,
+ bool throw_on_error) {
int ret = calendar_record_set_lli(rec, property, value);
if (CALENDAR_ERROR_NONE != ret) {
LoggerW("Can't set long long int value to record: %d", ret);
if (throw_on_error) {
- throw common::UnknownException("Set long long int to record failed.");
+ return PlatformResult(ErrorCode::UNKNOWN_ERR,
+ "Set long long int to record failed.");
}
}
+
+ return PlatformResult(ErrorCode::NO_ERROR);
}
-long long int CalendarItem::GetLli(int type, calendar_record_h rec,
- const std::string& property) {
+PlatformResult CalendarItem::GetLli(int type, calendar_record_h rec,
+ const std::string& property,
+ long long int* lli) {
LoggerD("get: %s", property.c_str());
- return GetLli(rec, GetPlatformProperty(type, property));
+ unsigned int prop;
+ PlatformResult status = GetPlatformProperty(type, property, &prop);
+ if (status.IsError()) return status;
+
+ return GetLli(rec, prop, lli);
}
-long long int CalendarItem::GetLli(calendar_record_h rec, unsigned int property,
- bool throw_on_error) {
- long long int value;
- int ret = calendar_record_get_lli(rec, property, &value);
+PlatformResult CalendarItem::GetLli(calendar_record_h rec,
+ unsigned int property, long long int* value,
+ bool throw_on_error) {
+ int ret = calendar_record_get_lli(rec, property, value);
if (CALENDAR_ERROR_NONE != ret) {
LoggerW("Can't get lli value form record: %d", ret);
if (throw_on_error) {
- throw common::UnknownException("Get lli from record failed.");
+ return PlatformResult(ErrorCode::UNKNOWN_ERR,
+ "Get lli from record failed.");
}
}
- return value;
+ return PlatformResult(ErrorCode::NO_ERROR);
}
Date CalendarItem::DateFromJson(const picojson::object& in) {
return DateFromJson(common::FromJson<picojson::object>(in, obj_name));
}
-picojson::value CalendarItem::DateToJson(Date date) {
- LoggerD("timestamp: %lld", date.utc_timestamp_);
+picojson::value CalendarItem::DateToJson(Date* date) {
+ LoggerD("timestamp: %lld", date->utc_timestamp_);
picojson::value date_val = picojson::value(picojson::object());
picojson::object& date_obj = date_val.get<picojson::object>();
date_obj["UTCTimestamp"] =
- picojson::value(static_cast<double>(date.utc_timestamp_));
- date_obj["year"] = picojson::value(static_cast<double>(date.year_));
- date_obj["month"] = picojson::value(static_cast<double>(date.month_));
- date_obj["day"] = picojson::value(static_cast<double>(date.day_));
- date_obj["timezone"] = picojson::value(date.time_zone_);
+ picojson::value(static_cast<double>(date->utc_timestamp_));
+ date_obj["year"] = picojson::value(static_cast<double>(date->year_));
+ date_obj["month"] = picojson::value(static_cast<double>(date->month_));
+ date_obj["day"] = picojson::value(static_cast<double>(date->day_));
+ date_obj["timezone"] = picojson::value(date->time_zone_);
return date_val;
}
-void CalendarItem::CategoriesFromJson(int type, calendar_record_h rec,
- const picojson::array& value) {
+PlatformResult CalendarItem::CategoriesFromJson(int type, calendar_record_h rec,
+ const picojson::array& value) {
std::string categories = "";
for (auto iter = value.begin(); iter != value.end(); ++iter) {
if (iter == value.begin()) {
}
}
- SetString(type, rec, "categories", categories);
+ PlatformResult status = SetString(type, rec, "categories", categories);
+ if (status.IsError()) return status;
+
+ return PlatformResult(ErrorCode::NO_ERROR);
}
-picojson::array CalendarItem::CategoriesToJson(int type, calendar_record_h rec) {
- LoggerD("enter");
+PlatformResult CalendarItem::CategoriesToJson(int type, calendar_record_h rec,
+ picojson::array* value) {
+ std::string categories;
+ PlatformResult status = GetString(type, rec, "categories", &categories);
+ if (status.IsError()) return status;
- std::string categories = GetString(type, rec, "categories");
+ *value = StringToArray(categories);
- return StringToArray(categories);
+ return PlatformResult(ErrorCode::NO_ERROR);
}
-void CalendarItem::AttendeesFromJson(int type, calendar_record_h rec,
- const picojson::array& value) {
- LoggerD("enter");
-
+PlatformResult CalendarItem::AttendeesFromJson(int type, calendar_record_h rec,
+ const picojson::array& value) {
// Remove the preset child attendees before adding new ones.
unsigned int property;
if (type == CALENDAR_BOOK_TYPE_EVENT) {
int ret = calendar_record_create(_calendar_attendee._uri, &attendee);
if (CALENDAR_ERROR_NONE != ret) {
LoggerE("Fail to create attendee record, error code: %d", ret);
- throw common::UnknownException("Fail to create attendee record");
+ return PlatformResult(ErrorCode::UNKNOWN_ERR, "Fail to create attendee record");
}
- CalendarRecord::SetString(attendee, _calendar_attendee.email,
- common::FromJson<std::string>(obj, "uri"));
+ PlatformResult status =
+ CalendarRecord::SetString(attendee, _calendar_attendee.email,
+ common::FromJson<std::string>(obj, "uri"));
+ if (status.IsError()) return status;
if (!IsNull(obj, "name")) {
- CalendarRecord::SetString(attendee, _calendar_attendee.name,
- common::FromJson<std::string>(obj, "name"));
+ status =
+ CalendarRecord::SetString(attendee, _calendar_attendee.name,
+ common::FromJson<std::string>(obj, "name"));
+ if (status.IsError()) return status;
}
- SetEnum(attendee, _calendar_attendee.role, kAttendeeRole,
- common::FromJson<std::string>(obj, "role"));
+ status = SetEnum(attendee, _calendar_attendee.role, kAttendeeRole,
+ common::FromJson<std::string>(obj, "role"));
+ if (status.IsError()) return status;
- SetEnum(attendee, _calendar_attendee.status, kAttendeeStatus,
- common::FromJson<std::string>(obj, "status"));
+ status = SetEnum(attendee, _calendar_attendee.status, kAttendeeStatus,
+ common::FromJson<std::string>(obj, "status"));
+ if (status.IsError()) return status;
- CalendarRecord::SetInt(attendee, _calendar_attendee.rsvp,
- common::FromJson<bool>(obj, "RSVP"));
+ status = CalendarRecord::SetInt(attendee, _calendar_attendee.rsvp,
+ common::FromJson<bool>(obj, "RSVP"));
+ if (status.IsError()) return status;
- SetEnum(attendee, _calendar_attendee.cutype, kAttendeeType,
- common::FromJson<std::string>(obj, "type"));
+ status = SetEnum(attendee, _calendar_attendee.cutype, kAttendeeType,
+ common::FromJson<std::string>(obj, "type"));
+ if (status.IsError()) return status;
if (!IsNull(obj, "group")) {
- CalendarRecord::SetString(attendee, _calendar_attendee.group,
+ status = CalendarRecord::SetString(attendee, _calendar_attendee.group,
common::FromJson<std::string>(obj, "group"));
+ if (status.IsError()) return status;
}
if (!IsNull(obj, "delegatorURI")) {
- CalendarRecord::SetString(
+ status = CalendarRecord::SetString(
attendee, _calendar_attendee.delegator_uri,
common::FromJson<std::string>(obj, "delegatorURI"));
+ if (status.IsError()) return status;
}
if (!IsNull(obj, "delegateURI")) {
- CalendarRecord::SetString(
+ status = CalendarRecord::SetString(
attendee, _calendar_attendee.delegatee_uri,
common::FromJson<std::string>(obj, "delegateURI"));
+ if (status.IsError()) return status;
}
if (!IsNull(obj, "contactRef")) {
- CalendarRecord::SetString(
+ status = CalendarRecord::SetString(
attendee, _calendar_attendee.uid,
common::FromJson<std::string>(obj, "contactRef", "contactId"));
+ if (status.IsError()) return status;
const std::string& address_book =
common::FromJson<std::string>(obj, "contactRef", "addressBookId");
- CalendarRecord::SetInt(attendee, _calendar_attendee.person_id,
- common::stol(address_book));
+
+ status = CalendarRecord::SetInt(attendee, _calendar_attendee.person_id,
+ common::stol(address_book));
+ if (status.IsError()) return status;
} else {
LoggerD("ContactRef not set");
}
- AddChildRecord(rec, property, attendee);
+ status = AddChildRecord(rec, property, attendee);
+ if (status.IsError()) return status;
}
-}
-
-picojson::array CalendarItem::AttendeesToJson(int type, calendar_record_h rec) {
- LoggerD("enter");
- picojson::array out = picojson::array();
+ return PlatformResult(ErrorCode::NO_ERROR);
+}
+PlatformResult CalendarItem::AttendeesToJson(int type, calendar_record_h rec,
+ picojson::array* out) {
unsigned int property;
if (type == CALENDAR_BOOK_TYPE_EVENT) {
property = _calendar_event.calendar_attendee;
}
unsigned int count = 0;
- if (!(count = GetChildRecordCount(rec, property))) {
+ PlatformResult status = GetChildRecordCount(rec, property, true, &count);
+ if (status.IsError()) return status;
+ if (!count) {
LoggerD("No attendees to set.");
- return out;
+ return PlatformResult(ErrorCode::NO_ERROR);
}
calendar_list_h list;
if (CALENDAR_ERROR_NONE !=
calendar_record_clone_child_record_list(rec, property, &list)) {
LoggerE("Can't get attendee list");
- return out;
+ return PlatformResult(ErrorCode::UNKNOWN_ERR, "Can't get attendee list");
}
CalendarListPtr(list, CalendarRecord::ListDeleter);
for (unsigned int i = 0; i < count; ++i) {
LoggerD("Processing the attendee %d", i);
- if (!GetChildRecordAt(rec, property, &attendee, i, false)) {
+ if (GetChildRecordAt(rec, property, &attendee, i).IsError()) {
LoggerW("Can't get attendee record");
continue;
}
picojson::value attendee_val = picojson::value(picojson::object());
picojson::object& attendee_obj = attendee_val.get<picojson::object>();
- attendee_obj["uri"] = picojson::value(
- CalendarRecord::GetString(attendee, _calendar_attendee.email, false));
-
- attendee_obj["name"] = picojson::value(
- CalendarRecord::GetString(attendee, _calendar_attendee.name, false));
+ std::string value_str;
+ PlatformResult status = CalendarRecord::GetString(
+ attendee, _calendar_attendee.email, &value_str);
+ if (status.IsError()) return status;
+ attendee_obj["uri"] = picojson::value(value_str);
+
+ status = CalendarRecord::GetString(attendee, _calendar_attendee.name,
+ &value_str);
+ if (status.IsError()) return status;
+ attendee_obj["name"] = picojson::value(value_str);
+
+ std::string enum_str;
+ status =
+ GetEnum(attendee, _calendar_attendee.role, kAttendeeRole, &enum_str);
+ if (status.IsError()) return status;
+ attendee_obj["role"] = picojson::value(enum_str);
+
+ status = GetEnum(attendee, _calendar_attendee.status, kAttendeeStatus,
+ &enum_str);
+ if (status.IsError()) return status;
+ attendee_obj["status"] = picojson::value(enum_str);
+
+ int value_int;
+ status =
+ CalendarRecord::GetInt(attendee, _calendar_attendee.rsvp, &value_int);
+ if (status.IsError()) return status;
+ attendee_obj["RSVP"] = picojson::value(static_cast<bool>(value_int));
+
+ status =
+ GetEnum(attendee, _calendar_attendee.cutype, kAttendeeType, &enum_str);
+ if (status.IsError()) return status;
+ attendee_obj["type"] = picojson::value(enum_str);
+
+ status = CalendarRecord::GetString(attendee, _calendar_attendee.group,
+ &value_str);
+ if (status.IsError()) return status;
+ attendee_obj["group"] = picojson::value(value_str);
+
+ status = CalendarRecord::GetString(
+ attendee, _calendar_attendee.delegator_uri, &value_str);
+ if (status.IsError()) return status;
+ attendee_obj["delegatorURI"] = picojson::value(value_str);
+
+ status = CalendarRecord::GetString(
+ attendee, _calendar_attendee.delegatee_uri, &value_str);
+ if (status.IsError()) return status;
+ attendee_obj["delegateURI"] = picojson::value(value_str);
- attendee_obj["role"] =
- picojson::value(GetEnum(attendee, _calendar_attendee.role, kAttendeeRole));
-
- attendee_obj["status"] = picojson::value(
- GetEnum(attendee, _calendar_attendee.status, kAttendeeStatus));
-
- attendee_obj["RSVP"] = picojson::value(
- (bool)CalendarRecord::GetInt(attendee, _calendar_attendee.rsvp, false));
-
- attendee_obj["type"] = picojson::value(
- GetEnum(attendee, _calendar_attendee.cutype, kAttendeeType));
-
- attendee_obj["group"] = picojson::value(
- CalendarRecord::GetString(attendee, _calendar_attendee.group, false));
-
- attendee_obj["delegatorURI"] = picojson::value(CalendarRecord::GetString(
- attendee, _calendar_attendee.delegator_uri, false));
+ // contactRef
+ std::string contact_id;
+ status = CalendarRecord::GetString(attendee, _calendar_attendee.uid,
+ &contact_id);
+ if (status.IsError()) return status;
- attendee_obj["delegateURI"] = picojson::value(CalendarRecord::GetString(
- attendee, _calendar_attendee.delegatee_uri, false));
+ int book_id;
+ status = CalendarRecord::GetInt(attendee, _calendar_attendee.person_id,
+ &book_id);
+ if (status.IsError()) return status;
- // contactRef
- const std::string& contact_id =
- CalendarRecord::GetString(attendee, _calendar_attendee.uid, false);
- int book_id =
- CalendarRecord::GetInt(attendee, _calendar_attendee.person_id, false);
attendee_obj["contactRef"] = picojson::value(
picojson::object{{"contactId", picojson::value(contact_id)},
{"addressBookId", picojson::value(std::to_string(book_id))}});
- out.push_back(attendee_val);
+ out->push_back(attendee_val);
}
- return out;
+ return PlatformResult(ErrorCode::NO_ERROR);
}
-void CalendarItem::AlarmsFromJson(int type, calendar_record_h rec,
- const picojson::array& alarms) {
- LoggerD("enter");
-
+PlatformResult CalendarItem::AlarmsFromJson(int type, calendar_record_h rec,
+ const picojson::array& alarms) {
unsigned int property;
if (type == CALENDAR_BOOK_TYPE_EVENT) {
property = _calendar_event.calendar_alarm;
int ret = calendar_record_create(_calendar_alarm._uri, &alarm);
if (CALENDAR_ERROR_NONE != ret) {
LoggerE("Fail to create attendee record, error code: %d", ret);
- throw common::UnknownException("Fail to create attendee record");
+ return PlatformResult(ErrorCode::UNKNOWN_ERR,
+ "Fail to create attendee record");
}
int tick_unit = CALENDAR_ALARM_TIME_UNIT_SPECIFIC;
if (!common::IsNull(obj, "absoluteDate")) {
Date absolute = DateFromJson(obj, "absoluteDate");
calendar_time_s absolute_date = DateToPlatform(absolute, false);
- SetCaltime(alarm, _calendar_alarm.alarm_time, absolute_date);
- CalendarRecord::SetInt(alarm, _calendar_alarm.tick_unit, tick_unit);
+ PlatformResult status =
+ SetCaltime(alarm, _calendar_alarm.alarm_time, absolute_date);
+ if (status.IsError()) return status;
+
+ status =
+ CalendarRecord::SetInt(alarm, _calendar_alarm.tick_unit, tick_unit);
+ if (status.IsError()) return status;
}
if (!common::IsNull(obj, "before")) {
LoggerW("Wrong alarm time unit: %s", unit.c_str());
}
- CalendarRecord::SetInt(alarm, _calendar_alarm.tick, tick);
- CalendarRecord::SetInt(alarm, _calendar_alarm.tick_unit, tick_unit);
+ PlatformResult status =
+ CalendarRecord::SetInt(alarm, _calendar_alarm.tick, tick);
+ if (status.IsError()) return status;
+
+ status =
+ CalendarRecord::SetInt(alarm, _calendar_alarm.tick_unit, tick_unit);
+ if (status.IsError()) return status;
}
- SetEnum(alarm, _calendar_alarm.action, kAlarmMethod,
- common::FromJson<std::string>(obj, "method"));
+ PlatformResult status =
+ SetEnum(alarm, _calendar_alarm.action, kAlarmMethod,
+ common::FromJson<std::string>(obj, "method"));
+ if (status.IsError()) return status;
- CalendarRecord::SetString(
+ status = CalendarRecord::SetString(
alarm, _calendar_alarm.description,
common::FromJson<std::string>(obj, "description"));
+ if (status.IsError()) return status;
- AddChildRecord(rec, property, alarm);
+ status = AddChildRecord(rec, property, alarm);
+ if (status.IsError()) return status;
}
-}
-picojson::array CalendarItem::AlarmsToJson(int type, calendar_record_h rec) {
- LoggerD("enter");
-
- picojson::array out = picojson::array();
+ return PlatformResult(ErrorCode::NO_ERROR);
+}
+PlatformResult CalendarItem::AlarmsToJson(int type, calendar_record_h rec,
+ picojson::array* out) {
unsigned int property;
if (type == CALENDAR_BOOK_TYPE_EVENT) {
property = _calendar_event.calendar_alarm;
}
unsigned int count = 0;
- if (!(count = GetChildRecordCount(rec, property))) {
+ PlatformResult status = GetChildRecordCount(rec, property, true, &count);
+ if (status.IsError()) return status;
+ if (!count) {
LoggerD("No attendees to set.");
- return out;
+ return PlatformResult(ErrorCode::NO_ERROR);
}
calendar_list_h list;
if (CALENDAR_ERROR_NONE !=
calendar_record_clone_child_record_list(rec, property, &list)) {
LoggerW("Can't get alarms list");
- return out;
+ return PlatformResult(ErrorCode::UNKNOWN_ERR, "Can't get alarms list");
}
CalendarListPtr(list, CalendarRecord::ListDeleter);
for (unsigned int i = 0; i < count; ++i) {
LoggerD("Processing the alarm %d", i);
- if (!GetChildRecordAt(rec, property, &alarm, i, false)) {
+ if (GetChildRecordAt(rec, property, &alarm, i).IsError()) {
LoggerW("Can't get alarm record");
continue;
}
picojson::value alarm_val = picojson::value(picojson::object());
picojson::object& alarm_obj = alarm_val.get<picojson::object>();
- tick_unit = CalendarRecord::GetInt(alarm, _calendar_alarm.tick_unit, false);
+ PlatformResult status =
+ CalendarRecord::GetInt(alarm, _calendar_alarm.tick_unit, &tick_unit);
+ if (status.IsError()) return status;
if (tick_unit == CALENDAR_ALARM_TIME_UNIT_SPECIFIC) {
- calendar_time_s result = GetCaltime(alarm, _calendar_alarm.alarm_time);
+ calendar_time_s result;
+ status = GetCaltime(alarm, _calendar_alarm.alarm_time, &result);
+ if (status.IsError()) return status;
+
alarm_obj["absoluteDate"] = picojson::value(static_cast<double>(result.time.utime));
} else {
- tick = CalendarRecord::GetInt(alarm, _calendar_alarm.tick, false);
+ status = CalendarRecord::GetInt(alarm, _calendar_alarm.tick, &tick);
+ if (status.IsError()) return status;
int length = 0;
std::string unit = kTimeDurationUnitSeconds;
{"unit", picojson::value(unit)}});
}
- alarm_obj["method"] =
- picojson::value(GetEnum(alarm, _calendar_alarm.action, kAlarmMethod));
+ std::string enum_str;
+ status = GetEnum(alarm, _calendar_alarm.action, kAlarmMethod, &enum_str);
+ if (status.IsError()) return status;
+ alarm_obj["method"] = picojson::value(enum_str);
- alarm_obj["description"] = picojson::value(
- CalendarRecord::GetString(alarm, _calendar_alarm.description, false));
+ std::string value_str;
+ status = CalendarRecord::GetString(alarm, _calendar_alarm.description,
+ &value_str);
+ alarm_obj["description"] = picojson::value(value_str);
+ if (status.IsError()) return status;
- out.push_back(alarm_val);
+ out->push_back(alarm_val);
}
- return out;
+ return PlatformResult(ErrorCode::NO_ERROR);
}
-void CalendarItem::RecurrenceRuleFromJson(calendar_record_h rec,
- const picojson::object& rrule) {
- LoggerD("enter");
-
+PlatformResult CalendarItem::RecurrenceRuleFromJson(
+ calendar_record_h rec, const picojson::object& rrule) {
const std::string& frequency =
common::FromJson<std::string>(rrule, "frequency");
- SetEnum(rec, _calendar_event.freq, kRecurrenceRuleFrequency, frequency);
+ PlatformResult status =
+ SetEnum(rec, _calendar_event.freq, kRecurrenceRuleFrequency, frequency);
+ if (status.IsError()) return status;
const unsigned short interval = common::FromJson<double>(rrule, "interval");
- CalendarRecord::SetInt(rec, _calendar_event.interval, interval);
+ status = CalendarRecord::SetInt(rec, _calendar_event.interval, interval);
+ if (status.IsError()) return status;
const long occurrence_count =
common::FromJson<double>(rrule, "occurrenceCount");
if (-1 != occurrence_count) {
- CalendarRecord::SetInt(rec, _calendar_event.count, occurrence_count);
- CalendarRecord::SetInt(rec, _calendar_event.range_type,
- CALENDAR_RANGE_COUNT);
+ status =
+ CalendarRecord::SetInt(rec, _calendar_event.count, occurrence_count);
+ if (status.IsError()) return status;
+
+ status = CalendarRecord::SetInt(rec, _calendar_event.range_type,
+ CALENDAR_RANGE_COUNT);
+ if (status.IsError()) return status;
}
if (!common::IsNull(rrule, "untilDate")) {
Date until = DateFromJson(rrule, "untilDate");
- SetCaltime(rec, _calendar_event.until_time, DateToPlatform(until, false));
- CalendarRecord::SetInt(rec, _calendar_event.range_type,
- CALENDAR_RANGE_UNTIL);
+ status = SetCaltime(rec, _calendar_event.until_time,
+ DateToPlatform(until, false));
+ if (status.IsError()) return status;
+
+ status = CalendarRecord::SetInt(rec, _calendar_event.range_type,
+ CALENDAR_RANGE_UNTIL);
+ if (status.IsError()) return status;
}
const picojson::array& byday_array =
byday.append("," + iter->get<std::string>());
}
}
- CalendarRecord::SetString(rec, _calendar_event.byday, byday);
+ status = CalendarRecord::SetString(rec, _calendar_event.byday, byday);
+ if (status.IsError()) return status;
const picojson::array& bysetpos_array =
common::FromJson<picojson::array>(rrule, "setPositions");
bysetpos.append("," + iter->get<std::string>());
}
}
- CalendarRecord::SetString(rec, _calendar_event.bysetpos, bysetpos);
+ status = CalendarRecord::SetString(rec, _calendar_event.bysetpos, bysetpos);
+ if (status.IsError()) return status;
- CalendarRecord::SetString(
+ status = CalendarRecord::SetString(
rec, _calendar_event.exdate,
- ExceptionsFromJson(common::FromJson<picojson::array>(rrule, "exceptions")));
+ ExceptionsFromJson(
+ common::FromJson<picojson::array>(rrule, "exceptions")));
+ if (status.IsError()) return status;
+
+ return PlatformResult(ErrorCode::NO_ERROR);
}
std::string CalendarItem::ExceptionsFromJson(const picojson::array &exceptions) {
return result;
}
-picojson::object CalendarItem::RecurrenceRuleToJson(calendar_record_h rec) {
- LoggerD("enter");
-
- picojson::object out = picojson::object();
+PlatformResult CalendarItem::RecurrenceRuleToJson(calendar_record_h rec,
+ picojson::object* out_ptr) {
+ picojson::object& out = *out_ptr;
- out["frequency"] =
- picojson::value(GetEnum(rec, _calendar_event.freq, kRecurrenceRuleFrequency));
+ std::string enum_str;
+ PlatformResult status =
+ GetEnum(rec, _calendar_event.freq, kRecurrenceRuleFrequency, &enum_str);
+ if (status.IsError()) return status;
+ out["frequency"] = picojson::value(enum_str);
- int interval = CalendarRecord::GetInt(rec, _calendar_event.interval, false);
+ int interval;
+ status =
+ CalendarRecord::GetInt(rec, _calendar_event.interval, &interval);
+ if (status.IsError()) return status;
out["interval"] = picojson::value(static_cast<double>(interval));
- int occurrence_count = CalendarRecord::GetInt(rec, _calendar_event.count);
- out["occurrenceCount"] = picojson::value(static_cast<double>(occurrence_count));
+ int occurrence_count;
+ status =
+ CalendarRecord::GetInt(rec, _calendar_event.count, &occurrence_count);
+ if (status.IsError()) return status;
+ out["occurrenceCount"] =
+ picojson::value(static_cast<double>(occurrence_count));
calendar_time_s cal = {CALENDAR_TIME_UTIME, {0}};
calendar_record_get_caltime(rec, _calendar_event.until_time, &cal);
if (cal.time.utime > 0 && CALENDAR_RECORD_NO_UNTIL != cal.time.utime) {
Date until = {cal.time.utime, 0, 0, 0, ""};
- out["untilDate"] = DateToJson(until);
+ out["untilDate"] = DateToJson(&until);
} else {
out["untilDate"] = picojson::value();
}
- out["daysOfTheWeek"] = picojson::value(
- StringToArray(CalendarRecord::GetString(rec, _calendar_event.byday)));
+ std::string value_str;
+ status = CalendarRecord::GetString(rec, _calendar_event.byday, &value_str);
+ if (status.IsError()) return status;
+ out["daysOfTheWeek"] = picojson::value(StringToArray(value_str));
- out["setPositions"] = picojson::value(
- StringToArray(CalendarRecord::GetString(rec, _calendar_event.bysetpos)));
+ status = CalendarRecord::GetString(rec, _calendar_event.bysetpos, &value_str);
+ if (status.IsError()) return status;
+ out["setPositions"] = picojson::value(StringToArray(value_str));
- const picojson::array& exceptions =
- StringToArray(CalendarRecord::GetString(rec, _calendar_event.exdate));
+ status = CalendarRecord::GetString(rec, _calendar_event.exdate, &value_str);
+ if (status.IsError()) return status;
+ const picojson::array& exceptions = StringToArray(value_str);
picojson::array dates = picojson::array();
for (auto& exception : exceptions) {
Date date = {common::stol(exception.get<std::string>()), 0, 0, 0, ""};
- dates.push_back(DateToJson(date));
+ dates.push_back(DateToJson(&date));
}
out["exceptions"] = picojson::value(dates);
- return out;
+ return PlatformResult(ErrorCode::NO_ERROR);
}
calendar_time_s CalendarItem::DateToPlatform(const Date& date,
bool is_all_day) {
- LoggerD("enter");
-
calendar_time_s cal;
if (is_all_day) {
return cal;
}
-Date CalendarItem::DateFromPlatform(int type, calendar_record_h rec,
- const std::string& property) {
- LoggerD("enter");
+PlatformResult CalendarItem::DateFromPlatform(int type, calendar_record_h rec,
+ const std::string& property,
+ Date* date_from_platform) {
+ calendar_time_s cal;
+ PlatformResult status = GetCaltime(type, rec, property + "_time", &cal);
+ if (status.IsError()) return status;
- calendar_time_s cal = GetCaltime(type, rec, property + "_time");
- std::string tzid = GetString(type, rec, property + "_tzid");
+ std::string tzid;
+ status = GetString(type, rec, property + "_tzid", &tzid);
+ if (status.IsError()) return status;
- Date date = {cal.time.utime, cal.time.date.year, cal.time.date.month,
- cal.time.date.mday, tzid};
+ date_from_platform->utc_timestamp_ = cal.time.utime;
+ date_from_platform->year_ = cal.time.date.year;
+ date_from_platform->month_ = cal.time.date.month;
+ date_from_platform->day_ = cal.time.date.mday;
+ date_from_platform->time_zone_ = tzid;
- return date;
+ return PlatformResult(ErrorCode::NO_ERROR);
}
-Date CalendarItem::DateFromPlatform(calendar_record_h rec,
- unsigned int property) {
- LoggerD("enter");
-
- calendar_time_s cal = GetCaltime(rec, property);
+PlatformResult CalendarItem::DateFromPlatform(calendar_record_h rec,
+ unsigned int property,
+ Date* date_from_platform) {
+ calendar_time_s cal;
+ PlatformResult status = GetCaltime(rec, property, &cal);
+ if (status.IsError()) return status;
- Date date = {cal.time.utime, cal.time.date.year, cal.time.date.month,
- cal.time.date.mday, ""};
+ date_from_platform->utc_timestamp_ = cal.time.utime;
+ date_from_platform->year_ = cal.time.date.year;
+ date_from_platform->month_ = cal.time.date.month;
+ date_from_platform->day_ = cal.time.date.mday;
+ date_from_platform->time_zone_ = "";
- return date;
+ return PlatformResult(ErrorCode::NO_ERROR);
}
-void CalendarItem::FromJson(int type, calendar_record_h rec,
- const picojson::object& in) {
- LoggerD("enter");
-
+PlatformResult CalendarItem::FromJson(int type, calendar_record_h rec,
+ const picojson::object& in) {
if (in.empty()) {
LoggerE("Empty CalendarItem object.");
- throw InvalidValuesException("Empty Calendar object.");
+ return PlatformResult(ErrorCode::INVALID_VALUES_ERR,
+ "Empty Calendar object.");
}
- SetString(type, rec, "description", in, true);
- SetString(type, rec, "summary", in, true);
- SetString(type, rec, "location", in, true);
- SetString(type, rec, "organizer", in, true);
+ PlatformResult status = SetString(type, rec, "description", in, true);
+ if (status.IsError()) return status;
+
+ status = SetString(type, rec, "summary", in, true);
+ if (status.IsError()) return status;
+
+ status = SetString(type, rec, "location", in, true);
+ if (status.IsError()) return status;
+
+ status = SetString(type, rec, "organizer", in, true);
+ if (status.IsError()) return status;
int is_all_day = common::FromJson<bool>(in, "isAllDay");
if (!common::IsNull(in, "startDate")) {
Date start = DateFromJson(in, "startDate");
- SetCaltime(type, rec, "startDate_time", DateToPlatform(start, is_all_day));
- SetString(type, rec, "startDate_tzid", start.time_zone_);
+ status = SetCaltime(type, rec, "startDate_time",
+ DateToPlatform(start, is_all_day));
+ if (status.IsError()) return status;
+
+ status = SetString(type, rec, "startDate_tzid", start.time_zone_);
+ if (status.IsError()) return status;
}
const std::string& endProperty =
if (!common::IsNull(in, endProperty.c_str())) {
Date end = DateFromJson(in, endProperty.c_str());
- SetCaltime(type, rec, endProperty + "_time",
+ status = SetCaltime(type, rec, endProperty + "_time",
DateToPlatform(end, is_all_day));
- SetString(type, rec, endProperty + "_tzid", end.time_zone_);
+ if (status.IsError()) return status;
+
+ status = SetString(type, rec, endProperty + "_tzid", end.time_zone_);
+ if (status.IsError()) return status;
}
- SetEnum(type, rec, "visibility", in, kItemVisibility);
+ status = SetEnum(type, rec, "visibility", in, kItemVisibility);
+ if (status.IsError()) return status;
if (!common::IsNull(in, "geolocation")) {
- SetDouble(type, rec, "latitude",
- common::FromJson<double>(in, "geolocation", "latitude"));
- SetDouble(type, rec, "longitude",
- common::FromJson<double>(in, "geolocation", "longitude"));
+ PlatformResult status =
+ SetDouble(type, rec, "latitude",
+ common::FromJson<double>(in, "geolocation", "latitude"));
+ if (status.IsError()) return status;
+
+ status =
+ SetDouble(type, rec, "longitude",
+ common::FromJson<double>(in, "geolocation", "longitude"));
+ if (status.IsError()) return status;
}
- CategoriesFromJson(type, rec,
- common::FromJson<picojson::array>(in, "categories"));
- AttendeesFromJson(type, rec, common::FromJson<picojson::array>(in, "attendees"));
- AlarmsFromJson(type, rec, common::FromJson<picojson::array>(in, "alarms"));
+ status = CategoriesFromJson(
+ type, rec, common::FromJson<picojson::array>(in, "categories"));
+ if (status.IsError()) return status;
+
+ status = AttendeesFromJson(
+ type, rec, common::FromJson<picojson::array>(in, "attendees"));
+ if (status.IsError()) return status;
+
+ status = AlarmsFromJson(type, rec,
+ common::FromJson<picojson::array>(in, "alarms"));
+ if (status.IsError()) return status;
if (type == CALENDAR_BOOK_TYPE_EVENT) {
- SetEnum(type, rec, "priority", in, kEventPriority);
- SetEnum(type, rec, "status", in, kEventStatus);
- SetEnum(type, rec, "availability", in, kEventAvailability);
+ status = SetEnum(type, rec, "priority", in, kEventPriority);
+ if (status.IsError()) return status;
+
+ status = SetEnum(type, rec, "status", in, kEventStatus);
+ if (status.IsError()) return status;
+
+ status = SetEnum(type, rec, "availability", in, kEventAvailability);
+ if (status.IsError()) return status;
if (!common::IsNull(in, "recurrenceRule")) {
- RecurrenceRuleFromJson(
+ status = RecurrenceRuleFromJson(
rec, common::FromJson<picojson::object>(in, "recurrenceRule"));
+ if (status.IsError()) return status;
}
} else {
- SetEnum(type, rec, "priority", in, kTaskPriority);
- SetEnum(type, rec, "status", in, kTaskStatus);
+ status = SetEnum(type, rec, "priority", in, kTaskPriority);
+ if (status.IsError()) return status;
+
+ status = SetEnum(type, rec, "status", in, kTaskStatus);
+ if (status.IsError()) return status;
if (!common::IsNull(in, "completedDate")) {
- SetLli(rec, _calendar_todo.completed_time,
- DateFromJson(in, "completedDate").utc_timestamp_);
+ PlatformResult status =
+ SetLli(rec, _calendar_todo.completed_time,
+ DateFromJson(in, "completedDate").utc_timestamp_);
+ if (status.IsError()) return status;
}
- SetInt(type, rec, "progress", in);
+
+ PlatformResult status = SetInt(type, rec, "progress", in);
+ if (status.IsError()) return status;
}
-}
-void CalendarItem::ToJson(int type, calendar_record_h rec,
- picojson::object* out_ptr) {
- LoggerD("enter");
+ return PlatformResult(ErrorCode::NO_ERROR);
+}
+PlatformResult CalendarItem::ToJson(int type, calendar_record_h rec,
+ picojson::object* out_ptr) {
if (NULL == rec) {
LoggerE("Calendar record is null");
- throw common::UnknownException("Calendar record is null");
+ return PlatformResult(ErrorCode::UNKNOWN_ERR, "Calendar record is null");
}
picojson::object& out = *out_ptr;
- int id = GetInt(type, rec, "id");
+ int id;
+ PlatformResult status = GetInt(type, rec, "id", &id);
+ if (status.IsError()) return status;
picojson::value id_val;
if (type == CALENDAR_BOOK_TYPE_EVENT) {
picojson::object& id_obj = id_val.get<picojson::object>();
id_obj["uid"] = picojson::value(std::to_string(id));
- const std::string& rid = GetString(type, rec, "recurrence_id");
+ std::string rid;
+ status = GetString(type, rec, "recurrence_id", &rid);
+ if (status.IsError()) return status;
+
if (rid.length() > 0) {
id_obj["rid"] = picojson::value(rid);
} else {
out["id"] = id_val;
- int calendar_id = GetInt(type, rec, "calendar_id");
+ int calendar_id;
+ status = GetInt(type, rec, "calendar_id", &calendar_id);
+ if (status.IsError()) return status;
out["calendarId"] = picojson::value(std::to_string(calendar_id));
- out["description"] = picojson::value(GetString(type, rec, "description"));
- out["summary"] = picojson::value(GetString(type, rec, "summary"));
- out["location"] = picojson::value(GetString(type, rec, "location"));
- out["organizer"] = picojson::value(GetString(type, rec, "organizer"));
- out["isAllDay"] = picojson::value((bool)GetInt(type, rec, "isAllDay"));
+ std::string value_str;
+ status = GetString(type, rec, "description", &value_str);
+ if (status.IsError()) return status;
+ out["description"] = picojson::value(value_str);
- // startDate
- out["startDate"] = DateToJson(DateFromPlatform(type, rec, "startDate"));
+ status = GetString(type, rec, "summary", &value_str);
+ if (status.IsError()) return status;
+ out["summary"] = picojson::value(value_str);
- // endDate / dueDate
- const std::string& endProperty =
- (type == CALENDAR_BOOK_TYPE_EVENT) ? "endDate" : "dueDate";
- out[endProperty] = DateToJson(DateFromPlatform(type, rec, endProperty));
+ status = GetString(type, rec, "location", &value_str);
+ if (status.IsError()) return status;
+ out["location"] = picojson::value(value_str);
- out["lastModificationDate"] = picojson::value(
- static_cast<double>(GetLli(type, rec, "lastModificationDate")));
+ status = GetString(type, rec, "organizer", &value_str);
+ if (status.IsError()) return status;
+ out["organizer"] = picojson::value(value_str);
- out["geolocation"] = picojson::value(picojson::object(
- {{"latitude", picojson::value(GetDouble(type, rec, "latitude"))},
- {"longitude", picojson::value(GetDouble(type, rec, "longitude"))}}));
+ int value_int;
+ status = GetInt(type, rec, "isAllDay", &value_int);
+ if (status.IsError()) return status;
+ out["isAllDay"] = picojson::value(static_cast<bool>(value_int));
- out["visibility"] =
- picojson::value(GetEnum(type, rec, "visibility", kItemVisibility));
+ // startDate
+ Date date_from_platform;
+ status = DateFromPlatform(type, rec, "startDate", &date_from_platform);
+ if (status.IsError())return status;
+ out["startDate"] = DateToJson(&date_from_platform);
- out["attendees"] = picojson::value(AttendeesToJson(type, rec));
- out["categories"] = picojson::value(CategoriesToJson(type, rec));
- out["alarms"] = picojson::value(AlarmsToJson(type, rec));
+ // endDate / dueDate
+ const std::string& endProperty =
+ (type == CALENDAR_BOOK_TYPE_EVENT) ? "endDate" : "dueDate";
+ status = DateFromPlatform(type, rec, endProperty, &date_from_platform);
+ if (status.IsError()) return status;
+ out[endProperty] = DateToJson(&date_from_platform);
+
+ long long int lli;
+ status = GetLli(type, rec, "lastModificationDate", &lli);
+ if (status.IsError()) return status;
+ out["lastModificationDate"] = picojson::value(static_cast<double>(lli));
+
+ double latitude;
+ status = GetDouble(type, rec, "latitude", &latitude);
+ if (status.IsError()) return status;
+
+ double longitude;
+ status = GetDouble(type, rec, "longitude", &longitude);
+ if (status.IsError()) return status;
+
+ out["geolocation"] = picojson::value(
+ picojson::object({{"latitude", picojson::value(latitude)},
+ {"longitude", picojson::value(longitude)}}));
+
+ std::string enum_str;
+ status = GetEnum(type, rec, "visibility", kItemVisibility, &enum_str);
+ if (status.IsError()) return status;
+ out["visibility"] = picojson::value(enum_str);
+
+ picojson::array attendees = picojson::array();
+ status = AttendeesToJson(type, rec, &attendees);
+ if (status.IsError()) return status;
+ out["attendees"] = picojson::value(attendees);
+
+ picojson::array categories = picojson::array();
+ status = CategoriesToJson(type, rec, &categories);
+ if (status.IsError()) return status;
+ out["categories"] = picojson::value(categories);
+
+ picojson::array alarms = picojson::array();
+ status = AlarmsToJson(type, rec, &alarms);
+ if (status.IsError()) return status;
+ out["alarms"] = picojson::value(alarms);
if (type == CALENDAR_BOOK_TYPE_EVENT) {
- out["status"] = picojson::value(GetEnum(type, rec, "status", kEventStatus));
- out["priority"] =
- picojson::value(GetEnum(type, rec, "priority", kEventPriority));
- out["availability"] =
- picojson::value(GetEnum(type, rec, "availability", kEventAvailability));
- out["recurrenceRule"] = picojson::value(RecurrenceRuleToJson(rec));
+ status = GetEnum(type, rec, "status", kEventStatus, &enum_str);
+ if (status.IsError()) return status;
+ out["status"] = picojson::value(enum_str);
+
+ status = GetEnum(type, rec, "priority", kEventPriority, &enum_str);
+ if (status.IsError()) return status;
+ out["priority"] = picojson::value(enum_str);
+
+ status = GetEnum(type, rec, "availability", kEventAvailability, &enum_str);
+ if (status.IsError()) return status;
+ out["availability"] = picojson::value(enum_str);
+
+ picojson::object rec_rule = picojson::object();
+ status = RecurrenceRuleToJson(rec, &rec_rule);
+ if (status.IsError()) return status;
+ out["recurrenceRule"] = picojson::value(rec_rule);
} else {
- out["status"] = picojson::value(GetEnum(type, rec, "status", kTaskStatus));
- out["priority"] =
- picojson::value(GetEnum(type, rec, "priority", kTaskPriority));
-
- out["completedDate"] = picojson::value(
- static_cast<double>(GetLli(rec, _calendar_todo.completed_time)));
- out["progress"] =
- picojson::value(static_cast<double>(GetInt(type, rec, "progress")));
+ status = GetEnum(type, rec, "status", kTaskStatus, &enum_str);
+ if (status.IsError()) return status;
+ out["status"] = picojson::value(enum_str);
+
+ status = GetEnum(type, rec, "priority", kTaskPriority, &enum_str);
+ if (status.IsError()) return status;
+ out["priority"] = picojson::value(enum_str);
+
+ long long int lli;
+ status = GetLli(rec, _calendar_todo.completed_time, &lli);
+ if (status.IsError()) return status;
+ out["completedDate"] = picojson::value(static_cast<double>(lli));
+
+ status = GetInt(type, rec, "progress", &value_int);
+ if (status.IsError()) return status;
+ out["progress"] = picojson::value(static_cast<double>(value_int));
}
+
+ return PlatformResult(ErrorCode::NO_ERROR);
}
picojson::array CalendarItem::StringToArray(const std::string& string) {
class CalendarItem : public CalendarRecord {
public:
- static CalendarRecordPtr Create(int type);
- static void Remove(int type, int id);
-
- static unsigned int GetPlatformProperty(int type,
- const std::string& property);
- static int StringToPlatformEnum(const std::string& type,
- const std::string& value);
- static std::string PlatformEnumToString(const std::string& field, int value);
+ static common::PlatformResult Create(int type, calendar_record_h *handle);
+ static common::PlatformResult Remove(int type, int id);
+
+ static common::PlatformResult GetPlatformProperty(int type,
+ const std::string& property,
+ unsigned int* value);
+ static common::PlatformResult StringToPlatformEnum(const std::string& type,
+ const std::string& value,
+ int* platform_enum);
+ static common::PlatformResult PlatformEnumToString(const std::string& field,
+ int value,
+ std::string* platform_str);
// string
- static void SetString(int type, calendar_record_h rec,
- const std::string& property,
- const picojson::object& in, bool optional = false);
- static void SetString(int type, calendar_record_h rec,
- const std::string& property, const std::string& value);
- static std::string GetString(int type, calendar_record_h rec,
- const std::string& property);
+ static common::PlatformResult SetString(int type, calendar_record_h rec,
+ const std::string& property,
+ const picojson::object& in,
+ bool optional = false);
+ static common::PlatformResult SetString(int type, calendar_record_h rec,
+ const std::string& property,
+ const std::string& value);
+ static common::PlatformResult GetString(int type, calendar_record_h rec,
+ const std::string& property,
+ std::string* value);
// int
- static void SetInt(int type, calendar_record_h rec,
- const std::string& property,
- const picojson::object& in, bool optional = false);
- static void SetInt(int type, calendar_record_h rec,
- const std::string& property, int value);
- static int GetInt(int type, calendar_record_h rec,
- const std::string& property);
+ static common::PlatformResult SetInt(int type, calendar_record_h rec,
+ const std::string& property,
+ const picojson::object& in,
+ bool optional = false);
+ static common::PlatformResult SetInt(int type, calendar_record_h rec,
+ const std::string& property, int value);
+ static common::PlatformResult GetInt(int type, calendar_record_h rec,
+ const std::string& property, int* value);
// enum
- static void SetEnum(int type, calendar_record_h rec,
- const std::string& property,
- const picojson::object& in,
- const std::string& enum_name);
- static void SetEnum(calendar_record_h rec, unsigned int property,
- const std::string& enum_name, const std::string& value);
- static std::string GetEnum(int type, calendar_record_h rec,
- const std::string& property,
- const std::string& enum_name);
- static std::string GetEnum(calendar_record_h rec, unsigned int property,
- const std::string& enum_name);
+ static common::PlatformResult SetEnum(int type, calendar_record_h rec,
+ const std::string& property,
+ const picojson::object& in,
+ const std::string& enum_name);
+ static common::PlatformResult SetEnum(calendar_record_h rec,
+ unsigned int property,
+ const std::string& enum_name,
+ const std::string& value);
+ static common::PlatformResult GetEnum(int type, calendar_record_h rec,
+ const std::string& property,
+ const std::string& enum_name,
+ std::string* enum_str);
+ static common::PlatformResult GetEnum(calendar_record_h rec,
+ unsigned int property,
+ const std::string& enum_name,
+ std::string* enum_str);
// double
- static void SetDouble(int type, calendar_record_h rec,
- const std::string& property, double value);
- static double GetDouble(int type, calendar_record_h rec,
- const std::string& property);
+ static common::PlatformResult SetDouble(int type, calendar_record_h rec,
+ const std::string& property,
+ double value);
+ static common::PlatformResult GetDouble(int type, calendar_record_h rec,
+ const std::string& property,
+ double *value);
// calendar_time_s
- static void SetCaltime(int type, calendar_record_h rec,
- const std::string& property, calendar_time_s value,
- bool throw_on_error = true);
- static void SetCaltime(calendar_record_h rec, unsigned int property,
- calendar_time_s value, bool throw_on_error = true);
- static calendar_time_s GetCaltime(int type, calendar_record_h rec,
- const std::string& property,
- bool throw_on_error = true);
- static calendar_time_s GetCaltime(calendar_record_h rec,
- unsigned int property,
- bool throw_on_error = true);
+ static common::PlatformResult SetCaltime(int type, calendar_record_h rec,
+ const std::string& property,
+ calendar_time_s value,
+ bool throw_on_error = true);
+ static common::PlatformResult SetCaltime(calendar_record_h rec,
+ unsigned int property,
+ calendar_time_s value,
+ bool throw_on_error = true);
+ static common::PlatformResult GetCaltime(int type, calendar_record_h rec,
+ const std::string& property,
+ calendar_time_s * cal_time,
+ bool throw_on_error = true);
+ static common::PlatformResult GetCaltime(calendar_record_h rec,
+ unsigned int property,
+ calendar_time_s * cal_time,
+ bool throw_on_error = true);
// long long int
- static void SetLli(calendar_record_h rec, unsigned int property,
- long long int value, bool throw_on_error = true);
- static long long int GetLli(int type, calendar_record_h rec,
- const std::string& property);
- static long long int GetLli(calendar_record_h rec, unsigned int property,
- bool throw_on_error = true);
+ static common::PlatformResult SetLli(calendar_record_h rec,
+ unsigned int property,
+ long long int value,
+ bool throw_on_error = true);
+ static common::PlatformResult GetLli(int type, calendar_record_h rec,
+ const std::string& property,
+ long long int* lli);
+ static common::PlatformResult GetLli(calendar_record_h rec,
+ unsigned int property,
+ long long int* value,
+ bool throw_on_error = true);
// conversions
- static void FromJson(int type, calendar_record_h record,
- const picojson::object& in);
- static void ToJson(int type, calendar_record_h record,
- picojson::object* out_ptr);
+ static common::PlatformResult FromJson(int type, calendar_record_h record,
+ const picojson::object& in);
+ static common::PlatformResult ToJson(int type, calendar_record_h record,
+ picojson::object* out_ptr);
static std::string ExceptionsFromJson(const picojson::array& exceptions);
static Date DateFromJson(const picojson::object& in);
private:
// from JSON to platform
- static void CategoriesFromJson(int type, calendar_record_h rec,
- const picojson::array& value);
- static void AttendeesFromJson(int type, calendar_record_h rec,
- const picojson::array& value);
- static void AlarmsFromJson(int type, calendar_record_h rec,
- const picojson::array& alarms);
- static void RecurrenceRuleFromJson(calendar_record_h rec,
- const picojson::object& rrule);
+ static common::PlatformResult CategoriesFromJson(
+ int type, calendar_record_h rec, const picojson::array& value);
+ static common::PlatformResult AttendeesFromJson(int type,
+ calendar_record_h rec,
+ const picojson::array& value);
+ static common::PlatformResult AlarmsFromJson(int type, calendar_record_h rec,
+ const picojson::array& alarms);
+ static common::PlatformResult RecurrenceRuleFromJson(
+ calendar_record_h rec, const picojson::object& rrule);
// from platform to JSON
- static picojson::value DateToJson(Date date);
- static picojson::array CategoriesToJson(int type, calendar_record_h rec);
- static picojson::array AttendeesToJson(int type, calendar_record_h rec);
- static picojson::array AlarmsToJson(int type, calendar_record_h rec);
- static picojson::object RecurrenceRuleToJson(calendar_record_h rec);
-
- static Date DateFromPlatform(int type, calendar_record_h rec,
- const std::string& property);
- static Date DateFromPlatform(calendar_record_h rec, unsigned int property);
+ static picojson::value DateToJson(Date *date);
+ static common::PlatformResult CategoriesToJson(int type,
+ calendar_record_h rec,
+ picojson::array* value);
+ static common::PlatformResult AttendeesToJson(int type, calendar_record_h rec,
+ picojson::array* out);
+ static common::PlatformResult AlarmsToJson(int type, calendar_record_h rec,
+ picojson::array* out);
+ static common::PlatformResult RecurrenceRuleToJson(calendar_record_h rec,
+ picojson::object* out_ptr);
+
+ static common::PlatformResult DateFromPlatform(int type,
+ calendar_record_h rec,
+ const std::string& property,
+ Date* date_from_platform);
+ static common::PlatformResult DateFromPlatform(calendar_record_h rec,
+ unsigned int property,
+ Date* date_from_platform);
static picojson::array StringToArray(const std::string& string);
using namespace common;
-inline void CheckReturn(int ret, const std::string& error_name) {
- if (CALENDAR_ERROR_NONE != ret) {
- LoggerE("%s : %d", error_name.c_str(), ret);
- throw UnknownException(error_name);
- }
-}
-
CalendarManager::CalendarManager() {
if (CALENDAR_ERROR_NONE == calendar_connect()) {
LoggerD("Calendar DB connected");
bool CalendarManager::IsConnected() { return is_connected_; }
-void CalendarManager::GetCalendars(const JsonObject& args,
- JsonArray& array) {
- LoggerD("enter");
-
+PlatformResult CalendarManager::GetCalendars(const JsonObject& args,
+ JsonArray& array) {
if (!is_connected_) {
- throw UnknownException("DB Connection failed.");
+ return PlatformResult(ErrorCode::UNKNOWN_ERR, "DB Connection failed.");
}
const std::string& type = FromJson<std::string>(args, "type");
calendar_list_h list = NULL;
CalendarListPtr list_ptr = CalendarListPtr(list, CalendarRecord::ListDeleter);
int ret = calendar_db_get_all_records(_calendar_book._uri, 0, 0, &list);
- CheckReturn(ret, "Failed to get list");
+ PlatformResult status =
+ CalendarRecord::CheckReturn(ret, "Failed to get list");
+ if (status.IsError()) return status;
int count = 0;
ret = calendar_list_get_count(list, &count);
- CheckReturn(ret, "Failed to get list size");
+ status = CalendarRecord::CheckReturn(ret, "Failed to get list size");
+ if (status.IsError()) return status;
LoggerD("Calendar list count: %d", count);
ret = calendar_list_first(list);
- CheckReturn(ret, "Failed to move list to the first position");
+ status = CalendarRecord::CheckReturn(
+ ret, "Failed to move list to the first position");
+ if (status.IsError()) return status;
int current_calendar_type = CalendarRecord::TypeToInt(type);
calendar_record_h calendar = NULL;
while (count-- > 0) {
ret = calendar_list_get_current_record_p(list, &calendar);
- CheckReturn(ret, "Failed to get current record");
+ status = CalendarRecord::CheckReturn(ret, "Failed to get current record");
+ if (status.IsError()) return status;
+
+ PlatformResult status = CalendarRecord::GetInt(
+ calendar, _calendar_book.store_type, &store_type);
+ if (status.IsError()) return status;
- store_type =
- CalendarRecord::GetInt(calendar, _calendar_book.store_type);
if (current_calendar_type != store_type) {
LoggerD("Different store type %d, requested: %d. Skipping...",
store_type, current_calendar_type);
array.push_back(JsonValue(JsonObject()));
- CalendarRecord::CalendarToJson(calendar,
- &array.back().get<JsonObject>());
+ status = CalendarRecord::CalendarToJson(calendar,
+ &array.back().get<JsonObject>());
+ if (status.IsError()) return status;
+
calendar_list_next(list);
}
-}
-void CalendarManager::GetCalendar(const JsonObject& args, JsonObject& out) {
- LoggerD("enter");
+ return PlatformResult(ErrorCode::NO_ERROR);
+}
+PlatformResult CalendarManager::GetCalendar(const JsonObject& args,
+ JsonObject& out) {
if (!is_connected_) {
- throw UnknownException("DB Connection failed.");
+ return PlatformResult(ErrorCode::UNKNOWN_ERR, "DB Connection failed.");
}
int id = common::stol(FromJson<std::string>(args, "id"));
+ calendar_record_h handle = nullptr;
+ PlatformResult status =
+ CalendarRecord::GetById(id, _calendar_book._uri, &handle);
+ if (status.IsError()) return status;
+
CalendarRecordPtr record_ptr =
- CalendarRecord::GetById(id, _calendar_book._uri);
+ CalendarRecordPtr(handle, CalendarRecord::Deleter);
int type = CalendarRecord::TypeToInt(FromJson<std::string>(args, "type"));
- int calendar_type =
- CalendarRecord::GetInt(record_ptr.get(), _calendar_book.store_type);
+ int calendar_type;
+ status = CalendarRecord::GetInt(record_ptr.get(), _calendar_book.store_type,
+ &calendar_type);
+ if (status.IsError()) return status;
+
if (type != calendar_type) {
LoggerD("Calendar type doesn't match requested type");
- throw NotFoundException("Calendar not found");
+ return PlatformResult(ErrorCode::NOT_FOUND_ERR, "Calendar not found");
}
- JsonValue result = JsonValue(JsonObject());
+ status = CalendarRecord::CalendarToJson(record_ptr.get(), &out);
+ if (status.IsError()) return status;
- CalendarRecord::CalendarToJson(record_ptr.get(), &out);
+ return PlatformResult(ErrorCode::NO_ERROR);
}
-void CalendarManager::AddCalendar(const JsonObject& args, JsonObject& out) {
- LoggerD("enter");
-
+PlatformResult CalendarManager::AddCalendar(const JsonObject& args,
+ JsonObject& out) {
if (!is_connected_) {
- throw UnknownException("DB Connection failed.");
+ return PlatformResult(ErrorCode::UNKNOWN_ERR, "DB Connection failed.");
}
const JsonObject& calendar = FromJson<JsonObject>(args, "calendar");
- CalendarRecordPtr record_ptr = CalendarRecord::CreateCalendar();
- CalendarRecord::CalendarFromJson(record_ptr.get(), calendar);
+ calendar_record_h handle = nullptr;
+ PlatformResult status = CalendarRecord::CreateCalendar(&handle);
+ if (status.IsError()) return status;
+
+ CalendarRecordPtr record_ptr =
+ CalendarRecordPtr(handle, CalendarRecord::Deleter);
+
+ status = CalendarRecord::CalendarFromJson(record_ptr.get(), calendar);
+ if (status.IsError()) return status;
int ret, record_id;
ret = calendar_db_insert_record(record_ptr.get(), &record_id);
- CheckReturn(ret, "Failed to insert calendar record into db");
+ status = CalendarRecord::CheckReturn(
+ ret, "Failed to insert calendar record into db");
+ if (status.IsError()) return status;
-// NativePlugin::ReportSuccess(JsonValue(static_cast<double>(record_id)), out);
+ return PlatformResult(ErrorCode::NO_ERROR);
}
-void CalendarManager::RemoveCalendar(const JsonObject& args,
- JsonObject& out) {
- LoggerD("enter");
-
+PlatformResult CalendarManager::RemoveCalendar(const JsonObject& args,
+ JsonObject& out) {
if (!is_connected_) {
- throw UnknownException("DB Connection failed.");
+ return PlatformResult(ErrorCode::UNKNOWN_ERR, "DB Connection failed.");
}
int id = common::stol(FromJson<std::string>(args, "id"));
if (id == kUnifiedCalendardId) {
LoggerE("Unified calendar can not be deleted");
- throw InvalidValuesException("Unified calendar can not be deleted");
+ return PlatformResult(ErrorCode::INVALID_VALUES_ERR,
+ "Unified calendar can not be deleted");
} else if (id == DEFAULT_EVENT_CALENDAR_BOOK_ID) {
LoggerE("Default event calendar can not be deleted");
- throw InvalidValuesException("Default event calendar can not be deleted");
+ return PlatformResult(ErrorCode::INVALID_VALUES_ERR,
+ "Default event calendar can not be deleted");
} else if (id == DEFAULT_TODO_CALENDAR_BOOK_ID) {
LoggerE("Default todo calendar can not be deleted");
- throw InvalidValuesException("Default todo calendar can not be deleted");
+ return PlatformResult(ErrorCode::INVALID_VALUES_ERR,
+ "Default todo calendar can not be deleted");
}
int ret = calendar_db_delete_record(_calendar_book._uri, id);
- CheckReturn(ret, "Failed to delete record from db");
+ PlatformResult status =
+ CalendarRecord::CheckReturn(ret, "Failed to delete record from db");
+ if (status.IsError()) return status;
+ return PlatformResult(ErrorCode::NO_ERROR);
}
}
}
#define CALENDAR_CALENDAR_MANAGER_H_
#include "common/picojson.h"
+#include "common/platform_result.h"
namespace extension {
namespace calendar {
* {status: 'success', result: {calendarsArray}}
* @endcode
*/
- void GetCalendars(const JsonObject& args,
- JsonArray& array);
- void GetCalendar(const JsonObject& args, JsonObject& out);
- void AddCalendar(const JsonObject& args, JsonObject& out);
- void RemoveCalendar(const JsonObject& args,
- JsonObject& out);
+ common::PlatformResult GetCalendars(const JsonObject& args, JsonArray& array);
+ common::PlatformResult GetCalendar(const JsonObject& args, JsonObject& out);
+ common::PlatformResult AddCalendar(const JsonObject& args, JsonObject& out);
+ common::PlatformResult RemoveCalendar(const JsonObject& args,
+ JsonObject& out);
static CalendarManager& GetInstance();
virtual ~CalendarManager();
#include <calendar-service2/calendar.h>
#include "common/logger.h"
-//#include "common/platform-exception.h"
#include "common/converter.h"
namespace extension {
using namespace common;
-inline void CheckReturn(int ret, const std::string& error_name) {
+PlatformResult CalendarRecord::CheckReturn(int ret,
+ const std::string& error_name) {
if (CALENDAR_ERROR_NONE != ret) {
LoggerE("%s : %d", error_name.c_str(), ret);
- throw UnknownException(error_name);
+ return PlatformResult(ErrorCode::UNKNOWN_ERR, error_name);
}
+
+ return PlatformResult(ErrorCode::NO_ERROR);
}
void CalendarRecord::QueryDeleter(calendar_query_h handle) {
}
}
-std::string CalendarRecord::GetString(calendar_record_h rec,
- unsigned int property,
- bool throw_on_error) {
+PlatformResult CalendarRecord::GetString(calendar_record_h rec,
+ unsigned int property,
+ std::string* str,
+ bool throw_on_error) {
char* value = NULL;
int ret = calendar_record_get_str(rec, property, &value);
if (CALENDAR_ERROR_NONE != ret) {
LoggerW("Can't get string value form record: %d", ret);
if (throw_on_error) {
- throw UnknownException("Get string from record failed.");
+ return PlatformResult(ErrorCode::UNKNOWN_ERR,
+ "Get string from record failed.");
}
}
- std::string str = "";
+ *str = "";
if (value) {
- str = std::string(value);
+ *str = std::string(value);
free(value);
}
- return str;
+ return PlatformResult(ErrorCode::NO_ERROR);
}
-void CalendarRecord::SetString(calendar_record_h record, unsigned int property,
- const std::string& value, bool throw_on_error) {
- LoggerD("enter");
-
+PlatformResult CalendarRecord::SetString(calendar_record_h record,
+ unsigned int property,
+ const std::string& value,
+ bool throw_on_error) {
int ret = calendar_record_set_str(record, property,
value.empty() ? NULL : value.c_str());
LoggerW("Can't set string value to record: %d", ret);
if (throw_on_error) {
- throw UnknownException("Set string to record failed.");
+ return PlatformResult(ErrorCode::UNKNOWN_ERR,
+ "Set string to record failed.");
}
}
+
+ return PlatformResult(ErrorCode::NO_ERROR);
}
-int CalendarRecord::GetInt(calendar_record_h rec, unsigned int property,
- bool throw_on_error) {
- int value;
- int ret = calendar_record_get_int(rec, property, &value);
+PlatformResult CalendarRecord::GetInt(calendar_record_h rec,
+ unsigned int property, int* value,
+ bool throw_on_error) {
+ int ret = calendar_record_get_int(rec, property, value);
if (CALENDAR_ERROR_NONE != ret) {
LoggerW("Can't get int value form record: %d", ret);
if (throw_on_error) {
- throw UnknownException("Get int from record failed.");
+ return PlatformResult(ErrorCode::UNKNOWN_ERR,
+ "Get int from record failed.");
}
}
- return value;
+ return PlatformResult(ErrorCode::NO_ERROR);
}
-void CalendarRecord::SetInt(calendar_record_h record, unsigned int property,
- int value, bool throw_on_error) {
- LoggerD("enter");
-
+PlatformResult CalendarRecord::SetInt(calendar_record_h record,
+ unsigned int property, int value,
+ bool throw_on_error) {
int ret = calendar_record_set_int(record, property, value);
if (CALENDAR_ERROR_NONE != ret) {
LoggerW("Can't set int value to record: %d", ret);
if (throw_on_error) {
- throw UnknownException("Set int to record failed.");
+ return PlatformResult(ErrorCode::UNKNOWN_ERR,
+ "Set int to record failed.");
}
}
+
+ return PlatformResult(ErrorCode::NO_ERROR);
}
std::string CalendarRecord::TypeToString(int type) {
return CALENDAR_BOOK_TYPE_NONE;
}
-const char* CalendarRecord::TypeToUri(const std::string& type) {
+PlatformResult CalendarRecord::TypeToUri(const std::string& type,
+ std::string* uri) {
if (kCalendarTypeEvent == type) {
- return _calendar_event._uri;
- }
- if (kCalendarTypeTask == type) {
- return _calendar_todo._uri;
+ *uri = _calendar_event._uri;
+ } else if (kCalendarTypeTask == type) {
+ *uri = _calendar_todo._uri;
+ } else {
+ return PlatformResult(ErrorCode::UNKNOWN_ERR, "Undefined record type");
}
- throw UnknownException("Undefined record type");
+ return PlatformResult(ErrorCode::NO_ERROR);
}
-const char* CalendarRecord::TypeToUri(int type) {
+PlatformResult CalendarRecord::TypeToUri(int type, std::string* uri) {
if (CALENDAR_BOOK_TYPE_EVENT == type) {
- return _calendar_event._uri;
+ *uri = _calendar_event._uri;
+ } else if (CALENDAR_BOOK_TYPE_TODO == type) {
+ *uri = _calendar_todo._uri;
+ } else {
+ return PlatformResult(ErrorCode::UNKNOWN_ERR, "Undefined record type");
}
- if (CALENDAR_BOOK_TYPE_TODO == type) {
- return _calendar_todo._uri;
- }
-
- throw UnknownException("Undefined record type");
+ return PlatformResult(ErrorCode::NO_ERROR);
}
-CalendarRecordPtr CalendarRecord::Create(const char* view_uri) {
- LoggerD("enter");
-
- calendar_record_h handle = nullptr;
- int ret = calendar_record_create(view_uri, &handle);
+PlatformResult CalendarRecord::Create(const char* view_uri,
+ calendar_record_h* handle) {
+ int ret = calendar_record_create(view_uri, handle);
if (CALENDAR_ERROR_NONE != ret || nullptr == handle) {
LoggerE("Fail to create calendar record, error code: %d", ret);
- throw NotFoundException("Fail to create calendar record");
+ return PlatformResult(ErrorCode::NOT_FOUND_ERR,
+ "Fail to create calendar record");
}
- return CalendarRecordPtr(handle, CalendarRecord::Deleter);
+ return PlatformResult(ErrorCode::NO_ERROR);
}
-CalendarRecordPtr CalendarRecord::CreateCalendar() {
- LoggerD("enter");
-
- return Create(_calendar_book._uri);
+PlatformResult CalendarRecord::CreateCalendar(calendar_record_h* handle) {
+ return Create(_calendar_book._uri, handle);
}
-CalendarRecordPtr CalendarRecord::GetById(int id, const char* view_uri) {
- calendar_record_h handle = nullptr;
-
- int ret = calendar_db_get_record(view_uri, id, &handle);
+PlatformResult CalendarRecord::GetById(int id, const char* view_uri,
+ calendar_record_h* handle) {
+ int ret = calendar_db_get_record(view_uri, id, handle);
if (CALENDAR_ERROR_NONE != ret || nullptr == handle) {
LoggerE("Fail to get calendar record %d for view %s, error code: %d", id,
view_uri, ret);
- throw NotFoundException("Fail to get record with given id");
+ return PlatformResult(ErrorCode::NOT_FOUND_ERR,
+ "Fail to get record with given id");
}
- return CalendarRecordPtr(handle, CalendarRecord::Deleter);
+ return PlatformResult(ErrorCode::NO_ERROR);
}
-int CalendarRecord::Insert(calendar_record_h rec) {
- LoggerD("enter");
-
- int record_id;
- int ret = calendar_db_insert_record(rec, &record_id);
+PlatformResult CalendarRecord::Insert(calendar_record_h rec, int* record_id) {
+ int ret = calendar_db_insert_record(rec, record_id);
if (CALENDAR_ERROR_NONE != ret) {
LoggerE("Cannot insert record, error code: %d", ret);
- throw NotFoundException("Cannot insert record");
+ return PlatformResult(ErrorCode::NOT_FOUND_ERR, "Cannot insert record");
}
- return record_id;
+ return PlatformResult(ErrorCode::NO_ERROR);
}
-void CalendarRecord::AddChildRecord(calendar_record_h rec,
- unsigned int property,
- calendar_record_h child) {
- LoggerD("enter");
-
+PlatformResult CalendarRecord::AddChildRecord(calendar_record_h rec,
+ unsigned int property,
+ calendar_record_h child) {
int ret = calendar_record_add_child_record(rec, property, child);
if (CALENDAR_ERROR_NONE != ret) {
if (child) {
calendar_record_destroy(child, true);
}
LoggerE("Cannot add child record, error code: %d", ret);
- throw NotFoundException("Cannot add child record");
+ return PlatformResult(ErrorCode::NOT_FOUND_ERR, "Cannot add child record");
}
+
+ return PlatformResult(ErrorCode::NO_ERROR);
}
void CalendarRecord::RemoveChildRecords(calendar_record_h rec,
unsigned int property_id) {
- LoggerD("enter");
-
unsigned int count = 0;
if (CALENDAR_ERROR_NONE !=
}
}
-unsigned int CalendarRecord::GetChildRecordCount(calendar_record_h rec,
- unsigned int property,
- bool throw_on_error) {
- unsigned int value;
- int ret = calendar_record_get_child_record_count(rec, property, &value);
+PlatformResult CalendarRecord::GetChildRecordCount(calendar_record_h rec,
+ unsigned int property,
+ bool throw_on_error,
+ unsigned int* value) {
+ int ret = calendar_record_get_child_record_count(rec, property, value);
if (CALENDAR_ERROR_NONE != ret) {
LoggerW("Can't get child record count: %d", ret);
if (throw_on_error) {
- throw UnknownException("Get child record count failed.");
+ return PlatformResult(ErrorCode::UNKNOWN_ERR,
+ "Get child record count failed.");
}
}
- return value;
+ return PlatformResult(ErrorCode::NO_ERROR);
}
-bool CalendarRecord::GetChildRecordAt(calendar_record_h rec,
- unsigned int property,
- calendar_record_h* result, int index,
- bool throw_on_error) {
+PlatformResult CalendarRecord::GetChildRecordAt(calendar_record_h rec,
+ unsigned int property,
+ calendar_record_h* result,
+ int index) {
int ret = calendar_record_get_child_record_at_p(rec, property, index, result);
if (CALENDAR_ERROR_NONE != ret) {
LoggerW("Can't get child record at: %d", ret);
- if (throw_on_error) {
- throw UnknownException("Get child record at failed.");
- }
-
- return false;
+ return PlatformResult(ErrorCode::UNKNOWN_ERR,
+ "Get child record at failed.");
}
- return true;
+ return PlatformResult(ErrorCode::NO_ERROR);
}
-void CalendarRecord::CalendarToJson(calendar_record_h rec,
- picojson::object* out_ptr) {
+PlatformResult CalendarRecord::CalendarToJson(calendar_record_h rec,
+ picojson::object* out_ptr) {
picojson::object& out = *out_ptr;
if (NULL == rec) {
LoggerE("Calendar record is null");
- throw UnknownException("Calendar record is null");
+ return PlatformResult(ErrorCode::UNKNOWN_ERR, "Calendar record is null");
}
- int id = GetInt(rec, _calendar_book.id);
- int account_id = GetInt(rec, _calendar_book.account_id);
- std::string name = GetString(rec, _calendar_book.name);
- std::string type = TypeToString(GetInt(rec, _calendar_book.store_type));
+ int id;
+ PlatformResult status = GetInt(rec, _calendar_book.id, &id);
+
+ int account_id;
+ GetInt(rec, _calendar_book.account_id, &account_id);
+
+ std::string name;
+ status = GetString(rec, _calendar_book.name, &name);
+ if (status.IsError()) return status;
+
+ int value;
+ status = GetInt(rec, _calendar_book.store_type, &value);
+ std::string type = TypeToString(value);
out.insert(std::make_pair("id", picojson::value(std::to_string(id))));
out.insert(
std::make_pair("accountId", picojson::value(std::to_string(account_id))));
out.insert(std::make_pair("name", picojson::value(name)));
out.insert(std::make_pair("type", picojson::value(type)));
+
+ return PlatformResult(ErrorCode::NO_ERROR);
}
-void CalendarRecord::CalendarFromJson(calendar_record_h rec,
- const picojson::object &in) {
+PlatformResult CalendarRecord::CalendarFromJson(calendar_record_h rec,
+ const picojson::object& in) {
if (in.empty()) {
LoggerE("Empty Calendar object.");
- throw InvalidValuesException("Empty Calendar object.");
+ return PlatformResult(ErrorCode::INVALID_VALUES_ERR,
+ "Empty Calendar object.");
}
const std::string& name = FromJson<std::string>(in, "name");
}
int ret = calendar_record_set_str(rec, _calendar_book.name, name.c_str());
- CheckReturn(ret, "Failed to set name");
+ PlatformResult status = CheckReturn(ret, "Failed to set name");
+ if (status.IsError()) return status;
ret = calendar_record_set_int(rec, _calendar_book.account_id, account_id);
- CheckReturn(ret, "Failed to set account_id");
+ status = CheckReturn(ret, "Failed to set account_id");
+ if (status.IsError()) return status;
ret = calendar_record_set_int(rec, _calendar_book.store_type, store_type);
- CheckReturn(ret, "Failed to set store_type");
+ status = CheckReturn(ret, "Failed to set store_type");
+ if (status.IsError()) return status;
+
+ return PlatformResult(ErrorCode::NO_ERROR);
}
} // namespace calendar
#include <memory>
#include <calendar-service2/calendar.h>
+#include "common/platform_result.h"
#include "common/picojson.h"
+
namespace extension {
namespace calendar {
static void Deleter(calendar_record_h handle);
static void ListDeleter(calendar_list_h handle);
- static std::string GetString(calendar_record_h rec, unsigned int property,
- bool throw_on_error = true);
- static void SetString(calendar_record_h rec, unsigned int property,
- const std::string& value, bool throw_on_error = true);
+ static common::PlatformResult GetString(calendar_record_h rec,
+ unsigned int property,
+ std::string* str,
+ bool throw_on_error = true);
+ static common::PlatformResult SetString(calendar_record_h rec,
+ unsigned int property,
+ const std::string& value,
+ bool throw_on_error = true);
- static int GetInt(calendar_record_h rec, unsigned int property,
- bool throw_on_error = true);
- static void SetInt(calendar_record_h rec, unsigned int property, int value,
- bool throw_on_error = true);
+ static common::PlatformResult GetInt(calendar_record_h rec,
+ unsigned int property,
+ int* value,
+ bool throw_on_error = true);
+ static common::PlatformResult SetInt(calendar_record_h rec,
+ unsigned int property, int value,
+ bool throw_on_error = true);
static std::string TypeToString(int type);
static std::string TypeToString(const char* view_uri);
static int TypeToInt(const std::string& type);
static int TypeToInt(const char* view_uri);
- static const char* TypeToUri(const std::string& type);
- static const char* TypeToUri(int type);
+ static common::PlatformResult TypeToUri(const std::string& type,
+ std::string* uri);
+ static common::PlatformResult TypeToUri(int type, std::string* uri);
- static int Insert(calendar_record_h rec);
+ static common::PlatformResult Insert(calendar_record_h rec, int* record_id);
- static void AddChildRecord(calendar_record_h rec, unsigned int property,
- calendar_record_h child);
+ static common::PlatformResult AddChildRecord(calendar_record_h rec,
+ unsigned int property,
+ calendar_record_h child);
static void RemoveChildRecords(calendar_record_h rec,
unsigned int property_id);
- static unsigned int GetChildRecordCount(calendar_record_h rec,
- unsigned int property,
- bool throw_on_error = true);
- static bool GetChildRecordAt(calendar_record_h rec, unsigned int property,
- calendar_record_h* result, int index,
- bool throw_on_error = true);
-
- static CalendarRecordPtr GetById(int id, const char* view_uri);
- static CalendarRecordPtr GetCalendar(int id);
- static CalendarRecordPtr GetItem(int id, const char* view_uri);
- static CalendarRecordPtr Create(const char* view_uri);
- static CalendarRecordPtr CreateCalendar();
-
- static void CalendarToJson(calendar_record_h rec,
- picojson::object* out_ptr);
- static void CalendarFromJson(calendar_record_h rec,
- const picojson::object& in);
+ static common::PlatformResult GetChildRecordCount(calendar_record_h rec,
+ unsigned int property,
+ bool throw_on_error,
+ unsigned int* value);
+ static common::PlatformResult GetChildRecordAt(calendar_record_h rec,
+ unsigned int property,
+ calendar_record_h* result,
+ int index);
+
+ static common::PlatformResult GetById(int id, const char* view_uri,
+ calendar_record_h *handle);
+ static common::PlatformResult Create(const char* view_uri,
+ calendar_record_h *calendar);
+ static common::PlatformResult CreateCalendar(calendar_record_h* handle);
+
+ static common::PlatformResult CalendarToJson(calendar_record_h rec,
+ picojson::object* out_ptr);
+ static common::PlatformResult CalendarFromJson(calendar_record_h rec,
+ const picojson::object& in);
+
+ static common::PlatformResult CheckReturn(int ret,
+ const std::string& error_name);
};
} // namespace calendar
}
]);
+ if (isNaN(Number(args.id))) {
+ throw new tizen.WebAPIException(tizen.WebAPIException.NOT_FOUND_ERR,
+ 'Calendar not found');
+ }
+
var callArgs = {
type: args.type,
id: args.id