ManagedObject Class Reference

The abstract class ManagedObject is a generic class that implements all the basic behaviour of a table of the database. More...

#include <ManagedObject.h>

Inheritance diagram for ManagedObject:
Inheritance graph
[legend]
Collaboration diagram for ManagedObject:
Collaboration graph
[legend]

List of all members.

Public Member Functions

 ManagedObject (string anEntityName)
 Construct an instance of ManagedObject linking with a corresponding table on database named "anEntityName".
 ManagedObject (string anEntityName, Row &aRow)
 Construct an instance of ManagedObject linking with a corresponding table on database named "anEntityName" and reading data from mysqlpp::Row.
 ~ManagedObject ()
void setBoolForKey (string aKey, bool aValue)
 Sets the specified property of the receiver to the specified boolean value.
void setFloatForKey (string aKey, float aValue)
 Sets the specified property of the receiver to the specified float value.
void setIntForKey (string aKey, int aValue)
 Sets the specified property of the receiver to the specified integer value.
void setValueForKey (string aKey, string aValue) throw (InvalidArgument)
 Sets the specified property of the receiver to the specified string value.
float floatForKey (string aKey) throw (InvalidArgument)
 Returns the value as float for the property identified by a given key.
int intForKey (string aKey) throw (InvalidArgument)
 Returns the value as integer for the property identified by a given key.
bool boolForKey (string aKey) throw (InvalidArgument)
 Returns the value as boolean for the property identified by a given key.
string valueForKey (string aKey) throw (InvalidArgument)
 Returns the value as string for the property identified by a given key.
ulonglong getLastInsertID () const
 Returns the ID of the autoincrement of the last query (INSERT).
bool store ()
 Attempts to commit unsaved changes to the persistent store: this function builds a REPLACE statement.
bool update ()
 Attempts to update unsaved changes to the persistent store.
virtual string primaryKey ()=0

Protected Attributes

set< string > _keys
 List of possible keys associated with this entity.
set< string > _updatedKeys
 List of the updated keys to update.
map< string, string > _fields
 Map of key/value associated to the entity.
string _entityName
 The entity name.
bool _fault
 Fault state: if true the entity need to be serialized.

Private Member Functions

void initEntity (string anEntityName)
 Build the list of keys (columns) for the specified entity (table).

Private Attributes

ulonglong lastInsertID

Detailed Description

The abstract class ManagedObject is a generic class that implements all the basic behaviour of a table of the database.

So, a managed object is associated with an entity of the database ; this information must be passed to the constructor of the class: when an instance of the class is created, a connection to the database is automatically requested in order to fetch all the available columns of the linked table.


Constructor & Destructor Documentation

ManagedObject::ManagedObject ( string  anEntityName  ) 

Construct an instance of ManagedObject linking with a corresponding table on database named "anEntityName".

The list of columns is fetched automatically.

References initEntity(), and LOG_CTOR.

ManagedObject::ManagedObject ( string  anEntityName,
Row &  aRow 
)

Construct an instance of ManagedObject linking with a corresponding table on database named "anEntityName" and reading data from mysqlpp::Row.

References _fault, _keys, _updatedKeys, initEntity(), LOG_CTOR, and setValueForKey().

ManagedObject::~ManagedObject (  ) 

References LOG_DTOR.


Member Function Documentation

bool ManagedObject::boolForKey ( string  aKey  )  throw (InvalidArgument)

Returns the value as boolean for the property identified by a given key.

Parameters:
[in] aKey The key to read from
Returns:
Value of the property
Exceptions:
InvalidArgument Thrown if the specified key doesn't belog to this entity
float ManagedObject::floatForKey ( string  aKey  )  throw (InvalidArgument)

Returns the value as float for the property identified by a given key.

Parameters:
[in] aKey The key to read from
Returns:
Value of the property
Exceptions:
InvalidArgument Thrown if the specified key doesn't belog to this entity

Referenced by Product::getPrice().

ulonglong ManagedObject::getLastInsertID (  )  const

Returns the ID of the autoincrement of the last query (INSERT).

Returns:
The last ID

References lastInsertID.

Referenced by Order::create().

void ManagedObject::initEntity ( string  anEntityName  )  [private]

Build the list of keys (columns) for the specified entity (table).

References _entityName, _fault, _keys, Database::getConnection(), Database::Instance(), lastInsertID, and LOG.

Referenced by ManagedObject().

int ManagedObject::intForKey ( string  aKey  )  throw (InvalidArgument)

Returns the value as integer for the property identified by a given key.

