summaryrefslogtreecommitdiffstats
path: root/mappingtreemodel.cpp
blob: 25d7921decd05b77626051e2eff4f69262112ab4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
/*
  This program is free software; you can redistribute it and/or
  modify it under the terms of the GNU General Public License
  as published by the Free Software Foundation; either version
  2 of the License, or (at your option) any later version.
*/

#include <QSqlDatabase>
#include <QSqlQuery>
#include <QHash>

#include "mappingtreemodel.h"
#include "smtreeitem.h"
#include "smglobals.h"

MappingTreeModel::MappingTreeModel(QStringList &headers, QObject *parent) : SmTreeModel(headers, parent), mForbidden("/"), mType(-1) {
    //init database
    mDb = QSqlDatabase::database("treedb");
    getMappingTypes();

    mSParentsQ = "SELECT mapping_parents.imapping_parents_id, mapping_parents.idescription_id, mapping_description.tdescription_name, mapping_description.tscreated, mapping_parents.iparent_id FROM mapping_parents, mapping_description WHERE  mapping_parents.iparent_id = :pid AND mapping_description.idescription_type = :type AND mapping_parents.idescription_id = mapping_description.idescription_id ORDER BY mapping_description.tdescription_name";
}

QStringList MappingTreeModel::mappingTypeNames() const {
    QStringList retval;
    foreach(mappingType t, mMappingTypes){
        retval << t.name.toString();
    }
    qSort(retval);
    return retval;
}

int MappingTreeModel::mappingTypeIdFromName(const QVariant &name) const{
    foreach(const mappingType t, mMappingTypes){
        if(t.name == name){
            return t.id.toInt();
        }
    }
    return -1;
}

QVariant MappingTreeModel::data(const QModelIndex &index, int role) const{
    if(!index.isValid()){
        return QVariant();
    }
    SmTreeItem *item = itemAt(index);
    if(role == NameRole){
        return item->data(Name);
    }
    if(role == MappingIdRole){
        return item->data(MappingId);
    }
    if(role == DescAddedRole){
        return item->data(DescAdded);
    }
    if(role == DescIdRole){
        return item->data(DescId);
    }
    if(role == MappingParentIdRole){
        return item->data(MappingParentId);
    }
    return SmTreeModel::data(index, role);
}

QList<QVariant> MappingTreeModel::childList(const QVariant &value, int column) const{
    QModelIndex itemIdx = findRecursive(value, column, createIndex(0, column, root()));
    SmTreeItem *item = static_cast<SmTreeItem*>(itemIdx.internalPointer());
    if(item){
        if(item->childCount()){
            return getChildListRecursive(item, column);
        }
    }
    return QList<QVariant>() << value;
}

QModelIndex MappingTreeModel::indexFromParentPath(const QList<int> &pPath, bool reverse) const{
    QList<int> path = pPath;
    if(reverse){
        std::reverse(path.begin(), path.end());
    }
    QModelIndex retval = QModelIndex();
    while(!path.isEmpty()){
        int curId = path.last();
        path.removeLast();
        retval = find(curId, MappingId, retval);
        if(!retval.isValid()){
            return QModelIndex();
        }
    }
    return retval;
}

bool MappingTreeModel::setData(const QModelIndex &idx, const QVariant &value, int role){
    if(!idx.isValid()){
        return false;
    }
    SmTreeItem *item = itemAt(idx);
    if(role == Qt::EditRole){
        if(idx.column() == Name){
            if(value.toString().contains(mForbidden)){
                return false;
            }
        }
        updateChild(item, value);
    }
    if(role == NameRole){
        if(value.toString().contains(mForbidden)){
            return false;
        }
        item->setData(Name, value);
    }
    if(role == MappingIdRole){
        item->setData(MappingId, value);
    }
    if(role == DescAddedRole){
        item->setData(DescAdded, value);
    }
    if(role == DescIdRole){
        item->setData(DescId, value);
    }
    return true;
}

