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
18 * Author : Maksym Mamontov <stg@madf.info>
21 #include "corps_impl.h"
23 #include "stg/admin.h"
24 #include "stg/common.h"
30 //-----------------------------------------------------------------------------
31 CORPORATIONS_IMPL::CORPORATIONS_IMPL(STORE * st)
35 WriteServLog(GetStgLogger()),
41 pthread_mutex_init(&mutex, NULL);
44 //-----------------------------------------------------------------------------
45 int CORPORATIONS_IMPL::Add(const CORP_CONF & corp, const ADMIN * admin)
47 STG_LOCKER lock(&mutex);
48 const PRIV * priv = admin->GetPriv();
52 std::string s = admin->GetLogStr() + " Add corporation \'" + corp.name + "\'. Access denied.";
53 strError = "Access denied.";
54 WriteServLog(s.c_str());
58 crp_iter si(find(data.begin(), data.end(), corp));
62 strError = "Corporation \'" + corp.name + "\' cannot not be added. Corporation already exist.";
63 WriteServLog("%s %s", admin->GetLogStr().c_str(), strError.c_str());
70 if (store->AddCorp(corp.name) == 0)
72 WriteServLog("%s Corporation \'%s\' added.",
73 admin->GetLogStr().c_str(), corp.name.c_str());
77 strError = "Corporation \'" + corp.name + "\' was not added. Error: " + store->GetStrError();
78 WriteServLog("%s %s", admin->GetLogStr().c_str(), strError.c_str());
82 //-----------------------------------------------------------------------------
83 int CORPORATIONS_IMPL::Del(const std::string & name, const ADMIN * admin)
85 STG_LOCKER lock(&mutex);
86 const PRIV * priv = admin->GetPriv();
90 std::string s = admin->GetLogStr() + " Delete corporation \'" + name + "\'. Access denied.";
91 strError = "Access denied.";
92 WriteServLog(s.c_str());
96 crp_iter si(find(data.begin(), data.end(), CORP_CONF(name)));
100 strError = "Corporation \'" + name + "\' cannot be deleted. Corporation does not exist.";
101 WriteServLog("%s %s", admin->GetLogStr().c_str(), strError.c_str());
105 std::map<int, const_crp_iter>::iterator csi;
106 csi = searchDescriptors.begin();
107 while (csi != searchDescriptors.end())
109 if (csi->second == si)
115 if (store->DelCorp(name) < 0)
117 strError = "Corporation \'" + name + "\' was not deleted. Error: " + store->GetStrError();
118 WriteServLog("%s %s", admin->GetLogStr().c_str(), strError.c_str());
123 WriteServLog("%s Corporation \'%s\' deleted.", admin->GetLogStr().c_str(), name.c_str());
126 //-----------------------------------------------------------------------------
127 int CORPORATIONS_IMPL::Change(const CORP_CONF & corp, const ADMIN * admin)
129 STG_LOCKER lock(&mutex);
130 const PRIV * priv = admin->GetPriv();
134 std::string s = admin->GetLogStr() + " Change corporation \'" + corp.name + "\'. Access denied.";
135 strError = "Access denied.";
136 WriteServLog(s.c_str());
140 crp_iter si(find(data.begin(), data.end(), corp));
142 if (si == data.end())
144 strError = "Corporation \'" + corp.name + "\' cannot be changed " + ". Corporation does not exist.";
145 WriteServLog("%s %s", admin->GetLogStr().c_str(), strError.c_str());
150 if (store->SaveCorp(corp))
152 WriteServLog("Cannot write corporation %s.", corp.name.c_str());
153 WriteServLog("%s", store->GetStrError().c_str());
157 WriteServLog("%s Corporation \'%s\' changed.",
158 admin->GetLogStr().c_str(), corp.name.c_str());
162 //-----------------------------------------------------------------------------
163 bool CORPORATIONS_IMPL::Read()
165 STG_LOCKER lock(&mutex);
166 std::vector<std::string> corpsList;
167 if (store->GetCorpsList(&corpsList) < 0)
169 WriteServLog(store->GetStrError().c_str());
173 for (size_t i = 0; i < corpsList.size(); i++)
177 if (store->RestoreCorp(&corp, corpsList[i]))
179 WriteServLog(store->GetStrError().c_str());
183 data.push_back(corp);
187 //-----------------------------------------------------------------------------
188 bool CORPORATIONS_IMPL::Find(const std::string & name, CORP_CONF * corp)
190 assert(corp != NULL && "Pointer to corporation is not null");
192 STG_LOCKER lock(&mutex);
196 crp_iter si(find(data.begin(), data.end(), CORP_CONF(name)));
198 if (si != data.end())
206 //-----------------------------------------------------------------------------
207 bool CORPORATIONS_IMPL::Exists(const std::string & name) const
209 STG_LOCKER lock(&mutex);
212 printfd(__FILE__, "no admin in system!\n");
216 const_crp_iter si(find(data.begin(), data.end(), CORP_CONF(name)));
218 if (si != data.end())
223 //-----------------------------------------------------------------------------
224 int CORPORATIONS_IMPL::OpenSearch() const
226 STG_LOCKER lock(&mutex);
228 searchDescriptors[handle] = data.begin();
231 //-----------------------------------------------------------------------------
232 int CORPORATIONS_IMPL::SearchNext(int h, CORP_CONF * corp) const
234 STG_LOCKER lock(&mutex);
235 if (searchDescriptors.find(h) == searchDescriptors.end())
237 WriteServLog("CORPORATIONS. Incorrect search handle.");
241 if (searchDescriptors[h] == data.end())
244 *corp = *searchDescriptors[h]++;
248 //-----------------------------------------------------------------------------
249 int CORPORATIONS_IMPL::CloseSearch(int h) const
251 STG_LOCKER lock(&mutex);
252 if (searchDescriptors.find(h) != searchDescriptors.end())
254 searchDescriptors.erase(searchDescriptors.find(h));
258 WriteServLog("CORPORATIONS. Incorrect search handle.");
261 //-----------------------------------------------------------------------------