]> git.stg.codes - stg.git/blob - stargazer/tariffs_impl.cpp
7e92657d797fcb97c1a6f818f8ed8630ddaa4b3a
[stg.git] / stargazer / tariffs_impl.cpp
1 /*
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.
6  *
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.
11  *
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
15  */
16
17 /*
18  *    Date: 07.11.2007
19  */
20
21 /*
22  *    Author : Boris Mikhailenko <stg34@stargazer.dp.ua>
23  */
24
25 /*
26  $Revision: 1.9 $
27  $Date: 2010/10/07 18:43:21 $
28  $Author: faust $
29  */
30
31 #include <cassert>
32 #include <algorithm>
33 #include <vector>
34
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"
41
42 using STG::TariffsImpl;
43
44 //-----------------------------------------------------------------------------
45 TariffsImpl::TariffsImpl(Store * st)
46     : store(st),
47       WriteServLog(Logger::get()),
48       noTariff(NO_TARIFF_NAME)
49 {
50 ReadTariffs();
51 }
52 //-----------------------------------------------------------------------------
53 int TariffsImpl::ReadTariffs()
54 {
55 std::lock_guard<std::mutex> lock(m_mutex);
56
57 std::vector<std::string> tariffsList;
58 if (store->GetTariffsList(&tariffsList))
59     {
60     WriteServLog("Cannot get tariffs list.");
61     WriteServLog("%s", store->GetStrError().c_str());
62     }
63
64 Data::size_type tariffsNum = tariffsList.size();
65
66 for (Data::size_type i = 0; i < tariffsNum; i++)
67     {
68     TariffData td;
69     if (store->RestoreTariff(&td, tariffsList[i]))
70         {
71         WriteServLog("Cannot read tariff %s.", tariffsList[i].c_str());
72         WriteServLog("%s", store->GetStrError().c_str());
73         return -1;
74         }
75     tariffs.push_back(TariffImpl(td));
76     }
77
78 return 0;
79 }
80 //-----------------------------------------------------------------------------
81 size_t TariffsImpl::Count() const
82 {
83 std::lock_guard<std::mutex> lock(m_mutex);
84 return tariffs.size();
85 }
86 //-----------------------------------------------------------------------------
87 const STG::Tariff* TariffsImpl::FindByName(const std::string & name) const
88 {
89 if (name == NO_TARIFF_NAME)
90     return &noTariff;
91
92 std::lock_guard<std::mutex> lock(m_mutex);
93 const auto ti = find(tariffs.begin(), tariffs.end(), TariffImpl(name));
94
95 if (ti != tariffs.end())
96     return &(*ti);
97
98 return NULL;
99 }
100 //-----------------------------------------------------------------------------
101 int TariffsImpl::Chg(const TariffData & td, const Admin * admin)
102 {
103 const auto priv = admin->GetPriv();
104
105 if (!priv->tariffChg)
106     {
107     std::string s = admin->GetLogStr() + " Change tariff \'"
108                + td.tariffConf.name + "\'. Access denied.";
109     strError = "Access denied.";
110     WriteServLog(s.c_str());
111     return -1;
112     }
113
114 std::lock_guard<std::mutex> lock(m_mutex);
115
116 auto ti = find(tariffs.begin(), tariffs.end(), TariffImpl(td.tariffConf.name));
117
118 if (ti == tariffs.end())
119     {
120     strError = "Tariff \'" + td.tariffConf.name + "\' cannot be changed. Tariff does not exist.";
121     WriteServLog("%s %s", admin->GetLogStr().c_str(), strError.c_str());
122     return -1;
123     }
124
125 *ti = td;
126
127 if (store->SaveTariff(td, td.tariffConf.name))
128     {
129     std::string error = "Tariff " + td.tariffConf.name + " writing error. " + store->GetStrError();
130     WriteServLog(error.c_str());
131     return -1;
132     }
133
134 WriteServLog("%s Tariff \'%s\' changed.",
135              admin->GetLogStr().c_str(), td.tariffConf.name.c_str());
136
137 return 0;
138 }
139 //-----------------------------------------------------------------------------
140 int TariffsImpl::Del(const std::string & name, const Admin * admin)
141 {
142 const auto priv = admin->GetPriv();
143
144 if (!priv->tariffChg)
145     {
146     std::string s = admin->GetLogStr() + " Delete tariff \'"
147                + name + "\'. Access denied.";
148     strError = "Access denied.";
149     WriteServLog(s.c_str());
150     return -1;
151     }
152
153 TariffData td;
154
155     {
156     std::lock_guard<std::mutex> lock(m_mutex);
157
158     const auto ti = find(tariffs.begin(), tariffs.end(), TariffImpl(name));
159
160     if (ti == tariffs.end())
161         {
162         strError = "Tariff \'" + name + "\' cannot be deleted. Tariff does not exist.";
163         WriteServLog("%s %s", admin->GetLogStr().c_str(), strError.c_str());
164         return -1;
165         }
166
167     if (store->DelTariff(name))
168         {
169         WriteServLog("Cannot delete tariff %s.", name.c_str());
170         WriteServLog("%s", store->GetStrError().c_str());
171         return -1;
172         }
173
174     td = ti->GetTariffData();
175
176     tariffs.erase(ti);
177     }
178
179 auto ni = onDelNotifiers.begin();
180 while (ni != onDelNotifiers.end())
181     {
182     (*ni)->Notify(td);
183     ++ni;
184     }
185
186 WriteServLog("%s Tariff \'%s\' deleted.",
187              admin->GetLogStr().c_str(),
188              name.c_str());
189 return 0;
190 }
191 //-----------------------------------------------------------------------------
192 int TariffsImpl::Add(const std::string & name, const Admin * admin)
193 {
194 const auto priv = admin->GetPriv();
195
196 if (!priv->tariffChg)
197     {
198     std::string s = admin->GetLogStr() + " Add tariff \'"
199                + name + "\'. Access denied.";
200     strError = "Access denied.";
201     WriteServLog(s.c_str());
202     return -1;
203     }
204
205     {
206     std::lock_guard<std::mutex> lock(m_mutex);
207
208     const auto ti = find(tariffs.begin(), tariffs.end(), TariffImpl(name));
209
210     if (ti != tariffs.end())
211         {
212         strError = "Tariff \'" + name + "\' cannot be added. Tariff already exist.";
213         WriteServLog("%s %s", admin->GetLogStr().c_str(), strError.c_str());
214         return -1;
215         }
216
217     tariffs.push_back(TariffImpl(name));
218     }
219
220 if (store->AddTariff(name) < 0)
221     {
222     strError = "Tariff " + name + " adding error. " + store->GetStrError();
223     WriteServLog(strError.c_str());
224     return -1;
225     }
226
227 // Fire all "on add" notifiers
228 auto ni = onAddNotifiers.begin();
229 while (ni != onAddNotifiers.end())
230     {
231     (*ni)->Notify(tariffs.back().GetTariffData());
232     ++ni;
233     }
234
235 WriteServLog("%s Tariff \'%s\' added.",
236                  admin->GetLogStr().c_str(), name.c_str());
237
238 return 0;
239 }
240 //-----------------------------------------------------------------------------
241 void TariffsImpl::GetTariffsData(std::vector<TariffData> * tdl) const
242 {
243 assert(tdl != NULL && "Tariffs data list is not null");
244 std::lock_guard<std::mutex> lock(m_mutex);
245
246 auto it = tariffs.begin();
247 for (; it != tariffs.end(); ++it)
248     {
249     tdl->push_back(it->GetTariffData());
250     }
251 }
252 //-----------------------------------------------------------------------------
253 void TariffsImpl::AddNotifierAdd(NotifierBase<TariffData> * n)
254 {
255 std::lock_guard<std::mutex> lock(m_mutex);
256 onAddNotifiers.insert(n);
257 }
258 //-----------------------------------------------------------------------------
259 void TariffsImpl::DelNotifierAdd(NotifierBase<TariffData> * n)
260 {
261 std::lock_guard<std::mutex> lock(m_mutex);
262 onAddNotifiers.erase(n);
263 }
264 //-----------------------------------------------------------------------------
265 void TariffsImpl::AddNotifierDel(NotifierBase<TariffData> * n)
266 {
267 std::lock_guard<std::mutex> lock(m_mutex);
268 onDelNotifiers.insert(n);
269 }
270 //-----------------------------------------------------------------------------
271 void TariffsImpl::DelNotifierDel(NotifierBase<TariffData> * n)
272 {
273 std::lock_guard<std::mutex> lock(m_mutex);
274 onDelNotifiers.erase(n);
275 }
276 //-----------------------------------------------------------------------------