bool MappingTreeModel::addMappingType(const QString &type){
    QSqlQuery addMappingTypeQ(mDb);
    addMappingTypeQ.prepare("INSERT INTO mappings_type (tmappings_type_name) VALUES(:value)");
    addMappingTypeQ.bindValue(":value", type);
    if(addMappingTypeQ.exec()){
        getMappingTypes();
        return true;
    }
    mLastError = addMappingTypeQ.lastError();
    return false;
}

bool MappingTreeModel::deleteMappingType(int typeId){
    QSqlQuery deleteMappingTypeQ(mDb);
    deleteMappingTypeQ.prepare("DELETE FROM mappings_type WHERE imappings_type_id = :id");
    deleteMappingTypeQ.bindValue(":id", typeId);
    if(deleteMappingTypeQ.exec()){
        getMappingTypes();
        return true;
    }
    mLastError = deleteMappingTypeQ.lastError();
    return false;
}

bool MappingTreeModel::addChild(const QVariant &name, const QModelIndex &parent){
    if(!parent.isValid()){
        return false;
    }
    if(name.toString().contains(mForbidden)){
        return false;
    }
    //check if we already have a mapping with this name
    int id = mMappings.value(name.toString(), -1);
    if(id == -1){
        id = addDescription(name.toString(), mType);
    }
    QSqlQuery addParentQ(mDb);
    addParentQ.prepare("INSERT INTO mapping_parents (idescription_id, iparent_id) VALUES(:id, :parentid)");
    addParentQ.bindValue(":id", id);
    addParentQ.bindValue(":parentid", parent.data(MappingIdRole));
    if(addParentQ.exec()){
        populate();
        return true;
    }
    mLastError = addParentQ.lastError();
    return false;
}

bool MappingTreeModel::renameChild(const QModelIndex &idx, const QString newName){
    if(!idx.isValid()){
        return false;
    }
    int descId = idx.data(DescIdRole).toInt();
    QSqlQuery renameQ(mDb);
    renameQ.prepare("UPDATE mapping_description SET tdescription_name = :name WHERE idescription_id = :id");
    renameQ.bindValue(":id", descId);
    renameQ.bindValue(":name", newName);
    if(renameQ.exec()){
        populate();
        return true;
    }
    return false;
}

bool MappingTreeModel::deleteChild(const QModelIndex &idx){
    if(!idx.isValid()){
        return false;
    }
    SmTreeItem *item = itemAt(idx);
    if(item->childCount() > 0){
        return false;
    }
    QSqlQuery deleteChildQ(mDb);
    deleteChildQ.prepare("DELETE FROM mapping_parents WHERE imapping_parents_id = :id");
    deleteChildQ.bindValue(":id", item->data(MappingId));
    if(deleteChildQ.exec()){
        removeRows(idx.row(), 1, idx.parent());
        return true;
    }
    mLastError = deleteChildQ.lastError();
    return false;
}

MappingData MappingTreeModel::mappingDataFromItem(SmTreeItem *item) const{
    MappingData retval;
    retval.mappingId = item->data(MappingTreeModel::MappingId).toInt();
    retval.descId = item->data(MappingTreeModel::DescId).toInt();
    retval.name = item->data(MappingTreeModel::Name).toString();
    retval.parents << item->data(MappingTreeModel::MappingId).toInt();
    retval.path << item->data(MappingTreeModel::Name).toString();
    SmTreeItem *pItem = item->parent();
    while(pItem->parent()){
        retval.parents << pItem->data(MappingTreeModel::MappingId).toInt();
        retval.path << pItem->data(MappingTreeModel::Name).toString();
        pItem = pItem->parent();
    }
    std::reverse(retval.parents.begin(), retval.parents.end());
    std::reverse(retval.path.begin(), retval.path.end());
    return retval;
}

