Subversion Repositories public iLand

Rev

Rev 1157 | Rev 1217 | 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"
1157 werner 42
#include "waterout.h"
173 werner 43
 
178 werner 44
 
605 werner 45
// on creation of the output manager
46
// an instance of every iLand output
47
// must be added to the list of outputs.
173 werner 48
OutputManager::OutputManager()
49
{
231 werner 50
    mTransactionOpen = false;
173 werner 51
    // add all the outputs
52
    mOutputs.append(new TreeOut);
1102 werner 53
    mOutputs.append(new TreeRemovedOut);
179 werner 54
    mOutputs.append(new StandOut);
837 werner 55
    mOutputs.append(new LandscapeOut);
1114 werner 56
    mOutputs.append(new LandscapeRemovedOut);
184 werner 57
    mOutputs.append(new DynamicStandOut);
228 werner 58
    mOutputs.append(new ProductionOut);
262 werner 59
    mOutputs.append(new StandDeadOut);
278 werner 60
    mOutputs.append(new ManagementOut);
504 werner 61
    mOutputs.append(new SaplingOut);
1182 werner 62
    mOutputs.append(new SaplingDetailsOut);
587 werner 63
    mOutputs.append(new CarbonOut);
605 werner 64
    mOutputs.append(new CarbonFlowOut);
1157 werner 65
    mOutputs.append(new WaterOut);
665 werner 66
}
184 werner 67
 
665 werner 68
void OutputManager::addOutput(Output *output)
69
{
70
    mOutputs.append(output);
173 werner 71
}
72
 
1054 werner 73
void OutputManager::removeOutput(const QString &tableName)
74
{
75
    Output *o = find(tableName);
76
    if (o) {
77
        mOutputs.removeAt(mOutputs.indexOf(o));
78
        delete o;
79
    }
80
}
665 werner 81
 
1054 werner 82
 
173 werner 83
OutputManager::~OutputManager()
84
{
85
    qDeleteAll(mOutputs);
86
}
87
 
88
void OutputManager::setup()
89
{
539 werner 90
    //close();
173 werner 91
    XmlHelper &xml = const_cast<XmlHelper&>(GlobalSettings::instance()->settings());
92
    QString nodepath;
93
    foreach(Output *o, mOutputs) {
176 werner 94
        nodepath = QString("output.%1").arg(o->tableName());
173 werner 95
        xml.setCurrentNode(nodepath);
96
        qDebug() << "setup of output" << o->name();
97
        o->setup();
181 werner 98
        bool enabled = xml.valueBool(".enabled", false);
99
        o->setEnabled(enabled);
100
        if (enabled)
101
            o->open();
173 werner 102
    }
395 werner 103
    endTransaction(); // just to be sure
173 werner 104
}
176 werner 105
 
106
Output *OutputManager::find(const QString& tableName)
107
{
108
    foreach(Output* p,mOutputs)
109
        if (p->tableName()==tableName)
110
            return p;
111
    return NULL;
112
}
113
 
222 werner 114
void OutputManager::save()
115
{
231 werner 116
    endTransaction();
222 werner 117
}
118
 
232 werner 119
void OutputManager::close()
120
{
121
    qDebug() << "outputs closed";
122
    foreach(Output *p, mOutputs)
123
        p->close();
124
}
125
 
395 werner 126
/** start a database transaction.
127
    do nothing if transaction is already open. */
231 werner 128
void OutputManager::startTransaction()
129
{
130
    if (!mTransactionOpen && GlobalSettings::instance()->dbout().isValid()) {
232 werner 131
        if (GlobalSettings::instance()->dbout().transaction()) {
132
            qDebug() << "opening transaction";
133
            mTransactionOpen = true;
134
        }
231 werner 135
    }
136
}
137
void OutputManager::endTransaction()
138
{
139
    if (mTransactionOpen && GlobalSettings::instance()->dbout().isValid()) {
232 werner 140
        if (GlobalSettings::instance()->dbout().commit()) {
141
            mTransactionOpen = false;
142
            qDebug() << "database transaction commited";
143
        }
144
    }
231 werner 145
}
146
 
176 werner 147
bool OutputManager::execute(const QString& tableName)
148
{
223 werner 149
    DebugTimer t("OutputManager::execute()");
225 werner 150
    t.setSilent();
176 werner 151
    Output *p = find(tableName);
152
    if (p) {
182 werner 153
        if (!p->isEnabled())
154
            return false;
155
        if(!p->isOpen())
156
            return false;
520 werner 157
        if (!p->isRowEmpty()) {
207 werner 158
            qWarning() << "Output" << p->name() << "invalid (not at new row)!!!";
159
            return false;
160
        }
161
 
231 werner 162
        startTransaction(); // just assure a transaction is open.... nothing happens if already inside a transaction
176 werner 163
        p->exec();
222 werner 164
 
176 werner 165
        return true;
166
    }
167
    qDebug() << "output" << tableName << "not found!";
168
    return false; // no output found
169
}
254 werner 170
 
171
QString OutputManager::wikiFormat()
172
{
173
    QString result;
174
    foreach(const Output *o, mOutputs)
175
        result+=o->wikiFormat() + "\n\n";
176
    return result;
177
}
665 werner 178