Subversion Repositories public iLand

Rev

Rev 125 | Rev 191 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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