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