MappingData MappingTreeModel::mappingDataFromIndex(QModelIndex &idx) const{
    if(!idx.isValid()){
        return MappingData();
    }
    SmTreeItem *item = static_cast<SmTreeItem*>(idx.internalPointer());
    return mappingDataFromItem(item);
}

void MappingTreeModel::populate(){
    if(mType == -1){
        return;
    }
    // get nodes with root as parent
    QSqlQuery rootQ(mDb);
    rootQ.prepare(mSParentsQ);
    rootQ.bindValue(":type", mType);
    rootQ.bindValue(":pid", -1);
    if(rootQ.exec()){
        SmTreeItem *rootItem = new SmTreeItem(NumFields);
        rootItem->setData(DescId, -1);
        rootItem->setData(MappingId, -1);
        rootItem->setData(MappingParentId, -1);
        while(rootQ.next()){
            QList<QVariant> childData;
            childData << rootQ.value(2) << rootQ.value(0) << rootQ.value(3) << rootQ.value(1) << rootQ.value(4);
            SmTreeItem *childItem = new SmTreeItem(childData, rootItem);
            rootItem->appendChild(childItem);
            getChildrenRecursive(childItem);
        }
        setRoot(rootItem);
        emit needExpansion();
    }
}

void MappingTreeModel::setType(int type){
    mType = type;
    getMappings();
}

void MappingTreeModel::getMappings(){
    QSqlQuery descriptionQ(mDb);
    descriptionQ.prepare("SELECT tdescription_name, idescription_id FROM mapping_description WHERE idescription_type = :type");
    descriptionQ.bindValue(":type", mType);
    if(descriptionQ.exec()){
        mMappings.clear();
        while(descriptionQ.next()){
            mMappings.insert(descriptionQ.value(0).toString(), descriptionQ.value(1).toInt());
        }
    }
}

int MappingTreeModel::addDescription(const QString &name, int type){
    mDb.transaction();
    QSqlQuery addDescriptionQ(mDb);
    addDescriptionQ.prepare("INSERT INTO mapping_description (tdescription_name, idescription_type) VALUES(:name, :type)");
    addDescriptionQ.bindValue(":name", name);
    addDescriptionQ.bindValue(":type", type);
    if(addDescriptionQ.exec()){
        int currval = -1;
        QSqlQuery curval("SELECT currval('mapping_id__seq')", mDb);
        while(curval.next()){
            currval = curval.value(0).toInt();
        }
        mMappings.insert(name, currval);
        mDb.commit();
        return currval;;
    }
    mDb.rollback();
    return -1;
}

void MappingTreeModel::getMappingTypes(){
    QSqlQuery typesQ(mDb);
    typesQ.prepare("SELECT imappings_type_id, tmappings_type_name FROM mappings_type");
    bool qRes = typesQ.exec();
    if(qRes){
        mMappingTypes.clear();
        while(typesQ.next()){
            mappingType t;
            t.id = typesQ.value(0);
            t.name = typesQ.value(1);
            mMappingTypes << t;
        }
        emit mappingTypesChanged();
    }
}

void MappingTreeModel::getChildrenRecursive(SmTreeItem *item){
    QSqlQuery cq(mDb);
    cq.prepare(mSParentsQ);
    cq.bindValue(":type", mType);
    cq.bindValue(":pid", item->data(MappingId));
    if(cq.exec()){
        while(cq.next()){
            QList<QVariant> childData;
            childData << cq.value(2) << cq.value(0) << cq.value(3) << cq.value(1) << cq.value(4);
            SmTreeItem *child = new SmTreeItem(childData, item);
            item->appendChild(child);
            getChildrenRecursive(child);
        }
    }
}

bool MappingTreeModel::updateChild(SmTreeItem *item, const QVariant &value){
    QSqlQuery updateMapping(mDb);
    updateMapping.prepare("UPDATE mapping_description SET tdescription_name = :new WHERE idescription_id = :id");
    updateMapping.bindValue(":id", item->data(DescId));
    updateMapping.bindValue(":new", value);
    if(updateMapping.exec()){
        populate();
        return true;
    }
    return false;
}

