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
22 * Author : Boris Mikhailenko <stg34@stargazer.dp.ua>
27 $Date: 2010/10/07 18:43:21 $
31 #include "tariffs_impl.h"
33 #include "stg/logger.h"
34 #include "stg/store.h"
35 #include "stg/admin.h"
36 #include "stg/admin_conf.h"
41 using STG::TariffsImpl;
43 //-----------------------------------------------------------------------------
44 TariffsImpl::TariffsImpl(Store * st)
46 WriteServLog(Logger::get()),
47 noTariff(NO_TARIFF_NAME)
50 //-----------------------------------------------------------------------------
51 int TariffsImpl::ReadTariffs()
53 std::lock_guard<std::mutex> lock(m_mutex);
55 std::vector<std::string> tariffsList;
56 if (store->GetTariffsList(&tariffsList))
58 WriteServLog("Cannot get tariffs list.");
59 WriteServLog("%s", store->GetStrError().c_str());
62 Data::size_type tariffsNum = tariffsList.size();
64 for (Data::size_type i = 0; i < tariffsNum; i++)
67 if (store->RestoreTariff(&td, tariffsList[i]))
69 WriteServLog("Cannot read tariff %s.", tariffsList[i].c_str());
70 WriteServLog("%s", store->GetStrError().c_str());
73 tariffs.push_back(TariffImpl(td));
78 //-----------------------------------------------------------------------------
79 size_t TariffsImpl::Count() const
81 std::lock_guard<std::mutex> lock(m_mutex);
82 return tariffs.size();
84 //-----------------------------------------------------------------------------
85 const STG::Tariff* TariffsImpl::FindByName(const std::string & name) const
87 if (name == NO_TARIFF_NAME)
90 std::lock_guard<std::mutex> lock(m_mutex);
91 const auto ti = find(tariffs.begin(), tariffs.end(), TariffImpl(name));
93 if (ti != tariffs.end())
98 //-----------------------------------------------------------------------------
99 int TariffsImpl::Chg(const TariffData & td, const Admin * admin)
101 const auto& priv = admin->priv();
105 std::string s = admin->logStr() + " Change tariff \'"
106 + td.tariffConf.name + "\'. Access denied.";
107 strError = "Access denied.";
108 WriteServLog(s.c_str());
112 std::lock_guard<std::mutex> lock(m_mutex);
114 auto ti = find(tariffs.begin(), tariffs.end(), TariffImpl(td.tariffConf.name));
116 if (ti == tariffs.end())
118 strError = "Tariff \'" + td.tariffConf.name + "\' cannot be changed. Tariff does not exist.";
119 WriteServLog("%s %s", admin->logStr().c_str(), strError.c_str());
125 if (store->SaveTariff(td, td.tariffConf.name))
127 std::string error = "Tariff " + td.tariffConf.name + " writing error. " + store->GetStrError();
128 WriteServLog(error.c_str());
132 WriteServLog("%s Tariff \'%s\' changed.",
133 admin->logStr().c_str(), td.tariffConf.name.c_str());
137 //-----------------------------------------------------------------------------
138 int TariffsImpl::Del(const std::string & name, const Admin * admin)
140 const auto& priv = admin->priv();
144 std::string s = admin->logStr() + " Delete tariff \'"
145 + name + "\'. Access denied.";
146 strError = "Access denied.";
147 WriteServLog(s.c_str());
154 std::lock_guard<std::mutex> lock(m_mutex);
156 const auto ti = find(tariffs.begin(), tariffs.end(), TariffImpl(name));
158 if (ti == tariffs.end())
160 strError = "Tariff \'" + name + "\' cannot be deleted. Tariff does not exist.";
161 WriteServLog("%s %s", admin->logStr().c_str(), strError.c_str());
165 if (store->DelTariff(name))
167 WriteServLog("Cannot delete tariff %s.", name.c_str());
168 WriteServLog("%s", store->GetStrError().c_str());
172 td = ti->GetTariffData();
177 m_onDelCallbacks.notify(td);
179 WriteServLog("%s Tariff \'%s\' deleted.",
180 admin->logStr().c_str(),
184 //-----------------------------------------------------------------------------
185 int TariffsImpl::Add(const std::string & name, const Admin * admin)
187 const auto& priv = admin->priv();
191 std::string s = admin->logStr() + " Add tariff \'"
192 + name + "\'. Access denied.";
193 strError = "Access denied.";
194 WriteServLog(s.c_str());
199 std::lock_guard<std::mutex> lock(m_mutex);
201 const auto ti = find(tariffs.begin(), tariffs.end(), TariffImpl(name));
203 if (ti != tariffs.end())
205 strError = "Tariff \'" + name + "\' cannot be added. Tariff already exist.";
206 WriteServLog("%s %s", admin->logStr().c_str(), strError.c_str());
210 tariffs.push_back(TariffImpl(name));
213 if (store->AddTariff(name) < 0)
215 strError = "Tariff " + name + " adding error. " + store->GetStrError();
216 WriteServLog(strError.c_str());
220 m_onAddCallbacks.notify(tariffs.back().GetTariffData());
222 WriteServLog("%s Tariff \'%s\' added.",
223 admin->logStr().c_str(), name.c_str());
227 //-----------------------------------------------------------------------------
228 void TariffsImpl::GetTariffsData(std::vector<TariffData> * tdl) const
230 assert(tdl != NULL && "Tariffs data list is not null");
231 std::lock_guard<std::mutex> lock(m_mutex);
233 auto it = tariffs.begin();
234 for (; it != tariffs.end(); ++it)
236 tdl->push_back(it->GetTariffData());
239 //-----------------------------------------------------------------------------