Subversion Repositories public iLand

Rev

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