Parameters:
[in] aKey The key to read from
Returns:
Value of the property
Exceptions:
InvalidArgument Thrown if the specified key doesn't belog to this entity

Referenced by Basket::addProduct(), ProductProxy::getAvailability(), Product::getCategory(), Order::Order(), NormalUser::placeOrder(), Basket::removeProduct(), and User::uniqueID().

virtual string ManagedObject::primaryKey (  )  [pure virtual]

Implemented in Category, Order, Product, and User.

Referenced by update().

void ManagedObject::setBoolForKey ( string  aKey,
bool  aValue 
)

Sets the specified property of the receiver to the specified boolean value.

Parameters:
[in] aKey The name of one of the receiver's properties.
[in] aValue The new value for the property specified by key.

References setValueForKey().

Referenced by Product::factory().

void ManagedObject::setFloatForKey ( string  aKey,
float  aValue 
)

Sets the specified property of the receiver to the specified float value.

Parameters:
[in] aKey The name of one of the receiver's properties.
[in] aValue The new value for the property specified by key.

References setValueForKey().

Referenced by UserMenu::changeProductDetail(), Order::create(), Product::factory(), and setIntForKey().

void ManagedObject::setIntForKey ( string  aKey,
int  aValue 
)

Sets the specified property of the receiver to the specified integer value.

Parameters:
[in] aKey The name of one of the receiver's properties.
[in] aValue The new value for the property specified by key.

References setFloatForKey().

Referenced by Category::categoryByID(), Order::create(), User::factory(), Product::factory(), and Category::factory().

void ManagedObject::setValueForKey ( string  aKey,
string  aValue 
) throw (InvalidArgument)

Sets the specified property of the receiver to the specified string value.

Parameters:
[in] aKey The name of one of the receiver's properties.
[in] aValue The new value for the property specified by key.
Exceptions:
InvalidArgument Thrown if the specified key doesn't belog to this entity

Referenced by Category::catalog(), Category::categoryByID(), UserMenu::changeProductDetail(), AdminUser::changeUserPassword(), Order::create(), User::factory(), Product::factory(), Category::factory(), ManagedObject(), setBoolForKey(), and setFloatForKey().

bool ManagedObject::store (  ) 

Attempts to commit unsaved changes to the persistent store: this function builds a REPLACE statement.

Fault state is reset if update was successful.

Note:
ManagedObject::store should be called only to store new record and not an existing one.
Returns:
True if save was successful
See also:
update

Iterate on all key/value in order to create the VALUE() part of REPLACE statement; we also fill a SQLQueryParams to pass to mysqlpp::query::execute.

References _entityName, _fault, _fields, _keys, Database::getConnection(), Database::Instance(), lastInsertID, and LOG.

Referenced by UserMenu::addNewCategory(), UserMenu::addNewProduct(), Order::create(), and User::factory().

bool ManagedObject::update (  ) 

Attempts to update unsaved changes to the persistent store.

Fault state is reset if update is successful.

Note:
ManagedObject::update should be called only to update existing record and not an to store a new record.
Returns:
True if update was successful
See also:
store

References _entityName, _fault, _fields, _updatedKeys, Database::getConnection(), Database::Instance(), LOG, primaryKey(), and valueMerge().

Referenced by UserMenu::changeProductDetail(), and AdminUser::changeUserPassword().

string ManagedObject::valueForKey ( string  aKey  )  throw (InvalidArgument)

Returns the value as string for the property identified by a given key.

Parameters:
[in] aKey The key to read from
Returns:
Value of the property
Exceptions:
InvalidArgument Thrown if the specified key doesn't belog to this entity

Referenced by User::fullName(), ProductProxy::getDescr(), ProductProxy::getName(), Category::getName(), operator<<(), and Order::products().


Member Data Documentation

string ManagedObject::_entityName [protected]

The entity name.

Referenced by initEntity(), store(), and update().

bool ManagedObject::_fault [protected]

Fault state: if true the entity need to be serialized.

Referenced by initEntity(), ManagedObject(), store(), and update().

map<string, string> ManagedObject::_fields [protected]

Map of key/value associated to the entity.

Referenced by store(), and update().

set<string> ManagedObject::_keys [protected]

List of possible keys associated with this entity.

Referenced by initEntity(), ManagedObject(), and store().

set<string> ManagedObject::_updatedKeys [protected]

List of the updated keys to update.

Referenced by ManagedObject(), and update().

Referenced by getLastInsertID(), initEntity(), and store().


The documentation for this class was generated from the following files:

Generated by  doxygen 1.6.2