summaryrefslogtreecommitdiff
path: root/src/plugins/geoservices/nokia_places_jsondb/qplacemanagerengine_jsondb.cpp
blob: 0eb65eeb530bbe87df6566761c548ec2367d4641 (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
/****************************************************************************
**
** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the QtLocation 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 "qplacemanagerengine_jsondb.h"

#include <QDebug>
#include <QtCore/qnumeric.h>
#include <qgeoboundingcircle.h>
#include <jsondb-error.h>

#include "detailsreply.h"
#include "reply.h"
#include "idreply.h"
#include "searchreply.h"
#include "unsupportedreplies.h"


//Json db response key constants
#define LENGTH QLatin1String("length")
#define DATA QLatin1String("data")

QT_USE_NAMESPACE

Q_DECLARE_METATYPE(QPlaceReply::Error)
Q_DECLARE_METATYPE(QPlaceReply *)

QPlaceManagerEngineJsonDb::QPlaceManagerEngineJsonDb(const QMap<QString, QVariant> &parameters,
                                                     QGeoServiceProvider::Error *error,
                                                     QString *errorString)
:   QPlaceManagerEngine(parameters),
    m_jsonDbHandler(this)

{
    qRegisterMetaType<QPlaceReply::Error>();
    qRegisterMetaType<QPlaceReply *>();
    qRegisterMetaType<QPlaceCategory>();

    connect(&m_jsonDbHandler, SIGNAL(jsonDbResponse(int,QVariant)),
            this, SLOT(processJsonDbResponse(int,QVariant)));
    connect(&m_jsonDbHandler, SIGNAL(jsonDbError(int,int,QString)),
            this, SLOT(processJsonDbError(int,int,QString)));
    *error = QGeoServiceProvider::NoError;
    errorString->clear();

}

QPlaceManagerEngineJsonDb::~QPlaceManagerEngineJsonDb()
{
}

QPlaceDetailsReply *QPlaceManagerEngineJsonDb::getPlaceDetails(const QString &placeId)
{
    DetailsReply *detailsReply = new DetailsReply(this);
    int reqId = m_jsonDbHandler.queryByUuid(placeId);
    m_idReplyMap.insert(reqId, detailsReply);
    return detailsReply;
}

QPlaceContentReply *QPlaceManagerEngineJsonDb::getContent(const QPlace &place, const QPlaceContentRequest &request)
{
    ContentReply *contentReply = new ContentReply(this);
    contentReply->triggerDone(QPlaceReply::UnsupportedError, tr("Fetching content is unsupported"));
    return contentReply;
}

QPlaceSearchReply *QPlaceManagerEngineJsonDb::search(const QPlaceSearchRequest &request)
{
    SearchReply *searchReply = new SearchReply(this);
    searchReply->setRequest(request);

    if (!m_jsonDbHandler.isConnected()) {
        searchReply->triggerDone(QPlaceReply::CommunicationError, "No connection to jsondb database");
        return searchReply;
    }

    if (request.searchArea() != 0) {
        QGeoBoundingArea *searchArea = request.searchArea();
        if (searchArea->type() == QGeoBoundingArea::BoxType) {
            QGeoBoundingBox *box = static_cast<QGeoBoundingBox *>(searchArea);
            if (!box->isValid()) {
                searchReply->triggerDone(QPlaceReply::BadArgumentError,
                                tr("Bounding box search area is invalid"));
                return searchReply;
            }
        } else if (searchArea->type() == QGeoBoundingArea::CircleType) {
            QGeoBoundingCircle *circle = static_cast<QGeoBoundingCircle *>(searchArea);
            if (!circle->center().isValid() || qIsNaN(circle->center().latitude()) || qIsNaN(circle->center().longitude())) {
                searchReply->triggerDone(QPlaceReply::BadArgumentError,
                                         tr("The center of the search area is an invalid coordinate"));
                return searchReply;
            }
            if (circle->contains(QGeoCoordinate(90,0)) || circle->contains(QGeoCoordinate(-90,0))) {
                searchReply->triggerDone(QPlaceReply::BadArgumentError,
                                         tr("The search area contains the north or south pole"));
                return searchReply;
            }
        }
    }

    QString searchQuery = JsonDbHandler::convertToQueryString(request);
    int reqId = m_jsonDbHandler.query(searchQuery);
    m_idReplyMap.insert(reqId, searchReply);
    return searchReply;
}

QPlaceSearchReply *QPlaceManagerEngineJsonDb::recommendations(const QPlace &place, const QPlaceSearchRequest &request)
{
    SearchReply *searchReply = new SearchReply(this);
    searchReply->triggerDone(QPlaceReply::UnsupportedError, tr("Recommendations is unsupported"));
    return searchReply;
}

QPlaceTextPredictionReply *QPlaceManagerEngineJsonDb::textPredictions(const QPlaceSearchRequest &request)
{
    TextPredictionReply *textPredictionReply = new TextPredictionReply(this);
    textPredictionReply->triggerDone(QPlaceReply::UnsupportedError, tr("Text prediction is unsupported"));
    return textPredictionReply;
}

QPlaceIdReply *QPlaceManagerEngineJsonDb::savePlace(const QPlace &place)
{
    IdReply *saveReply = new IdReply(QPlaceIdReply::SavePlace, this);
    if (!m_jsonDbHandler.isConnected()) {
        saveReply->triggerDone(QPlaceReply::CommunicationError, "No connection to jsondb database");
        return saveReply;
    }

    if (place.visibility() & QtLocation::PublicVisibility) {
        saveReply->triggerDone(QPlaceReply::UnsupportedError, tr("Saving to public scope in unsupported"));
    } else {
        QVariant jsonPlace = JsonDbHandler::convertToJsonVariant(place);
        int reqId;
        if (place.placeId().isEmpty())
            reqId = m_jsonDbHandler.write(jsonPlace);
        else
            reqId = m_jsonDbHandler.update(jsonPlace);

        m_idReplyMap.insert(reqId, saveReply);
    }
    return saveReply;
}

QPlaceIdReply *QPlaceManagerEngineJsonDb::saveCategory(const QPlaceCategory &category, const QString &parentId)
{
    IdReply *saveReply = new IdReply(QPlaceIdReply:: SaveCategory ,this);
    if (!m_jsonDbHandler.isConnected()) {
        saveReply->triggerDone(QPlaceReply::CommunicationError, "No connection to jsondb database");
        return saveReply;
    }

    QVariantMap categoryMap;
    QVariantMap parentMap;

    bool isUpdate = false;
    bool isSameParent = false;

    int reqId;

    categoryMap = m_jsonDbHandler.findCategoryJson(category.categoryId());
    if (!categoryMap.isEmpty()) {
        isUpdate = true;
        //check if the existing parent is different from the new one
        //if it is, then resave the old parent without the category as a child
        parentMap = m_jsonDbHandler.findParentCategoryJson(category.categoryId());
        if (!parentMap.isEmpty()) {
            if (parentMap.value(UUID) != parentId) {
                QList<QVariant> siblingUuids = parentMap.value(CHILDREN_UUIDS).toList();
                siblingUuids.removeAll(QVariant(category.categoryId()));
                parentMap.insert(CHILDREN_UUIDS, siblingUuids);
                reqId = m_jsonDbHandler.update(parentMap);
                m_jsonDbHandler.waitForRequest(reqId);
            } else {
                isSameParent = true;
            }
        }
    }

    //check if the parent category exists
    bool isTopLevel = parentId.isEmpty();
    if (!isTopLevel) {
        if (!isSameParent) {
            parentMap = m_jsonDbHandler.findCategoryJson(parentId);
            if (parentMap.isEmpty()) {
                saveReply->triggerDone(QPlaceReply::CategoryDoesNotExistError, tr("Parent category does not exist"));
                return saveReply;
            }
        }
    } else {
        parentMap.clear();
    }

    if (!isUpdate) {
        reqId = m_jsonDbHandler.write(JsonDbHandler::convertToJsonVariant(category, isTopLevel));
    } else {
        reqId = m_jsonDbHandler.update(JsonDbHandler::convertToJsonVariant(category, isTopLevel));
        saveReply->setIsUpdate();
    }

    QVariantMap responseMap = m_jsonDbHandler.waitForRequest(reqId);
    if (!responseMap.contains(UUID)) {
        saveReply->triggerDone(QPlaceReply::UnknownError, tr("Could not save category"));
        return saveReply;
    }

    QString categoryUuid = responseMap.value(UUID).toString();
    saveReply->setId(categoryUuid);

    if (!isTopLevel) {
        //if the specified parent is a new parent, save the specified parent with it's new child
        if (!isSameParent) {
            QList<QVariant> childrenUuids = parentMap.value(CHILDREN_UUIDS).toList();
            childrenUuids.append(categoryUuid);
            parentMap.insert(CHILDREN_UUIDS, childrenUuids);
            reqId = m_jsonDbHandler.update(parentMap);
            m_idReplyMap.insert(reqId, saveReply);
            return saveReply;
        }
    }

    //TODO: this variable is only needed for for the workaround for emitting signals
    // when jsondb notifications are introduced this should be removed.
    QPlaceCategory category_ = m_jsonDbHandler.findCategory(saveReply->id());

    saveReply->setId(categoryUuid);
    saveReply->triggerDone();

    if (!saveReply->isUpdate())
        emit categoryAdded(category_, parentMap.value(UUID).toString());
    else
        emit categoryUpdated(category_, parentMap.value(UUID).toString());

    return saveReply;
}

QPlaceIdReply *QPlaceManagerEngineJsonDb::removePlace(const QPlace &place)
{
    IdReply *removeReply = new IdReply(QPlaceIdReply::RemovePlace, this);

    if (!m_jsonDbHandler.isConnected()) {
        removeReply->triggerDone(QPlaceReply::CommunicationError, "No connection to jsondb database");
        return removeReply;
    }

    int reqId = m_jsonDbHandler.remove(place.placeId());
    removeReply->setId(place.placeId());
    m_idReplyMap.insert(reqId, removeReply);
    return removeReply;
}

QPlaceIdReply *QPlaceManagerEngineJsonDb::removeCategory(const QString &categoryId)
{
    IdReply *removeReply = new IdReply(QPlaceIdReply::RemoveCategory, this);
    removeReply->setId(categoryId);
    if (categoryId.isEmpty()) {
        removeReply->triggerDone(QPlaceReply::CategoryDoesNotExistError, tr("Trying to remove category with empty category id"));
        return removeReply;
    }

    //check if category exists
    QVariantMap categoryMap = m_jsonDbHandler.findCategoryJson(categoryId);
    if (categoryMap.isEmpty()) {
        removeReply->triggerDone(QPlaceReply::CategoryDoesNotExistError, tr("Trying to remove non-existent category"));
        return removeReply;
    }

    QVariantMap parentCategoryMap = m_jsonDbHandler.findParentCategoryJson(categoryId);
    recursiveRemoveHelper(categoryId, parentCategoryMap.value(UUID).toString());

    removeReply->setParentCategoryId(parentCategoryMap.value(UUID).toString());
    removeReply->setId(categoryId);

    //resave the parent without the given category as a child
    int reqId;
    if (!parentCategoryMap.isEmpty()) {
        QStringList childrenUuids;
        childrenUuids = parentCategoryMap.value(CHILDREN_UUIDS).toStringList();
        childrenUuids.removeAll(categoryId);
        parentCategoryMap.insert(CHILDREN_UUIDS, childrenUuids);
        reqId = m_jsonDbHandler.update(parentCategoryMap);
        m_idReplyMap.insert(reqId, removeReply);
    } else {
        //trigger the reply as done
        removeReply->triggerDone();
        emit categoryRemoved(removeReply->id(), removeReply->parentCategoryId());
    }

    return removeReply;
}

QPlaceReply *QPlaceManagerEngineJsonDb::initializeCategories()
{
    Reply *reply = new Reply(this);
    reply->triggerDone();
    return reply;
}

QList<QPlaceCategory> QPlaceManagerEngineJsonDb::childCategories(const QString &parentId) const
{
    int reqId;

    if (parentId.isEmpty()) {
        reqId= m_jsonDbHandler.query(QString("[?") + TYPE + QLatin1String(" = \"") + PLACE_CATEGORY_TYPE + QLatin1String("\"]")
                                               + QLatin1String("[?") + TOP_LEVEL_CATEGORY + QLatin1String(" = true]"));
    } else {
        reqId= m_jsonDbHandler.query(QString("[?") + TYPE + QLatin1String(" = \"") + PLACE_CATEGORY_TYPE + QLatin1String("\"]")
                                               + QLatin1String("[?") + UUID + QLatin1String(" = \"") + parentId + QLatin1String("\"]"));

        QVariantMap responseMap = m_jsonDbHandler.waitForRequest(reqId);

        if (responseMap.value(LENGTH).toInt() <= 0)
            return QList<QPlaceCategory>();

        QVariantMap categoryMap = responseMap.value("data").toList().at(0).toMap();
        QStringList childUuids = categoryMap.value(CHILDREN_UUIDS).toStringList();
        for (int i=0; i < childUuids.count(); ++i)
            childUuids[i].prepend("\"").append("\"");
        QString queryString = QString("[?") + TYPE + QLatin1String(" = \"") + PLACE_CATEGORY_TYPE + QLatin1String("\"]")
                        + QLatin1String("[?") + UUID + QLatin1String(" in [") + childUuids.join(",") + QLatin1String("]]");
        reqId= m_jsonDbHandler.query(queryString);
    }

    QVariantMap responseMap = m_jsonDbHandler.waitForRequest(reqId);
    return m_jsonDbHandler.convertJsonResponseToCategories(responseMap);
}

QLocale QPlaceManagerEngineJsonDb::locale() const
{
    return QLocale();
}

void QPlaceManagerEngineJsonDb::setLocale(const QLocale &locale)
{
    Q_UNUSED(locale);
}

QUrl QPlaceManagerEngineJsonDb::constructIconUrl(const QPlaceIcon &icon, const QSize &size, QPlaceIcon::IconFlags flags)
{
    return icon.fullUrl();
}

void QPlaceManagerEngineJsonDb::processJsonDbResponse(int id, const QVariant &data)
{
    QPlaceReply *reply = m_idReplyMap.value(id,0);
    if (reply) {
        switch (reply->type()) {
        case QPlaceReply::IdReply: {
                IdReply *idReply = qobject_cast<IdReply *>(reply);

                switch (idReply->operationType()) {
                case QPlaceIdReply::SavePlace:
                        /*
                        Expected data format
                        {
                        "uuid":<uuid>,
                        "_version": <version>
                        }*/
                    idReply->setId(data.toMap().value(UUID).toString());
                    break;
                case QPlaceIdReply::RemovePlace:{
                    /*
                    Expected data format example
                    {
                    "count":1,
                    "data":[
                        {
                        "_uuid":"8c196304-509c-5c45-0a07-0ea25a280f10523d"
                        }],
                    "error":[]
                    }*/
                    QVariantMap jsonResponse = data.toMap();
                    if (jsonResponse.value(QLatin1String("count")).toInt() >= 0) {
                        QVariantMap jsonResponse = data.toMap();
                        QString uuid = jsonResponse.value(QLatin1String("data"))
                                .toList().at(0).toMap().value(UUID).toString();
                        if (uuid != idReply->id()) {
                            idReply->triggerDone(QPlaceReply::UnknownError,
                                                 tr("JsonDb Response UUID does not match that in request"
                                                    "for a removal operation\n"
                                                    "JsonDb UUID: %1"
                                                    "Request UUID: %2").arg(uuid).arg(idReply->id()));
                        }
                    } else {
                        idReply->triggerDone(QPlaceReply::UnknownError,
                                             tr("JsonDb response does not contain a uuid"
                                                "for a removal request"));
                    }

                    if (!jsonResponse.value(QLatin1String("error")).toStringList().isEmpty()
                            && idReply->error() == QPlaceReply::NoError) {
                        idReply->triggerDone(QPlaceReply::UnknownError,
                                             tr("JsonDb response had unexpected errors: %1")
                                             .arg(jsonResponse.value(QLatin1String("error"))
                                                    .toStringList().join(QLatin1String(","))));
                    }

                    break;
                }
                case QPlaceIdReply::RemoveCategory:
                case QPlaceIdReply::SaveCategory:
                    //id should've already been set, note the ids from the response
                    //won't match those from the reply since the response might be
                    //be changes to the parent category
                    break;
                default:
                    //Other types should not be possible
                    break;
                }

                if (idReply->error() == QPlaceReply::NoError)
                    idReply->triggerDone();

                //TODO: This is workaround code, it is intended that we use
                //       jsondb notifications to emit the added, updated
                //       and removed signals.
                switch (idReply->operationType()) {
                    case QPlaceIdReply::RemovePlace:
                        emit placeRemoved(idReply->id());
                        break;
                    case QPlaceIdReply::SavePlace:
                        emit placeAdded(idReply->id());
                        break;
                    case QPlaceIdReply::RemoveCategory:
                        emit categoryRemoved(idReply->id(), idReply->parentCategoryId());
                        break;
                    case QPlaceIdReply::SaveCategory:
                        //the uuid in the response here is expected to be that of the parent
                        //the child uuid is already set in the reply.
                        if (idReply->isUpdate())
                            emit categoryUpdated(m_jsonDbHandler.findCategory(idReply->id()), data.toMap().value(UUID).toString());
                        else
                            emit categoryAdded(m_jsonDbHandler.findCategory(idReply->id()), data.toMap().value(UUID).toString());
                        break;
                default:
                    //no other cases should exist
                    break;
                }
                break;
            }
        case QPlaceReply::SearchReply: {
                SearchReply *searchReply = qobject_cast<SearchReply *>(reply);
                QList<QPlace> places = m_jsonDbHandler.convertJsonResponseToPlaces(data);
                QList<QPlaceSearchResult> results;
                QPlaceSearchResult result;
                result.setType(QPlaceSearchResult::PlaceResult);
                bool resultsAlreadySet = false;

                if (searchReply->request().searchArea() != 0) {
                    QGeoBoundingArea *searchArea = searchReply->request().searchArea();
                    if (searchArea->type() == QGeoBoundingArea::CircleType) {
                        QGeoBoundingCircle *circle = static_cast<QGeoBoundingCircle*>(searchArea);
                        foreach (const QPlace &place, places) {
                            qreal dist = circle->center().distanceTo(place.location().coordinate());
                            if (dist < circle->radius() || qFuzzyCompare(dist, circle->radius()) || circle->radius() < 0.0) {
                                result.setDistance(dist);
                                result.setPlace(place);

                                if (searchReply->request().relevanceHint() == QPlaceSearchRequest::DistanceHint) {
                                    //TODO: we can optimize this insertion sort
                                    bool added = false;
                                    for (int i=0; i < results.count(); ++i) {
                                        if (result.distance() < results.at(i).distance() || qFuzzyCompare(result.distance(),results.at(i).distance())) {
                                            results.insert(i, result);
                                            added = true;
                                            break;
                                        }
                                    }
                                    if (!added)
                                        results.append(result);
                                } else {
                                    results.append(result);
                                }
                            }
                        }
                        resultsAlreadySet = true;

                        //TODO: we can optimize this using a bounding box to cull candidates
                        //      and then use distance comparisons for the rest.

                    } else if (searchArea->type() == QGeoBoundingArea::BoxType) {
                        //There seem to be some issues with using the comparison operators
                        //so for now we filter in the plugin code
                        QGeoBoundingBox *box = static_cast<QGeoBoundingBox *>(searchArea);
                        double tly = box->topLeft().latitude();
                        double bry = box->bottomRight().latitude();
                        double tlx = box->topLeft().longitude();
                        double brx = box->bottomRight().longitude();

                        QGeoCoordinate coord;
                        foreach (const QPlace &place, places) {
                            coord = place.location().coordinate();
                            if (coord.latitude() > tly)
                                places.removeAll(place);
                            if (coord.latitude() < bry)
                                places.removeAll(place);

                            bool lonWrap = (tlx > brx); //box wraps over the dateline

                            if (!lonWrap) {
                                if (coord.longitude() < tlx || coord.longitude() > brx) {
                                    places.removeAll(place);
                                }
                            } else {
                                if (coord.longitude() < tlx && coord.longitude() > brx) {
                                    places.removeAll(place);
                                }
                            }
                        }
                    }
                }

                //if we haven't already filled in the results, fill them in now
                if (!resultsAlreadySet) {
                    foreach (const QPlace &place, places) {
                        result.setPlace(place);
                        results.append(result);
                    }
                }

                searchReply->setResults(results);
                searchReply->triggerDone();
                break;
            }
        case QPlaceReply::DetailsReply: {
                QVariantMap jsonMap = data.toMap();
                DetailsReply *detailsReply = qobject_cast<DetailsReply *>(reply);
                if (jsonMap.value("length").toInt() <= 0) {
                    detailsReply->triggerDone(QPlaceReply::PlaceDoesNotExistError,
                                              tr("Specified place does not exist"));
                } else {
                    QList<QPlace> places = m_jsonDbHandler.convertJsonResponseToPlaces(data);
                    Q_ASSERT(!places.isEmpty());
                    detailsReply->setPlace(places.first());
                    detailsReply->triggerDone();
                }
                break;
            }
        case QPlaceReply::Reply: {
                Reply *reply_ = qobject_cast<Reply *>(reply);
                reply_->triggerDone();
                break;
            }
        }
        m_idReplyMap.remove(id);
    }
}

