Subversion Repositories public iLand

Rev

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

Rev Author Line No. Line
1
 
671 werner 2
/********************************************************************************************
3
**    iLand - an individual based forest landscape and disturbance model
4
**    http://iland.boku.ac.at
5
**    Copyright (C) 2009-  Werner Rammer, Rupert Seidl
6
**
7
**    This program is free software: you can redistribute it and/or modify
8
**    it under the terms of the GNU General Public License as published by
9
**    the Free Software Foundation, either version 3 of the License, or
10
**    (at your option) any later version.
11
**
12
**    This program is distributed in the hope that it will be useful,
13
**    but WITHOUT ANY WARRANTY; without even the implied warranty of
14
**    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
**    GNU General Public License for more details.
16
**
17
**    You should have received a copy of the GNU General Public License
18
**    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
********************************************************************************************/
20
 
178 werner 21
/** @class OutputManager
22
   Global container that handles data output.
23
 
24
  */
25
 
173 werner 26
#include "global.h"
27
#include "outputmanager.h"
808 werner 28
#include "debugtimer.h"
173 werner 29
#include <QtCore>
30
 
31
// tree outputs
32
#include "treeout.h"
179 werner 33
#include "standout.h"
837 werner 34
#include "landscapeout.h"
262 werner 35
#include "standdeadout.h"
278 werner 36
#include "managementout.h"
184 werner 37
#include "dynamicstandout.h"
228 werner 38
#include "productionout.h"
504 werner 39
#include "saplingout.h"
587 werner 40
#include "carbonout.h"
605 werner 41
#include "carbonflowout.h"
173 werner 42
 
178 werner 43
 
605 werner 44
// on creation of the output manager
45
// an instance of every iLand output
46
// must be added to the list of outputs.
173 werner 47
OutputManager::OutputManager()
48
{
231 werner 49
    mTransactionOpen = false;
173 werner 50
    // add all the outputs
51
    mOutputs.append(new TreeOut);
1102 werner 52
    mOutputs.append(new TreeRemovedOut);
179 werner 53
    mOutputs.append(new StandOut);
837 werner 54
    mOutputs.append(new LandscapeOut);
184 werner 55
    mOutputs.append(new DynamicStandOut);
228 werner 56
    mOutputs.append(new ProductionOut);
262 werner 57
    mOutputs.append(new StandDeadOut);
278 werner 58
    mOutputs.append(new ManagementOut);
504 werner 59
    mOutputs.append(new SaplingOut);
587 werner 60
    mOutputs.append(new CarbonOut);
605 werner 61
    mOutputs.append(new CarbonFlowOut);
665 werner 62
}
184 werner 63
 
665 werner 64
void OutputManager::addOutput(Output *output)
65
{
66
    mOutputs.append(output);
173 werner 67
}
68
 
1054 werner 69
void OutputManager::removeOutput(const QString &tableName)
70
{
71
    Output *o = find(tableName);
72
    if (o) {
73
        mOutputs.removeAt(mOutputs.indexOf(o));
74
        delete o;
75
    }
76
}
665 werner 77
 
1054 werner 78
 
173 werner 79
OutputManager::~OutputManager()
80
{
81
    qDeleteAll(mOutputs);
82
}
83
 
84
void OutputManager::setup()
85
{
539 werner 86
    //close();
173 werner 87
    XmlHelper &xml = const_cast<XmlHelper&>(GlobalSettings::instance()->settings());
88
    QString nodepath;
89
    foreach(Output *o, mOutputs) {
176 werner 90
        nodepath = QString("output.%1").arg(o->tableName());
173 werner 91
        xml.setCurrentNode(nodepath);
92
        qDebug() << "setup of output" << o->name();
93
        o->setup();
181 werner 94
        bool enabled = xml.valueBool(".enabled", false);
95
        o->setEnabled(enabled);
96
        if (enabled)
97
            o->open();
173 werner 98
    }
395 werner 99
    endTransaction(); // just to be sure
173 werner 100
}
176 werner 101
 
102
Output *OutputManager::find(const QString& tableName)
103
{
104
    foreach(Output* p,mOutputs)
105
        if (p->tableName()==tableName)
106
            return p;
107
    return NULL;
108
}
109
 
222 werner 110
void OutputManager::save()
111
{
231 werner 112
    endTransaction();
222 werner 113
}
114
 
232 werner 115
void OutputManager::close()
116
{
117
    qDebug() << "outputs closed";
118
    foreach(Output *p, mOutputs)
119
        p->close();
120
}
121
 
395 werner 122
/** start a database transaction.
123
    do nothing if transaction is already open. */
231 werner 124
void OutputManager::startTransaction()
125
{
126
    if (!mTransactionOpen && GlobalSettings::instance()->dbout().isValid()) {
232 werner 127
        if (GlobalSettings::instance()->dbout().transaction()) {
128
            qDebug() << "opening transaction";
129
            mTransactionOpen = true;
130
        }
231 werner 131
    }
132
}
133
void OutputManager::endTransaction()
134
{
135
    if (mTransactionOpen && GlobalSettings::instance()->dbout().isValid()) {
232 werner 136
        if (GlobalSettings::instance()->dbout().commit()) {
137
            mTransactionOpen = false;
138
            qDebug() << "database transaction commited";
139
        }
140
    }
231 werner 141
}
142
 
176 werner 143
bool OutputManager::execute(const QString& tableName)
144
{
223 werner 145
    DebugTimer t("OutputManager::execute()");
225 werner 146
    t.setSilent();
176 werner 147
    Output *p = find(tableName);
148
    if (p) {
182 werner 149
        if (!p->isEnabled())
150
            return false;
151
        if(!p->isOpen())
152
            return false;
520 werner 153
        if (!p->isRowEmpty()) {
207 werner 154
            qWarning() << "Output" << p->name() << "invalid (not at new row)!!!";
155
            return false;
156
        }
157
 
231 werner 158
        startTransaction(); // just assure a transaction is open.... nothing happens if already inside a transaction
176 werner 159
        p->exec();
222 werner 160
 
176 werner 161
        return true;
162
    }
163
    qDebug() << "output" << tableName << "not found!";
164
    return false; // no output found
165
}
254 werner 166
 
167
QString OutputManager::wikiFormat()
168
{
169
    QString result;
170
    foreach(const Output *o, mOutputs)
171
        result+=o->wikiFormat() + "\n\n";
172
    return result;
173
}
665 werner 174