summaryrefslogtreecommitdiffstats
path: root/src/imports/jsondb/jsondbsortinglistmodel.cpp
blob: f24acb111eca86a7a9c6d42e8db4a4e00239ae4d (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
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
/****************************************************************************
**
** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/
**
** This file is part of the QtAddOn.JsonDb module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** GNU Lesser General Public License Usage
** This file may be used under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation and
** appearing in the file LICENSE.LGPL included in the packaging of this
** file. Please review the following information to ensure the GNU Lesser
** General Public License version 2.1 requirements will be met:
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU General
** Public License version 3.0 as published by the Free Software Foundation
** and appearing in the file LICENSE.GPL included in the packaging of this
** file. Please review the following information to ensure the GNU General
** Public License version 3.0 requirements will be met:
** http://www.gnu.org/copyleft/gpl.html.
**
** Other Usage
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "jsondbsortinglistmodel.h"
#include "jsondbsortinglistmodel_p.h"
#include "plugin.h"
#include "jsondatabase.h"
#include <qdebug.h>
/*!
  \internal
  \class JsonDbSortingListModel
*/

QT_BEGIN_NAMESPACE_JSONDB

JsonDbSortingListModelPrivate::JsonDbSortingListModelPrivate(JsonDbSortingListModel *q)
    : q_ptr(q)
    , componentComplete(false)
    , resetModel(true)
    , overflow(false)
    , limit(-1)
    , chunkSize(100)
    , state(JsonDbSortingListModel::None)
    , errorCode(0)
    , isCallable(false)
{
}

void JsonDbSortingListModelPrivate::init()
{
}

JsonDbSortingListModelPrivate::~JsonDbSortingListModelPrivate()
{
    // Why do we need to do this while destroying the object
    clearNotifications();
}

void JsonDbSortingListModelPrivate::removeLastItem()
{
    Q_Q(JsonDbSortingListModel);
    int index = objectUuids.count()-1;
    q->beginRemoveRows(parent, index, index);
    QMultiMap<SortingKey, QString>::iterator lastPos = objectUuids.begin() + index;
    const QString &uuid = (lastPos).value();
    objects.remove(uuid);
    objectSortValues.remove(uuid);
    objectUuids.erase(lastPos);
    q->endRemoveRows();
    emit q->rowCountChanged(objects.count());
}

// insert item notification handler
// + add items, for chunked read
void JsonDbSortingListModelPrivate::addItem(const QVariantMap &newItem, int partitionIndex)
{
    Q_Q(JsonDbSortingListModel);
    QVariantMap item = newItem;
    if (isCallable)
        generateCustomData(item);
    const QString &uuid = item.value(QLatin1String("_uuid")).toString();
    // ignore duplicates.
    if (objects.contains(uuid))
        return;
    SortingKey key(partitionIndex, item, ascendingOrders, orderPaths);
    QMap<SortingKey, QString>::const_iterator begin = objectUuids.constBegin();
    QMap<SortingKey, QString>::const_iterator end = objectUuids.constEnd();
    QMap<SortingKey, QString>::const_iterator i = objectUuids.upperBound(key);
    int index = iterator_position(begin, end, i);
    if (limit > 0  &&  objectUuids.count() == limit) {
        if (index < limit) {
            // when we hit the limit make space for a single item
            removeLastItem();
        } else {
            // beyond the last element, ignore the object.
            return;
        }
    }
    q->beginInsertRows(parent, index, index);
    objects.insert(uuid, item);
    objectUuids.insert(key, uuid);
    objectSortValues.insert(uuid, key);
    if (limit > 0  &&  objectUuids.count() == limit) {
        overflow = true;
    }
    q->endInsertRows();
    emit q->rowCountChanged(objects.count());
}

// deleteitem notification handler
void JsonDbSortingListModelPrivate::deleteItem(const QVariantMap &item, int partitionIndex)
{
    Q_UNUSED(partitionIndex);
    Q_Q(JsonDbSortingListModel);
    QString uuid = item.value(QLatin1String("_uuid")).toString();
    QMap<QString, SortingKey>::const_iterator keyIndex =  objectSortValues.constFind(uuid);
    if (keyIndex != objectSortValues.constEnd()) {
        SortingKey key = keyIndex.value();
        QMap<SortingKey, QString>::const_iterator begin = objectUuids.constBegin();
        QMap<SortingKey, QString>::const_iterator end = objectUuids.constEnd();
        QMap<SortingKey, QString>::const_iterator i = objectUuids.constFind(key);
        if (i != end) {
            int index = iterator_position(begin, end, i);
            q->beginRemoveRows(parent, index, index);
            objects.remove(uuid);
            objectUuids.remove(key);
            objectSortValues.remove(uuid);
            q->endRemoveRows();
            emit q->rowCountChanged(objects.count());
            if (overflow) {
                // when overflow is set, we need to re-run the query to fill the list
                //QMetaObject::invokeMethod(q, "_q_refreshModel", Qt::QueuedConnection);
                _q_refreshModel();

            }
        }
    }
}

// updateitem notification handler
void JsonDbSortingListModelPrivate::updateItem(const QVariantMap &changedItem, int partitionIndex)
{
    Q_Q(JsonDbSortingListModel);
    QVariantMap item = changedItem;
    if (isCallable)
        generateCustomData(item);
    QString uuid = item.value(QLatin1String("_uuid")).toString();
    QMap<QString, SortingKey>::const_iterator keyIndex =  objectSortValues.constFind(uuid);
    if (keyIndex != objectSortValues.constEnd()) {
        SortingKey key = keyIndex.value();
        SortingKey newKey(partitionIndex, item, ascendingOrders, orderPaths);
        QMap<SortingKey, QString>::const_iterator begin = objectUuids.constBegin();
        QMap<SortingKey, QString>::const_iterator end = objectUuids.constEnd();
        QMap<SortingKey, QString>::const_iterator oldPos = objectUuids.constFind(key);
        int oldIndex = iterator_position(begin, end, oldPos);
        // keys are same, modify the object
        if (key == newKey) {
            objects.remove(uuid);
            objects.insert(uuid, item);
            QModelIndex modelIndex = q->createIndex(oldIndex, 0);
            emit q->dataChanged(modelIndex, modelIndex);
            return;
        }
        // keys are different
        QMap<SortingKey, QString>::const_iterator newPos = objectUuids.upperBound(newKey);
        int newIndex = iterator_position(begin, end, newPos);
        if (overflow && oldIndex <= limit-1 && newIndex >= limit-1) {
            // new position is the last or beyond the limit
            // this will do a refresh
            deleteItem(item, partitionIndex);
            return;
        }
        if (newIndex != oldIndex && newIndex != oldIndex+1) {
            q->beginMoveRows(parent, oldIndex, oldIndex, parent, newIndex);
            objects.remove(uuid);
            objects.insert(uuid, item);
            objectUuids.remove(key);
            objectUuids.insert(newKey, uuid);
            objectSortValues.remove(uuid);
            objectSortValues.insert(uuid, newKey);
            q->endMoveRows();

        } else {
            // same position, update the object
            objects.remove(uuid);
            objects.insert(uuid, item);
            objectUuids.remove(key);
            objectUuids.insert(newKey, uuid);
            objectSortValues.remove(uuid);
            objectSortValues.insert(uuid, newKey);
        }
        newPos = objectUuids.constFind(newKey);
        begin = objectUuids.constBegin();
        end = objectUuids.constEnd();
        newIndex = iterator_position(begin, end, newPos);
        QModelIndex modelIndex = q->createIndex(newIndex, 0);
        emit q->dataChanged(modelIndex, modelIndex);
    } else {
        addItem(item, partitionIndex);
    }
}

void JsonDbSortingListModelPrivate::generateCustomData(QVariantMap &valMap)
{
    Q_Q(JsonDbSortingListModel);
    QJSValueList args;
    args << injectCallback.engine()->toScriptValue(valMap);
    QJSValue retVal = injectCallback.call(args);
    QVariantMap customData = qjsvalue_cast<QVariant>(retVal).toMap();
    QVariantMap::const_iterator it = customData.constBegin(), e = customData.constEnd();
    for (; it != e; ++it) {
        valMap.insert(it.key(), it.value());
    }
}

void JsonDbSortingListModelPrivate::generateCustomData(QVariantList &objects)
{
    if (!isCallable)
        return;
    int count = objects.count();
    for (int i = 0; i < count; i++) {
        QVariantMap val = objects[i].toMap();
        generateCustomData(val);
        objects[i] = val;
    }
}

void JsonDbSortingListModelPrivate::fillData(const QVariantList &newItems, int partitionIndex)
{
    Q_Q(JsonDbSortingListModel);
    QVariantList items = newItems;
    generateCustomData(items);
    RequestInfo &r = partitionObjectDetails[partitionIndex];
    r.lastSize = items.size();
    if (resetModel) {
        // for the first chunk, add all items
        q->beginResetModel();
        objects.clear();
        objectUuids.clear();
        objectSortValues.clear();
        for (int i = 0; i < r.lastSize; i++) {
            const QVariantMap &item = items.at(i).toMap();
            const QString &uuid = item.value(QLatin1String("_uuid")).toString();
            SortingKey key(partitionIndex, item, ascendingOrders, orderPaths);
            objects.insert(uuid, item);
            objectUuids.insertMulti(key, uuid);
            objectSortValues.insert(uuid, key);
        }
        if (limit > 0  && r.lastSize > limit) {
            // remove extra objects
            QMap<SortingKey, QString>::iterator start = objectUuids.begin();
            for (int i = limit; i < r.lastSize; i++) {
                const QString &uuid = (start+i).value();
                objects.remove(uuid);
                objectSortValues.remove(uuid);
            }
            // remove extra items from the sorted map
            start += limit;
            while (start != objectUuids.end()) {
                start = objectUuids.erase(start);
            }
       }
        q->endResetModel();
        emit q->rowCountChanged(objects.count());
        resetModel = false;
    } else {
        // subsequent chunks, insert items, preserving the limit.
        for (int i = 0; i < r.lastSize; i++) {
            addItem(items.at(i).toMap(), partitionIndex);
        }
    }

    // Check if requests from different partitions returned
    // all the results
    bool allRequestsFinished = true;
    for (int i = 0; i<partitionObjectDetails.count(); i++) {
        if (partitionObjectDetails[i].lastSize >= chunkSize || partitionObjectDetails[i].lastSize == -1) {
            allRequestsFinished = false;
            break;
        }
    }
    if (allRequestsFinished) {
        // retrieved all elements
        state = JsonDbSortingListModel::Ready;
        emit q->stateChanged(state);
        for (int i = 0; i<pendingNotifications.size(); i++) {
            const NotifyItem &pending = pendingNotifications[i];
            sendNotifications(pending.partitionIndex, pending.item, pending.action);
        }
        pendingNotifications.clear();
        // overflow status is used when handling notifications.
        if (limit > 0 && objects.count() >= limit)
            overflow = true;
        else
            overflow = false;
    } else if (r.lastSize >= chunkSize){
        // more items, fetch next chunk
        fetchPartition(partitionIndex, false);
    }
}

void JsonDbSortingListModelPrivate::sortObjects()
{
    Q_Q(JsonDbSortingListModel);
    if (!objects.count())
        return;
    emit q->layoutAboutToBeChanged();
    QMap<QString, SortingKey> currentObjectSortValues(objectSortValues);
    objectUuids.clear();
    objectSortValues.clear();
    QHashIterator<QString, QVariantMap> i(objects);
    while (i.hasNext()) {
        i.next();
        const QVariantMap &object = i.value();
        const QString &uuid = object.value("_uuid", QString()).toString();
        int partitionIndex = currentObjectSortValues[uuid].partitionIndex();
        SortingKey key(partitionIndex, object, ascendingOrders, orderPaths);
        objectUuids.insertMulti(key, uuid);
        objectSortValues.insert(uuid, key);
    }
    emit q->layoutChanged();
    if (overflow && state != JsonDbSortingListModel::Querying)
        _q_refreshModel();
}

//Clears all the state information.
void JsonDbSortingListModelPrivate::reset()
{
    Q_Q(JsonDbSortingListModel);
    q->beginResetModel();
    clearNotifications();
    objects.clear();
    objectUuids.clear();
    objectSortValues.clear();
    q->endResetModel();
    emit q->rowCountChanged(0);
    state = JsonDbSortingListModel::None;
    emit q->stateChanged(state);
 }

void JsonDbSortingListModelPrivate::fetchPartition(int index, bool reset)
{
    Q_Q(JsonDbSortingListModel);
    if (index >= partitionObjects.count())
        return;

    if (state != JsonDbSortingListModel::Querying) {
        state =  JsonDbSortingListModel::Querying;
        emit q->stateChanged(state);
    }
    RequestInfo &r = partitionObjectDetails[index];
    QPointer<JsonDbPartition> p = partitionObjects[index];
    Q_ASSERT(p);
    if (reset) {
        r.lastSize = -1;
        r.lastOffset = 0;
    } else {
        r.lastOffset += chunkSize;
    }
    QJsonDbReadRequest *request = valueRequests[index]->newRequest(index);
    request->setQuery(query);
    QVariantMap::ConstIterator i = queryBindings.constBegin();
    while (i != queryBindings.constEnd()) {
        request->bindValue(i.key(), QJsonValue::fromVariant(i.value()));
        ++i;
    }
    request->setProperty("queryOffset", r.lastOffset);
    request->setQueryLimit(chunkSize);
    request->setPartition(p->name());
    JsonDatabase::sharedConnection().send(request);
}

void JsonDbSortingListModelPrivate::fetchModel(bool reset)
{
    resetModel = reset;
    if (resetModel) {
        objects.clear();
        objectUuids.clear();
        objectSortValues.clear();
    }
    for (int i = 0; i<partitionObjects.count(); i++) {
        fetchPartition(i);
    }
}

void JsonDbSortingListModelPrivate::clearNotification(int index)
{
    if (index >= partitionObjects.count())
        return;

    RequestInfo &r = partitionObjectDetails[index];
    if (r.watcher) {
        JsonDatabase::sharedConnection().removeWatcher(r.watcher);
    }
    r.clear();
}

void JsonDbSortingListModelPrivate::clearNotifications()
{
    for (int i = 0; i<partitionObjects.count(); i++)
        clearNotification(i);
}

void JsonDbSortingListModelPrivate::createOrUpdateNotification(int index)
{
    Q_Q(JsonDbSortingListModel);
    if (index >= partitionObjects.count())
        return;
    clearNotification(index);
    QJsonDbWatcher *watcher = new QJsonDbWatcher();
    watcher->setQuery(query);
    watcher->setWatchedActions(QJsonDbWatcher::Created | QJsonDbWatcher::Updated |QJsonDbWatcher::Removed);
    watcher->setPartition(partitionObjects[index]->name());
    QVariantMap::ConstIterator i = queryBindings.constBegin();
    while (i != queryBindings.constEnd()) {
        watcher->bindValue(i.key(), QJsonValue::fromVariant(i.value()));
        ++i;
    }
    QObject::connect(watcher, SIGNAL(notificationsAvailable(int)),
                     q, SLOT(_q_notificationsAvailable()));
    QObject::connect(watcher, SIGNAL(error(QtJsonDb::QJsonDbWatcher::ErrorCode,QString)),
                     q, SLOT(_q_notificationError(QtJsonDb::QJsonDbWatcher::ErrorCode,QString)));
    JsonDatabase::sharedConnection().addWatcher(watcher);
    partitionObjectDetails[index].watcher = watcher;
}

void JsonDbSortingListModelPrivate::createOrUpdateNotifications()
{
    for (int i = 0; i<partitionObjects.count(); i++) {
        createOrUpdateNotification(i);
    }
}

void JsonDbSortingListModelPrivate::parseSortOrder()
{
    QRegExp orderMatch("\\[([/\\\\[\\]])[ ]*([^\\[\\]]+)[ ]*\\]");
    ascendingOrders.clear();
    orderProperties.clear();
    orderPaths.clear();
    int matchIndex = 0, firstMatch = -1;
    while ((matchIndex = orderMatch.indexIn(sortOrder, matchIndex)) >= 0) {
        bool ascendingOrder = false;
        if (!orderMatch.cap(1).compare(QLatin1String("/")))
            ascendingOrder = true;
        ascendingOrders << ascendingOrder;
        orderProperties << orderMatch.cap(2);
        orderPaths << orderMatch.cap(2).split('.');
        if (firstMatch == -1)
            firstMatch = matchIndex;
        matchIndex += orderMatch.matchedLength();
    }
}

int JsonDbSortingListModelPrivate::indexOfWatcher(QJsonDbWatcher *watcher)
{
    for (int i = 0; i < partitionObjectDetails.count(); i++) {
        if (watcher == partitionObjectDetails[i].watcher)
            return i;
    }
    return -1;
}

QVariant JsonDbSortingListModelPrivate::getItem(int index)
{
    if (index < 0 || index >= objects.size())
        return QVariant();
    QMap<SortingKey, QString>::const_iterator begin = objectUuids.constBegin();
    return objects.value((begin+index).value());
}

QVariant JsonDbSortingListModelPrivate::getItem(int index, int role)
{
    if (index < 0 || index >= objects.size())
        return QVariant();
    QMap<SortingKey, QString>::const_iterator begin = objectUuids.constBegin();
    return lookupProperty(objects.value((begin+index).value()), properties[role]);
}

JsonDbPartition* JsonDbSortingListModelPrivate::getItemPartition(int index)
{
    if (index < 0 || index >= objects.size())
        return 0;
    QMap<SortingKey, QString>::const_iterator begin = objectUuids.constBegin();
    int partitionIndex = (begin+index).key().partitionIndex();
    if (partitionIndex <= partitionObjects.count())
        return partitionObjects[partitionIndex];
    return 0;
}

int JsonDbSortingListModelPrivate::indexOf(const QString &uuid) const
{
    if (!objects.contains(uuid))
        return -1;
    const SortingKey &key = objectSortValues.value(uuid);
    QMap<SortingKey, QString>::const_iterator begin = objectUuids.constBegin();
    QMap<SortingKey, QString>::const_iterator end = objectUuids.constEnd();
    QMap<SortingKey, QString>::const_iterator i = objectUuids.find(key);
    return iterator_position(begin, end, i);
}

void JsonDbSortingListModelPrivate::sendNotifications(int partitionIndex, const QVariantMap &v, QJsonDbWatcher::Action action)
{
    if (action == QJsonDbWatcher::Created) {
        addItem(v, partitionIndex);
    } else if (action == QJsonDbWatcher::Removed) {
        deleteItem(v, partitionIndex);
    } else if (action == QJsonDbWatcher::Updated) {
        updateItem(v, partitionIndex);
    }
}

void JsonDbSortingListModelPrivate::_q_valueResponse(int index, const QList<QJsonObject> &v)
{
    fillData(qjsonobject_list_to_qvariantlist(v), index);
}

void JsonDbSortingListModelPrivate::_q_readError(QtJsonDb::QJsonDbRequest::ErrorCode code, const QString & message)
{
    Q_Q(JsonDbSortingListModel);
    qWarning() << QString("JsonDb error: %1 %2").arg(code).arg(message);
    int oldErrorCode = errorCode;
    errorCode = code;
    errorString = message;
    if (oldErrorCode != errorCode)
        emit q->errorChanged(q->error());
}

void JsonDbSortingListModelPrivate::_q_refreshModel()
{
    // ignore active requests.
    fetchModel(false);
}

void JsonDbSortingListModelPrivate::_q_notificationsAvailable()
{
    Q_Q(JsonDbSortingListModel);
    QJsonDbWatcher *watcher = qobject_cast<QJsonDbWatcher *>(q->sender());
    int partitionIndex = indexOfWatcher(watcher);
    if (!watcher || partitionIndex == -1)
        return;
    QList<QJsonDbNotification> list = watcher->takeNotifications();
    for (int i = 0; i < list.count(); i++) {
        const QJsonDbNotification & notification = list[i];
        QVariantMap object = notification.object().toVariantMap();
        if (state == JsonDbSortingListModel::Querying) {
            NotifyItem  pending;
            pending.partitionIndex = partitionIndex;
            pending.item = object;
            pending.action = notification.action();
            pendingNotifications.append(pending);
        } else if (state == JsonDbSortingListModel::Ready) {
            sendNotifications(partitionIndex, object, notification.action());
        }
    }
}

void JsonDbSortingListModelPrivate::_q_notificationError(QtJsonDb::QJsonDbWatcher::ErrorCode code, const QString &message)
{
    Q_Q(JsonDbSortingListModel);
    qWarning() << QString("JsonDbSortingListModel Notification error: %1 %2").arg(code).arg(message);
    int oldErrorCode = errorCode;
    errorCode = code;
    errorString = message;
    if (oldErrorCode != errorCode)
        emit q->errorChanged(q->error());
}

void JsonDbSortingListModelPrivate::appendPartition(JsonDbPartition *v)
{
    Q_Q(JsonDbSortingListModel);
    partitionObjects.append(QPointer<JsonDbPartition>(v));
    partitionObjectDetails.append(RequestInfo());
    ModelRequest *valueRequest = new ModelRequest();
    QObject::connect(valueRequest, SIGNAL(finished(int,QList<QJsonObject>,QString)),
                     q, SLOT(_q_valueResponse(int,QList<QJsonObject>)));
    QObject::connect(valueRequest, SIGNAL(error(QtJsonDb::QJsonDbRequest::ErrorCode,QString)),
                     q, SLOT(_q_readError(QtJsonDb::QJsonDbRequest::ErrorCode,QString)));
    valueRequests.append(valueRequest);
    if (componentComplete && !query.isEmpty()) {
        createOrUpdateNotification(partitionObjects.count()-1);
        if (state == JsonDbSortingListModel::None) {
            resetModel = true;
        }
        fetchPartition(partitionObjects.count()-1);
    }
}

void JsonDbSortingListModelPrivate::partitions_append(QQmlListProperty<JsonDbPartition> *p, JsonDbPartition *v)
{
    JsonDbSortingListModel *q = qobject_cast<JsonDbSortingListModel *>(p->object);
    JsonDbSortingListModelPrivate *pThis = (q) ? q->d_func() : 0;
    if (!v) {
        qWarning("Invalid partition object");
        return;
    }

    if (pThis) {
        pThis->appendPartition(v);
    }
}

int JsonDbSortingListModelPrivate::partitions_count(QQmlListProperty<JsonDbPartition> *p)
{
    JsonDbSortingListModel *q = qobject_cast<JsonDbSortingListModel *>(p->object);
    JsonDbSortingListModelPrivate *pThis = (q) ? q->d_func() : 0;
    if (pThis) {
        return pThis->partitionObjects.count();
    }
    return 0;
}

JsonDbPartition* JsonDbSortingListModelPrivate::partitions_at(QQmlListProperty<JsonDbPartition> *p, int idx)
{
    JsonDbSortingListModel *q = qobject_cast<JsonDbSortingListModel *>(p->object);
    JsonDbSortingListModelPrivate *pThis = (q) ? q->d_func() : 0;
    if (pThis && idx < pThis->partitionObjects.count()) {
        return pThis->partitionObjects.at(idx);
    }
    return 0;
}

void JsonDbSortingListModelPrivate::clearPartitions()
{
    partitionObjects.clear();
    partitionObjectDetails.clear();
    while (!valueRequests.isEmpty()) {
        delete valueRequests[0];
        valueRequests.removeFirst();
    }
    reset();
}

void JsonDbSortingListModelPrivate::partitions_clear(QQmlListProperty<JsonDbPartition> *p)
{
    JsonDbSortingListModel *q = qobject_cast<JsonDbSortingListModel *>(p->object);
    JsonDbSortingListModelPrivate *pThis = (q) ? q->d_func() : 0;
    if (pThis) {
        pThis->clearPartitions();
    }
}

/*!
    \qmltype JsonDbSortingListModel
    \instantiates JsonDbSortingListModel
    \inqmlmodule QtJsonDb 1.0
    \inherits ListModel
    \since 1.0

    The JsonDbSortingListModel provides a read-only ListModel usable with views such as
    ListView or GridView displaying data items matching a query. The sorting is done by
    the model after retrieving the whole result set. Maximum number of items in the model
    can be set by queryLimit. Whan a limit is set and the query result has more items,
    only 'queryLimit' number of items will be saved in the model.

    The model is initalised by retrieving the results in chunk. After receving the first
    chunk, the model is reset with items from it. For subsequent chunks of data, the items
    might get removed or inserted. The state will be "Querying" during initialization and will
    be changed to "Ready".

    \code
    import QtJsonDb 1.0 as JsonDb

    JsonDb.JsonDbSortingListModel {
        id: contactsModel
        query: '[?_type="Contact"]'
        queryLimit: 100
        partitions: [JsonDb.Partition {
            name:"com.nokia.shared"
        }]
        sortOrder: "[/firstName]"
        roleNames: ["firstName", "lastName", "phoneNumber"]
    }
    ListView {
        model: contactsModel
        Row {
            spacing: 10
            Text {
                text: firstName + " " + lastName
            }
            Text {
                text: phoneNumber
            }
        }
    }
    \endcode
*/

JsonDbSortingListModel::JsonDbSortingListModel(QObject *parent)
    : QAbstractListModel(parent)
    , d_ptr(new JsonDbSortingListModelPrivate(this))
{
    Q_D(JsonDbSortingListModel);
    d->init();
}

JsonDbSortingListModel::~JsonDbSortingListModel()
{
}

void JsonDbSortingListModel::classBegin()
{
}

void JsonDbSortingListModel::componentComplete()
{
    Q_D(JsonDbSortingListModel);
    d->componentComplete = true;
    if (!d->query.isEmpty() && d->partitionObjects.count()) {
        d->createOrUpdateNotifications();
        d->fetchModel();
    }
}

/*!
    \qmlproperty int QtJsonDb1::JsonDbSortingListModel::rowCount
    The number of items in the model.
*/
int JsonDbSortingListModel::rowCount(const QModelIndex &parent) const
{
    Q_UNUSED(parent);
    Q_D(const JsonDbSortingListModel);
    return d->objects.count();
}

QVariant JsonDbSortingListModel::data(const QModelIndex &modelIndex, int role) const
{
    JsonDbSortingListModel *pThis = const_cast<JsonDbSortingListModel *>(this);
    return pThis->d_func()->getItem(modelIndex.row(), role);
}

QHash<int, QByteArray> JsonDbSortingListModel::roleNames() const
{
    Q_D(const JsonDbSortingListModel);
    return d->roleNames;
}

/*!
    \qmlproperty ListOrObject QtJsonDb1::JsonDbSortingListModel::roleNames

    Controls which properties to expose from the objects matching the query.

    Setting \a roleNames to a list of strings causes the model to expose
    corresponding object values as roles to the delegate for each item viewed.

    \code
    JsonDb.JsonDbSortingListModel {
        id: listModel
        query: "[?_type=\"MyType\"]"
        partitions:[ JsonDb.Partition {
            name:"com.nokia.shared"
        }]
        roleNames: ['a', 'b']
    }
    ListView {
        model: listModel
        Text {
            text: a + ":" + b
        }
    \endcode

    Setting \a roleNames to a dictionary remaps properties in the object
    to the specified roles in the model.

    In the following example, role \a a would yield the value of
    property \a aLongName in the objects. Role \a liftedProperty would
    yield the value of \a o.nested.property for each matching object \a
    o in the database.

    \code
    function makeRoleNames() {
        return { 'a': 'aLongName', 'liftedProperty': 'nested.property' };
    }
    JsonDb.JsonDbSortingListModel {
        id: listModel
        query: "[?_type=\"MyType\"]"
        partitions: [JsonDb.Partition {
            name:"com.nokia.shared"
        }]
        roleNames: makeRoleNames()
    }
    ListView {
        model: listModel
        Text {
            text: a + " " + liftedProperty
        }
    }
    \endcode
*/

QVariant JsonDbSortingListModel::scriptableRoleNames() const
{
    Q_D(const JsonDbSortingListModel);
    return d->roleMap;
}

void JsonDbSortingListModel::setScriptableRoleNames(const QVariant &vroles)
{
    Q_D(JsonDbSortingListModel);
    d->properties.clear();
    d->roleNames.clear();
    if (vroles.type() == QVariant::Map) {
        QVariantMap roles = vroles.toMap();
        d->roleMap = roles;
        int i = 0;
        for (QVariantMap::const_iterator it = roles.begin(); it != roles.end(); ++it) {
            d->roleNames.insert(i, it.key().toLatin1());
            d->properties.insert(i, removeArrayOperator(it.value().toString()).split('.'));
            i++;
        }
    } else {
        QVariantList roleList = vroles.toList();
        d->roleMap.clear();
        for (int i = 0; i < roleList.size(); i++) {
            QString role = roleList[i].toString();
            d->roleMap[role] = role;
            d->roleNames.insert(i, role.toLatin1());
            d->properties.insert(i, removeArrayOperator(role).split('.'));
        }
    }
}

/*!
    \qmlproperty string QtJsonDb1::JsonDbSortingListModel::query

    The query string in JsonQuery format used by the model to fetch
    items from the database. Setting an empty query clears all the elements

    In the following example, the JsonDbSortingListModel would contain all
    the objects with \a _type contains the value \a "CONTACT" from partition
    called "com.nokia.shared"

    \qml
    JsonDb.JsonDbSortingListModel {
        id: listModel
        query: "[?_type=\"CONTACT\"]"
        partitions:[ JsonDb.Partition {
            name:"com.nokia.shared"
        }]
    }
    \endqml

    \sa QtJsonDb1::JsonDbSortingListModel::bindings

*/
QString JsonDbSortingListModel::query() const
{
    Q_D(const JsonDbSortingListModel);
    return d->query;
}

void JsonDbSortingListModel::setQuery(const QString &newQuery)
{
    Q_D(JsonDbSortingListModel);

    const QString oldQuery = d->query;
    if (oldQuery == newQuery)
        return;

    d->query = newQuery;
    if (rowCount() && d->query.isEmpty()) {
        d->reset();
    }

    if (!d->componentComplete || d->query.isEmpty() || !d->partitionObjects.count())
        return;

    d->createOrUpdateNotifications();
    d->fetchModel();
}

/*!
    \qmlproperty object QtJsonDb1::JsonDbSortingListModel::bindings
    Holds the bindings for the placeholders used in the query string. Note that
    the placeholder marker '%' should not be included as part of the keys.

    \qml
    JsonDb.JsonDbSortingListModel {
        query: '[?_type="Contact"][?name=%firstName]'
        bindings :{'firstName':'Book'}
        partitions:[ JsonDb.Partition {
            name:"com.nokia.shared"
        }]
    }
    \endqml

    \sa QtJsonDb1::JsonDbSortingListModel::query

*/

QVariantMap JsonDbSortingListModel::bindings() const
{
    Q_D(const JsonDbSortingListModel);
    return d->queryBindings;
}

void JsonDbSortingListModel::setBindings(const QVariantMap &newBindings)
{
    Q_D(JsonDbSortingListModel);
    d->queryBindings = newBindings;

    if (!d->componentComplete || d->query.isEmpty() || !d->partitionObjects.count())
        return;
    d->createOrUpdateNotifications();
    d->fetchModel();
}

/*!
    \qmlproperty int QtJsonDb1::JsonDbSortingListModel::queryLimit
    Holds the maximum no of items for the model.

    \code
    JsonDb.JsonDbSortingListModel {
        id: listModel
        query: "[?_type=\"CONTACT\"]"
        queryLimit: 100
        partitions: [JsonDb.Partition {
            name:"com.nokia.shared"
        }]
    }
    \endcode

*/

int JsonDbSortingListModel::queryLimit() const
{
    Q_D(const JsonDbSortingListModel);
    return d->limit;
}

void JsonDbSortingListModel::setQueryLimit(int newQueryLimit)
{
    Q_D(JsonDbSortingListModel);
    if (newQueryLimit == d->limit)
        return;

    d->limit = newQueryLimit;
    if (!d->componentComplete || d->query.isEmpty() || !d->partitionObjects.count())
        return;

    d->fetchModel();
}

/*!
    \qmlproperty bool QtJsonDb1::JsonDbSortingListModel::overflow
    \readonly
    This will be true if actual numer of results is more than the queryLimit
*/

bool JsonDbSortingListModel::overflow() const
{
    Q_D(const JsonDbSortingListModel);
    return d->overflow;
}

void JsonDbSortingListModel::partitionNameChanged(const QString &partitionName)
{
    Q_UNUSED(partitionName);
    Q_D(JsonDbSortingListModel);

    if (!d->componentComplete || d->query.isEmpty() || !d->partitionObjects.count())
        return;

    d->createOrUpdateNotifications();
    d->fetchModel();
}

/*!
    \qmlproperty list QtJsonDb1::JsonDbSortingListModel::partitions
    Holds the list of partition objects for the model.
    \code
    JsonDb.JsonDbSortingListModel {
        id: contacts
        query: '[?_type="Contact"]'
        partitions :[nokiaPartition, nokiaPartition2]
        roleNames: ["firstName", "lastName", "_uuid", "_version"]
        sortOrder:"[/firstName]"
    }

    \endcode
*/


QQmlListProperty<JsonDbPartition> JsonDbSortingListModel::partitions()
{
    return QQmlListProperty<JsonDbPartition>(this, 0
                                                     , &JsonDbSortingListModelPrivate::partitions_append
                                                     , &JsonDbSortingListModelPrivate::partitions_count
                                                     , &JsonDbSortingListModelPrivate::partitions_at
                                                     , &JsonDbSortingListModelPrivate::partitions_clear);
}

/*!
    \qmlproperty string QtJsonDb1::JsonDbSortingListModel::sortOrder

    The order used by the model to sort the items. The sortOrder has to be
    specified in the JsonQuery format. The sorting is done by the model and not
    by the database. This makes it possible to support multiple sortkeys.
    When a qyeryLimit is set and the query result contains more items than the
    the limit (an overflow), the model will discard the rest of the items. The
    items are discarded based on their sorted position.

    If the queryLimit is set, changing a sortkey can trigger a refetch of the
    model in case of overflows.

    In the following example, the JsonDbSortingListModel would contain all
    the objects of type \a "CONTACT" sorted by their \a firstName field

    \qml
    JsonDb.JsonDbSortingListModel {
        id: listModel
        query: "[?_type=\"CONTACT\"]"
        partitions: [ JsonDb.Partition {
            name:"com.nokia.shared"
        }]
        sortOrder: "[/firstName]"
    }
    \endqml

*/

QString JsonDbSortingListModel::sortOrder() const
{
    Q_D(const JsonDbSortingListModel);
    return d->sortOrder;
}

void JsonDbSortingListModel::setSortOrder(const QString &newSortOrder)
{
    Q_D(JsonDbSortingListModel);

    const QString oldSortOrder = d->sortOrder;
    d->sortOrder = newSortOrder;
    if (oldSortOrder != newSortOrder) {
        d->parseSortOrder();
        d->sortObjects();
    }
}

/*!
    \qmlproperty State QtJsonDb1::JsonDbSortingListModel::state
    \readonly
    The current state of the model.
    \list
    \li State.None - The model is not initialized
    \li State.Querying - It is querying the results from server
    \li State.Ready - Results are ready
    \endlist
*/

JsonDbSortingListModel::State JsonDbSortingListModel::state() const
{
    Q_D(const JsonDbSortingListModel);
    return d->state;
}

/*!
    \qmlmethod int QtJsonDb1::JsonDbSortingListModel::indexOf(string uuid)

    Returns the index of the object with the \a uuid in the model. If the object is
    not found it returns -1
*/
int JsonDbSortingListModel::indexOf(const QString &uuid) const
{
    Q_D(const JsonDbSortingListModel);
    return d->indexOf(uuid);
}

/*!
    \qmlmethod object QtJsonDb1::JsonDbSortingListModel::get(int index)

    Returns the object at the specified \a index in the model. The result.object property
    contains the object in its raw form as returned by the query, the rolenames
    are not applied. The object.partition is the partition for the returned.
    If the index is out of range it returns an object with empty partition & object properties.
    \code
        onClicked: {
            var item = contacts.get(listView.currentIndex);
            item.object.firstName = item.object.firstName+ "*";
            item.partition.update(item.object, updateCallback);
        }

    \endcode
*/

QJSValue JsonDbSortingListModel::get(int index) const
{
    JsonDbSortingListModel *pThis = const_cast<JsonDbSortingListModel *>(this);
    QVariant object = pThis->d_func()->getItem(index);
    JsonDbPartition*partition = pThis->d_func()->getItemPartition(index);
    QJSValue result = g_declEngine->newObject();
    result.setProperty(QLatin1String("object"), g_declEngine->toScriptValue(object));
    result.setProperty(QLatin1String("partition"), g_declEngine->newQObject(partition));
    return result;
}

/*!
    \qmlmethod object QtJsonDb1::JsonDbSortingListModel::get(int index, string property)

    Retrieves the value of the \a property for the object at \a index. If the index
    is out of range or the property name is not valid it returns an empty object.
*/

QVariant JsonDbSortingListModel::get(int index, const QString &property) const
{
    Q_D(const JsonDbSortingListModel);
    return data(createIndex(index, 0),  d->roleNames.key(property.toLatin1(), -1));
}

/*!
    \qmlmethod object QtJsonDb1::JsonDbSortingListModel::getPartition(int index)

    Returns the partition object at the specified \a index in the model. If
    the index is out of range it returns an empty object.
*/

JsonDbPartition* JsonDbSortingListModel::getPartition(int index) const
{
    JsonDbSortingListModel *pThis = const_cast<JsonDbSortingListModel *>(this);
    return pThis->d_func()->getItemPartition(index);
}
/*!
    \qmlsignal QtJsonDb1::JsonDbSortingListModel::onStateChanged(State state)

    This handler is called when the a model \a state is changed.
*/

/*!
    \qmlsignal QtJsonDb1::JsonDbSortingListModel::onRowCountChanged(int newRowCount)

    This handler is called when the number of items in the model has changed.
*/

/*!
    \qmlproperty object QtJsonDb1::JsonDbSortingListModel::error
    \readonly

    This property holds the current error information for the object. It contains:
    \list
    \li error.code -  code for the current error.
    \li error.message - detailed explanation of the error
    \endlist
*/

QVariantMap JsonDbSortingListModel::error() const
{
    Q_D(const JsonDbSortingListModel);
    QVariantMap errorMap;
    errorMap.insert(QLatin1String("code"), d->errorCode);
    errorMap.insert(QLatin1String("message"), d->errorString);
    return errorMap;
}

QJSValue JsonDbSortingListModel::propertyInjector() const
{
    Q_D(const JsonDbSortingListModel);
    return d->injectCallback;

}

void JsonDbSortingListModel::setPropertyInjector(const QJSValue &callback)
{
    Q_D(JsonDbSortingListModel);
    d->injectCallback = callback;
    d->isCallable = callback.isCallable();
    refreshItems();
}

void JsonDbSortingListModel::refreshItems()
{
    Q_D(JsonDbSortingListModel);
    if (d->objectUuids.count()) {
        d->fetchModel();
    }
}
#include "moc_jsondbsortinglistmodel.cpp"
QT_END_NAMESPACE_JSONDB