///////////////////////////////////////////////////////////
// BmDbThread.h
// Implementation of the Class BmDbThread
// Created on: 05-五月-2009 11:40:31
// Original author: Tim Kuo
///////////////////////////////////////////////////////////
#if !defined(EA_6594F28D_DB37_4636_8095_CAF8A14A53D9__INCLUDED_)
#define EA_6594F28D_DB37_4636_8095_CAF8A14A53D9__INCLUDED_
#include "DbThread.h"
class Business;
class BusinessType;
class Message;
#include <list>
using namespace std;
class BmDbThread : public DbThread
{
public:
virtual ~BmDbThread();
protected:
virtual void WorkerThreadMain(Message& action);
private:
bool addBusiness(Business* data);
bool addBusinessType(BusinessType* data);
bool addImage(uint32 id, QByteArray& data);
Business* getBusiness(uint32 id);
list<BusinessType>* getBusinessTypes();
bool modifyBusiness(Business* data);
bool modifyBusinessType(BusinessType* data);
bool removeBusiness(uint32 id);
bool removeBusinessType(uint32 id);
bool removeImage(uint32 id);
list<Business>* getAllBusiness();
};
#endif // !defined(EA_6594F28D_DB37_4636_8095_CAF8A14A53D9__INCLUDED_)
///////////////////////////////////////////////////////////
// BmDbThread.cpp
// Implementation of the Class BmDbThread
// Created on: 05-五月-2009 11:40:31
// Original author: Tim Kuo
///////////////////////////////////////////////////////////
#include "BmDbThread.h"
#include "Business.h"
#include "BusinessType.h"
#include "Message.h"
#include "messagedef.h"
#include <QByteArray>
#include <QSqlQuery>
#include <QVariant>
#include "dbquery.h"
:DbThread(parent, priority)
{
setDbServer("QSQLITE", DBCONNECTION_BM, "", "", "", 0);
}
BmDbThread::~BmDbThread(){
}
void BmDbThread::WorkerThreadMain(Message& action)
{
DBHEX("bm db thread processing action: ", action.type());
switch(action.type()) {
case ACTION_GETBUSINESSTYPE:
{
m_tempMsg = new Message(EVENT_BUSINESSTYPE, getBusinessTypes());
break;
}
case ACTION_GETALLBUSINESS:
{
m_tempMsg = new Message(EVENT_ALLBUSINESS, getAllBusiness());
break;
}
case ACTION_GETBUSINESS:
{
uint32* id = static_cast<uint32*>(action.data());
m_tempMsg = new Message(EVENT_BUSINESS, getBusiness(*id));
delete id;
break;
}
case ACTION_GETBUSINESSPIC:
{
uint32* id = static_cast<uint32*>(action.data());
m_tempMsg = new Message(EVENT_BUSINESSPIC, getImage(*id));
delete id;
break;
}
case ACTION_ADDBUSINESS:
{
Business* business = static_cast<Business*>(action.data());
QByteArray* image
= static_cast<QByteArray
*>
(action.
data2());
Business* addedBusiness = NULL;
m_tempMsg = new Message(EVENT_ADDBUSINESS);
if(addBusiness(business)) {
addedBusiness = getBusiness(business->id());
m_tempMsg->setData(addedBusiness);
if(NULL != addedBusiness && NULL != image && !image->isEmpty()) {
if(addImage(addedBusiness->id(), *image))
m_tempMsg->setData2(getImage(addedBusiness->id()));
image->clear();
}
}
delete business;
delete image;
break;
}
case ACTION_REMOVEBUSINESS:
{
uint32* id = static_cast<uint32*>(action.data());
uint32* result = new uint32(*id);
if(!removeBusiness(*id))
*result = 0;
m_tempMsg = new Message(EVENT_REMOVEBUSINESS, result);
delete id;
break;
}
case ACTOIN_MODIFYBUSINESS:
{
Business* toModify = static_cast<Business*>(action.data());
QByteArray* image
= static_cast<QByteArray
*>
(action.
data2());
Business* modified = NULL;
m_tempMsg = new Message(EVENT_MODIFYBUSINESS);
if(modifyBusiness(toModify)) {
modified = getBusiness(toModify->id());
m_tempMsg->setData(modified);
if(NULL != image && !image->isEmpty() && toModify->id() != 0) {
if(addImage(modified->id(), *image))
m_tempMsg->setData2(getImage(modified->id()));
}
}
delete toModify;
break;
}
case ACTION_VIEWBUSINESSRECORD:
{
break;
}
case ACTION_SETBUSINESSTYPE:
{
list<BusinessType>* toSet =
static_cast<list<BusinessType>*>(action.data());
list<BusinessType>* result = new list<BusinessType>;
list<BusinessType>::iterator it = toSet->begin();
while(toSet->end() != it) {
if(!addBusinessType(&(*it)))
result->push_back(*it);
it++;
}
delete toSet;
m_tempMsg = new Message(EVENT_SETBUSINESSTYPE, result);
break;
}
case ACTION_REMOVEBUSINESSTYPE:
{
list<BusinessType>* toRemove =
static_cast<list<BusinessType>*>(action.data());
list<BusinessType>* result = new list<BusinessType>;
list<BusinessType>::iterator it = toRemove->begin();
while(toRemove->end() != it) {
if(!removeBusinessType(it->getId()))
result->push_back(*it);
it++;
}
delete toRemove;
m_tempMsg = new Message(EVENT_REMOVEBUSINESSTYPE, result);
break;
}
default: break;
}
if(NULL != m_tempMsg) {
postEvent(m_tempMsg, EventDb);
m_tempMsg = NULL;
}
}
Bookmarks