Subversion Repositories public iLand

Rev

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