1 | /* |
---|
2 | * DAAccountListManager.cpp |
---|
3 | * |
---|
4 | * Created on: Aug 30, 2009 |
---|
5 | * Author: saemy |
---|
6 | */ |
---|
7 | |
---|
8 | #include "DAAccountListManager.h" |
---|
9 | |
---|
10 | #include <QSqlQuery> |
---|
11 | |
---|
12 | #include <igateway.h> |
---|
13 | #include <StorageExceptions.h> |
---|
14 | #include <typeconvert.h> |
---|
15 | |
---|
16 | #include "../../business/BCGatewayManager.h" |
---|
17 | |
---|
18 | #include "DAStorage.h" |
---|
19 | |
---|
20 | DAAccountListManager* DAAccountListManager::instance_=0; |
---|
21 | DAAccountListManager* DAAccountListManager::instance(){ |
---|
22 | return instance_ ? instance_ : (instance_ = new DAAccountListManager); |
---|
23 | } |
---|
24 | |
---|
25 | |
---|
26 | IAccount* DAAccountListManager::getAccount(int accountId, bool skeletonOnly /* = false */) { |
---|
27 | QSqlQuery query = storage()->createQuery(); |
---|
28 | query.exec("SELECT type " |
---|
29 | "FROM t_accounts " |
---|
30 | "WHERE (accountId = '" + QString::number(accountId) + "'); "); |
---|
31 | if (!query.next()) { |
---|
32 | throw Storage::EReadException(tr("No such account in the database! [accountId: %1]").arg(accountId)); |
---|
33 | } |
---|
34 | |
---|
35 | IGateway* gateway = BCGatewayManager::instance()->getGateway(query.value(0).toString()); |
---|
36 | |
---|
37 | // TODO: What if this gateway is NULL (is not loaded)? |
---|
38 | gateway->setDefaultStorage(storage()); |
---|
39 | |
---|
40 | IAccount* account = gateway->createAccountInstance(); |
---|
41 | account->setId(accountId); |
---|
42 | |
---|
43 | if (!skeletonOnly) { |
---|
44 | account->getStorageOfficer()->readFromStorage(); |
---|
45 | } |
---|
46 | |
---|
47 | return account; |
---|
48 | } |
---|
49 | |
---|
50 | QSet<IAccount*> DAAccountListManager::getAccountList(bool skeletonsOnly /* = false */) { |
---|
51 | QSet<IAccount*> accountList; |
---|
52 | |
---|
53 | QSqlQuery query = storage()->createQuery(); |
---|
54 | query.exec("SELECT accountId " |
---|
55 | "FROM t_accounts;"); |
---|
56 | while (query.next()) { |
---|
57 | try{ |
---|
58 | IAccount* account = getAccount(query.value(0).toInt(), skeletonsOnly); |
---|
59 | accountList.insert(account); |
---|
60 | }catch (const Storage::EReadException& e){ |
---|
61 | // TODO: What shall be done here? |
---|
62 | } |
---|
63 | } |
---|
64 | |
---|
65 | return accountList; |
---|
66 | } |
---|
67 | |
---|
68 | |
---|
69 | IStorage* DAAccountListManager::storage() { |
---|
70 | return DAStorage::instance(); |
---|
71 | } |
---|