Subversion Repositories public iLand

Rev

Rev 278 | Rev 504 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 278 Rev 395
1
Redirecting to URL 'https://iland.boku.ac.at/svn/iland/tags/release_1.0/src/output/outputmanager.cpp':
1
Redirecting to URL 'https://iland.boku.ac.at/svn/iland/tags/release_1.0/src/output/outputmanager.cpp':
2
/** @class OutputManager
2
/** @class OutputManager
3
   Global container that handles data output.
3
   Global container that handles data output.
4

4

5
  */
5
  */
6
6
7
#include "global.h"
7
#include "global.h"
8
#include "outputmanager.h"
8
#include "outputmanager.h"
9
#include "helper.h"
9
#include "helper.h"
10
#include <QtCore>
10
#include <QtCore>
11
11
12
// tree outputs
12
// tree outputs
13
#include "treeout.h"
13
#include "treeout.h"
14
#include "standout.h"
14
#include "standout.h"
15
#include "standdeadout.h"
15
#include "standdeadout.h"
16
#include "managementout.h"
16
#include "managementout.h"
17
#include "dynamicstandout.h"
17
#include "dynamicstandout.h"
18
#include "productionout.h"
18
#include "productionout.h"
19
19
20
20
21
21
22
OutputManager::OutputManager()
22
OutputManager::OutputManager()
23
{
23
{
24
    mTransactionOpen = false;
24
    mTransactionOpen = false;
25
    // add all the outputs
25
    // add all the outputs
26
    mOutputs.append(new TreeOut);
26
    mOutputs.append(new TreeOut);
27
    mOutputs.append(new StandOut);
27
    mOutputs.append(new StandOut);
28
    mOutputs.append(new DynamicStandOut);
28
    mOutputs.append(new DynamicStandOut);
29
    mOutputs.append(new ProductionOut);
29
    mOutputs.append(new ProductionOut);
30
    mOutputs.append(new StandDeadOut);
30
    mOutputs.append(new StandDeadOut);
31
    mOutputs.append(new ManagementOut);
31
    mOutputs.append(new ManagementOut);
32
32
33
}
33
}
34
34
35
OutputManager::~OutputManager()
35
OutputManager::~OutputManager()
36
{
36
{
37
    qDeleteAll(mOutputs);
37
    qDeleteAll(mOutputs);
38
}
38
}
39
39
40
void OutputManager::setup()
40
void OutputManager::setup()
41
{
41
{
42
    close();
42
    close();
43
    XmlHelper &xml = const_cast<XmlHelper&>(GlobalSettings::instance()->settings());
43
    XmlHelper &xml = const_cast<XmlHelper&>(GlobalSettings::instance()->settings());
44
    QString nodepath;
44
    QString nodepath;
45
    foreach(Output *o, mOutputs) {
45
    foreach(Output *o, mOutputs) {
46
        nodepath = QString("output.%1").arg(o->tableName());
46
        nodepath = QString("output.%1").arg(o->tableName());
47
        xml.setCurrentNode(nodepath);
47
        xml.setCurrentNode(nodepath);
48
        qDebug() << "setup of output" << o->name();
48
        qDebug() << "setup of output" << o->name();
49
        o->setup();
49
        o->setup();
50
        bool enabled = xml.valueBool(".enabled", false);
50
        bool enabled = xml.valueBool(".enabled", false);
51
        o->setEnabled(enabled);
51
        o->setEnabled(enabled);
52
        if (enabled)
52
        if (enabled)
53
            o->open();
53
            o->open();
54
    }
54
    }
55
    endTransaction(); // just to be sur
-
 
-
 
55
    endTransaction(); // just to be sure
56
}
56
}
57
57
58
Output *OutputManager::find(const QString& tableName)
58
Output *OutputManager::find(const QString& tableName)
59
{
59
{
60
    foreach(Output* p,mOutputs)
60
    foreach(Output* p,mOutputs)
61
        if (p->tableName()==tableName)
61
        if (p->tableName()==tableName)
62
            return p;
62
            return p;
63
    return NULL;
63
    return NULL;
64
}
64
}
65
65
66
void OutputManager::save()
66
void OutputManager::save()
67
{
67
{
68
    endTransaction();
68
    endTransaction();
69
}
69
}
70
70
71
void OutputManager::close()
71
void OutputManager::close()
72
{
72
{
73
    qDebug() << "outputs closed";
73
    qDebug() << "outputs closed";
74
    foreach(Output *p, mOutputs)
74
    foreach(Output *p, mOutputs)
75
        p->close();
75
        p->close();
76
}
76
}
77
77
-
 
