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 "tariffs_impl.h"
43 //-----------------------------------------------------------------------------
44 TARIFFS_IMPL::TARIFFS_IMPL(STORE * st)
48 WriteServLog(GetStgLogger()),
51 noTariff(NO_TARIFF_NAME),
55 pthread_mutex_init(&mutex, NULL);
58 //-----------------------------------------------------------------------------
59 TARIFFS_IMPL::~TARIFFS_IMPL()
61 pthread_mutex_destroy(&mutex);
63 //-----------------------------------------------------------------------------
64 int TARIFFS_IMPL::ReadTariffs()
66 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
68 vector<string> tariffsList;
69 if (store->GetTariffsList(&tariffsList))
71 WriteServLog("Cannot get tariffs list.");
72 WriteServLog("%s", store->GetStrError().c_str());
75 Tariffs::size_type tariffsNum = tariffsList.size();
77 for (Tariffs::size_type i = 0; i < tariffsNum; i++)
80 if (store->RestoreTariff(&td, tariffsList[i]))
82 WriteServLog("Cannot read tariff %s.", tariffsList[i].c_str());
83 WriteServLog("%s", store->GetStrError().c_str());
86 tariffs.push_back(TARIFF_IMPL(td));
91 //-----------------------------------------------------------------------------
92 size_t TARIFFS_IMPL::Count() const
94 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
95 return tariffs.size();
97 //-----------------------------------------------------------------------------
98 const TARIFF * TARIFFS_IMPL::FindByName(const string & name) const
100 if (name == NO_TARIFF_NAME)
103 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
104 list<TARIFF_IMPL>::const_iterator ti;
105 ti = find(tariffs.begin(), tariffs.end(), TARIFF_IMPL(name));
107 if (ti != tariffs.end())
112 //-----------------------------------------------------------------------------
113 int TARIFFS_IMPL::Chg(const TARIFF_DATA & td, const ADMIN * admin)
115 const PRIV * priv = admin->GetPriv();
117 if (!priv->tariffChg)
119 string s = admin->GetLogStr() + " Change tariff \'"
120 + td.tariffConf.name + "\'. Access denied.";
121 strError = "Access denied.";
122 WriteServLog(s.c_str());
126 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
128 list<TARIFF_IMPL>::iterator ti;
129 ti = find(tariffs.begin(), tariffs.end(), TARIFF_IMPL(td.tariffConf.name));
131 if (ti == tariffs.end())
133 strError = "Tariff \'" + td.tariffConf.name + "\' cannot be changed. Tariff does not exist.";
134 WriteServLog("%s %s", admin->GetLogStr().c_str(), strError.c_str());
140 if (store->SaveTariff(td, td.tariffConf.name))
142 string error = "Tariff " + td.tariffConf.name + " writing error. " + store->GetStrError();
143 WriteServLog(error.c_str());
147 WriteServLog("%s Tariff \'%s\' changed.",
148 admin->GetLogStr().c_str(), td.tariffConf.name.c_str());
152 //-----------------------------------------------------------------------------
153 int TARIFFS_IMPL::Del(const string & name, const ADMIN * admin)
155 const PRIV * priv = admin->GetPriv();
157 if (!priv->tariffChg)
159 string s = admin->GetLogStr() + " Delete tariff \'"
160 + name + "\'. Access denied.";
161 strError = "Access denied.";
162 WriteServLog(s.c_str());
169 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
171 list<TARIFF_IMPL>::iterator ti;
172 ti = find(tariffs.begin(), tariffs.end(), TARIFF_IMPL(name));
174 if (ti == tariffs.end())
176 strError = "Tariff \'" + name + "\' cannot be deleted. Tariff does not exist.";
177 WriteServLog("%s %s", admin->GetLogStr().c_str(), strError.c_str());
181 if (store->DelTariff(name))
183 WriteServLog("Cannot delete tariff %s.", name.c_str());
184 WriteServLog("%s", store->GetStrError().c_str());
188 td = ti->GetTariffData();
193 std::set<NOTIFIER_BASE<TARIFF_DATA> *>::iterator ni = onDelNotifiers.begin();
194 while (ni != onDelNotifiers.end())
200 WriteServLog("%s Tariff \'%s\' deleted.",
201 admin->GetLogStr().c_str(),
205 //-----------------------------------------------------------------------------
206 int TARIFFS_IMPL::Add(const string & name, const ADMIN * admin)
208 const PRIV * priv = admin->GetPriv();
210 if (!priv->tariffChg)
212 string s = admin->GetLogStr() + " Add tariff \'"
213 + name + "\'. Access denied.";
214 strError = "Access denied.";
215 WriteServLog(s.c_str());
220 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
222 list<TARIFF_IMPL>::iterator ti;
223 ti = find(tariffs.begin(), tariffs.end(), TARIFF_IMPL(name));
225 if (ti != tariffs.end())
227 strError = "Tariff \'" + name + "\' cannot be added. Tariff already exist.";
228 WriteServLog("%s %s", admin->GetLogStr().c_str(), strError.c_str());
232 tariffs.push_back(TARIFF_IMPL(name));
235 if (store->AddTariff(name) < 0)
237 strError = "Tariff " + name + " adding error. " + store->GetStrError();
238 WriteServLog(strError.c_str());
242 // Fire all "on add" notifiers
243 std::set<NOTIFIER_BASE<TARIFF_DATA> *>::iterator ni = onAddNotifiers.begin();
244 while (ni != onAddNotifiers.end())
246 (*ni)->Notify(tariffs.back().GetTariffData());
250 WriteServLog("%s Tariff \'%s\' added.",
251 admin->GetLogStr().c_str(), name.c_str());
255 //-----------------------------------------------------------------------------
256 void TARIFFS_IMPL::GetTariffsData(std::list<TARIFF_DATA> * tdl)
258 assert(tdl != NULL && "Tariffs data list is not null");
259 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
261 Tariffs::const_iterator it = tariffs.begin();
262 for (; it != tariffs.end(); ++it)
264 tdl->push_back(it->GetTariffData());
267 //-----------------------------------------------------------------------------
268 void TARIFFS_IMPL::AddNotifierAdd(NOTIFIER_BASE<TARIFF_DATA> * n)
270 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
271 onAddNotifiers.insert(n);
273 //-----------------------------------------------------------------------------
274 void TARIFFS_IMPL::DelNotifierAdd(NOTIFIER_BASE<TARIFF_DATA> * n)
276 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
277 onAddNotifiers.erase(n);
279 //-----------------------------------------------------------------------------
280 void TARIFFS_IMPL::AddNotifierDel(NOTIFIER_BASE<TARIFF_DATA> * n)
282 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
283 onDelNotifiers.insert(n);
285 //-----------------------------------------------------------------------------
286 void TARIFFS_IMPL::DelNotifierDel(NOTIFIER_BASE<TARIFF_DATA> * n)
288 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
289 onDelNotifiers.erase(n);
291 //-----------------------------------------------------------------------------