#include "configuration.h" #include "tariff_timebase.h" #include "time_range_header.h" #include "tariff_timestep_config.h" #include #include /// MemberType Configuration::IdentifyJsonMember(const char* member_name) { if (strcmp(member_name, "Currency") == 0) return MemberType::CurrencyType; if (strcmp(member_name, "PaymentMethod") == 0) return MemberType::PaymentMethodType; if (strcmp(member_name, "PaymentRate") == 0) return MemberType::PaymentRateType; if (strcmp(member_name, "PaymentOption") == 0) return MemberType::PaymentOptionType; if (strcmp(member_name, "Duration") == 0) return MemberType::DurationType; //if (strcmp(member_name, "WeekDays") == 0) return MemberType::WeekDaysType; if (strcmp(member_name, "WeekDaysWorktime") == 0) return MemberType::WeekDaysWorkTimeType; if (strcmp(member_name, "SpecialDaysWorktime") == 0) return MemberType::SpecialDaysWorktimeType; if (strcmp(member_name, "SpecialDays") == 0) return MemberType::SpecialDaysType; if (strcmp(member_name, "PeriodYear") == 0) return MemberType::PeriodYearType; if (strcmp(member_name, "DailyTicket") == 0) return MemberType::DailyTicketType; if (strcmp(member_name, "TimeBase") == 0) return MemberType::TimeBaseType; if (strcmp(member_name, "Customer") == 0) return MemberType::CustomerType; if (strcmp(member_name, "TimeRange") == 0) return MemberType::TimeRangeType; if (strcmp(member_name, "TimeStepConfig") == 0) return MemberType::TimeStepConfigType; else return MemberType::UnknownType; } /// bool Configuration::ParseJson(Configuration* cfg, const char* json) { try { if (cfg == nullptr) { printf("TariffConfiguration pointer not set\n"); return false; } if (json == NULL) { printf("%s", "Input JSON string is NULL\n"); return false; } // Parse JSON to document Document document; document.Parse(json); // Return if parse error has been found ParseErrorCode err = document.GetParseError(); if (err != 0) { printf("%s %d (%s)\n", "Unable to parse JSON, error code:", err, GetParseError_En(err)); return false; } // Validate JSON, check if it's a JSON object printf("%s", "JSON parsing has been successful\n"); if (!document.IsObject()) { printf("%s", "Error: not a (valid) JSON object\n"); return false; } printf("%s", "Valid JSON object identified\n"); // Validate JSON, check configuration members if (!document.HasMember("Currency") || !document.HasMember("PaymentMethod") || !document.HasMember("PaymentOption") || !document.HasMember("PaymentRate") || !document.HasMember("Duration") //|| !document.HasMember("WeekDays") //|| !document.HasMember("SpecialDaysWorktime") //|| !document.HasMember("SpecialDays") ) { printf("%s", "Error: not a valid configuration JSON\n"); return false; } printf("%s", "Valid JSON configuration identified\n"); ATBCurrency Currency; ATBDuration Duration; ATBPaymentMethod PaymentMethod; ATBPaymentRate PaymentRate; ATBSpecialDaysWorktime SpecialDaysWorktime; ATBSpecialDays SpecialDays; ATBWeekDays WeekDays; ATBWeekDaysWorktime WeekDaysWorktime; ATBPaymentOption PaymentOption; ATBPeriodYear YearPeriod; ATBDailyTicket DailyTicket; ATBTimeBase TimeBase; ATBCustomer Customer; ATBTimeRange TimeRange; ATBTimeStepConfig TimeStepConfig; MemberType mb_type = MemberType::UnknownType; // Get all JSON object members // This code should run only once (to load JSON variables into memory) for (auto i = document.MemberBegin(); i != document.MemberEnd(); i++) { // Get name of all general members of JSON, don't print name if NULL const char* mb_name = i->name.GetString(); if (mb_name == NULL) continue; if (document[mb_name].IsString()) { QString const _mb_name(mb_name); if (_mb_name.startsWith("Project", Qt::CaseInsensitive)) { cfg->project.project = document[mb_name].GetString(); continue; } if (_mb_name.startsWith("Version", Qt::CaseInsensitive)) { cfg->project.version = document[mb_name].GetString(); continue; } if (_mb_name.startsWith("Info", Qt::CaseInsensitive)) { cfg->project.info = document[mb_name].GetString(); continue; } } // ... everything else should be an array if (!document[mb_name].IsArray()) { continue; } printf(" -%s\n", mb_name); // Get array for each JSON object member auto mb_array = document[mb_name].GetArray(); if (mb_array.Size() <= 0) break; //Iterate over provided array for (rapidjson::SizeType j = 0; j < mb_array.Size(); j++) { // Get all inner objects, don't print name if NULL auto inner_obj = mb_array[j].GetObject(); if (inner_obj.MemberCount() <= 0) break; // Iterate over inner object JSON members for (auto k = inner_obj.MemberBegin(); k != inner_obj.MemberEnd(); k++) { // Get inner object JSON member, don't print name if NULL const char* inner_obj_name = k->name.GetString(); if (inner_obj_name == NULL) { printf("Inner object name is NULL\n"); continue; } // Identify member type mb_type = IdentifyJsonMember(mb_name); switch (mb_type) { case MemberType::UnknownType: break; case MemberType::TimeRangeType: if (QString(inner_obj_name) == QString("time_range_id")) { if (k->value.IsInt()) { TimeRange.time_range_id = k->value.GetInt(); } } else if (QString(inner_obj_name) == QString("time_range_from")) { if (k->value.IsString()) { QTime timeRangeFrom = QTime::fromString(QString::fromStdString(k->value.GetString()), Qt::ISODate); TimeRange.time_range_from = timeRangeFrom; } } else if (QString(inner_obj_name) == QString("time_range_to")) { if (k->value.IsString()) { QTime timeRangeTo = QTime::fromString(QString::fromStdString(k->value.GetString()), Qt::ISODate); TimeRange.time_range_to = timeRangeTo; } } break; case MemberType::TimeBaseType: if (QString(inner_obj_name) == QString("tbase_id")) { if (k->value.IsInt()) { TimeBase.tbase_id = k->value.GetInt(); } } else if (QString(inner_obj_name) == QString("tbase_type")) { if (k->value.IsInt()) { int timeBase = k->value.GetInt(); switch (timeBase) { case (int)ATBTimeBase::TimeBaseType::ABSOLUTE: TimeBase.tbase_type = ATBTimeBase::TimeBaseType::ABSOLUTE; break; case (int)ATBTimeBase::TimeBaseType::RELATIVE: TimeBase.tbase_type = ATBTimeBase::TimeBaseType::RELATIVE; break; } } } break; case MemberType::TimeStepConfigType: if (QString(inner_obj_name) == QString("tsconfig_id")) { if (k->value.IsInt()) { TimeStepConfig.tsconfig_id = k->value.GetInt(); } } else if (QString(inner_obj_name) == QString("tsconfig_label")) { if (k->value.IsString()) { TimeStepConfig.tsconfig_label = QString::fromStdString(k->value.GetString()); } } break; case MemberType::DailyTicketType: if (QString(inner_obj_name) == QString("daily_ticket_payment_option_id")) { if (k->value.IsInt()) { DailyTicket.daily_ticket_payment_option_id = k->value.GetInt(); } } else if (QString(inner_obj_name) == QString("daily_ticket_id")) { if (k->value.IsInt()) { DailyTicket.daily_ticket_id = k->value.GetInt(); } } else if (QString(inner_obj_name) == QString("daily_ticket_price_id")) { if (k->value.IsInt()) { DailyTicket.daily_ticket_price_id = k->value.GetInt(); } } else if (QString(inner_obj_name) == QString("daily_ticket_tb_id")) { if (k->value.IsInt()) { DailyTicket.daily_ticket_tb_id = k->value.GetInt(); } } else if (QString(inner_obj_name) == QString("daily_ticket_clearance_customer_ids")) { if (k->value.IsArray()) { auto customerIds = k->value.GetArray(); for (rapidjson::SizeType i=0; i < customerIds.Size(); ++i) { if (customerIds[i].IsInt()) { DailyTicket.daily_ticket_clearance_customer_ids.append(customerIds[i].GetInt()); } } } } else if (QString(inner_obj_name) == QString("daily_ticket_from_min")) { if (k->value.IsString()) { std::string const from_min = k->value.GetString(); char const *from_min_c_str = from_min.c_str(); // for debugging DailyTicket.daily_ticket_from_min = QTime::fromString(from_min_c_str, Qt::ISODate); } } else if (QString(inner_obj_name) == QString("daily_ticket_to_max")) { if (k->value.IsString()) { std::string const to_max = k->value.GetString(); char const *to_max_c_str = to_max.c_str(); DailyTicket.daily_ticket_to_max = QTime::fromString(to_max_c_str, Qt::ISODate); } } else if (QString(inner_obj_name) == QString("daily_ticket_from_offset_min")) { if (k->value.IsInt()) { int const from_offset_min = k->value.GetInt(); DailyTicket.daily_ticket_from_offset_min = from_offset_min; } } else if (QString(inner_obj_name) == QString("daily_ticket_to_offset_max")) { if (k->value.IsInt()) { int to_offset_max = k->value.GetInt(); DailyTicket.daily_ticket_to_offset_max = to_offset_max; } } break; case MemberType::CustomerType: if (QString(inner_obj_name) == QString("cust_id")) { if (k->value.IsInt()) { Customer.cust_id = k->value.GetInt(); } } else if (QString(inner_obj_name) == QString("cust_type")) { if (k->value.IsInt()) { int cust_type = k->value.GetInt(); switch (cust_type) { case (int)ATBCustomer::CustomerType::ADULT: Customer.cust_type = ATBCustomer::CustomerType::ADULT; break; case (int)ATBCustomer::CustomerType::CHILD: Customer.cust_type = ATBCustomer::CustomerType::CHILD; break; case (int)ATBCustomer::CustomerType::TEEN: Customer.cust_type = ATBCustomer::CustomerType::TEEN; break; } } } else if (QString(inner_obj_name) == QString("cust_label")) { if (k->value.IsString()) { QString label(QString::fromStdString(k->value.GetString())); if (label.contains("ADULT", Qt::CaseInsensitive)) { Customer.cust_label = label; } else if (label.contains("CHILD", Qt::CaseInsensitive)) { Customer.cust_label = label; } else if (label.contains("TEEN", Qt::CaseInsensitive)) { Customer.cust_label = label; } } } break; case MemberType::CurrencyType: if (strcmp(inner_obj_name, "pcu_id") == 0) Currency.pcu_id = k->value.GetInt(); else if (strcmp(inner_obj_name, "pcu_sign") == 0) Currency.pcu_sign = k->value.GetString(); else if (strcmp(inner_obj_name, "pcu_major") == 0) Currency.pcu_major = k->value.GetString(); else if (strcmp(inner_obj_name, "pcu_minor") == 0) Currency.pcu_minor = k->value.GetString(); else if (strcmp(inner_obj_name, "pcu_active") == 0) Currency.pcu_active = k->value.GetBool(); break; case MemberType::PaymentMethodType: if (strcmp(inner_obj_name, "pme_id") == 0) PaymentMethod.pme_id = k->value.GetInt(); else if (strcmp(inner_obj_name, "pme_label") == 0) PaymentMethod.pme_label = k->value.GetString(); break; case MemberType::PaymentRateType: if (strcmp(inner_obj_name, "pra_payment_option_id") == 0) PaymentRate.pra_payment_option_id = k->value.GetInt(); else if (strcmp(inner_obj_name, "pra_payment_unit_id") == 0) PaymentRate.pra_payment_unit_id = k->value.GetInt(); else if (strcmp(inner_obj_name, "pra_price") == 0) PaymentRate.pra_price = k->value.GetDouble(); break; case MemberType::PaymentOptionType: if (strcmp(inner_obj_name, "pop_id") == 0) PaymentOption.pop_id = k->value.GetInt(); else if (strcmp(inner_obj_name, "pop_label") == 0) PaymentOption.pop_label = k->value.GetString(); else if (strcmp(inner_obj_name, "pop_payment_method_id") == 0) PaymentOption.pop_payment_method_id = k->value.GetInt(); else if (strcmp(inner_obj_name, "pop_day_end_time") == 0) PaymentOption.pop_day_end_time = k->value.GetString(); else if (strcmp(inner_obj_name, "pop_day_night_end_time") == 0) PaymentOption.pop_day_night_end_time = k->value.GetString(); else if (strcmp(inner_obj_name, "pop_price_night") == 0) PaymentOption.pop_price_night = k->value.GetDouble(); else if (strcmp(inner_obj_name, "pop_min_time") == 0) PaymentOption.pop_min_time = k->value.GetDouble(); else if (strcmp(inner_obj_name, "pop_max_time") == 0) PaymentOption.pop_max_time = k->value.GetDouble(); else if (strcmp(inner_obj_name, "pop_min_price") == 0) PaymentOption.pop_min_price = k->value.GetDouble(); else if (strcmp(inner_obj_name, "pop_carry_over") == 0) PaymentOption.pop_carry_over = k->value.GetInt(); else if (strcmp(inner_obj_name, "pop_carry_over_time_range_id") == 0) PaymentOption.pop_carry_over_time_range_id = k->value.GetInt(); else if (strcmp(inner_obj_name, "pop_daily_card_price") == 0) PaymentOption.pop_daily_card_price = k->value.GetInt(); else if (strcmp(inner_obj_name, "pop_business_hours") == 0) PaymentOption.pop_business_hours = k->value.GetInt(); else if (strcmp(inner_obj_name, "pop_time_step_config") == 0) PaymentOption.pop_time_step_config = k->value.GetInt(); this->currentPaymentOptions = PaymentOption; break; case MemberType::DurationType: if (strcmp(inner_obj_name, "pun_id") == 0) Duration.pun_id = k->value.GetInt(); else if (strcmp(inner_obj_name, "pun_label") == 0) Duration.pun_label = k->value.GetString(); else if (strcmp(inner_obj_name, "pun_duration") == 0) Duration.pun_duration = k->value.GetDouble(); else if (strcmp(inner_obj_name, "pun_duration_min") == 0) Duration.pun_duration_min = k->value.GetInt(); else if (strcmp(inner_obj_name, "pun_duration_max") == 0) Duration.pun_duration_max = k->value.GetInt(); break; case MemberType::SpecialDaysWorktimeType: if (strcmp(inner_obj_name, "pedwt_id") == 0) SpecialDaysWorktime.pedwt_id = k->value.GetInt(); else if (strcmp(inner_obj_name, "pedwt_period_exc_day_id") == 0) SpecialDaysWorktime.pedwt_period_exc_day_id = k->value.GetInt(); else if (strcmp(inner_obj_name, "pedwt_time_from") == 0) SpecialDaysWorktime.pedwt_time_from = k->value.GetString(); else if (strcmp(inner_obj_name, "pedwt_time_to") == 0) SpecialDaysWorktime.pedwt_time_to = k->value.GetString(); else if (strcmp(inner_obj_name, "pedwt_price") == 0) SpecialDaysWorktime.pedwt_price = k->value.GetDouble(); break; /*case MemberType::WeekDaysType: if (strcmp(inner_obj_name, "pdiw_id") == 0) WeekDays.pdiw_id = k->value.GetInt(); else if (strcmp(inner_obj_name, "pdiw_label") == 0) WeekDays.pdiw_label = k->value.GetString(); else if (strcmp(inner_obj_name, "pdiw_index") == 0) WeekDays.pdiw_index = k->value.GetInt(); break;*/ case MemberType::WeekDaysWorkTimeType: if (strcmp(inner_obj_name, "pwd_id") == 0) WeekDaysWorktime.pwd_id = k->value.GetInt(); else if (strcmp(inner_obj_name, "pwd_period_week_day_id") == 0) WeekDaysWorktime.pwd_period_week_day_id = k->value.GetInt(); else if (strcmp(inner_obj_name, "pwd_period_day_in_week_id") == 0) WeekDaysWorktime.pwd_period_day_in_week_id = k->value.GetInt(); else if (strcmp(inner_obj_name, "pwd_time_from") == 0) WeekDaysWorktime.pwd_time_from = k->value.GetString(); else if (strcmp(inner_obj_name, "pwd_time_to") == 0) WeekDaysWorktime.pwd_time_to = k->value.GetString(); break; case MemberType::SpecialDaysType: if (strcmp(inner_obj_name, "ped_id") == 0) SpecialDays.ped_id = k->value.GetInt(); else if (strcmp(inner_obj_name, "ped_label") == 0) SpecialDays.ped_label = k->value.GetString(); else if (strcmp(inner_obj_name, "ped_date_start") == 0) SpecialDays.ped_date_start = k->value.GetString(); else if (strcmp(inner_obj_name, "ped_date_end") == 0) SpecialDays.ped_date_end = k->value.GetString(); else if (strcmp(inner_obj_name, "ped_period_special_day_id") == 0) SpecialDays.ped_period_special_day_id = k->value.GetInt(); else if (strcmp(inner_obj_name, "ped_year") == 0) SpecialDays.ped_year = k->value.GetInt(); break; case MemberType::PeriodYearType: if (strcmp(inner_obj_name, "pye_id") == 0) YearPeriod.pye_id = k->value.GetInt(); else if (strcmp(inner_obj_name, "pye_label") == 0) YearPeriod.pye_label = k->value.GetString(); else if (strcmp(inner_obj_name, "pye_start_month") == 0) YearPeriod.pye_start_month = k->value.GetInt(); else if (strcmp(inner_obj_name, "pye_start_day") == 0) YearPeriod.pye_start_day = k->value.GetInt(); else if (strcmp(inner_obj_name, "pye_end_month") == 0) YearPeriod.pye_end_month = k->value.GetInt(); else if (strcmp(inner_obj_name, "pye_end_day") == 0) YearPeriod.pye_end_day = k->value.GetInt(); break; default: break; } //#pragma endregion } // Push to specific list depending on member type switch (mb_type) { case MemberType::UnknownType: break; case MemberType::PaymentMethodType: cfg->PaymentMethod.insert(pair(PaymentMethod.pme_id, PaymentMethod)); break; case MemberType::PaymentRateType: cfg->PaymentRate.insert(pair(PaymentRate.pra_payment_option_id, PaymentRate)); break; case MemberType::PaymentOptionType: cfg->PaymentOption.insert(pair(PaymentOption.pop_payment_method_id, PaymentOption)); break; case MemberType::DurationType: cfg->Duration.insert(pair(Duration.pun_id, Duration)); break; case MemberType::SpecialDaysWorktimeType: cfg->SpecialDaysWorktime.insert(pair(SpecialDaysWorktime.pedwt_period_exc_day_id, SpecialDaysWorktime)); break; //case MemberType::WeekDaysType: // cfg->WeekDays.insert(pair(WeekDays.pdiw_index, WeekDays)); // break; case MemberType::WeekDaysWorkTimeType: cfg->WeekDaysWorktime.insert(pair(WeekDaysWorktime.pwd_period_day_in_week_id, WeekDaysWorktime)); break; case MemberType::SpecialDaysType: cfg->SpecialDays.insert(pair(SpecialDays.ped_id, SpecialDays)); break; case MemberType::PeriodYearType: cfg->YearPeriod.insert(pair(YearPeriod.pye_id, YearPeriod)); break; case MemberType::DailyTicketType: cfg->DailyTicket.insert(pair(DailyTicket.daily_ticket_id, DailyTicket)); qCritical() << DailyTicket; DailyTicket.reset(); break; case MemberType::CustomerType: // qCritical() << Customer; break; case MemberType::TimeBaseType: qCritical() << TimeBase; cfg->TimeBase.insert(pair(TimeBase.tbase_id, TimeBase)); break; case MemberType::TimeRangeType: qCritical() << TimeRange; cfg->TimeRange.insert(pair(TimeRange.time_range_id, TimeRange)); break; case MemberType::TimeStepConfigType: qCritical() << TimeStepConfig; cfg->TimeStepConfig.insert(pair(TimeStepConfig.tsconfig_id, TimeStepConfig)); break; default: break; } } } return true; } catch (...) { printf("%s\n", "General exception has occurred while parsing JSON\n"); return false; } } ATBPaymentOption const &Configuration::getPaymentOptions() const { return this->currentPaymentOptions; } ATBPaymentOption &Configuration::getPaymentOptions() { return this->currentPaymentOptions; }