QList<QVariant> MappingTreeModel::getChildListRecursive(SmTreeItem *item, int column) const{
    QList<QVariant> retval;
    if(!item){
        return retval;
    }
    for(int i = 0; i < item->childCount(); ++i){
        if(item->child(i)->childCount()){
            retval << getChildListRecursive(item->child(i), column);
        }else{
            retval << item->child(i)->data(column);
        }
    }
    return retval;
}

QString MappingTreeModel::basePath(SmTreeItem *item) const{
    QStringList pItems;
    SmTreeItem *cur = item;
    while(cur != root()){
        pItems << cur->data(Name).toString();
        cur = cur->parent();
    }
    if(pItems.isEmpty()){
        return QString();
    }
    std::reverse(pItems.begin(), pItems.end());
    return pItems.join("/");
}

int MappingTreeModel::lowerBound(SmTreeItem *item, const QVariant &value, int column) const {
    for(int i = 0; i < item->childCount(); ++i){
        SmTreeItem *child = item->child(i);
        if(child->data(column).toString() > value.toString()){
            return i;
        }
    }
    return item->childCount();
}

MappingTreeResultModel::MappingTreeResultModel(const QStringList &headers, QObject *parent) : SmTreeModel(headers, parent) {
    mSourceModel = static_cast<MappingTreeModel*>(SmGlobals::instance()->model("MappingTree"));
}

Qt::ItemFlags MappingTreeResultModel::flags(const QModelIndex &index) const {
    Q_UNUSED(index);
    return (Qt::ItemIsEnabled | Qt::ItemIsSelectable);
}

QVariant MappingTreeResultModel::data(const QModelIndex &index, int role) const{
    if(!index.isValid()){
        return QVariant();
    }
    SmTreeItem *item = itemAt(index);
    if(role == NameRole){
        return item->data(Name);
    }
    if(role == MappingIdRole){
        return item->data(MappingId);
    }
    if(role == DescIdRole){
        return item->data(DescId);
    }
    if(role == ParentIdRole){
        return item->data(ParentId);
    }
    return SmTreeModel::data(index, role);
}

bool MappingTreeResultModel::setData(const QModelIndex &index, const QVariant &value, int role){
    SmTreeItem *item = itemAt(index);
    if(role == NameRole){
        item->setData(Name, value);
        return true;
    }
    if(role == MappingIdRole){
        item->setData(MappingId, value);
        return true;
    }
    if(role == ParentIdRole){
        item->setData(ParentId, value);
        return true;
    }
    if(role == DescIdRole){
        item->setData(DescId, value);
        return true;
    }
    return SmTreeModel::setData(index, value, role);
}

void MappingTreeResultModel::addItem(const MappingData &data){
    QList<int> pPath = data.parents;

    if(!mCurrentData.contains(data)){
        mCurrentData << data;
    }

    QList<int> curPath;
    std::reverse(pPath.begin(), pPath.end());

    MappingTreeModel *mapModel = qobject_cast<MappingTreeModel*>(SmGlobals::instance()->model("MappingTree"));
    QModelIndex curIdx = QModelIndex();
    int curId = -1;
    beginResetModel();
    while(!pPath.isEmpty()){
        curId = pPath.last();
        curPath << curId;
        pPath.removeLast();
        QModelIndex lastIdx = curIdx;
        curIdx = find(curId, MappingId, curIdx);
        if(!curIdx.isValid()){
            QModelIndex sourceIdx = mapModel->indexFromParentPath(curPath, true);
            if(!sourceIdx.isValid()){
                return; //should never happen!
            }
            SmTreeItem *pItem;
            if(!lastIdx.isValid()){
                pItem = root();
            }else{
                pItem = static_cast<SmTreeItem*>(lastIdx.internalPointer());
            }
            int row = pItem->childCount();
            const QString curName = sourceIdx.data(MappingTreeModel::NameRole).toString();
            for(int i = 0; i < pItem->childCount(); ++i){
                if(pItem->child(i)->data(Name).toString() > curName){
                    row = i;
                }
            }
            QVariantList data;
            data << curName << sourceIdx.data(MappingTreeModel::MappingIdRole) << sourceIdx.data(MappingTreeModel::MappingParentIdRole) << sourceIdx.data(MappingTreeModel::DescIdRole);
            SmTreeItem *newItem = new SmTreeItem(data, pItem);
            pItem->insertChild(row, newItem);
            curIdx = createIndex(row, 0, newItem);
        }
    }
    endResetModel();
}

