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()),
50 noTariff(NO_TARIFF_NAME)
52 pthread_mutex_init(&mutex, NULL);
55 //-----------------------------------------------------------------------------
56 TARIFFS_IMPL::~TARIFFS_IMPL()
58 pthread_mutex_destroy(&mutex);
60 //-----------------------------------------------------------------------------
61 int TARIFFS_IMPL::ReadTariffs()
63 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
65 vector<string> tariffsList;
66 if (store->GetTariffsList(&tariffsList))
68 WriteServLog("Cannot get tariffs list.");
69 WriteServLog("%s", store->GetStrError().c_str());
72 int tariffsNum = tariffsList.size();
74 for (int i = 0; i < tariffsNum; i++)
77 if (store->RestoreTariff(&td, tariffsList[i]))
79 WriteServLog("Cannot read tariff %s.", tariffsList[i].c_str());
80 WriteServLog("%s", store->GetStrError().c_str());
83 tariffs.push_back(TARIFF_IMPL(td));
88 //-----------------------------------------------------------------------------
89 int TARIFFS_IMPL::GetTariffsNum() const
91 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
92 return tariffs.size();
94 //-----------------------------------------------------------------------------
95 const TARIFF * TARIFFS_IMPL::FindByName(const string & name) const
97 if (name == NO_TARIFF_NAME)
100 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
101 list<TARIFF_IMPL>::const_iterator ti;
102 ti = find(tariffs.begin(), tariffs.end(), TARIFF_IMPL(name));
104 if (ti != tariffs.end())
109 //-----------------------------------------------------------------------------
110 int TARIFFS_IMPL::Chg(const TARIFF_DATA & td, const ADMIN * admin)
112 const PRIV * priv = admin->GetPriv();
114 if (!priv->tariffChg)
116 string s = admin->GetLogStr() + " Change tariff \'"
117 + td.tariffConf.name + "\'. Access denied.";
118 strError = "Access denied.";
119 WriteServLog(s.c_str());
123 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
125 list<TARIFF_IMPL>::iterator ti;
126 ti = find(tariffs.begin(), tariffs.end(), TARIFF_IMPL(td.tariffConf.name));
128 if (ti == tariffs.end())
130 strError = "Tariff \'" + td.tariffConf.name + "\' cannot be changed. Tariff does not exist.";
131 WriteServLog("%s %s", admin->GetLogStr().c_str(), strError.c_str());
137 if (store->SaveTariff(td, td.tariffConf.name))
139 string error = "Tariff " + td.tariffConf.name + " writing error. " + store->GetStrError();
140 WriteServLog(error.c_str());
144 WriteServLog("%s Tariff \'%s\' changed.",
145 admin->GetLogStr().c_str(), td.tariffConf.name.c_str());
149 //-----------------------------------------------------------------------------
150 int TARIFFS_IMPL::Del(const string & name, const ADMIN * admin)
152 const PRIV * priv = admin->GetPriv();
154 if (!priv->tariffChg)
156 string s = admin->GetLogStr() + " Delete tariff \'"
157 + name + "\'. Access denied.";
158 strError = "Access denied.";
159 WriteServLog(s.c_str());
163 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
165 list<TARIFF_IMPL>::iterator ti;
166 ti = find(tariffs.begin(), tariffs.end(), TARIFF_IMPL(name));
168 if (ti == tariffs.end())
170 strError = "Tariff \'" + name + "\' cannot be deleted. Tariff does not exist.";
171 WriteServLog("%s %s", admin->GetLogStr().c_str(), strError.c_str());
175 if (store->DelTariff(name))
177 WriteServLog("Cannot delete tariff %s.", name.c_str());
178 WriteServLog("%s", store->GetStrError().c_str());
184 WriteServLog("%s Tariff \'%s\' deleted.",
185 admin->GetLogStr().c_str(),
189 //-----------------------------------------------------------------------------
190 int TARIFFS_IMPL::Add(const string & name, const ADMIN * admin)
192 const PRIV * priv = admin->GetPriv();
194 if (!priv->tariffChg)
196 string s = admin->GetLogStr() + " Add tariff \'"
197 + name + "\'. Access denied.";
198 strError = "Access denied.";
199 WriteServLog(s.c_str());
203 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
205 list<TARIFF_IMPL>::iterator ti;
206 ti = find(tariffs.begin(), tariffs.end(), TARIFF_IMPL(name));
208 if (ti != tariffs.end())
210 strError = "Tariff \'" + name + "\' cannot be added. Tariff already exist.";
211 WriteServLog("%s %s", admin->GetLogStr().c_str(), strError.c_str());
215 tariffs.push_back(TARIFF_IMPL(name));
217 if (store->AddTariff(name) < 0)
219 strError = "Tariff " + name + " adding error. " + store->GetStrError();
220 WriteServLog(strError.c_str());
224 WriteServLog("%s Tariff \'%s\' added.",
225 admin->GetLogStr().c_str(), name.c_str());
229 //-----------------------------------------------------------------------------
230 void TARIFFS_IMPL::GetTariffsData(std::list<TARIFF_DATA> * tdl)
232 assert(tdl != NULL && "Tariffs data list is not null");
233 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
235 std::list<TARIFF_IMPL>::const_iterator it = tariffs.begin();
236 for (; it != tariffs.end(); ++it)
238 tdl->push_back(it->GetTariffData());
241 //-----------------------------------------------------------------------------