void QPlaceManagerEngineJsonDb::processJsonDbError(int id, int code, const QString &jsonDbErrorString)
{
    QPlaceReply::Error error = QPlaceReply::UnknownError;
    QString errorString = tr("Unknown error occurred operation: jsondb error code =%1, erroString=%2").
                  arg(code).arg(jsonDbErrorString);

    QPlaceReply *placeReply = m_idReplyMap.value(id,0);
    if (placeReply) {
        switch (placeReply->type()) {
        case QPlaceReply::IdReply: {
            IdReply *idReply = qobject_cast<IdReply *>(placeReply);
            switch (code) {
            case JsonDbError::MissingObject:
                switch (idReply->operationType()) {
                case QPlaceIdReply::SavePlace:
                case QPlaceIdReply::RemovePlace:
                    error = QPlaceReply::PlaceDoesNotExistError;
                    errorString = tr("Place does not exist");
                    break;
                case QPlaceIdReply::SaveCategory:
                case QPlaceIdReply::RemoveCategory:
                    error = QPlaceReply::CategoryDoesNotExistError;
                    errorString = tr("Category does not exist");
                }
            }
            idReply->triggerDone(error, errorString);
            break;
        }
        case QPlaceReply::SearchReply: {
                SearchReply *searchReply = qobject_cast<SearchReply*>(placeReply);
                searchReply->triggerDone(error, errorString);
                break;
            }
        case QPlaceReply::Reply: {
                Reply *reply = qobject_cast<Reply*>(placeReply);
                reply->triggerDone(error, errorString);
                break;
            }
        }
        m_idReplyMap.remove(id);
    }
}

void QPlaceManagerEngineJsonDb::recursiveRemoveHelper(const QString &categoryId, const QString &parentId)
{

    QVariantMap categoryMap = m_jsonDbHandler.findCategoryJson(categoryId);
    if (categoryMap.isEmpty())
        return;

    QStringList childUuids = categoryMap.value(CHILDREN_UUIDS).toStringList();
    foreach (const QString &uuid, childUuids)
        recursiveRemoveHelper(uuid, categoryId);

    int reqId = m_jsonDbHandler.remove(categoryId);
    m_jsonDbHandler.waitForRequest(reqId);
    emit categoryRemoved(categoryId, parentId);
}