void MappingTreeResultModel::removeItem(const QModelIndex &idx){
    if(!idx.isValid()){
        return;
    }
    SmTreeItem *curItem = static_cast<SmTreeItem*>(idx.internalPointer());
    MappingData rmData = mSourceModel->mappingDataFromItem(curItem);
    beginResetModel();
    int row = curItem->row();
    SmTreeItem *parent = curItem->parent();
    parent->removeChild(row);
    int count = mCurrentData.removeAll(rmData);
    int toRemove = -1;
    if(count == 0){
        for(int i = 0; i < mCurrentData.count(); ++i){
            MappingData cur = mCurrentData.at(i);
            if(cur.parents.contains(rmData.mappingId)){
                toRemove = i;
                break;
            }
        }
    }
    if(toRemove > -1 && toRemove < mCurrentData.count()){
        mCurrentData.removeAt(toRemove);
    }
    endResetModel();
}

QList<QVariant> MappingTreeResultModel::getMappings(SmTreeItem *start) const{
    QList<QVariant> retval;
    for(int i = 0; i < start->childCount(); ++i){
        SmTreeItem *childItem = start->child(i);
        if(childItem->childCount()){
            retval.append(getMappings(childItem));
        }else{
            retval << childItem->data(MappingId);
        }
    }
    return retval;
}

QList<QVariant> MappingTreeResultModel::columnValues(int column) const {
    return columnValuesRecursive(root(), column);
}

void MappingTreeResultModel::clearData(){
    setRoot(new SmTreeItem(NumFields));
    mCurrentData.clear();
}

int MappingTreeResultModel::hasChild(SmTreeItem *item, const QVariant &name, int column) const{
    for(int i = 0; i < item->childCount(); ++i){
        if(item->child(i)->data(column) == name){
            return i;
        }
    }
    return -1;
}

QList<QVariant> MappingTreeResultModel::columnValuesRecursive(SmTreeItem *parent, int column) const {
    QList<QVariant> retval;
    if(!parent->childCount()){
        return retval;
    }
    for(int i = 0; i < parent->childCount(); ++i){
        SmTreeItem *child = parent->child(i);
        QVariant value = child->data(column);
        if(value.canConvert(QVariant::Int) && (value.toInt() != -1)){
            retval << value;
        }
        retval << columnValuesRecursive(child, column);
    }
    return retval;
}

MappingData::MappingData() : mappingId(-1), descId(-1) {}

bool MappingData::operator ==(const MappingData &other){
    bool retval = (
                    mappingId == other.mappingId &&
                    descId == other.descId &&
                    name == other.name &&
                    parents == other.parents &&
                    path == other.path
                );
    return retval;
}

QDataStream &operator <<(QDataStream &out, const MappingData &v){
    out << v.mappingId << v.descId << v.name << v.parents << v.path;
    return out;
}

QDataStream &operator >>(QDataStream &in, MappingData &v){
    in >> v.mappingId;
    in >> v.descId;
    in >> v.name;
    in >> v.parents;
    in >> v.path;
    return in;
}

bool MappingData::isValid(){
    return !(mappingId == -1 && descId == -1);
}