summaryrefslogtreecommitdiffstats
path: root/plugins/organizer/maemo5/qorganizercaldbaccess.cpp
blob: 131c63983d5878052a1a817f02b831fd41d66b49 (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
/****************************************************************************
**
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the Qt Mobility Components.
**
** $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 "qorganizercaldbaccess.h"
#include "qorganizerdbcache.h"
#include "qorganizermaemo5ids_p.h"
#include "qtorganizer.h"

#include <CalendarErrors.h>
#include <CEvent.h>
#include <CTodo.h>
#include <CJournal.h>
#include <CCalendar.h>
#include <CAlarm.h>
#include <CRecurrence.h>
#include <CAttendee.h>
#include <COrganizer.h>
#include <CProperties.h>

/**
 * entryType enum
 * This enum is from the Maemo5 calendar backend file Common.h
 * The enumeration is pasted here to avoid including Common.h file,
 * which contains dependencies to D-Bus handling and other components
 * that are not actually needed here.
*/
enum entryType {
    E_CALENDAR = 0,
    E_EVENT,
    E_TODO,
    E_JOURNAL,
    E_BDAY,
    E_SPARE,
    E_VTIMEZONE
};

/**
 * constants from the calendar backend
 * These constants are pasted here to avoid including Common.h file,
 * which contains dependencies to D-Bus handling and other components
 * that are not actually needed here.
*/
const int HAS_RECURRENCE = 3;
const int HAS_ALARM = 4;
const int HAS_RECURRENCE_ALARM = 5;

QTM_USE_NAMESPACE

const QString selectComponentCalIdType = QString("select CalendarId, ComponentType from Components where Id = :compId");

const QString selectInnerJoinBatchGuid = QString("select * from components left join componentdetails on components.Id = componentdetails.Id Left Join alarm on components.Id = alarm.Id Left Join Recursive on components.Id = Recursive.Id where Calendarid = :calId AND Components.ComponentType = :compType AND Components.Uid = :compUid");

const QString cookieFixer = QString("update ALARM set CookieId = :newCookie where CookieId = :oldCookie");

OrganizerCalendarDatabaseAccess::OrganizerCalendarDatabaseAccess(OrganizerDbCache* dbCache) : m_dbCache(dbCache)
{
}

OrganizerCalendarDatabaseAccess::~OrganizerCalendarDatabaseAccess()
{
    close();
}

bool OrganizerCalendarDatabaseAccess::open(QString databasePathName)
{
    if (!m_db.isOpen()) {
        m_db = QSqlDatabase::addDatabase("QSQLITE");
        m_db.setHostName("localhost");
        m_db.setDatabaseName(databasePathName);
        bool ok = m_db.open();
        return ok;
    }
    else {
        // database is already open
        return true;
    }
}

void OrganizerCalendarDatabaseAccess::close()
{
    m_db.close();
}

int OrganizerCalendarDatabaseAccess::calIdOf(QOrganizerItemId id)
{
    quint32 convertedId = readItemLocalId(id);
    if (m_dbCache->containsCalId(convertedId)) {
        return m_dbCache->takeCalId(convertedId);
    }
    else {
        QSqlQuery query;
        if (!query.prepare(selectComponentCalIdType))
            return -1;
        query.bindValue(":compId", QString::number(convertedId));

        int retn = -1;
        if (query.exec()) {
            if (query.next())
                retn = query.value(0).toInt();
        }

        m_dbCache->insertCalId(convertedId, retn);
        return retn;
    }
}

int OrganizerCalendarDatabaseAccess::typeOf(QOrganizerItemId id)
{
    quint32 convertedId = readItemLocalId(id);
    if (m_dbCache->containsTypeId(convertedId)) {
        return m_dbCache->takeTypeId(convertedId);
    }
    else {
        QSqlQuery query;
        if (!query.prepare(selectComponentCalIdType))
            return -1;
        query.bindValue(":compId", QString::number(convertedId));

        int retn = -1;
        if (query.exec()) {
            if (query.next())
                retn = query.value(1).toInt();
        }

        m_dbCache->insertTypeId(convertedId, retn);
        return retn;
    }
}

std::vector<CEvent *> OrganizerCalendarDatabaseAccess::getEvents(int calId, std::string guid, int &pErrorCode)
{
    std::vector<CEvent*> listEvent;

    OrganizerGuidCacheKey cacheKey(calId, E_EVENT, QString::fromStdString(guid));
    if (m_dbCache->containsEventVector(cacheKey))
    {
        // found in cache
        m_dbCache->takeEventVector(cacheKey, listEvent);
        return listEvent;
    }

    const int columnNumber = 49;
    CEvent *event = 0;
    CAlarm *pAlarm = 0;
    CRecurrence *pRec = 0;
    int iI_EventCount = 0;
    int iJ_EventCount = 0;
    pErrorCode = CALENDAR_OPERATION_SUCCESSFUL;
    std::vector<long> vCookie;
    std::vector<std::string> vERule;
    std::vector<std::string> vEdate;
    std::vector<std::string> vRdate;
    std::vector<std::string> vRRule;

    QSqlQuery pQuery;
    if (!pQuery.prepare(selectInnerJoinBatchGuid)) {
        pErrorCode = CALENDAR_DATABASE_ERROR;
        return listEvent;
    }
    pQuery.bindValue(":calId", QString::number(calId));
    pQuery.bindValue(":compType", QString::number(E_EVENT));
    pQuery.bindValue(":compUid", QString::fromStdString(guid));

    bool ok = pQuery.exec();

    sqliteErrorMapper(pQuery.lastError(), pErrorCode);
    if (!ok)
        return listEvent;

    while (pQuery.next()) {
        event = new CEvent();
        pAlarm = new CAlarm();
        pRec = new CRecurrence();
        for (iJ_EventCount = 0; iJ_EventCount < columnNumber; ++iJ_EventCount) {
            switch(iJ_EventCount) {
            case 0: // ID1
                event->setId(pQuery.value(iJ_EventCount).toString().toStdString());
                break;

            case 1:
                break;

            case 2: // ID3
                event->setType(pQuery.value(iJ_EventCount).toInt());
                break;

            case 3: // ID4
                event->setFlags(pQuery.value(iJ_EventCount).toInt());
                break;

            case 4: // ID5
                event->setDateStart(pQuery.value(iJ_EventCount).toInt());
                break;

            case 5: // ID6
                event->setDateEnd(pQuery.value(iJ_EventCount).toInt());
                break;

            case 6: // ID7
                if (!pQuery.value(iJ_EventCount).toString().isEmpty())
                    event->setSummary(pQuery.value(iJ_EventCount).toString().toStdString());
                break;

            case 7: // ID8
                if (!pQuery.value(iJ_EventCount).toString().isEmpty())
                    event->setLocation(pQuery.value(iJ_EventCount).toString().toStdString());
                break;

            case 8: // ID9
                if (!pQuery.value(iJ_EventCount).toString().isEmpty())
                    event->setDescription(pQuery.value(iJ_EventCount).toString().toStdString());
                break;

            case 9: // ID10
                event->setStatus(pQuery.value(iJ_EventCount).toInt());
                break;

            case 10: // ID11
                if (!pQuery.value(iJ_EventCount).toString().isEmpty())
                    event->setGUid(pQuery.value(iJ_EventCount).toString().toStdString());
                break;

            case 11: // ID12
                event->setUntil(pQuery.value(iJ_EventCount).toInt());
                break;

            case 12: // ID13
                event->setAllDay(pQuery.value(iJ_EventCount).toInt());
                break;

            case 13: // ID14
                event->setCreatedTime(pQuery.value(iJ_EventCount).toInt());
                break;

            case 14: // ID15
                event->setLastModified(pQuery.value(iJ_EventCount).toInt());
                break;

            case 15: // ID16
                event->setTzid(pQuery.value(iJ_EventCount).toString().toStdString());
                break;

            case 16: // ID17
            case 17: // ID18
            case 18: // ID19
                break;

            case 19: // ID20
                if (pQuery.value(iJ_EventCount).toInt())
                    event->setClas(pQuery.value(iJ_EventCount).toString().toStdString());
                break;

            case 20: // ID21
                if (!pQuery.value(iJ_EventCount).toString().isEmpty())
                    event->setGeo(pQuery.value(iJ_EventCount).toString().toStdString());
                break;

            case 21: // ID22
                event->setPriority(pQuery.value(iJ_EventCount).toInt());
                break;

            case 22: // ID23
                event->setDateStamp(pQuery.value(iJ_EventCount).toInt());
                break;

            case 23: // ID24
                event->setSequence(pQuery.value(iJ_EventCount).toInt());
                break;

            case 24: // ID25
                if (!pQuery.value(iJ_EventCount).toString().isEmpty())
                    event->setTransparency(pQuery.value(iJ_EventCount).toString().toStdString());
                break;

            case 25: // ID26
                event->setUid(pQuery.value(iJ_EventCount).toInt());
                break;

            case 26: // ID27
                if (!pQuery.value(iJ_EventCount).toString().isEmpty())
                    event->setUrl(pQuery.value(iJ_EventCount).toString().toStdString());
                break;

            case 27: // ID28
                if (!pQuery.value(iJ_EventCount).toString().isEmpty()) {
                    std::vector<std::string> vAtt;
                    vAtt.push_back(pQuery.value(iJ_EventCount).toString().toStdString());
                    event->setAttachments(vAtt);
                }
                break;

            case 28: // ID29
                if (!pQuery.value(iJ_EventCount).toString().isEmpty())
                    event->setCategories(pQuery.value(iJ_EventCount).toString().toStdString());
                break;

            case 29: // ID30
                if (!pQuery.value(iJ_EventCount).toString().isEmpty())
                    event->setComments(pQuery.value(iJ_EventCount).toString().toStdString());
                break;

            case 30: // ID31
                if (!pQuery.value(iJ_EventCount).toString().isEmpty())
                    event->setContact(pQuery.value(iJ_EventCount).toString().toStdString());
                break;

            case 31: // ID32
                if (!pQuery.value(iJ_EventCount).toString().isEmpty())
                    event->setRelated(pQuery.value(iJ_EventCount).toString().toStdString());
                break;

            case 32: // ID33
                if (!pQuery.value(iJ_EventCount).toString().isEmpty())
                    event->setResources(pQuery.value(iJ_EventCount).toString().toStdString());
                break;

            case 33: // ID34
            case 34: // ID35
            case 35: // ID36
                break;

            case 36: // ID37
                if (!pQuery.value(iJ_EventCount).toString().isEmpty())
                    pAlarm->setTrigger(pQuery.value(iJ_EventCount).toInt());
                break;

            case 37: // ID38
                if (!pQuery.value(iJ_EventCount).toString().isEmpty())
                    pAlarm->setRepeat(pQuery.value(iJ_EventCount).toInt());
                break;

            case 38: // ID39
                if (!pQuery.value(iJ_EventCount).toString().isEmpty())
                    pAlarm->setDuration(pQuery.value(iJ_EventCount).toInt());
                break;

            case 39: // ID40
                if (!pQuery.value(iJ_EventCount).toString().isEmpty())
                    pAlarm->setAction(pQuery.value(iJ_EventCount).toInt());
                break;

            case 40: // ID41
                if (!pQuery.value(iJ_EventCount).toString().isEmpty()) {
                    vCookie.push_back(pQuery.value(iJ_EventCount).toInt());
                    pAlarm->setCookie(vCookie);
                }
                break;

            case 41: // ID42
                if (!pQuery.value(iJ_EventCount).toString().isEmpty())
                    pAlarm->setAttach(pQuery.value(iJ_EventCount).toString().toStdString());
                break;

            case 42: // ID43
                break;

            case 43: // ID44
                if (!pQuery.value(iJ_EventCount).toString().isEmpty()) {
                    vRRule = event->extractSubStrings(pQuery.value(iJ_EventCount).toString().toStdString(), EXCLAMATION);
                    pRec->setRrule(vRRule);
                }
                break;

            case 44: // ID45
                if (!pQuery.value(iJ_EventCount).toString().isEmpty()) {
                    vRdate = event->extractSubStrings(pQuery.value(iJ_EventCount).toString().toStdString(), SEMI_COLON);
                    pRec->setRDays(vRdate);
                }
                break;

            case 45: // ID46
                if (!pQuery.value(iJ_EventCount).toString().isEmpty()) {
                    vEdate = event->extractSubStrings(pQuery.value(iJ_EventCount).toString().toStdString(), SEMI_COLON);
                    pRec->setEDays(vEdate);
                }
                break;

            case 46: // ID47
                if (!pQuery.value(iJ_EventCount).toString().isEmpty()) {
                    vERule = event->extractSubStrings(pQuery.value(iJ_EventCount).toString().toStdString(), EXCLAMATION);
                    pRec->setErule(vERule);
                }
                break;

            case 47: // ID48
                if (!pQuery.value(iJ_EventCount).toString().isEmpty())
                    pRec->setRecurId(pQuery.value(iJ_EventCount).toInt());
                break;

            case 48: // ID49
                if (!pQuery.value(iJ_EventCount).toString().isEmpty())
                    pRec->setRtype(pQuery.value(iJ_EventCount).toInt());
                break;
            }
        }

        if ((event->getFlags() == HAS_RECURRENCE) ||
            (event->getFlags() == HAS_RECURRENCE_ALARM)) {
            event->setRecurrence(pRec);
        }
        delete pRec;
        pRec = 0;

        if ((event->getFlags() == HAS_ALARM) ||
            (event->getFlags() == HAS_RECURRENCE_ALARM)) {
            event->setAlarm(pAlarm);
        }
        delete pAlarm;
        pAlarm = 0;

        std::vector<CAttendee *> listAttendee;

        listAttendee = event->retrieveAttendeeDetails();
        COrganizer *pOrg = 0;

        pOrg = event->retrieveOrganizerDetails();
        if (listAttendee.size())
            event->setAttendees(listAttendee);

        std::vector<CAttendee *>::iterator listAttendeeIterator;
        for (listAttendeeIterator = listAttendee.begin(); listAttendeeIterator != listAttendee.end(); ++listAttendeeIterator)
            delete *listAttendeeIterator;

        if (pOrg) {
            event->setOrganizer(pOrg);
            delete pOrg;
            pOrg = 0;
        }

        /*retrieve xprop */
        std::vector<CProperties *> vPropList;
        vPropList = event->retrieveXPropertyDetails();
        event->setXProperties(vPropList);

        std::vector<CProperties *>::iterator vPropListIterator;
        for (vPropListIterator = vPropList.begin(); vPropListIterator != vPropList.end(); ++vPropListIterator)
            delete *vPropListIterator;

        /*retrieve params */
        std::map<std::string, std::vector<CParameters *> > paramMap;
        paramMap = event->retrieveParameterDetails();

        event->setHashMap(paramMap);
        paramMap.clear();

        /* push the event in to the list */
        listEvent.push_back(event);

        ++iI_EventCount;
    }

    // put to cache
    m_dbCache->insertEventVector(cacheKey, listEvent);

    return listEvent;
}

std::vector<CTodo *> OrganizerCalendarDatabaseAccess::getTodos(int calId, std::string guid, int &pErrorCode)
{
    std::vector<CTodo*> listTodo;

    OrganizerGuidCacheKey cacheKey(calId, E_TODO, QString::fromStdString(guid));
    if (m_dbCache->containsTodoVector(cacheKey))
    {
        // found in cache
        m_dbCache->takeTodoVector(cacheKey, listTodo);
        return listTodo;
    }

    const int columnNumber = 49;
    CTodo *todo = 0;
    CAlarm *pAlarm = 0;
    int iI_TodoCount = 0;
    int iJ_TodoCount = 0;
    pErrorCode = CALENDAR_OPERATION_SUCCESSFUL;
    std::vector<long> vCookie;

    QSqlQuery pQuery;
    if (!pQuery.prepare(selectInnerJoinBatchGuid)) {
        pErrorCode = CALENDAR_DATABASE_ERROR;
        return listTodo;
    }
    pQuery.bindValue(":calId", QString::number(calId));
    pQuery.bindValue(":compType", QString::number(E_TODO));
    pQuery.bindValue(":compUid", QString::fromStdString(guid));
    bool ok = pQuery.exec();

    sqliteErrorMapper(pQuery.lastError(), pErrorCode);
    if (!ok)
        return listTodo;

    while (pQuery.next()) {
        todo = new CTodo();
        pAlarm = new CAlarm();
        for (iJ_TodoCount = 0; iJ_TodoCount < columnNumber; ++iJ_TodoCount) {
            switch(iJ_TodoCount) {
            case 0: // ID1
                todo->setId(pQuery.value(iJ_TodoCount).toString().toStdString());
                break;

            case 1:
                break;

            case 2: // ID3
                todo->setType(pQuery.value(iJ_TodoCount).toInt());
                break;

            case 3: // ID4
                todo->setFlags(pQuery.value(iJ_TodoCount).toInt());
                break;

            case 4: // ID5
                todo->setDateStart(pQuery.value(iJ_TodoCount).toInt());
                break;

            case 5: // ID6
                todo->setDateEnd(pQuery.value(iJ_TodoCount).toInt());
                break;

            case 6: // ID7
                if (!pQuery.value(iJ_TodoCount).toString().isEmpty())
                    todo->setSummary(pQuery.value(iJ_TodoCount).toString().toStdString());
                break;

            case 7: // ID8
                if (!pQuery.value(iJ_TodoCount).toString().isEmpty())
                    todo->setLocation(pQuery.value(iJ_TodoCount).toString().toStdString());
                break;

            case 8: // ID9
                if (!pQuery.value(iJ_TodoCount).toString().isEmpty())
                    todo->setDescription(pQuery.value(iJ_TodoCount).toString().toStdString());
                break;

            case 9: // ID10
                todo->setStatus(pQuery.value(iJ_TodoCount).toInt());
                break;

            case 10: // ID11
                if (!pQuery.value(iJ_TodoCount).toString().isEmpty())
                    todo->setGUid(pQuery.value(iJ_TodoCount).toString().toStdString());
                break;

            case 11: // ID12
                todo->setUntil(pQuery.value(iJ_TodoCount).toInt());
                break;

            case 12: // ID13
                todo->setAllDay(pQuery.value(iJ_TodoCount).toInt());
                break;

            case 13: // ID14
                todo->setCreatedTime(pQuery.value(iJ_TodoCount).toInt());
                break;

            case 14: // ID15
                todo->setLastModified(pQuery.value(iJ_TodoCount).toInt());
                break;

            case 15: // ID16
                todo->setTzid(pQuery.value(iJ_TodoCount).toString().toStdString());
                break;

            case 16: // ID17
            case 17: // ID18
            case 18: // ID19
                break;

            case 19: // ID20
                if (pQuery.value(iJ_TodoCount).toInt())
                    todo->setClas(pQuery.value(iJ_TodoCount).toString().toStdString());
                break;

            case 20: // ID21
                if (!pQuery.value(iJ_TodoCount).toString().isEmpty())
                    todo->setGeo(pQuery.value(iJ_TodoCount).toString().toStdString());
                break;

            case 21: // ID22
                todo->setPriority(pQuery.value(iJ_TodoCount).toInt());
                break;

            case 22: // ID23
                todo->setDateStamp(pQuery.value(iJ_TodoCount).toInt());
                break;

            case 23: // ID24
                todo->setSequence(pQuery.value(iJ_TodoCount).toInt());
                break;

            case 24: // ID25
                break;

            case 25: // ID26
                todo->setUid(pQuery.value(iJ_TodoCount).toInt());
                break;

            case 26: // ID27
                if (!pQuery.value(iJ_TodoCount).toString().isEmpty())
                    todo->setUrl(pQuery.value(iJ_TodoCount).toString().toStdString());
                break;

            case 27: // ID28
                break;

            case 28: // ID29
                if (!pQuery.value(iJ_TodoCount).toString().isEmpty())
                    todo->setCategories(pQuery.value(iJ_TodoCount).toString().toStdString());
                break;

            case 29: // ID30
                if (!pQuery.value(iJ_TodoCount).toString().isEmpty())
                    todo->setComments(pQuery.value(iJ_TodoCount).toString().toStdString());
                break;

            case 30: // ID31
                if (!pQuery.value(iJ_TodoCount).toString().isEmpty())
                    todo->setContact(pQuery.value(iJ_TodoCount).toString().toStdString());
                break;

            case 31: // ID32
                if (!pQuery.value(iJ_TodoCount).toString().isEmpty())
                    todo->setRelated(pQuery.value(iJ_TodoCount).toString().toStdString());
                break;

            case 32: // ID33
                if (!pQuery.value(iJ_TodoCount).toString().isEmpty())
                    todo->setResources(pQuery.value(iJ_TodoCount).toString().toStdString());
                break;

            case 33: // ID34
                todo->setPercentComplete(pQuery.value(iJ_TodoCount).toInt());
                break;

            case 34: // ID35
                todo->setCompleted(pQuery.value(iJ_TodoCount).toInt());
                break;

            case 35: // ID36
                break;

            case 36: // ID37
                if (!pQuery.value(iJ_TodoCount).toString().isEmpty())
                    pAlarm->setTrigger(pQuery.value(iJ_TodoCount).toInt());
                break;

            case 37: // ID38
                if (!pQuery.value(iJ_TodoCount).toString().isEmpty())
                    pAlarm->setRepeat(pQuery.value(iJ_TodoCount).toInt());
                break;

            case 38: // ID39
                if (!pQuery.value(iJ_TodoCount).toString().isEmpty())
                    pAlarm->setDuration(pQuery.value(iJ_TodoCount).toInt());
                break;

            case 39: // ID40
                if (!pQuery.value(iJ_TodoCount).toString().isEmpty())
                    pAlarm->setAction(pQuery.value(iJ_TodoCount).toInt());
                break;

            case 40: // ID41
                if (!pQuery.value(iJ_TodoCount).toString().isEmpty()) {
                    vCookie.push_back(pQuery.value(iJ_TodoCount).toInt());
                    pAlarm->setCookie(vCookie);
                }
                break;

            case 41: // ID42
                if (!pQuery.value(iJ_TodoCount).toString().isEmpty())
                    pAlarm->setAttach(pQuery.value(iJ_TodoCount).toString().toStdString());
                break;

            default:
                break;
            }
        }

        if (todo->getFlags() == HAS_ALARM)
            todo->setAlarm(pAlarm);

        delete pAlarm;
        pAlarm = 0;

        /*retrieve xprop */
        std::vector<CProperties *> vPropList;
        vPropList = todo->retrieveXPropertyDetails();
        todo->setXProperties(vPropList);

        std::vector<CProperties *>::iterator vPropListIterator;
        for (vPropListIterator = vPropList.begin(); vPropListIterator != vPropList.end(); ++vPropListIterator)
            delete *vPropListIterator;

        /*retrieve params */
        std::map<std::string, std::vector<CParameters *> > paramMap;
        paramMap = todo->retrieveParameterDetails();
        todo->setHashMap(paramMap);
        paramMap.clear();

        /* push the todo in to the list */
        listTodo.push_back(todo);

        ++iI_TodoCount;
    }

    // put to cache
    m_dbCache->insertTodoVector(cacheKey, listTodo);

    return listTodo;
}

std::vector<CJournal *> OrganizerCalendarDatabaseAccess::getJournals(int calId, std::string guid, int &pErrorCode)
{
    std::vector<CJournal*> listJournal;

    OrganizerGuidCacheKey cacheKey(calId, E_JOURNAL, QString::fromStdString(guid));
    if (m_dbCache->containsJournalVector(cacheKey))
    {
        // found in cache
        m_dbCache->takeJournalVector(cacheKey, listJournal);
        return listJournal;
    }

    const int columnNumber = 49;
    CJournal *journal = 0;
    int iI_JourCount = 0;
    int iJ_JourCount = 0;
    pErrorCode = CALENDAR_OPERATION_SUCCESSFUL;

    QSqlQuery pQuery;
    if (!pQuery.prepare(selectInnerJoinBatchGuid)) {
        pErrorCode = CALENDAR_DATABASE_ERROR;
        return listJournal;
    }
    pQuery.bindValue(":calId", QString::number(calId));
    pQuery.bindValue(":compType", QString::number(E_JOURNAL));
    pQuery.bindValue(":compUid", QString::fromStdString(guid));
    bool ok = pQuery.exec();

    sqliteErrorMapper(pQuery.lastError(), pErrorCode);
    if (!ok)
        return listJournal;

    while (pQuery.next()) {
        journal = new CJournal();
        for (iJ_JourCount = 0; iJ_JourCount < columnNumber; ++iJ_JourCount) {
            switch(iJ_JourCount) {
            case 0: // ID1
                journal->setId(pQuery.value(iJ_JourCount).toString().toStdString());
                break;

            case 1:
                break;

            case 2: // ID3
                journal->setType(pQuery.value(iJ_JourCount).toInt());
                break;

            case 3: // ID4
                journal->setFlags(pQuery.value(iJ_JourCount).toInt());
                break;

            case 4: // ID5
                journal->setDateStart(pQuery.value(iJ_JourCount).toInt());
                break;

            case 5: // ID6
                journal->setDateEnd(pQuery.value(iJ_JourCount).toInt());
                break;

            case 6: // ID7
                if (!pQuery.value(iJ_JourCount).toString().isEmpty())
                    journal->setSummary(pQuery.value(iJ_JourCount).toString().toStdString());
                break;

            case 7: // ID8
                if (!pQuery.value(iJ_JourCount).toString().isEmpty())
                    journal->setLocation(pQuery.value(iJ_JourCount).toString().toStdString());
                break;

            case 8: // ID9
                if (!pQuery.value(iJ_JourCount).toString().isEmpty())
                    journal->setDescription(pQuery.value(iJ_JourCount).toString().toStdString());
                break;

            case 9: // ID10
                journal->setStatus(pQuery.value(iJ_JourCount).toInt());
                break;

            case 10: // ID11
                if (!pQuery.value(iJ_JourCount).toString().isEmpty())
                    journal->setGUid(pQuery.value(iJ_JourCount).toString().toStdString());
                break;

            case 11: // ID12
                journal->setUntil(pQuery.value(iJ_JourCount).toInt());
                break;

            case 12: // ID13
                journal->setAllDay(pQuery.value(iJ_JourCount).toInt());
                break;

            case 13: // ID14
                journal->setCreatedTime(pQuery.value(iJ_JourCount).toInt());
                break;

            case 14: // ID15
                journal->setLastModified(pQuery.value(iJ_JourCount).toInt());
                break;

            case 15: // ID16
                journal->setTzid(pQuery.value(iJ_JourCount).toString().toStdString());
                break;

            case 16: // ID17
            case 17: // ID18
            case 18: // ID19
                break;

            case 19: // ID20
                if (pQuery.value(iJ_JourCount).toInt())
                    journal->setClas(pQuery.value(iJ_JourCount).toString().toStdString());
                break;

            case 20: // ID21
            case 21: // ID22
                break;

            case 22: // ID23
                journal->setDateStamp(pQuery.value(iJ_JourCount).toInt());
                break;

            case 23: // ID24
                journal->setSequence(pQuery.value(iJ_JourCount).toInt());
                break;

            case 24: // ID25
                break;

            case 25: // ID26
                journal->setUid(pQuery.value(iJ_JourCount).toInt());
                break;

            case 26: // ID27
                if (!pQuery.value(iJ_JourCount).toString().isEmpty())
                    journal->setUrl(pQuery.value(iJ_JourCount).toString().toStdString());
                break;

            case 27: // ID28
                break;

            case 28: // ID29
                if (!pQuery.value(iJ_JourCount).toString().isEmpty())
                    journal->setCategories(pQuery.value(iJ_JourCount).toString().toStdString());
                break;

            case 29: // ID30
                if (!pQuery.value(iJ_JourCount).toString().isEmpty())
                    journal->setComments(pQuery.value(iJ_JourCount).toString().toStdString());
                break;

            case 30: // ID31
                if (!pQuery.value(iJ_JourCount).toString().isEmpty())
                    journal->setContact(pQuery.value(iJ_JourCount).toString().toStdString());
                break;

            case 31: // ID32
                if (!pQuery.value(iJ_JourCount).toString().isEmpty())
                    journal->setRelated(pQuery.value(iJ_JourCount).toString().toStdString());
                break;

            case 32: // ID33
                if (!pQuery.value(iJ_JourCount).toString().isEmpty())
                    journal->setResources(pQuery.value(iJ_JourCount).toString().toStdString());
                break;

            default:
                break;
            }
        }

        /*retrieve xprop */
        std::vector<CProperties *> vPropList;
        vPropList = journal->retrieveXPropertyDetails();
        journal->setXProperties(vPropList);

        std::vector<CProperties *>::iterator vPropListIterator;
        for (vPropListIterator = vPropList.begin(); vPropListIterator != vPropList.end(); ++vPropListIterator)
            delete *vPropListIterator;

        /*retrieve params */
        std::map<std::string, std::vector<CParameters *> > paramMap;
        paramMap = journal->retrieveParameterDetails();
        journal->setHashMap(paramMap);
        paramMap.clear();

        /* push the journal in to the list */
        listJournal.push_back(journal);

        ++iI_JourCount;
    }

    // put to cache
    m_dbCache->insertJournalVector(cacheKey, listJournal);

    return listJournal;
}

CEvent* OrganizerCalendarDatabaseAccess::getEvent(CCalendar* cal, const std::string& id, int& calError)
{
    OrganizerIdCacheKey cacheKey(cal->getCalendarId(), QString::fromStdString(id));
    if (m_dbCache->containsEvent(cacheKey)) {
        return m_dbCache->takeEvent(cacheKey);
    }
    else {
        CEvent* event = cal->getEvent(id, calError);
        m_dbCache->insertEvent(cacheKey, event);
        return event;
    }
}

CTodo* OrganizerCalendarDatabaseAccess::getTodo(CCalendar* cal, const std::string& id, int& calError)
{
    OrganizerIdCacheKey cacheKey(cal->getCalendarId(), QString::fromStdString(id));
    if (m_dbCache->containsTodo(cacheKey)) {
        return m_dbCache->takeTodo(cacheKey);
    }
    else {
        CTodo* todo = cal->getTodo(id, calError);
        m_dbCache->insertTodo(cacheKey, todo);
        return todo;
    }
}

CJournal* OrganizerCalendarDatabaseAccess::getJournal(CCalendar* cal, const std::string& id, int& calError)
{
    OrganizerIdCacheKey cacheKey(cal->getCalendarId(), QString::fromStdString(id));
    if (m_dbCache->containsJournal(cacheKey)) {
        return m_dbCache->takeJournal(cacheKey);
    }
    else {
        CJournal* journal = cal->getJournal(id, calError);
        m_dbCache->insertJournal(cacheKey, journal);
        return journal;
    }
}

void OrganizerCalendarDatabaseAccess::getIdList(CCalendar* cal, int compType, int& calError, std::vector<std::string> &result)
{
    OrganizerCalIdTypeIdCacheKey cacheKey(cal->getCalendarId(), compType);
    if (m_dbCache->containsIds(cacheKey)) {
        m_dbCache->takeIdsVector(cacheKey, result);
    }
    else {
        result = cal->getIdList(compType, calError);
        m_dbCache->insertIds(cacheKey, result);
    }
}

void OrganizerCalendarDatabaseAccess::fixAlarmCookie(QPair<qint32, qint32> change)
{
    QSqlQuery query;
    if (!query.prepare(cookieFixer))
        return;
    query.bindValue(":oldCookie", change.first);
    query.bindValue(":newCookie", change.second);
    query.exec();
    if (m_dbCache)
        m_dbCache->invalidate();
}

void OrganizerCalendarDatabaseAccess::sqliteErrorMapper(const QSqlError &sqlError, int& errorCode)
{
    switch (sqlError.type()) {
        case QSqlError::NoError:
            errorCode = CALENDAR_OPERATION_SUCCESSFUL;
            break;
        case QSqlError::ConnectionError:
        case QSqlError::StatementError:
        case QSqlError::TransactionError:
        case QSqlError::UnknownError:
        default :
            errorCode = CALENDAR_DATABASE_ERROR;
            break;
    }
}