]> git.stg.codes - stg.git/blob - libs/srvconf/parsers/chg_tariff.cpp
Merge pull request #1 from yuriio147/mysql-store-db-port
[stg.git] / libs / srvconf / parsers / chg_tariff.cpp
1 /*
2  *    This program is free software; you can redistribute it and/or modify
3  *    it under the terms of the GNU General Public License as published by
4  *    the Free Software Foundation; either version 2 of the License, or
5  *    (at your option) any later version.
6  *
7  *    This program is distributed in the hope that it will be useful,
8  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
9  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  *    GNU General Public License for more details.
11  *
12  *    You should have received a copy of the GNU General Public License
13  *    along with this program; if not, write to the Free Software
14  *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
15  */
16
17 /*
18  *    Author : Maxim Mamontov <faust@stargazer.dp.ua>
19  */
20
21 #include "chg_tariff.h"
22
23 #include "optional_utils.h"
24
25 #include "stg/tariff_conf.h"
26 #include "stg/common.h"
27
28 #include <sstream>
29
30 #include <strings.h>
31
32 using namespace STG;
33
34 namespace
35 {
36
37 template <typename A, typename T>
38 void appendSlashedResetable(std::ostream& stream, const std::string& name, const A& array, T A::value_type::* field)
39 {
40     std::string res;
41     for (typename A::size_type i = 0; i < array.size(); ++i)
42     {
43         if ((array[i].*field).empty()) // All values must be set
44             return;
45         if (!res.empty())
46             res += "/";
47         res += std::to_string((array[i].*field).data());
48     }
49     stream << "<" << name << " value=\"" << res << "\"/>";
50 }
51
52 } // namespace anonymous
53
54 std::string ChgTariff::serialize(const TariffDataOpt& data, const std::string& /*encoding*/)
55 {
56     std::ostringstream stream;
57
58     appendResetableTag(stream, "fee", data.tariffConf.fee);
59     appendResetableTag(stream, "passiveCost", data.tariffConf.passiveCost);
60     appendResetableTag(stream, "free", data.tariffConf.free);
61
62     if (!data.tariffConf.traffType.empty())
63         stream << "<traffType value=\"" + Tariff::toString(data.tariffConf.traffType.data()) + "\"/>";
64
65     if (!data.tariffConf.period.empty())
66         switch (data.tariffConf.period.data())
67         {
68             case Tariff::DAY: stream << "<period value=\"day\"/>"; break;
69             case Tariff::MONTH: stream << "<period value=\"month\"/>"; break;
70         }
71
72     if (!data.tariffConf.changePolicy.empty())
73         switch (data.tariffConf.changePolicy.data())
74         {
75             case Tariff::ALLOW: stream << "<changePolicy value=\"allow\"/>"; break;
76             case Tariff::TO_CHEAP: stream << "<changePolicy value=\"to_cheap\"/>"; break;
77             case Tariff::TO_EXPENSIVE: stream << "<changePolicy value=\"to_expensive\"/>"; break;
78             case Tariff::DENY: stream << "<changePolicy value=\"deny\"/>"; break;
79         }
80
81     appendResetableTag(stream, "changePolicyTimeout", data.tariffConf.changePolicyTimeout);
82     for (size_t i = 0; i < DIR_NUM; ++i)
83         if (!data.dirPrice[i].hDay.empty() &&
84             !data.dirPrice[i].mDay.empty() &&
85             !data.dirPrice[i].hNight.empty() &&
86             !data.dirPrice[i].mNight.empty())
87             stream << "<time" << i << " value=\"" << data.dirPrice[i].hDay.data() << ":"
88                                                   << data.dirPrice[i].mDay.data() << "-"
89                                                   << data.dirPrice[i].hNight.data() << ":"
90                                                   << data.dirPrice[i].mNight.data() << "\"/>";
91
92     appendSlashedResetable(stream, "priceDayA", data.dirPrice, &DirPriceDataOpt::priceDayA);
93     appendSlashedResetable(stream, "priceDayB", data.dirPrice, &DirPriceDataOpt::priceDayB);
94     appendSlashedResetable(stream, "priceNightA", data.dirPrice, &DirPriceDataOpt::priceNightA);
95     appendSlashedResetable(stream, "priceNightB", data.dirPrice, &DirPriceDataOpt::priceNightB);
96     appendSlashedResetable(stream, "singlePrice", data.dirPrice, &DirPriceDataOpt::singlePrice);
97     appendSlashedResetable(stream, "noDiscount", data.dirPrice, &DirPriceDataOpt::noDiscount);
98     appendSlashedResetable(stream, "threshold", data.dirPrice, &DirPriceDataOpt::threshold);
99
100     return stream.str();
101 }