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/locker.h"
34 #include "stg/logger.h"
35 #include "stg/store.h"
36 #include "stg/admin.h"
37 #include "stg/admin_conf.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 m_onDelCallbacks.notify(td);
180 WriteServLog("%s Tariff \'%s\' deleted.",
181 admin->logStr().c_str(),
185 //-----------------------------------------------------------------------------
186 int TariffsImpl::Add(const std::string & name, const Admin * admin)
188 const auto& priv = admin->priv();
192 std::string s = admin->logStr() + " Add tariff \'"
193 + name + "\'. Access denied.";
194 strError = "Access denied.";
195 WriteServLog(s.c_str());
200 std::lock_guard<std::mutex> lock(m_mutex);
202 const auto ti = find(tariffs.begin(), tariffs.end(), TariffImpl(name));
204 if (ti != tariffs.end())
206 strError = "Tariff \'" + name + "\' cannot be added. Tariff already exist.";
207 WriteServLog("%s %s", admin->logStr().c_str(), strError.c_str());
211 tariffs.push_back(TariffImpl(name));
214 if (store->AddTariff(name) < 0)
216 strError = "Tariff " + name + " adding error. " + store->GetStrError();
217 WriteServLog(strError.c_str());
221 m_onAddCallbacks.notify(tariffs.back().GetTariffData());
223 WriteServLog("%s Tariff \'%s\' added.",
224 admin->logStr().c_str(), name.c_str());
228 //-----------------------------------------------------------------------------
229 void TariffsImpl::GetTariffsData(std::vector<TariffData> * tdl) const
231 assert(tdl != NULL && "Tariffs data list is not null");
232 std::lock_guard<std::mutex> lock(m_mutex);
234 auto it = tariffs.begin();
235 for (; it != tariffs.end(); ++it)
237 tdl->push_back(it->GetTariffData());
240 //-----------------------------------------------------------------------------