Subversion Repositories public iLand

Rev

Rev 278 | Rev 504 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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