Used a more uniform of logging and added a commandline parser.
[UnsignedByte.git] / src / DB / Savables / Command.cpp
blob8d5e3606a3cd36259aafeed838c991067f76f6a8
1 /***************************************************************************
2 * Copyright (C) 2008 by Sverre Rabbelier *
3 * sverre@rabbelier.nl *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 3 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
21 #include "Command.h"
22 #include "Global.h"
23 #include "GrantGroup.h"
24 #include "GrantGroupManager.h"
25 #include "PermissionManager.h"
26 #include "Managers.h"
27 #include "Account.h"
28 #include "Permission.h"
29 #include "TableImpls.h"
31 using mud::Command;
33 Command::Command(SavableManagerPtr Command) :
34 m_command(Command)
36 Assert(Command);
39 Command::~Command(void)
41 Unlock();
44 const std::string& Command::getName() const
46 return m_command->getValue(db::TableImpls::Get()->COMMANDS->NAME)->getStringValue();
49 const std::string& Command::getHelp() const
51 return m_command->getValue(db::TableImpls::Get()->COMMANDS->HELP)->getStringValue();
54 value_type Command::getGrantGroup() const
56 return m_command->getValue(db::TableImpls::Get()->COMMANDS->GRANTGROUP)->getIntegerValue();
59 bool Command::canHighForce() const
61 return m_command->getValue(db::TableImpls::Get()->COMMANDS->HIGHFORCE)->getBoolValue();
64 bool Command::canForce() const
66 return m_command->getValue(db::TableImpls::Get()->COMMANDS->FORCE)->getBoolValue();
69 bool Command::canLowForce() const
71 return m_command->getValue(db::TableImpls::Get()->COMMANDS->LOWFORCE)->getBoolValue();
75 void Command::setName(const std::string& name)
77 Lock();
78 ValuePtr value(new FieldValue(db::TableImpls::Get()->COMMANDS->NAME, name));
79 m_command->setValue(value);
82 void Command::setHelp(const std::string& name)
84 Lock();
85 ValuePtr value(new FieldValue(db::TableImpls::Get()->COMMANDS->HELP, name));
86 m_command->setValue(value);
89 void Command::setGrantGroup(value_type grantgroup)
91 Lock();
92 ValuePtr value(new FieldValue(db::TableImpls::Get()->COMMANDS->GRANTGROUP, grantgroup));
93 m_command->setValue(value);
96 void Command::setHighForce(bool highforce)
98 Lock();
99 ValuePtr value(new FieldValue(db::TableImpls::Get()->COMMANDS->HIGHFORCE, highforce ? 0 : 1));
100 m_command->setValue(value);
103 void Command::setForce(bool force)
105 Lock();
106 ValuePtr value(new FieldValue(db::TableImpls::Get()->COMMANDS->FORCE, force ? 0 : 1));
107 m_command->setValue(value);
110 void Command::setLowForce(bool lowforce)
112 Lock();
113 ValuePtr value(new FieldValue(db::TableImpls::Get()->COMMANDS->LOWFORCE, lowforce ? 0 : 1));
114 m_command->setValue(value);
118 bool Command::getGrant(value_type accountid)
122 KeysPtr keys(new Keys(db::TableImpls::Get()->PERMISSIONS));
123 keys->addKey(db::TableImpls::Get()->PERMISSIONS->FKGRANTGROUPS, getGrantGroup());
124 keys->addKey(db::TableImpls::Get()->PERMISSIONS->FKACCOUNTS, accountid);
126 PermissionPtr permission = mud::Managers::Get()->Permission->GetByKeys(keys);
128 return permission->hasGrant();
129 } catch(RowNotFoundException& e) {
130 return getDefaultGrant();
134 bool Command::getDefaultGrant()
138 long id = getGrantGroup();
139 GrantGroupPtr grp = mud::Managers::Get()->GrantGroup->GetByKey(id);
140 return grp->getDefaultGrant();
142 catch(RowNotFoundException& e) {
143 return mud::Managers::Get()->Permission->defaultGrant;
147 bool Command::getLog(value_type accountid)
151 KeysPtr keys(new Keys(db::TableImpls::Get()->PERMISSIONS));
152 keys->addKey(db::TableImpls::Get()->PERMISSIONS->FKGRANTGROUPS, getGrantGroup());
153 keys->addKey(db::TableImpls::Get()->PERMISSIONS->FKACCOUNTS, accountid);
155 PermissionPtr permission = mud::Managers::Get()->Permission->GetByKeys(keys);
157 return permission->hasLog();
158 } catch(RowNotFoundException& e) {
159 return getDefaultLog();
163 bool Command::getDefaultLog()
167 long id = getGrantGroup();
168 GrantGroupPtr grp = mud::Managers::Get()->GrantGroup->GetByKey(id);
169 return grp->getDefaultLog();
171 catch(RowNotFoundException& e) {
172 return mud::Managers::Get()->Permission->defaultLog;
176 void Command::Delete()
178 Lock();
179 m_command->erase();
182 void Command::Save()
184 Lock();
185 m_command->save();
188 void Command::Discard()
190 Lock();
191 m_command->discard();
194 bool Command::Exists()
196 return m_command->exists();
199 SavableManagerPtr Command::getManager() const
201 return m_command;
204 TableImplPtr Command::getTable() const
206 return m_command->getTable();