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)
51 //-----------------------------------------------------------------------------
52 int TariffsImpl::ReadTariffs()
54 std::lock_guard<std::mutex> lock(m_mutex);
56 std::vector<std::string> tariffsList;
57 if (store->GetTariffsList(&tariffsList))
59 WriteServLog("Cannot get tariffs list.");
60 WriteServLog("%s", store->GetStrError().c_str());
63 Data::size_type tariffsNum = tariffsList.size();
65 for (Data::size_type i = 0; i < tariffsNum; i++)
68 if (store->RestoreTariff(&td, tariffsList[i]))
70 WriteServLog("Cannot read tariff %s.", tariffsList[i].c_str());
71 WriteServLog("%s", store->GetStrError().c_str());
74 tariffs.push_back(TariffImpl(td));
79 //-----------------------------------------------------------------------------
80 size_t TariffsImpl::Count() const
82 std::lock_guard<std::mutex> lock(m_mutex);
83 return tariffs.size();
85 //-----------------------------------------------------------------------------
86 const STG::Tariff* TariffsImpl::FindByName(const std::string & name) const
88 if (name == NO_TARIFF_NAME)
91 std::lock_guard<std::mutex> lock(m_mutex);
92 const auto ti = find(tariffs.begin(), tariffs.end(), TariffImpl(name));
94 if (ti != tariffs.end())
99 //-----------------------------------------------------------------------------
100 int TariffsImpl::Chg(const TariffData & td, const Admin * admin)
102 const auto& priv = admin->priv();
106 std::string s = admin->logStr() + " Change tariff \'"
107 + td.tariffConf.name + "\'. Access denied.";
108 strError = "Access denied.";
109 WriteServLog(s.c_str());
113 std::lock_guard<std::mutex> lock(m_mutex);
115 auto ti = find(tariffs.begin(), tariffs.end(), TariffImpl(td.tariffConf.name));
117 if (ti == tariffs.end())
119 strError = "Tariff \'" + td.tariffConf.name + "\' cannot be changed. Tariff does not exist.";
120 WriteServLog("%s %s", admin->logStr().c_str(), strError.c_str());
126 if (store->SaveTariff(td, td.tariffConf.name))
128 std::string error = "Tariff " + td.tariffConf.name + " writing error. " + store->GetStrError();
129 WriteServLog(error.c_str());
133 WriteServLog("%s Tariff \'%s\' changed.",
134 admin->logStr().c_str(), td.tariffConf.name.c_str());
138 //-----------------------------------------------------------------------------
139 int TariffsImpl::Del(const std::string & name, const Admin * admin)
141 const auto& priv = admin->priv();
145 std::string s = admin->logStr() + " Delete tariff \'"
146 + name + "\'. Access denied.";
147 strError = "Access denied.";
148 WriteServLog(s.c_str());
155 std::lock_guard<std::mutex> lock(m_mutex);
157 const auto ti = find(tariffs.begin(), tariffs.end(), TariffImpl(name));
159 if (ti == tariffs.end())
161 strError = "Tariff \'" + name + "\' cannot be deleted. Tariff does not exist.";
162 WriteServLog("%s %s", admin->logStr().c_str(), strError.c_str());
166 if (store->DelTariff(name))
168 WriteServLog("Cannot delete tariff %s.", name.c_str());
169 WriteServLog("%s", store->GetStrError().c_str());
173 td = ti->GetTariffData();
178 auto ni = onDelNotifiers.begin();
179 while (ni != onDelNotifiers.end())
185 WriteServLog("%s Tariff \'%s\' deleted.",
186 admin->logStr().c_str(),
190 //-----------------------------------------------------------------------------
191 int TariffsImpl::Add(const std::string & name, const Admin * admin)
193 const auto& priv = admin->priv();
197 std::string s = admin->logStr() + " Add tariff \'"
198 + name + "\'. Access denied.";
199 strError = "Access denied.";
200 WriteServLog(s.c_str());
205 std::lock_guard<std::mutex> lock(m_mutex);
207 const auto ti = find(tariffs.begin(), tariffs.end(), TariffImpl(name));
209 if (ti != tariffs.end())
211 strError = "Tariff \'" + name + "\' cannot be added. Tariff already exist.";
212 WriteServLog("%s %s", admin->logStr().c_str(), strError.c_str());
216 tariffs.push_back(TariffImpl(name));
219 if (store->AddTariff(name) < 0)
221 strError = "Tariff " + name + " adding error. " + store->GetStrError();
222 WriteServLog(strError.c_str());
226 // Fire all "on add" notifiers
227 auto ni = onAddNotifiers.begin();
228 while (ni != onAddNotifiers.end())
230 (*ni)->Notify(tariffs.back().GetTariffData());
234 WriteServLog("%s Tariff \'%s\' added.",
235 admin->logStr().c_str(), name.c_str());
239 //-----------------------------------------------------------------------------
240 void TariffsImpl::GetTariffsData(std::vector<TariffData> * tdl) const
242 assert(tdl != NULL && "Tariffs data list is not null");
243 std::lock_guard<std::mutex> lock(m_mutex);
245 auto it = tariffs.begin();
246 for (; it != tariffs.end(); ++it)
248 tdl->push_back(it->GetTariffData());
251 //-----------------------------------------------------------------------------
252 void TariffsImpl::AddNotifierAdd(NotifierBase<TariffData> * n)
254 std::lock_guard<std::mutex> lock(m_mutex);
255 onAddNotifiers.insert(n);
257 //-----------------------------------------------------------------------------
258 void TariffsImpl::DelNotifierAdd(NotifierBase<TariffData> * n)
260 std::lock_guard<std::mutex> lock(m_mutex);
261 onAddNotifiers.erase(n);
263 //-----------------------------------------------------------------------------
264 void TariffsImpl::AddNotifierDel(NotifierBase<TariffData> * n)
266 std::lock_guard<std::mutex> lock(m_mutex);
267 onDelNotifiers.insert(n);
269 //-----------------------------------------------------------------------------
270 void TariffsImpl::DelNotifierDel(NotifierBase<TariffData> * n)
272 std::lock_guard<std::mutex> lock(m_mutex);
273 onDelNotifiers.erase(n);
275 //-----------------------------------------------------------------------------