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.
 
   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.
 
  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
 
  18  *    Author : Boris Mikhailenko <stg34@stargazer.dp.ua>
 
  19  *    Author : Maxim Mamontov <faust@stargazer.dp.ua>
 
  22 #include "parser_tariffs.h"
 
  24 #include "stg/tariffs.h"
 
  25 #include "stg/users.h"
 
  26 #include "stg/resetable.h"
 
  28 #include <cstdio> // snprintf
 
  31 using STG::PARSER::GET_TARIFFS;
 
  32 using STG::PARSER::ADD_TARIFF;
 
  33 using STG::PARSER::DEL_TARIFF;
 
  34 using STG::PARSER::CHG_TARIFF;
 
  36 const char * GET_TARIFFS::tag = "GetTariffs";
 
  37 const char * ADD_TARIFF::tag  = "AddTariff";
 
  38 const char * DEL_TARIFF::tag  = "DelTariff";
 
  39 const char * CHG_TARIFF::tag  = "SetTariff";
 
  44 const double pt_mega = 1024 * 1024;
 
  46 template <typename A, typename C, typename F>
 
  47 std::string AOS2String(const A & array, size_t size, const F C::* field, F multiplier)
 
  50     for (size_t i = 0; i < size; ++i)
 
  54         res += x2str((array[i].*field) * multiplier);
 
  60 bool str2res(const std::string& source, RESETABLE<T>& dest, T divisor)
 
  63     if (str2x(source, value))
 
  65     dest = value / divisor;
 
  69 template <typename A, typename C, typename F>
 
  70 bool String2AOS(const std::string & source, A & array, size_t size, RESETABLE<F> C::* field, F divisor)
 
  73     std::string::size_type from = 0;
 
  74     std::string::size_type pos = 0;
 
  75     while (index < size && (pos = source.find('/', from)) != std::string::npos)
 
  77         if (!str2res(source.substr(from, pos - from), array[index].*field, divisor))
 
  82     if (str2res(source.substr(from), array[index].*field, divisor))
 
  89 void GET_TARIFFS::CreateAnswer()
 
  91     m_answer = "<Tariffs>";
 
  93     std::list<TARIFF_DATA> dataList;
 
  94     m_tariffs.GetTariffsData(&dataList);
 
  95     std::list<TARIFF_DATA>::const_iterator it = dataList.begin();
 
  96     for (; it != dataList.end(); ++it)
 
  98         m_answer += "<tariff name=\"" + it->tariffConf.name + "\">";
 
 100         for (size_t i = 0; i < DIR_NUM; i++)
 
 101             m_answer += "<Time" + x2str(i) + " value=\"" +
 
 102                 x2str(it->dirPrice[i].hDay)   + ":" + x2str(it->dirPrice[i].mDay)   + "-" +
 
 103                 x2str(it->dirPrice[i].hNight) + ":" + x2str(it->dirPrice[i].mNight) + "\"/>";
 
 105         m_answer += "<PriceDayA value=\"" + AOS2String(it->dirPrice, DIR_NUM, &DIRPRICE_DATA::priceDayA, pt_mega) + "\"/>" +
 
 106                   "<PriceDayB value=\"" + AOS2String(it->dirPrice, DIR_NUM, &DIRPRICE_DATA::priceDayB, pt_mega) + "\"/>" +
 
 107                   "<PriceNightA value=\"" + AOS2String(it->dirPrice, DIR_NUM, &DIRPRICE_DATA::priceNightA, pt_mega) + "\"/>" +
 
 108                   "<PriceNightB value=\"" + AOS2String(it->dirPrice, DIR_NUM, &DIRPRICE_DATA::priceNightB, pt_mega) + "\"/>" +
 
 109                   "<Threshold value=\"" + AOS2String(it->dirPrice, DIR_NUM, &DIRPRICE_DATA::threshold, 1) + "\"/>" +
 
 110                   "<SinglePrice value=\"" + AOS2String(it->dirPrice, DIR_NUM, &DIRPRICE_DATA::singlePrice, 1) + "\"/>" +
 
 111                   "<NoDiscount value=\"" + AOS2String(it->dirPrice, DIR_NUM, &DIRPRICE_DATA::noDiscount, 1) + "\"/>" +
 
 112                   "<Fee value=\"" + x2str(it->tariffConf.fee) + "\"/>" +
 
 113                   "<PassiveCost value=\"" + x2str(it->tariffConf.passiveCost) + "\"/>" +
 
 114                   "<Free value=\"" + x2str(it->tariffConf.free) + "\"/>" +
 
 115                   "<TraffType value=\"" + TARIFF::TraffTypeToString(it->tariffConf.traffType) + "\"/>" +
 
 116                   "<Period value=\"" + TARIFF::PeriodToString(it->tariffConf.period) + "\"/>" +
 
 117                   "<ChangePolicy value=\"" + TARIFF::ChangePolicyToString(it->tariffConf.changePolicy) + "\"/>" +
 
 121     m_answer += "</Tariffs>";
 
 124 int ADD_TARIFF::Start(void *, const char * el, const char ** attr)
 
 126     if (strcasecmp(el, m_tag.c_str()) != 0)
 
 136 void ADD_TARIFF::CreateAnswer()
 
 138     if (m_tariffs.Add(tariff, &m_currAdmin) == 0)
 
 139         m_answer = "<" + m_tag + " Result=\"Ok\"/>";
 
 141         m_answer = "<" + m_tag + " Result=\"Error. " + m_tariffs.GetStrError() + "\"/>";
 
 144 int DEL_TARIFF::Start(void *, const char * el, const char ** attr)
 
 146     if (strcasecmp(el, m_tag.c_str()) != 0)
 
 156 void DEL_TARIFF::CreateAnswer()
 
 158     if (m_users.TariffInUse(tariff))
 
 159         m_answer = "<" + m_tag + " Result=\"Error. Tariff \'" + tariff + "\' cannot be deleted, it is in use.\"/>";
 
 160     else if (m_tariffs.Del(tariff, &m_currAdmin) == 0)
 
 161         m_answer = "<" + m_tag + " Result=\"Ok\"/>";
 
 163         m_answer = "<" + m_tag + " Result=\"Error. " + m_tariffs.GetStrError() + "\"/>";
 
 166 int CHG_TARIFF::Start(void *, const char * el, const char ** attr)
 
 172         if (strcasecmp(el, m_tag.c_str()) == 0)
 
 174             const TARIFF * tariff = m_tariffs.FindByName(attr[1]);
 
 176                 td = tariff->GetTariffData();
 
 182         if (strcasecmp(el, "PriceDayA") == 0)
 
 184             if (!String2AOS(attr[1], td.dirPrice, DIR_NUM, &DIRPRICE_DATA_RES::priceDayA, pt_mega))
 
 185                 return -1; // TODO: log it
 
 190         if (strcasecmp(el, "PriceDayB") == 0)
 
 192             if (!String2AOS(attr[1], td.dirPrice, DIR_NUM, &DIRPRICE_DATA_RES::priceDayB, pt_mega))
 
 193                 return -1; // TODO: log it
 
 198         if (strcasecmp(el, "PriceNightA") == 0)
 
 200             if (!String2AOS(attr[1], td.dirPrice, DIR_NUM, &DIRPRICE_DATA_RES::priceNightA, pt_mega))
 
 201                 return -1; // TODO: log it
 
 206         if (strcasecmp(el, "PriceNightB") == 0)
 
 208             if (!String2AOS(attr[1], td.dirPrice, DIR_NUM, &DIRPRICE_DATA_RES::priceNightB, pt_mega))
 
 209                 return -1; // TODO: log it
 
 214         if (strcasecmp(el, "Threshold") == 0)
 
 216             if (!String2AOS(attr[1], td.dirPrice, DIR_NUM, &DIRPRICE_DATA_RES::threshold, 1))
 
 217                 return -1; // TODO: log it
 
 222         if (strcasecmp(el, "SinglePrice") == 0)
 
 224             if (!String2AOS(attr[1], td.dirPrice, DIR_NUM, &DIRPRICE_DATA_RES::singlePrice, 1))
 
 225                 return -1; // TODO: log it
 
 230         if (strcasecmp(el, "NoDiscount") == 0)
 
 232             if (!String2AOS(attr[1], td.dirPrice, DIR_NUM, &DIRPRICE_DATA_RES::noDiscount, 1))
 
 233                 return -1; // TODO: log it
 
 238         for (int j = 0; j < DIR_NUM; j++)
 
 241             snprintf(st, 50, "Time%d", j);
 
 242             if (strcasecmp(el, st) == 0)
 
 248                 if (ParseTariffTimeStr(attr[1], h1, m1, h2, m2) == 0)
 
 250                     td.dirPrice[j].hDay = h1;
 
 251                     td.dirPrice[j].mDay = m1;
 
 252                     td.dirPrice[j].hNight = h2;
 
 253                     td.dirPrice[j].mNight = m2;
 
 259         if (strcasecmp(el, "Fee") == 0)
 
 262             if (strtodouble2(attr[1], fee) == 0)
 
 263                 td.tariffConf.fee = fee;
 
 267         if (strcasecmp(el, "PassiveCost") == 0)
 
 270             if (strtodouble2(attr[1], pc) == 0)
 
 271                 td.tariffConf.passiveCost = pc;
 
 275         if (strcasecmp(el, "Free") == 0)
 
 278             if (strtodouble2(attr[1], free) == 0)
 
 279                 td.tariffConf.free = free;
 
 283         if (strcasecmp(el, "TraffType") == 0)
 
 285             td.tariffConf.traffType = TARIFF::StringToTraffType(attr[1]);
 
 289         if (strcasecmp(el, "Period") == 0)
 
 291             td.tariffConf.period = TARIFF::StringToPeriod(attr[1]);
 
 295         if (strcasecmp(el, "ChangePolicy") == 0)
 
 297             td.tariffConf.changePolicy = TARIFF::StringToChangePolicy(attr[1]);
 
 304 void CHG_TARIFF::CreateAnswer()
 
 306     if (!td.tariffConf.name.data().empty())
 
 308         TARIFF_DATA tariffData = td.GetData();
 
 309         if (m_tariffs.Chg(tariffData, &m_currAdmin) == 0)
 
 310             m_answer = "<" + m_tag + " Result=\"ok\"/>";
 
 312             m_answer = "<" + m_tag + " Result=\"Change tariff error! " + m_tariffs.GetStrError() + "\"/>";
 
 315         m_answer = "<" + m_tag + " Result=\"Change tariff error!\"/>";