Subversion Repositories public iLand

Rev

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