Subversion Repositories public iLand

Rev

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