Subversion Repositories public iLand

Rev

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