Subversion Repositories public iLand

Rev

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