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 $
35 #include "stg/locker.h"
36 #include "stg/logger.h"
37 #include "stg/store.h"
38 #include "stg/admin.h"
39 #include "stg/admin_conf.h"
40 #include "tariffs_impl.h"
42 using STG::TariffsImpl;
44 //-----------------------------------------------------------------------------
45 TariffsImpl::TariffsImpl(Store * st)
47 WriteServLog(Logger::get()),
48 noTariff(NO_TARIFF_NAME)
52 //-----------------------------------------------------------------------------
53 int TariffsImpl::ReadTariffs()
55 std::lock_guard<std::mutex> lock(m_mutex);
57 std::vector<std::string> tariffsList;
58 if (store->GetTariffsList(&tariffsList))
60 WriteServLog("Cannot get tariffs list.");
61 WriteServLog("%s", store->GetStrError().c_str());
64 Data::size_type tariffsNum = tariffsList.size();
66 for (Data::size_type i = 0; i < tariffsNum; i++)
69 if (store->RestoreTariff(&td, tariffsList[i]))
71 WriteServLog("Cannot read tariff %s.", tariffsList[i].c_str());
72 WriteServLog("%s", store->GetStrError().c_str());
75 tariffs.push_back(TariffImpl(td));
80 //-----------------------------------------------------------------------------
81 size_t TariffsImpl::Count() const
83 std::lock_guard<std::mutex> lock(m_mutex);
84 return tariffs.size();
86 //-----------------------------------------------------------------------------
87 const STG::Tariff* TariffsImpl::FindByName(const std::string & name) const
89 if (name == NO_TARIFF_NAME)
92 std::lock_guard<std::mutex> lock(m_mutex);
93 const auto ti = find(tariffs.begin(), tariffs.end(), TariffImpl(name));
95 if (ti != tariffs.end())
100 //-----------------------------------------------------------------------------
101 int TariffsImpl::Chg(const TariffData & td, const Admin * admin)
103 const auto& priv = admin->priv();
107 std::string s = admin->logStr() + " Change tariff \'"
108 + td.tariffConf.name + "\'. Access denied.";
109 strError = "Access denied.";
110 WriteServLog(s.c_str());
114 std::lock_guard<std::mutex> lock(m_mutex);
116 auto ti = find(tariffs.begin(), tariffs.end(), TariffImpl(td.tariffConf.name));
118 if (ti == tariffs.end())
120 strError = "Tariff \'" + td.tariffConf.name + "\' cannot be changed. Tariff does not exist.";
121 WriteServLog("%s %s", admin->logStr().c_str(), strError.c_str());
127 if (store->SaveTariff(td, td.tariffConf.name))
129 std::string error = "Tariff " + td.tariffConf.name + " writing error. " + store->GetStrError();
130 WriteServLog(error.c_str());
134 WriteServLog("%s Tariff \'%s\' changed.",
135 admin->logStr().c_str(), td.tariffConf.name.c_str());
139 //-----------------------------------------------------------------------------
140 int TariffsImpl::Del(const std::string & name, const Admin * admin)
142 const auto& priv = admin->priv();
146 std::string s = admin->logStr() + " Delete tariff \'"
147 + name + "\'. Access denied.";
148 strError = "Access denied.";
149 WriteServLog(s.c_str());
156 std::lock_guard<std::mutex> lock(m_mutex);
158 const auto ti = find(tariffs.begin(), tariffs.end(), TariffImpl(name));
160 if (ti == tariffs.end())
162 strError = "Tariff \'" + name + "\' cannot be deleted. Tariff does not exist.";
163 WriteServLog("%s %s", admin->logStr().c_str(), strError.c_str());
167 if (store->DelTariff(name))
169 WriteServLog("Cannot delete tariff %s.", name.c_str());
170 WriteServLog("%s", store->GetStrError().c_str());
174 td = ti->GetTariffData();
179 auto ni = onDelNotifiers.begin();
180 while (ni != onDelNotifiers.end())
186 WriteServLog("%s Tariff \'%s\' deleted.",
187 admin->logStr().c_str(),
191 //-----------------------------------------------------------------------------
192 int TariffsImpl::Add(const std::string & name, const Admin * admin)
194 const auto& priv = admin->priv();
198 std::string s = admin->logStr() + " Add tariff \'"
199 + name + "\'. Access denied.";
200 strError = "Access denied.";
201 WriteServLog(s.c_str());
206 std::lock_guard<std::mutex> lock(m_mutex);
208 const auto ti = find(tariffs.begin(), tariffs.end(), TariffImpl(name));
210 if (ti != tariffs.end())
212 strError = "Tariff \'" + name + "\' cannot be added. Tariff already exist.";
213 WriteServLog("%s %s", admin->logStr().c_str(), strError.c_str());
217 tariffs.push_back(TariffImpl(name));
220 if (store->AddTariff(name) < 0)
222 strError = "Tariff " + name + " adding error. " + store->GetStrError();
223 WriteServLog(strError.c_str());
227 // Fire all "on add" notifiers
228 auto ni = onAddNotifiers.begin();
229 while (ni != onAddNotifiers.end())
231 (*ni)->Notify(tariffs.back().GetTariffData());
235 WriteServLog("%s Tariff \'%s\' added.",
236 admin->logStr().c_str(), name.c_str());
240 //-----------------------------------------------------------------------------
241 void TariffsImpl::GetTariffsData(std::vector<TariffData> * tdl) const
243 assert(tdl != NULL && "Tariffs data list is not null");
244 std::lock_guard<std::mutex> lock(m_mutex);
246 auto it = tariffs.begin();
247 for (; it != tariffs.end(); ++it)
249 tdl->push_back(it->GetTariffData());
252 //-----------------------------------------------------------------------------
253 void TariffsImpl::AddNotifierAdd(NotifierBase<TariffData> * n)
255 std::lock_guard<std::mutex> lock(m_mutex);
256 onAddNotifiers.insert(n);
258 //-----------------------------------------------------------------------------
259 void TariffsImpl::DelNotifierAdd(NotifierBase<TariffData> * n)
261 std::lock_guard<std::mutex> lock(m_mutex);
262 onAddNotifiers.erase(n);
264 //-----------------------------------------------------------------------------
265 void TariffsImpl::AddNotifierDel(NotifierBase<TariffData> * n)
267 std::lock_guard<std::mutex> lock(m_mutex);
268 onDelNotifiers.insert(n);
270 //-----------------------------------------------------------------------------
271 void TariffsImpl::DelNotifierDel(NotifierBase<TariffData> * n)
273 std::lock_guard<std::mutex> lock(m_mutex);
274 onDelNotifiers.erase(n);
276 //-----------------------------------------------------------------------------