]> git.stg.codes - stg.git/blob - projects/stargazer/tariffs_impl.cpp
Explicit constructor initialization for the base class added
[stg.git] / projects / 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 "tariffs_impl.h"
40
41 using namespace std;
42
43 //-----------------------------------------------------------------------------
44 TARIFFS_IMPL::TARIFFS_IMPL(STORE * st)
45     : TARIFFS(),
46       tariffs(),
47       store(st),
48       WriteServLog(GetStgLogger()),
49       strError(),
50       noTariff(NO_TARIFF_NAME)
51 {
52 pthread_mutex_init(&mutex, NULL);
53 ReadTariffs();
54 }
55 //-----------------------------------------------------------------------------
56 TARIFFS_IMPL::~TARIFFS_IMPL()
57 {
58 pthread_mutex_destroy(&mutex);
59 }
60 //-----------------------------------------------------------------------------
61 int TARIFFS_IMPL::ReadTariffs()
62 {
63 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
64
65 vector<string> tariffsList;
66 if (store->GetTariffsList(&tariffsList))
67     {
68     WriteServLog("Cannot get tariffs list.");
69     WriteServLog("%s", store->GetStrError().c_str());
70     }
71
72 int tariffsNum = tariffsList.size();
73
74 for (int i = 0; i < tariffsNum; i++)
75     {
76     TARIFF_DATA td;
77     if (store->RestoreTariff(&td, tariffsList[i]))
78         {
79         WriteServLog("Cannot read tariff %s.", tariffsList[i].c_str());
80         WriteServLog("%s", store->GetStrError().c_str());
81         return -1;
82         }
83     tariffs.push_back(TARIFF_IMPL(td));
84     }
85
86 return 0;
87 }
88 //-----------------------------------------------------------------------------
89 int TARIFFS_IMPL::GetTariffsNum() const
90 {
91 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
92 return tariffs.size();
93 }
94 //-----------------------------------------------------------------------------
95 const TARIFF * TARIFFS_IMPL::FindByName(const string & name) const
96 {
97 if (name == NO_TARIFF_NAME)
98     return &noTariff;
99
100 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
101 list<TARIFF_IMPL>::const_iterator ti;
102 ti = find(tariffs.begin(), tariffs.end(), TARIFF_IMPL(name));
103
104 if (ti != tariffs.end())
105     return &(*ti);
106
107 return NULL;
108 }
109 //-----------------------------------------------------------------------------
110 int TARIFFS_IMPL::Chg(const TARIFF_DATA & td, const ADMIN * admin)
111 {
112 const PRIV * priv = admin->GetPriv();
113
114 if (!priv->tariffChg)
115     {
116     string s = admin->GetLogStr() + " Change tariff \'"
117                + td.tariffConf.name + "\'. Access denied.";
118     strError = "Access denied.";
119     WriteServLog(s.c_str());
120     return -1;
121     }
122
123 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
124
125 list<TARIFF_IMPL>::iterator ti;
126 ti = find(tariffs.begin(), tariffs.end(), TARIFF_IMPL(td.tariffConf.name));
127
128 if (ti == tariffs.end())
129     {
130     strError = "Tariff \'" + td.tariffConf.name + "\' cannot be changed. Tariff does not exist.";
131     WriteServLog("%s %s", admin->GetLogStr().c_str(), strError.c_str());
132     return -1;
133     }
134
135 *ti = td;
136
137 if (store->SaveTariff(td, td.tariffConf.name))
138     {
139     string error = "Tariff " + td.tariffConf.name + " writing error. " + store->GetStrError();
140     WriteServLog(error.c_str());
141     return -1;
142     }
143
144 WriteServLog("%s Tariff \'%s\' changed.",
145              admin->GetLogStr().c_str(), td.tariffConf.name.c_str());
146
147 return 0;
148 }
149 //-----------------------------------------------------------------------------
150 int TARIFFS_IMPL::Del(const string & name, const ADMIN * admin)
151 {
152 const PRIV * priv = admin->GetPriv();
153
154 if (!priv->tariffChg)
155     {
156     string s = admin->GetLogStr() + " Delete tariff \'"
157                + name + "\'. Access denied.";
158     strError = "Access denied.";
159     WriteServLog(s.c_str());
160     return -1;
161     }
162
163 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
164
165 list<TARIFF_IMPL>::iterator ti;
166 ti = find(tariffs.begin(), tariffs.end(), TARIFF_IMPL(name));
167
168 if (ti == tariffs.end())
169     {
170     strError = "Tariff \'" + name + "\' cannot be deleted. Tariff does not exist.";
171     WriteServLog("%s %s", admin->GetLogStr().c_str(), strError.c_str());
172     return -1;
173     }
174
175 if (store->DelTariff(name))
176     {
177     WriteServLog("Cannot delete tariff %s.", name.c_str());
178     WriteServLog("%s", store->GetStrError().c_str());
179     return -1;
180     }
181
182 tariffs.erase(ti);
183
184 WriteServLog("%s Tariff \'%s\' deleted.",
185              admin->GetLogStr().c_str(),
186              name.c_str());
187 return 0;
188 }
189 //-----------------------------------------------------------------------------
190 int TARIFFS_IMPL::Add(const string & name, const ADMIN * admin)
191 {
192 const PRIV * priv = admin->GetPriv();
193
194 if (!priv->tariffChg)
195     {
196     string s = admin->GetLogStr() + " Add tariff \'"
197                + name + "\'. Access denied.";
198     strError = "Access denied.";
199     WriteServLog(s.c_str());
200     return -1;
201     }
202
203 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
204
205 list<TARIFF_IMPL>::iterator ti;
206 ti = find(tariffs.begin(), tariffs.end(), TARIFF_IMPL(name));
207
208 if (ti != tariffs.end())
209     {
210     strError = "Tariff \'" + name + "\' cannot be added. Tariff already exist.";
211     WriteServLog("%s %s", admin->GetLogStr().c_str(), strError.c_str());
212     return -1;
213     }
214
215 tariffs.push_back(TARIFF_IMPL(name));
216
217 if (store->AddTariff(name) < 0)
218     {
219     strError = "Tariff " + name + " adding error. " + store->GetStrError();
220     WriteServLog(strError.c_str());
221     return -1;
222     }
223
224 WriteServLog("%s Tariff \'%s\' added.",
225                  admin->GetLogStr().c_str(), name.c_str());
226
227 return 0;
228 }
229 //-----------------------------------------------------------------------------
230 void TARIFFS_IMPL::GetTariffsData(std::list<TARIFF_DATA> * tdl)
231 {
232 assert(tdl != NULL && "Tariffs data list is not null");
233 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
234
235 std::list<TARIFF_IMPL>::const_iterator it = tariffs.begin();
236 for (; it != tariffs.end(); ++it)
237     {
238     tdl->push_back(it->GetTariffData());
239     }
240 }
241 //-----------------------------------------------------------------------------