78
/** start a database transaction.
-
 
79
    do nothing if transaction is already open. */
78
void OutputManager::startTransaction()
80
void OutputManager::startTransaction()
79
{
81
{
80
    if (!mTransactionOpen && GlobalSettings::instance()->dbout().isValid()) {
82
    if (!mTransactionOpen && GlobalSettings::instance()->dbout().isValid()) {
81
        if (GlobalSettings::instance()->dbout().transaction()) {
83
        if (GlobalSettings::instance()->dbout().transaction()) {
82
            qDebug() << "opening transaction";
84
            qDebug() << "opening transaction";
83
            mTransactionOpen = true;
85
            mTransactionOpen = true;
84
        }
86
        }
85
    }
87
    }
86
}
88
}
87
void OutputManager::endTransaction()
89
void OutputManager::endTransaction()
88
{
90
{
89
    if (mTransactionOpen && GlobalSettings::instance()->dbout().isValid()) {
91
    if (mTransactionOpen && GlobalSettings::instance()->dbout().isValid()) {
90
        if (GlobalSettings::instance()->dbout().commit()) {
92
        if (GlobalSettings::instance()->dbout().commit()) {
91
            mTransactionOpen = false;
93
            mTransactionOpen = false;
92
            qDebug() << "database transaction commited";
94
            qDebug() << "database transaction commited";
93
        }
95
        }
94
    }
96
    }
95
}
97
}
96
98
97
bool OutputManager::execute(const QString& tableName)
99
bool OutputManager::execute(const QString& tableName)
98
{
100
{
99
    DebugTimer t("OutputManager::execute()");
101
    DebugTimer t("OutputManager::execute()");
100
    t.setSilent();
102
    t.setSilent();
101
    Output *p = find(tableName);
103
    Output *p = find(tableName);
102
    if (p) {
104
    if (p) {
103
        if (!p->isEnabled())
105
        if (!p->isEnabled())
104
            return false;
106
            return false;
105
        if(!p->isOpen())
107
        if(!p->isOpen())
106
            return false;
108
            return false;
107
        if (!p->onNewRow()) {
109
        if (!p->onNewRow()) {
108
            qWarning() << "Output" << p->name() << "invalid (not at new row)!!!";
110
            qWarning() << "Output" << p->name() << "invalid (not at new row)!!!";
109
            return false;
111
            return false;
110
        }
112
        }
111
113
112
        startTransaction(); // just assure a transaction is open.... nothing happens if already inside a transaction
114
        startTransaction(); // just assure a transaction is open.... nothing happens if already inside a transaction
113
        p->exec();
115
        p->exec();
114
116
115
        return true;
117
        return true;
116
    }
118
    }
117
    qDebug() << "output" << tableName << "not found!";
119
    qDebug() << "output" << tableName << "not found!";
118
    return false; // no output found
120
    return false; // no output found
119
}
121
}
120
122
121
QString OutputManager::wikiFormat()
123
QString OutputManager::wikiFormat()
122
{
124
{
123
    QString result;
125
    QString result;
124
    foreach(const Output *o, mOutputs)
126
    foreach(const Output *o, mOutputs)
125
        result+=o->wikiFormat() + "\n\n";
127
        result+=o->wikiFormat() + "\n\n";
126
    return result;
128
    return result;
127
}
129
}
128
 
130