Subversion Repositories public iLand

Rev

Rev 179 | Rev 209 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 179 Rev 191
1
Redirecting to URL 'https://iland.boku.ac.at/svn/iland/tags/release_1.0/src/core/speciesset.cpp':
1
Redirecting to URL 'https://iland.boku.ac.at/svn/iland/tags/release_1.0/src/core/speciesset.cpp':
2
#include <QtCore>
2
#include <QtCore>
3
#include <QtSql>
3
#include <QtSql>
4
#include "global.h"
4
#include "global.h"
5
#include "xmlhelper.h"
5
#include "xmlhelper.h"
6
#include "speciesset.h"
6
#include "speciesset.h"
7
#include "species.h"
7
#include "species.h"
8
8
9
SpeciesSet::SpeciesSet()
9
SpeciesSet::SpeciesSet()
10
{
10
{
11
    mSetupQuery = 0;
11
    mSetupQuery = 0;
12
}
12
}
13
13
14
SpeciesSet::~SpeciesSet()
14
SpeciesSet::~SpeciesSet()
15
{
15
{
16
   clear();
16
   clear();
17
}
17
}
18
18
19
void SpeciesSet::clear()
19
void SpeciesSet::clear()
20
{
20
{
21
    qDeleteAll(mSpecies.values());
21
    qDeleteAll(mSpecies.values());
22
    mSpecies.clear();
22
    mSpecies.clear();
23
    mActiveSpecies.clear();
23
    mActiveSpecies.clear();
24
}
24
}
25
25
26
const Species *SpeciesSet::species(const int &index)
26
const Species *SpeciesSet::species(const int &index)
27
{
27
{
28
    foreach(Species *s, mSpecies)
28
    foreach(Species *s, mSpecies)
29
        if (s->index() == index)
29
        if (s->index() == index)
30
            return s;
30
            return s;
31
    return NULL;
31
    return NULL;
32
}
32
}
33
33
34
/** loads active species from a database table and creates/setups the species.
34
/** loads active species from a database table and creates/setups the species.
35
    The function uses the global database-connection.
35
    The function uses the global database-connection.
36
  */
36
  */
37
int SpeciesSet::setup()
37
int SpeciesSet::setup()
38
{
38
{
39
    const XmlHelper &xml = GlobalSettings::instance()->settings();
39
    const XmlHelper &xml = GlobalSettings::instance()->settings();
40
    QString tableName = xml.value("species.source", "species");
-
 
41
    QString readerFile = xml.value("species.reader", "reader.bin");
-
 
-
 
40
    QString tableName = xml.value("model.species.source", "species");
-
 
41
    QString readerFile = xml.value("model.species.reader", "reader.bin");
42
    readerFile = GlobalSettings::instance()->path(readerFile, "lip");
42
    readerFile = GlobalSettings::instance()->path(readerFile, "lip");
43
    mReaderStamp.load(readerFile);
43
    mReaderStamp.load(readerFile);
44
44
45
    QSqlQuery query(GlobalSettings::instance()->dbin());
45
    QSqlQuery query(GlobalSettings::instance()->dbin());
46
    mSetupQuery = &query;
46
    mSetupQuery = &query;
47
    QString sql = QString("select * from %1").arg(tableName);
47
    QString sql = QString("select * from %1").arg(tableName);
48
    query.exec(sql);
48
    query.exec(sql);
49
    clear();
49
    clear();
50
    qDebug() << "attempting to load a species set from" << tableName;
50
    qDebug() << "attempting to load a species set from" << tableName;
51
    while (query.next()) {
51
    while (query.next()) {
52
        if (var("active").toInt()==0)
52
        if (var("active").toInt()==0)
53
            continue;
53
            continue;
54
54
55
        Species *s = new Species(this); // create
55
        Species *s = new Species(this); // create
56
        // call setup routine (which calls SpeciesSet::var() to retrieve values
56
        // call setup routine (which calls SpeciesSet::var() to retrieve values
57
        s->setup();
57
        s->setup();
58
58
59
        mSpecies.insert(s->id(), s); // store
59
        mSpecies.insert(s->id(), s); // store
60
        if (s->active())
60
        if (s->active())
61
            mActiveSpecies.append(s);
61
            mActiveSpecies.append(s);
62
    } // while query.next()
62
    } // while query.next()
63
    qDebug() << "loaded" << mSpecies.count() << "active species:";
63
    qDebug() << "loaded" << mSpecies.count() << "active species:";
64
    qDebug() << mSpecies.keys();
64
    qDebug() << mSpecies.keys();
65
65
66
    mSetupQuery = 0;
66
    mSetupQuery = 0;
67
    return mSpecies.count();
67
    return mSpecies.count();
68
68
69
}
69
}
70
/** retrieves variables from the datasource available during the setup of species.
70
/** retrieves variables from the datasource available during the setup of species.
71
  */
71
  */
72
QVariant SpeciesSet::var(const QString& varName)
72
QVariant SpeciesSet::var(const QString& varName)
73
{
73
{
74
    Q_ASSERT(mSetupQuery!=0);
74
    Q_ASSERT(mSetupQuery!=0);
75
75
76
    int idx = mSetupQuery->record().indexOf(varName);
76
    int idx = mSetupQuery->record().indexOf(varName);
77
    if (idx>=0)
77
    if (idx>=0)
78
        return mSetupQuery->value(idx);
78
        return mSetupQuery->value(idx);
79
    throw IException(QString("SpeciesSet: variable not set: %1").arg(varName));
79
    throw IException(QString("SpeciesSet: variable not set: %1").arg(varName));
80
    //throw IException(QString("load species parameter: field %1 not found!").arg(varName));
80
    //throw IException(QString("load species parameter: field %1 not found!").arg(varName));
81
    // lookup in defaults
81
    // lookup in defaults
82
    //qDebug() << "variable" << varName << "not found - using default.";
82
    //qDebug() << "variable" << varName << "not found - using default.";
83
    //return GlobalSettings::instance()->settingDefaultValue(varName);
83
    //return GlobalSettings::instance()->settingDefaultValue(varName);
84
}
84
}
85
 
85