summaryrefslogtreecommitdiff
path: root/src/components/application_manager/src/resumption/resumption_data_db.cc
blob: eedfc48b7327d39b7538b36345d5695498158571 (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
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
/*
 * Copyright (c) 2015, Ford Motor Company
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice, this
 * list of conditions and the following disclaimer.
 *
 * Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following
 * disclaimer in the documentation and/or other materials provided with the
 * distribution.
 *
 * Neither the name of the Ford Motor Company nor the names of its contributors
 * may be used to endorse or promote products derived from this software
 * without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */
#include <unistd.h>
#include <string>

#include "application_manager/application_manager_impl.h"
#include "application_manager/application_manager_settings.h"
#include "application_manager/message_helper.h"
#include "application_manager/resumption/resumption_data_db.h"
#include "application_manager/resumption/resumption_sql_queries.h"
#include "application_manager/rpc_plugins/rc_rpc_plugin/include/rc_rpc_plugin/rc_module_constants.h"
#include "application_manager/smart_object_keys.h"
#include "utils/gen_hash.h"
#include "utils/helpers.h"
#include "utils/scope_guard.h"

namespace {
const std::string kDatabaseName = "resumption";
}

namespace resumption {
SDL_CREATE_LOG_VARIABLE("Resumption")

ResumptionDataDB::ResumptionDataDB(
    DbStorage db_storage,
    const application_manager::ApplicationManager& application_manager)
    : ResumptionData(application_manager) {
  if (db_storage == In_File_Storage) {
    db_ = new utils::dbms::SQLDatabase(kDatabaseName);
#ifndef __QNX__
    std::string path = application_manager_.get_settings().app_storage_folder();
    if (!path.empty()) {
      db_->set_path(path + "/");
    }
  } else if (db_storage == In_Memory_Storage) {
    db_ = new utils::dbms::SQLDatabase();
#endif  // __QNX__
  } else {
    SDL_LOG_AUTO_TRACE();
    SDL_LOG_ERROR("Get not existed type of database storage");
  }
}

ResumptionDataDB::~ResumptionDataDB() {
  db_->Close();
  delete db_;
}

bool ResumptionDataDB::Init() {
  SDL_LOG_AUTO_TRACE();

  if (!db_->Open()) {
    SDL_LOG_ERROR("Failed opening database.");
    SDL_LOG_INFO("Starting opening retries.");
    const uint16_t attempts =
        application_manager_.get_settings().attempts_to_open_resumption_db();
    SDL_LOG_DEBUG("Total attempts number is: " << attempts);
    bool is_opened = false;
    const uint16_t open_attempt_timeout_ms =
        application_manager_.get_settings()
            .open_attempt_timeout_ms_resumption_db();
    const useconds_t sleep_interval_mcsec = open_attempt_timeout_ms * 1000;
    SDL_LOG_DEBUG("Open attempt timeout(ms) is: " << open_attempt_timeout_ms);
    for (int i = 0; i < attempts; ++i) {
      usleep(sleep_interval_mcsec);
      SDL_LOG_INFO("Attempt: " << i + 1);
      if (db_->Open()) {
        SDL_LOG_INFO("Database opened.");
        is_opened = true;
        break;
      }
    }
    if (!is_opened) {
      SDL_LOG_ERROR("Open retry sequence failed. Tried "
                    << attempts << " attempts with " << open_attempt_timeout_ms
                    << " open timeout(ms) for each.");
      return false;
    }
  }
#ifndef __QNX__
  if (!db_->IsReadWrite()) {
    SDL_LOG_ERROR("There are no read/write permissions for database");
    return false;
  }
#endif  // __QNX__
  utils::dbms::SQLQuery query(db());
  if (!query.Exec(kCreateSchema)) {
    SDL_LOG_ERROR(
        "Failed creating schema of database: " << query.LastError().text());
    return false;
  }
  utils::dbms::SQLQuery query_checks_resumption(db());
  if (!query_checks_resumption.Prepare(kChecksResumptionData) ||
      !query_checks_resumption.Exec()) {
    SDL_LOG_ERROR("Failed verification or execution query kChecksResumptionData"
                  << query_checks_resumption.LastError().text());
    return false;
  }
  if (0 == query_checks_resumption.GetInteger(0)) {
    utils::dbms::SQLQuery query_insert_resumption(db());
    if (!query_insert_resumption.Prepare(kInsertInitData) ||
        !query_insert_resumption.Exec()) {
      SDL_LOG_ERROR("Failed insert init data to database: "
                    << query_insert_resumption.LastError().text());
      return false;
    }
  }
  return true;
}

void ResumptionDataDB::SaveApplication(
    app_mngr::ApplicationSharedPtr application) {
  using namespace app_mngr;
  using namespace mobile_api;
  using namespace helpers;
  SDL_LOG_AUTO_TRACE();
  DCHECK_OR_RETURN_VOID(application);
  bool application_exist = false;
  const std::string& policy_app_id = application->policy_app_id();
  const std::string& device_mac = application->mac_address();
  SDL_LOG_INFO("app_id : " << application->app_id() << " policy_app_id : "
                           << policy_app_id << " device_id : " << device_mac);

  if (!CheckExistenceApplication(
          policy_app_id, device_mac, application_exist)) {
    SDL_LOG_ERROR("Problem with access to DB");
    return;
  }

  if (application->is_application_data_changed()) {
    if (application_exist &&
        !DeleteSavedApplication(policy_app_id, device_mac)) {
      SDL_LOG_ERROR("Deleting of application data is not finished");
      return;
    }

    if (!SaveApplicationToDB(application, policy_app_id, device_mac)) {
      SDL_LOG_ERROR("Saving of application data is not finished");
      return;
    }
    SDL_LOG_INFO("All data from application were saved successfully");
    application->set_is_application_data_changed(false);
  } else if (application_exist) {
    if (!UpdateApplicationData(application, policy_app_id, device_mac)) {
      SDL_LOG_ERROR("Updating application data is failed");
      return;
    }
    SDL_LOG_INFO("Application data were updated successfully");
  } else if (!InsertApplicationData(application, policy_app_id, device_mac)) {
    SDL_LOG_ERROR("Saving data of application is failed");
    return;
  }
  WriteDb();
}

bool ResumptionDataDB::IsHMIApplicationIdExist(uint32_t hmi_app_id) const {
  SDL_LOG_AUTO_TRACE();

  return CheckExistenceHMIId(hmi_app_id);
}

uint32_t ResumptionDataDB::GetHMIApplicationID(
    const std::string& policy_app_id, const std::string& device_id) const {
  SDL_LOG_AUTO_TRACE();

  uint32_t hmi_app_id = 0;
  SelectHMIId(policy_app_id, device_id, hmi_app_id);
  return hmi_app_id;
}

void ResumptionDataDB::IncrementIgnOffCount() {
  SDL_LOG_AUTO_TRACE();

  utils::dbms::SQLQuery query_update_suspend_data(db());
  utils::dbms::SQLQuery query_update_last_ign_off_time(db());
  /*
    application_lifes - contains amount of ignition cycles during which
    db stores data of application
   */
  const int application_lifes = 3;

  if (DeleteAppWithIgnCount(application_lifes)) {
    SDL_LOG_INFO("Saved application with ign_off_count = " << application_lifes
                                                           << " was deleted");
  } else {
    SDL_LOG_WARN("Problem with removing applications");
  }

  if (query_update_suspend_data.Prepare(kUpdateSuspendData)) {
    if (query_update_suspend_data.Exec()) {
      SDL_LOG_INFO("Data ign_off_count and suspend_count were updated");
    }
  }

  if (query_update_last_ign_off_time.Prepare(kUpdateLastIgnOffTime)) {
    query_update_last_ign_off_time.Bind(0, static_cast<int64_t>(time(NULL)));
    if (query_update_last_ign_off_time.Exec()) {
      SDL_LOG_INFO("Data last_ign_off_time was updated");
    }
  }
}

bool ResumptionDataDB::DeleteAppWithIgnCount(const int application_lifes) {
  SDL_LOG_AUTO_TRACE();
  utils::dbms::SQLQuery select_apps_for_removing(db());
  utils::dbms::SQLQuery count_app(db());

  if (!select_apps_for_removing.Prepare(kSelectApplicationsIgnOffCount) ||
      !count_app.Prepare(kCountApplicationsIgnOff)) {
    SDL_LOG_WARN(
        "Problem with verification query select_apps_for_removing or"
        " query count_app");
    return false;
  }
  /* Positions of binding data for "query count_app" :
    field "ign_off_count" from table "application" = 0*/
  count_app.Bind(0, application_lifes);
  if (!count_app.Exec() || !count_app.GetInteger(0)) {
    SDL_LOG_WARN("Problem with execution or count app=0");
    return false;
  }
  std::string policy_app_id;
  std::string device_id;
  /* Positions of binding data for "select_apps_for_removing" :
     field "ign_off_count" from table "application" = 0*/
  select_apps_for_removing.Bind(0, application_lifes);
  while (select_apps_for_removing.Next()) {
    device_id = select_apps_for_removing.GetString(0);
    policy_app_id = select_apps_for_removing.GetString(1);
    if (!DeleteSavedApplication(policy_app_id, device_id)) {
      SDL_LOG_WARN("Problem with removing application data");
      return false;
    }
  }
  SDL_LOG_WARN("Applications data were removed successfully");
  WriteDb();
  return true;
}

bool ResumptionDataDB::GetHashId(const std::string& policy_app_id,
                                 const std::string& device_id,
                                 std::string& hash_id) const {
  SDL_LOG_AUTO_TRACE();

  return SelectHashId(policy_app_id, device_id, hash_id);
}

void ResumptionDataDB::DecrementIgnOffCount() {
  SDL_LOG_AUTO_TRACE();

  UpdateDataOnAwake();
}

bool ResumptionDataDB::GetSavedApplication(
    const std::string& policy_app_id,
    const std::string& device_id,
    smart_objects::SmartObject& saved_app) const {
  SDL_LOG_AUTO_TRACE();
  bool application_exist = false;

  if (!CheckExistenceApplication(policy_app_id, device_id, application_exist) ||
      !application_exist) {
    SDL_LOG_ERROR("Problem with access to DB or application does not exists");
    return false;
  }

  if (!SelectDataFromAppTable(policy_app_id, device_id, saved_app)) {
    SDL_LOG_ERROR("Problem with restoring of data from application table");
    return false;
  }

  if (!SelectFilesData(policy_app_id, device_id, saved_app)) {
    SDL_LOG_ERROR("Problem with restoring of files data");
    return false;
  }

  if (!SelectSubmenuData(policy_app_id, device_id, saved_app)) {
    SDL_LOG_ERROR("Problem with restoring of submenu data");
    return false;
  }

  if (!SelectCommandData(policy_app_id, device_id, saved_app)) {
    SDL_LOG_ERROR("Problem with restoring of command data");
    return false;
  }

  if (!SelectSubscriptionsData(policy_app_id, device_id, saved_app)) {
    SDL_LOG_ERROR("Problem with restoring of subscriptions data");
    return false;
  }

  if (!SelectChoiceSetData(policy_app_id, device_id, saved_app)) {
    SDL_LOG_ERROR("Problem with restoring of choice set data");
    return false;
  }

  if (!SelectGlobalPropertiesData(policy_app_id, device_id, saved_app)) {
    SDL_LOG_ERROR("Problem with restoring of global properties data");
    return false;
  }

  if (!SelectUserLocationData(policy_app_id, device_id, saved_app)) {
    SDL_LOG_ERROR("Problem with restoring of user location data");
    return false;
  }

  SDL_LOG_INFO("Application data were successfully fetched from data base");
  return true;
}

bool ResumptionDataDB::RemoveApplicationFromSaved(
    const std::string& policy_app_id, const std::string& device_id) {
  SDL_LOG_AUTO_TRACE();
  bool application_exist = false;
  if (!CheckExistenceApplication(policy_app_id, device_id, application_exist) ||
      !application_exist) {
    SDL_LOG_ERROR(
        "Problem with access to DB or application does not"
        " exist");
    return false;
  }
  bool result = false;
  if (DeleteSavedApplication(policy_app_id, device_id)) {
    WriteDb();
    result = true;
  }
  return result;
}

uint32_t ResumptionDataDB::GetIgnOffTime() const {
  SDL_LOG_AUTO_TRACE();
  return SelectIgnOffTime();
}

uint32_t ResumptionDataDB::GetGlobalIgnOnCounter() const {
  SDL_LOG_AUTO_TRACE();
  sync_primitives::AutoLock autolock(resumption_lock_);

  utils::dbms::SQLQuery query(db());
  if (!query.Prepare(kSelectGlobalIgnOnCounter)) {
    SDL_LOG_ERROR("Problem with prepare query : " << kSelectGlobalIgnOnCounter);
    return 1;
  }

  if (!query.Exec()) {
    SDL_LOG_ERROR("Problem with exec query : " << kSelectGlobalIgnOnCounter);
    return 1;
  }

  const auto global_ign_on_counter = query.GetUInteger(0);
  SDL_LOG_DEBUG("Global Ign On Counter = " << global_ign_on_counter);
  return global_ign_on_counter;
}

void ResumptionDataDB::IncrementGlobalIgnOnCounter() {
  SDL_LOG_AUTO_TRACE();
  sync_primitives::AutoLock autolock(resumption_lock_);

  db_->BeginTransaction();
  utils::dbms::SQLQuery query_update_global_ign_on_count(db());
  if (query_update_global_ign_on_count.Prepare(kUpdateGlobalIgnOnCount)) {
    if (query_update_global_ign_on_count.Exec()) {
      SDL_LOG_DEBUG("Data query_update_global_ign_on_count was updated");
    }
  }
  db_->CommitTransaction();
  WriteDb();
}

void ResumptionDataDB::ResetGlobalIgnOnCount() {
  SDL_LOG_AUTO_TRACE();
  sync_primitives::AutoLock autolock(resumption_lock_);

  SDL_LOG_DEBUG("Global IGN ON counter resetting");

  utils::dbms::SQLQuery query_update_global_ign_on_count(db());
  if (query_update_global_ign_on_count.Prepare(kResetGlobalIgnOnCount)) {
    if (query_update_global_ign_on_count.Exec()) {
      SDL_LOG_DEBUG("Data was updated");
    }
  }
}

ssize_t ResumptionDataDB::IsApplicationSaved(
    const std::string& policy_app_id, const std::string& device_id) const {
  SDL_LOG_AUTO_TRACE();
  bool application_exist = false;
  if (CheckExistenceApplication(policy_app_id, device_id, application_exist) &&
      application_exist) {
    SDL_LOG_INFO("Application exists in stored data");
    return 0;
  }
  return -1;
}

void ResumptionDataDB::GetDataForLoadResumeData(
    smart_objects::SmartObject& saved_data) const {
  SDL_LOG_AUTO_TRACE();
  SelectDataForLoadResumeData(saved_data);
}

bool ResumptionDataDB::SelectHMILevel(const std::string& policy_app_id,
                                      const std::string& device_id,
                                      int& hmi_level) const {
  SDL_LOG_AUTO_TRACE();
  utils::dbms::SQLQuery query_count(db());
  utils::dbms::SQLQuery query_select(db());
  if (query_count.Prepare(kSelectCountHMILevel) &&
      query_select.Prepare(kSelectHMILevel)) {
    /* Positions of binding data for "query_count" and "query_select" :
       field "deviceID" from table "application" = 0
       field "appID" from table "application" = 1 */
    query_count.Bind(0, device_id);
    query_count.Bind(1, policy_app_id);
    query_select.Bind(0, device_id);
    query_select.Bind(1, policy_app_id);
    /* Position of data in "query_select" :
       field "hmiLevel" from table "application" = 0 */
    if (query_count.Exec() && query_count.GetInteger(0) &&
        query_select.Exec()) {
      hmi_level = query_select.GetInteger(0);
      return true;
    }
  }
  return false;
}

bool ResumptionDataDB::CheckExistenceHMIId(uint32_t hmi_app_id) const {
  SDL_LOG_AUTO_TRACE();

  utils::dbms::SQLQuery query(db());
  if (query.Prepare(kCheckHMIId)) {
    query.Bind(0, static_cast<int64_t>(hmi_app_id));
    if (query.Exec() && (query.GetInteger(0))) {
      SDL_LOG_INFO("Saved data has HMI appID = " << hmi_app_id);
      return true;
    }
  }
  SDL_LOG_FATAL("HMI appID = " << hmi_app_id << " doesn't exist in saved data");
  return false;
}

void ResumptionDataDB::SelectHMIId(const std::string& policy_app_id,
                                   const std::string& device_id,
                                   uint32_t& hmi_id) const {
  SDL_LOG_AUTO_TRACE();

  utils::dbms::SQLQuery query_select(db());
  utils::dbms::SQLQuery query_check(db());
  /* Positions of binding data for "query_select" and "query_check" :
     field "deviceID" from table "application" = 0
     field "appID" from table "application" = 1 */
  if (query_select.Prepare(kSelectHMIId) &&
      query_check.Prepare(kSelectCountHMIId)) {
    query_select.Bind(0, device_id);
    query_select.Bind(1, policy_app_id);
    query_check.Bind(0, device_id);
    query_check.Bind(1, policy_app_id);
    /* Position of data in "query_select" :
       field "hmiAppID" from table "application" = 0 */
    if (query_check.Exec() && query_check.GetInteger(0) &&
        query_select.Exec()) {
      hmi_id = query_select.GetUInteger(0);
      SDL_LOG_INFO("HMI appID = " << hmi_id);
      return;
    }
  }
  SDL_LOG_FATAL(
      "Saved data doesn't have application with "
      "device id = "
      << device_id << " and policy appID = " << policy_app_id);
}

bool ResumptionDataDB::SelectHashId(const std::string& policy_app_id,
                                    const std::string& device_id,
                                    std::string& hash_id) const {
  SDL_LOG_AUTO_TRACE();
  utils::dbms::SQLQuery count(db());
  utils::dbms::SQLQuery select_hash(db());
  if (!select_hash.Prepare(kSelectHashId) || !count.Prepare(kCountHashId)) {
    SDL_LOG_WARN(
        "Problem with verification count query or"
        " select_hash query");
    return false;
  }
  /* Positions of binding data for "count" and "select_hash" :
     field "deviceID" from table "application" = 0
     field "appID" from table "application" = 1 */
  count.Bind(0, device_id);
  count.Bind(1, policy_app_id);
  select_hash.Bind(0, device_id);
  select_hash.Bind(1, policy_app_id);
  /* Position of data in "select_hash" :
     field "hashID" from table "application" = 0 */
  if (count.Exec() && count.GetInteger(0) && select_hash.Exec()) {
    hash_id = select_hash.GetString(0);
    SDL_LOG_INFO("Saved hash ID = " << hash_id);
    return true;
  }
  SDL_LOG_WARN(
      "Saved data doesn't have application with "
      "device id = "
      << device_id << " and policy appID = " << policy_app_id << "or hashID");
  return false;
}

uint32_t ResumptionDataDB::SelectIgnOffTime() const {
  SDL_LOG_AUTO_TRACE();

  uint32_t ignOffTime = 0;
  utils::dbms::SQLQuery query(db());
  if (query.Prepare(kSelectIgnOffTime)) {
    if (query.Exec()) {
      ignOffTime = query.GetUInteger(0);
      SDL_LOG_INFO("Last ign off time = " << ignOffTime);
      return ignOffTime;
    }
  }
  SDL_LOG_ERROR("Problem with prepare query");
  return ignOffTime;
}

bool ResumptionDataDB::CheckExistenceApplication(
    const std::string& policy_app_id,
    const std::string& device_id,
    bool& application_exist) const {
  SDL_LOG_AUTO_TRACE();
  bool result = false;
  utils::dbms::SQLQuery query(db());
  /* Positions of binding data for "query":
     field "deviceID" from table "application" = 0
     field "appID" from table "application" = 1 */
  if (query.Prepare(kCheckApplication)) {
    query.Bind(0, device_id);
    query.Bind(1, policy_app_id);
    result = query.Exec();
  }
  /* Position of data in "query" :
     amount of application = 0 */
  if (result && query.GetInteger(0)) {
    SDL_LOG_INFO("Saved data has application with policy appID = "
                 << policy_app_id << " and deviceID = " << device_id);
    application_exist = true;
  } else if (result) {
    SDL_LOG_INFO("Saved data does not contain application");
    application_exist = false;
  } else {
    SDL_LOG_ERROR("Problem with access DB");
  }
  return result;
}

void ResumptionDataDB::SelectDataForLoadResumeData(
    smart_objects::SmartObject& saved_data) const {
  using namespace app_mngr;
  using namespace smart_objects;
  SDL_LOG_AUTO_TRACE();
  utils::dbms::SQLQuery select_data(db());
  utils::dbms::SQLQuery count_application(db());
  if (!select_data.Prepare(kSelectDataForLoadResumeData) ||
      !count_application.Prepare(kCountApplications)) {
    SDL_LOG_WARN(
        "Problem with verification select_data query"
        " or count application");
    return;
  }

  if (!count_application.Exec() || !count_application.GetInteger(0)) {
    SDL_LOG_WARN(
        "Problem with execution count_application query"
        " or appliction table does not contain data");
    return;
  }
  SmartObject so_array_data(SmartType_Array);
  uint32_t i = 0;
  /* Position of data in "select_data" :
     field "hmiLevel" from table "application" = 0
     field "ign_off_count" from table "application" = 1
     field "timeStamp" from table "application" = 2
     field "appID" from table "application" = 3
     field "deviceID" from table "application" = 4 */
  while (select_data.Next()) {
    SmartObject so_obj(SmartType_Map);
    so_obj[strings::hmi_level] = select_data.GetInteger(0);
    so_obj[strings::ign_off_count] = select_data.GetInteger(1);
    so_obj[strings::time_stamp] = select_data.GetUInteger(2);
    so_obj[strings::app_id] = select_data.GetString(3);
    so_obj[strings::device_id] = select_data.GetString(4);
    so_array_data[i++] = so_obj;
  }
  saved_data = so_array_data;
}

void ResumptionDataDB::UpdateHmiLevel(const std::string& policy_app_id,
                                      const std::string& device_id,
                                      mobile_apis::HMILevel::eType hmi_level) {
  SDL_LOG_AUTO_TRACE();

  utils::dbms::SQLQuery query(db());
  /* Positions of binding data for "query":
     field "hmiLevel" from table "application" = 0
     field "deviceID" from table "application" = 1
     field "appID" from table "application" = 2 */
  if (query.Prepare(kUpdateHMILevel)) {
    query.Bind(0, hmi_level);
    query.Bind(1, device_id);
    query.Bind(2, policy_app_id);
    if (query.Exec()) {
      SDL_LOG_INFO("Saved data has application with policy appID = "
                   << policy_app_id << " and deviceID = " << device_id
                   << " has new HMI level = " << hmi_level);
      WriteDb();
    }
  }
}

void ResumptionDataDB::Persist() {
  WriteDb();
}

bool ResumptionDataDB::RefreshDB() const {
  utils::dbms::SQLQuery query(db());
  if (!query.Exec(resumption::kDropSchema)) {
    SDL_LOG_WARN("Failed dropping database: " << query.LastError().text());
    return false;
  }
  if (!query.Exec(resumption::kCreateSchema)) {
    SDL_LOG_ERROR(
        "Failed creating schema of database: " << query.LastError().text());
    return false;
  }
  if (!query.Exec(resumption::kInsertInitData)) {
    SDL_LOG_ERROR(
        "Failed insert init data to database: " << query.LastError().text());
    return false;
  }
  return true;
}

bool ResumptionDataDB::GetAllData(smart_objects::SmartObject& data) const {
  SDL_LOG_AUTO_TRACE();
  utils::dbms::SQLQuery query(db());
  if (!query.Prepare(resumption::kSelectAllApps)) {
    SDL_LOG_ERROR("Can't get applications data from DB.");
    return false;
  }

  data = smart_objects::SmartObject(smart_objects::SmartType_Array);

  uint32_t index = 0;
  std::string app_id;
  std::string device_id;
  while (query.Next()) {
    app_id = query.GetString(0);
    device_id = query.GetString(1);
    if (GetSavedApplication(app_id, device_id, data[index])) {
      ++index;
    }
  }
  return true;
}

bool ResumptionDataDB::SaveAllData(const smart_objects::SmartObject& data) {
  SDL_LOG_AUTO_TRACE();
  if (smart_objects::SmartType_Array != data.getType()) {
    SDL_LOG_ERROR("Unexpected type for resumption data.");
    return false;
  }
  const smart_objects::SmartArray* apps = data.asArray();
  smart_objects::SmartArray::const_iterator it_apps = apps->begin();
  for (; apps->end() != it_apps; ++it_apps) {
    if (!SaveApplicationToDB((*it_apps),
                             (*it_apps)["appID"].asString(),
                             (*it_apps)["deviceID"].asString())) {
      return false;
    }
  }
  return true;
}

bool ResumptionDataDB::IsDBVersionActual() const {
  SDL_LOG_AUTO_TRACE();
  utils::dbms::SQLQuery query(db());
  if (!query.Prepare(resumption::kSelectDBVersion) || !query.Exec()) {
    SDL_LOG_ERROR("Failed to get DB version: " << query.LastError().text());
    return false;
  }

  const int32_t saved_db_version = query.GetInteger(0);
  const int32_t current_db_version = GetDBVersion();
  SDL_LOG_DEBUG("Saved DB version is: " << saved_db_version
                                        << ". Current DB vesion is: "
                                        << current_db_version);

  return current_db_version == saved_db_version;
}

bool ResumptionDataDB::UpdateDBVersion() const {
  SDL_LOG_AUTO_TRACE();
  utils::dbms::SQLQuery query(db());
  if (!query.Prepare(resumption::kUpdateDBVersion)) {
    SDL_LOG_ERROR(
        "Incorrect DB version update query: " << query.LastError().text());
    return false;
  }

  query.Bind(0, GetDBVersion());

  if (!query.Exec()) {
    SDL_LOG_ERROR("DB version update failed: " << query.LastError().text());
    return false;
  }

  return true;
}

bool ResumptionDataDB::DropAppDataResumption(const std::string& device_id,
                                             const std::string& app_id) {
  SDL_LOG_AUTO_TRACE();

  utils::ScopeGuard guard =
      utils::MakeObjGuard(*db_, &utils::dbms::SQLDatabase::RollbackTransaction);

  db_->BeginTransaction();
  if (!DeleteSavedFiles(app_id, device_id)) {
    return false;
  }
  if (!DeleteSavedSubMenu(app_id, device_id)) {
    return false;
  }
  if (!DeleteSavedSubscriptions(app_id, device_id)) {
    return false;
  }
  if (!DeleteSavedCommands(app_id, device_id)) {
    return false;
  }
  if (!DeleteSavedChoiceSet(app_id, device_id)) {
    return false;
  }
  if (!DeleteSavedGlobalProperties(app_id, device_id)) {
    return false;
  }
  if (!DeleteUserLocation(app_id, device_id)) {
    return false;
  }
  if (!UpdateGrammarID(app_id, device_id, 0)) {
    return false;
  }
  db_->CommitTransaction();

  guard.Dismiss();
  return true;
}

const int32_t ResumptionDataDB::GetDBVersion() const {
  return utils::Djb2HashFromString(resumption::kCreateSchema);
}

bool ResumptionDataDB::SelectFilesData(
    const std::string& policy_app_id,
    const std::string& device_id,
    smart_objects::SmartObject& saved_app) const {
  SDL_LOG_AUTO_TRACE();
  using namespace app_mngr;
  using namespace smart_objects;
  uint32_t count_item = 0;
  if (!SelectCountFromArray(
          count_item, kSelectCountFiles, policy_app_id, device_id)) {
    return false;
  }

  saved_app[strings::application_files] = SmartObject(SmartType_Array);

  if (0 == count_item) {
    SDL_LOG_INFO("Application does not contain files data");
    return true;
  }
  utils::dbms::SQLQuery select_files(db());
  if (!PrepareSelectQuery(
          select_files, policy_app_id, device_id, kSelectFiles)) {
    SDL_LOG_WARN("Problem with verification select_files");
    return false;
  }
  saved_app[strings::application_files] = SmartObject(SmartType_Array);
  /* Position of data in "select_files" :
     field "fileType" from table "file" = 0
     field "is_download_complete" from table "file" = 1
     field "persistentFile" from table "file" = 2
     field "syncFileName" from table "file" = 3*/
  uint32_t i = 0;
  while (select_files.Next()) {
    SmartObject array_item(SmartType_Map);
    array_item[strings::file_type] = select_files.GetInteger(0);
    array_item[strings::is_download_complete] = select_files.GetBoolean(1);
    array_item[strings::persistent_file] = select_files.GetBoolean(2);
    array_item[strings::sync_file_name] = select_files.GetString(3);
    saved_app[strings::application_files][i++] = array_item;
  }
  SDL_LOG_INFO("File data was restored successfully");
  return true;
}

bool ResumptionDataDB::SelectSubmenuData(
    const std::string& policy_app_id,
    const std::string& device_id,
    smart_objects::SmartObject& saved_app) const {
  SDL_LOG_AUTO_TRACE();
  using namespace app_mngr;
  using namespace smart_objects;
  uint32_t count_item = 0;
  if (!SelectCountFromArray(
          count_item, kSelectCountSubMenu, policy_app_id, device_id)) {
    return false;
  }

  saved_app[strings::application_submenus] = SmartObject(SmartType_Array);

  if (0 == count_item) {
    SDL_LOG_INFO("Application does not contain submenu data");
    return true;
  }
  utils::dbms::SQLQuery select_sub_menu(db());
  if (!PrepareSelectQuery(
          select_sub_menu, policy_app_id, device_id, kSelectSubMenu)) {
    SDL_LOG_WARN("Problem with verification select_sub_menu");
    return false;
  }
  saved_app[strings::application_submenus] = SmartObject(SmartType_Array);
  /* Position of data in "select_sub_menu" :
     field "menuID" from table "subMenu" = 0
     field "menuName" from table "subMenu" = 1
     field "position" from table "subMenu" = 2*/
  uint32_t i = 0;
  while (select_sub_menu.Next()) {
    SmartObject array_item(SmartType_Map);
    array_item[strings::menu_id] = select_sub_menu.GetInteger(0);
    array_item[strings::menu_name] = select_sub_menu.GetString(1);
    if (!(select_sub_menu.IsNull(2))) {
      array_item[strings::position] = select_sub_menu.GetInteger(2);
    }
    saved_app[strings::application_submenus][i++] = array_item;
  }
  SDL_LOG_INFO("Sub menu data was restored successfully");
  return true;
}

bool ResumptionDataDB::SelectCommandData(
    const std::string& policy_app_id,
    const std::string& device_id,
    smart_objects::SmartObject& saved_app) const {
  SDL_LOG_AUTO_TRACE();
  using namespace app_mngr;
  using namespace smart_objects;
  uint32_t count_item = 0;
  if (!SelectCountFromArray(
          count_item, kSelectCountCommands, policy_app_id, device_id)) {
    return false;
  }

  saved_app[strings::application_commands] = SmartObject(SmartType_Array);

  if (0 == count_item) {
    SDL_LOG_INFO("Application does not contain commands data");
    return true;
  }

  utils::dbms::SQLQuery select_commands(db());
  if (!PrepareSelectQuery(
          select_commands, policy_app_id, device_id, kSelectCommands)) {
    return false;
  }
  int64_t command_key = 0;
  int32_t command_idx = -1;
  size_t vr_cmd_idx = 0;
  bool vr_command_exist = false;
  /* Position of data in "select_commands" :
     field "idcommand" from table "command" = 0
     field "cmdID" from table "command" = 1
     field "menuName" from table "command" = 2
     field "parentID" from table "command" = 3
     field "position" from table "command" = 4
     field "value" from table "image" = 5
     field "imageType" from table "image" = 6
     field "vrCommand" from table "vrCommandsArray" = 7*/
  while (select_commands.Next()) {
    if (command_key != select_commands.GetLongInt(0)) {
      ++command_idx;
      saved_app[strings::application_commands][command_idx] =
          SmartObject(SmartType_Map);
      SmartObject& so_item =
          saved_app[strings::application_commands][command_idx];
      so_item[strings::cmd_id] = select_commands.GetInteger(1);
      SmartObject menu_params(SmartType_Map);
      SmartObject cmd_icon(SmartType_Map);
      if (!(select_commands.IsNull(2))) {
        menu_params[strings::menu_name] = select_commands.GetString(2);
      }
      if (!(select_commands.IsNull(3))) {
        menu_params[hmi_request::parent_id] = select_commands.GetInteger(3);
      }
      if (!(select_commands.IsNull(4))) {
        menu_params[strings::position] = select_commands.GetInteger(4);
      }
      if (!menu_params.empty()) {
        so_item[strings::menu_params] = menu_params;
      }
      if (!(select_commands.IsNull(5))) {
        cmd_icon[strings::value] = select_commands.GetString(5);
      }
      if (!(select_commands.IsNull(6))) {
        cmd_icon[strings::image_type] = select_commands.GetInteger(6);
      }
      if (!cmd_icon.empty()) {
        so_item[strings::cmd_icon] = cmd_icon;
      }
      if (!(select_commands.IsNull(7))) {
        vr_command_exist = true;
        so_item[strings::vr_commands] = SmartObject(SmartType_Array);
      } else {
        vr_command_exist = false;
      }
      vr_cmd_idx = 0;
      command_key = select_commands.GetLongInt(0);
    }
    if (vr_command_exist) {
      saved_app[strings::application_commands][command_idx]
               [strings::vr_commands][vr_cmd_idx++] =
                   select_commands.GetString(7);
    }
  }
  SDL_LOG_INFO("Commands were restored from DB successfully");
  return true;
}

bool ResumptionDataDB::SelectSubscriptionsData(
    const std::string& policy_app_id,
    const std::string& device_id,
    smart_objects::SmartObject& saved_app) const {
  SDL_LOG_AUTO_TRACE();
  using namespace app_mngr;
  using namespace smart_objects;
  uint32_t count_item = 0;
  if (!SelectCountFromArray(
          count_item, kSelectCountSubscriptions, policy_app_id, device_id)) {
    return false;
  }

  saved_app[strings::application_subscriptions] = SmartObject(SmartType_Map);

  if (0 == count_item) {
    SDL_LOG_INFO("Application does not contain subscriptions data");
    return true;
  }
  utils::dbms::SQLQuery select_subscriptions(db());
  if (!PrepareSelectQuery(select_subscriptions,
                          policy_app_id,
                          device_id,
                          kSelectSubscriptions)) {
    SDL_LOG_WARN("Problem with verification select_subscriptions");
    return false;
  }
  SmartObject application_buttons(SmartType_Array);
  SmartObject application_vehicle_info(SmartType_Array);
  size_t buttons_idx = 0;
  size_t vi_idx = 0;
  /* Position of data in "select_subscriptions" :
     field "vehicleValue" from table "applicationSubscriptionsArray" = 0
     field "ButtonNameValue" from table "applicationSubscriptionsArray" = 1*/
  while (select_subscriptions.Next()) {
    if (!select_subscriptions.IsNull(0)) {
      application_vehicle_info[vi_idx++] = select_subscriptions.GetInteger(0);
    }
    if (!select_subscriptions.IsNull(1)) {
      application_buttons[buttons_idx++] = select_subscriptions.GetInteger(1);
    }
  }
  if (!application_buttons.empty()) {
    saved_app[strings::application_subscriptions]
             [strings::application_buttons] = application_buttons;
  }

  if (!application_vehicle_info.empty()) {
    saved_app[strings::application_subscriptions]
             [strings::application_vehicle_info] = application_vehicle_info;
  }
  SDL_LOG_INFO("Subscriptions were restored from DB successfully");
  return true;
}

bool ResumptionDataDB::SelectUserLocationData(
    const std::string& policy_app_id,
    const std::string& device_id,
    smart_objects::SmartObject& saved_app) const {
  SDL_LOG_AUTO_TRACE();
  using namespace app_mngr;
  using namespace smart_objects;
  uint32_t count_item = 0;
  if (!SelectCountFromArray(
          count_item, kSelectCountUserLocation, policy_app_id, device_id)) {
    SDL_LOG_ERROR("Select query has been failed");
    return false;
  }

  if (0 == count_item) {
    SDL_LOG_DEBUG("Application does not contain user_location data");
    return true;
  }
  utils::dbms::SQLQuery select_user_location(db());
  if (!PrepareSelectQuery(select_user_location,
                          policy_app_id,
                          device_id,
                          kSelectUserLocation)) {
    SDL_LOG_ERROR("Failed to prepare user location select query");
    return false;
  }

  if (!select_user_location.Exec()) {
    SDL_LOG_ERROR("Failed to execute user location select query");
    return false;
  }
  /* Position of data in "select_user_location" :
     field "col"        from table "applicationUserLocation" = 0
     field "colspan"    from table "applicationUserLocation" = 1
     field "level"      from table "applicationUserLocation" = 2
     field "levelspan"  from table "applicationUserLocation" = 3
     field "row"        from table "applicationUserLocation" = 4
     field "rowspan"    from table "applicationUserLocation" = 5*/
  smart_objects::SmartObject grid =
      smart_objects::SmartObject(smart_objects::SmartType_Map);
  grid[rc_rpc_plugin::strings::kCol] = select_user_location.GetInteger(0);
  grid[rc_rpc_plugin::strings::kColspan] = select_user_location.GetInteger(1);
  grid[rc_rpc_plugin::strings::kLevel] = select_user_location.GetInteger(2);
  grid[rc_rpc_plugin::strings::kLevelspan] = select_user_location.GetInteger(3);
  grid[rc_rpc_plugin::strings::kRow] = select_user_location.GetInteger(4);
  grid[rc_rpc_plugin::strings::kRowspan] = select_user_location.GetInteger(5);

  saved_app[strings::user_location][rc_rpc_plugin::strings::kGrid] = grid;

  return true;
}

bool ResumptionDataDB::SelectChoiceSetData(
    const std::string& policy_app_id,
    const std::string& device_id,
    smart_objects::SmartObject& saved_app) const {
  SDL_LOG_AUTO_TRACE();
  using namespace app_mngr;
  using namespace smart_objects;
  uint32_t count_item = 0;
  if (!SelectCountFromArray(
          count_item, kSelectCountChoiceSet, policy_app_id, device_id)) {
    return false;
  }

  saved_app[strings::application_choice_sets] = SmartObject(SmartType_Array);

  if (0 == count_item) {
    SDL_LOG_INFO("Application does not contain choice set data");
    return true;
  }
  utils::dbms::SQLQuery select_choice_set(db());
  if (!PrepareSelectQuery(
          select_choice_set, policy_app_id, device_id, kSelectChoiceSets)) {
    SDL_LOG_WARN("Problem with verification select_choice_set");
    return false;
  }

  int64_t application_choice_set_key = 0;
  int64_t choice_key = 0;
  int32_t choice_set_idx = -1;
  int32_t choice_idx = -1;
  size_t vr_cmd_idx = 0;
  /* Position of data in "select_choice_set" :
     field "idapplicationChoiceSet" from table "applicationChoiceSet" = 0
     field "grammarID" from table "applicationChoiceSet" = 1
     field "interactionChoiceSetID" from table "applicationChoiceSet" = 2
     field "idchoice" from table "choice" = 3
     field "choiceID" from table "choice" = 4
     field "menuName" from table "choice" = 5
     field "secondaryText" from table "choice" = 6
     field "tertiaryText" from table "choice" = 7
     field "idimage" from table "choice" = 8
     field "idsecondaryImage" from table "choice" = 9
     field "vrCommand" from table "vrCommandsArray" = 10*/
  while (select_choice_set.Next()) {
    if (application_choice_set_key != select_choice_set.GetLongInt(0)) {
      ++choice_set_idx;
      saved_app[strings::application_choice_sets][choice_set_idx] =
          SmartObject(SmartType_Map);
      SmartObject& choice_set_item =
          saved_app[strings::application_choice_sets][choice_set_idx];
      choice_set_item[strings::grammar_id] = select_choice_set.GetInteger(1);
      choice_set_item[strings::interaction_choice_set_id] =
          select_choice_set.GetInteger(2);
      saved_app[strings::application_choice_sets][choice_set_idx]
               [strings::choice_set] = SmartObject(SmartType_Array);
      application_choice_set_key = select_choice_set.GetLongInt(0);
      choice_idx = -1;
    }
    if (choice_key != select_choice_set.GetLongInt(3)) {
      ++choice_idx;
      choice_key = select_choice_set.GetLongInt(3);

      saved_app[strings::application_choice_sets][choice_set_idx]
               [strings::choice_set][choice_idx] = SmartObject(SmartType_Map);
      SmartObject& choice_item =
          saved_app[strings::application_choice_sets][choice_set_idx]
                   [strings::choice_set][choice_idx];
      choice_item[strings::choice_id] = select_choice_set.GetUInteger(4);
      choice_item[strings::menu_name] = select_choice_set.GetString(5);
      if (!(select_choice_set.IsNull(6))) {
        choice_item[strings::secondary_text] = select_choice_set.GetString(6);
      }
      if (!(select_choice_set.IsNull(7))) {
        choice_item[strings::tertiary_text] = select_choice_set.GetString(7);
      }
      if (!(select_choice_set.IsNull(8))) {
        SmartObject image(SmartType_Map);
        if (!SelectImageData(select_choice_set.GetLongInt(8), image)) {
          return false;
        }
        choice_item[strings::image] = image;
      }
      if (!(select_choice_set.IsNull(9))) {
        SmartObject secondary_image(SmartType_Map);
        if (!SelectImageData(select_choice_set.GetLongInt(9),
                             secondary_image)) {
          return false;
        }
        choice_item[strings::secondary_image] = secondary_image;
      }
      choice_item[strings::vr_commands] = SmartObject(SmartType_Array);
      vr_cmd_idx = 0;
    }
    saved_app[strings::application_choice_sets][choice_set_idx]
             [strings::choice_set][choice_idx][strings::vr_commands]
             [vr_cmd_idx++] = select_choice_set.GetString(10);
  }

  SDL_LOG_INFO("Choice sets were restored from DB successfully");
  return true;
}

bool ResumptionDataDB::SelectGlobalPropertiesData(
    const std::string& policy_app_id,
    const std::string& device_id,
    smart_objects::SmartObject& saved_app) const {
  SDL_LOG_AUTO_TRACE();
  using namespace app_mngr;
  using namespace smart_objects;
  uint32_t count_item = 0;
  if (!SelectCountFromArray(
          count_item, kSelectCountGlobalProperties, policy_app_id, device_id)) {
    return false;
  }

  saved_app[strings::application_global_properties] =
      SmartObject(SmartType_Map);

  if (0 == count_item) {
    SDL_LOG_INFO("Application does not contain global properties data");
    return true;
  }
  utils::dbms::SQLQuery select_globalproperties(db());
  if (!PrepareSelectQuery(select_globalproperties,
                          policy_app_id,
                          device_id,
                          kSelectGlobalProperties)) {
    SDL_LOG_WARN("Problem with verification select_globalproperties");
    return false;
  }
  saved_app[strings::application_global_properties] =
      SmartObject(SmartType_Map);
  SmartObject& global_properties =
      saved_app[strings::application_global_properties];
  SmartObject keyboard_properties(SmartType_Map);
  SmartObject help_prompt(SmartType_Array);
  SmartObject timeout_prompt(SmartType_Array);
  size_t help_prompt_idx = 0;
  size_t timeout_prompt_idx = 0;
  int64_t global_properties_key = 0;
  /* Position of data in "select_globalproperties" :
     field "idglobalProperties" from table "globalProperties" = 0
     field "vrHelpTitle" from table "globalProperties" = 1
     field "menuTitle" from table "globalProperties" = 2
     field "idmenuIcon" from table "globalProperties" = 3
     field "language" from table "globalProperties" = 4
     field "keyboardLayout" from table "globalProperties" = 5
     field "keypressMode" from table "globalProperties" = 6
     field "autoCompleteText" from table "globalProperties" = 7
     field "idhelpPrompt" from table "helpTimeoutPromptArray" = 8
     field "idtimeoutPrompt" from table "helpTimeoutPromptArray" = 9*/
  while (select_globalproperties.Next()) {
    if (global_properties_key != select_globalproperties.GetLongInt(0)) {
      global_properties_key = select_globalproperties.GetLongInt(0);
      if (!select_globalproperties.IsNull(1)) {
        global_properties[strings::vr_help_title] =
            select_globalproperties.GetString(1);
      }
      if (!select_globalproperties.IsNull(2)) {
        global_properties[strings::menu_title] =
            select_globalproperties.GetString(2);
      }
      if (!select_globalproperties.IsNull(3)) {
        SmartObject image(SmartType_Map);
        if (!SelectImageData(select_globalproperties.GetLongInt(3), image)) {
          return false;
        }
        global_properties[strings::menu_icon] = image;
      }
      if (!select_globalproperties.IsNull(4)) {
        keyboard_properties[strings::language] =
            select_globalproperties.GetInteger(4);
      }
      if (!select_globalproperties.IsNull(5)) {
        keyboard_properties[hmi_request::keyboard_layout] =
            select_globalproperties.GetInteger(5);
      }
      if (!select_globalproperties.IsNull(6)) {
        keyboard_properties[strings::key_press_mode] =
            select_globalproperties.GetInteger(6);
      }
      if (!select_globalproperties.IsNull(7)) {
        keyboard_properties[strings::auto_complete_text] =
            select_globalproperties.GetString(7);
      }
    }
    if (!select_globalproperties.IsNull(8)) {
      SmartObject tts_chunk(SmartType_Map);
      if (!SelectTTSChunkData(select_globalproperties.GetLongInt(8),
                              tts_chunk)) {
        return false;
      }
      help_prompt[help_prompt_idx++] = tts_chunk;
    }
    if (!select_globalproperties.IsNull(9)) {
      SmartObject tts_chunk(SmartType_Map);
      if (!SelectTTSChunkData(select_globalproperties.GetLongInt(9),
                              tts_chunk)) {
        return false;
      }
      timeout_prompt[timeout_prompt_idx++] = tts_chunk;
    }
  }
  if (help_prompt_idx != 0) {
    global_properties[strings::help_prompt] = help_prompt;
  }
  if (timeout_prompt_idx != 0) {
    global_properties[strings::timeout_prompt] = timeout_prompt;
  }
  if (!keyboard_properties.empty()) {
    global_properties[strings::keyboard_properties] = keyboard_properties;
    if (!SelectCharactersData(
            global_properties_key,
            global_properties[strings::keyboard_properties])) {
      return false;
    }
  }
  if (!SelectVrHelpItemsData(global_properties_key, global_properties)) {
    return false;
  }
  return true;
}

bool ResumptionDataDB::SelectVrHelpItemsData(
    int64_t global_properties_key,
    smart_objects::SmartObject& global_properties) const {
  SDL_LOG_AUTO_TRACE();
  using namespace app_mngr;
  using namespace smart_objects;
  utils::dbms::SQLQuery checks_vrhelp_item(db());
  if (!checks_vrhelp_item.Prepare(kChecksVrHelpItem)) {
    SDL_LOG_WARN("Problem with verification checks_vrhelp_item query");
    return false;
  }
  checks_vrhelp_item.Bind(0, global_properties_key);
  if (!checks_vrhelp_item.Exec()) {
    SDL_LOG_WARN("Problem with execution checks_vrhelp_item query");
    return false;
  }
  if (0 == checks_vrhelp_item.GetInteger(0)) {
    SDL_LOG_INFO("Global properties doesn't contain vr help item");
    return true;
  }
  utils::dbms::SQLQuery select_vrhelp_item(db());
  if (!select_vrhelp_item.Prepare(kSelectVrHelpItem)) {
    SDL_LOG_WARN("Problem with verification select_vrhelp_item query");
    return false;
  }
  global_properties[strings::vr_help] = SmartObject(SmartType_Array);
  SmartObject& vr_help_items = global_properties[strings::vr_help];
  select_vrhelp_item.Bind(0, global_properties_key);
  size_t vr_help_item_idx = 0;
  /* Position of data in "select_vrhelp_item" :
     field "text" from table "vrHelpItem" = 0
     field "position" from table "vrHelpItem" = 1
     field "imageType" from table "image" = 2
     field "value" from table "image" = 3*/
  while (select_vrhelp_item.Next()) {
    SmartObject item(SmartType_Map);
    item[strings::text] = select_vrhelp_item.GetString(0);
    item[strings::position] = select_vrhelp_item.GetInteger(1);
    if (!select_vrhelp_item.IsNull(2) && !select_vrhelp_item.IsNull(3)) {
      SmartObject image(SmartType_Map);
      image[strings::image_type] = select_vrhelp_item.GetInteger(2);
      image[strings::value] = select_vrhelp_item.GetString(3);
      item[strings::image] = image;
    }
    vr_help_items[vr_help_item_idx++] = item;
  }
  SDL_LOG_INFO("VR Help items were restored successfully");
  return true;
}

bool ResumptionDataDB::SelectCharactersData(
    int64_t global_properties_key,
    smart_objects::SmartObject& keyboard_properties) const {
  SDL_LOG_AUTO_TRACE();
  using namespace app_mngr;
  using namespace smart_objects;
  utils::dbms::SQLQuery checks_characters(db());
  if (!checks_characters.Prepare(kChecksCharacter)) {
    SDL_LOG_WARN("Problem with verification checks_characters query");
    return false;
  }
  checks_characters.Bind(0, global_properties_key);
  if (!checks_characters.Exec()) {
    SDL_LOG_WARN("Problem with execution checks_characters query");
    return false;
  }
  if (0 == checks_characters.GetInteger(0)) {
    SDL_LOG_INFO(
        "Keyboard properties doesn't contain table limited character list");
    return true;
  }
  utils::dbms::SQLQuery select_characters(db());
  if (!select_characters.Prepare(kSelectCharacter)) {
    SDL_LOG_WARN("Problem with verification select_characters query");
    return false;
  }

  keyboard_properties[strings::limited_character_list] =
      SmartObject(SmartType_Array);
  SmartObject& characters =
      keyboard_properties[strings::limited_character_list];
  select_characters.Bind(0, global_properties_key);
  size_t characters_idx = 0;
  /* Position of data in "select_characters" :
     field "limitedCharacterList" from table "tableLimitedCharacterList" = 0*/
  while (select_characters.Next()) {
    characters[characters_idx++] = select_characters.GetString(0);
  }
  return true;
}

bool ResumptionDataDB::SelectImageData(
    int64_t image_key, smart_objects::SmartObject& image) const {
  SDL_LOG_AUTO_TRACE();
  using namespace app_mngr;
  utils::dbms::SQLQuery select_image(db());

  if (!select_image.Prepare(kSelectImage)) {
    SDL_LOG_WARN("Problem with verification select_image query");
    return false;
  }
  select_image.Bind(0, image_key);
  if (!select_image.Exec()) {
    SDL_LOG_WARN("Problem with execution select_image query");
    return false;
  }
  /* Position of data in "select_image" :
     field "imageType" from table "image" = 0
     field "value" from table "image" = 1*/
  image[strings::image_type] = select_image.GetInteger(0);
  image[strings::value] = select_image.GetString(1);
  return true;
}

bool ResumptionDataDB::SelectTTSChunkData(
    int64_t tts_chunk_key, smart_objects::SmartObject& tts_chunk) const {
  SDL_LOG_AUTO_TRACE();
  using namespace app_mngr;
  utils::dbms::SQLQuery select_tts_chunk(db());

  if (!select_tts_chunk.Prepare(kSelectTTSChunk)) {
    SDL_LOG_WARN("Problem with verification select_tts_chunk query");
    return false;
  }
  select_tts_chunk.Bind(0, tts_chunk_key);
  if (!select_tts_chunk.Exec()) {
    SDL_LOG_WARN("Problem with execution select_tts_chunk query");
    return false;
  }
  /* Position of data in "select_tts_chunk" :
     field "text" from table "TTSChunk" = 0
     field "type" from table "TTSChunk" = 1*/
  tts_chunk[strings::text] = select_tts_chunk.GetString(0);
  tts_chunk[strings::type] = select_tts_chunk.GetInteger(1);
  return true;
}

bool ResumptionDataDB::SelectDataFromAppTable(
    const std::string& policy_app_id,
    const std::string& device_id,
    smart_objects::SmartObject& saved_app) const {
  SDL_LOG_AUTO_TRACE();
  using namespace app_mngr;
  utils::dbms::SQLQuery query(db());
  if (!query.Prepare(kSelectAppTable)) {
    SDL_LOG_WARN("Problem with verification kSelectAppTable query");
    return false;
  }
  query.Bind(0, policy_app_id);
  query.Bind(1, device_id);
  if (!query.Exec()) {
    SDL_LOG_WARN("Problem with execution kSelectAppTable query");
    return false;
  }

  //  Position of data in "query" :
  //  field "appID" from table "application" = 0
  //  field "connection_key" from table "application" = 1
  //  field "grammarID" from table "application" = 2
  //  field "hashID" from table "application" = 3
  //  field "hmiAppID" from table "application" = 4
  //  field "hmiLevel" from table "application" = 5
  //  field "ign_off_count" from table "application" = 6
  //  field "timeStamp" from table "application" = 7
  //  field "deviceID" from table "application" = 8
  //  field "isMediaApplication" from table "application" = 9
  //  field "IsSubscribedForWayPoints" from table "application" = 10
  uint32_t connection_key = query.GetUInteger(1);

  saved_app[strings::app_id] = query.GetString(0);
  saved_app[strings::connection_key] = connection_key;
  uint32_t grammarID = query.GetUInteger(2);
  if (grammarID) {
    saved_app[strings::grammar_id] = grammarID;
  }
  saved_app[strings::hash_id] = query.GetString(3);
  saved_app[strings::hmi_app_id] = query.GetUInteger(4);
  saved_app[strings::hmi_level] = query.GetInteger(5);
  saved_app[strings::ign_off_count] = query.GetInteger(6);
  saved_app[strings::time_stamp] = query.GetUInteger(7);
  saved_app[strings::device_id] = query.GetString(8);
  saved_app[strings::is_media_application] = query.GetBoolean(9);
  saved_app[strings::subscribed_for_way_points] = query.GetBoolean(10);

  SDL_LOG_INFO("Data from application table was restored successfully");
  return true;
}

bool ResumptionDataDB::SelectCountFromArray(
    uint32_t& count_item,
    const std::string& text_query,
    const std::string& policy_app_id,
    const std::string& device_id) const {
  SDL_LOG_AUTO_TRACE();
  utils::dbms::SQLQuery query(db());
  if (!query.Prepare(text_query)) {
    SDL_LOG_WARN("Problem with verification query");
    return false;
  }
  query.Bind(0, policy_app_id);
  query.Bind(1, device_id);
  if (!query.Exec()) {
    SDL_LOG_WARN("Problem with execution query");
    return false;
  }
  count_item = query.GetInteger(0);
  SDL_LOG_INFO("count_item=" << count_item);
  return true;
}

bool ResumptionDataDB::DeleteSavedApplication(const std::string& policy_app_id,
                                              const std::string& device_id) {
  SDL_LOG_AUTO_TRACE();

  utils::ScopeGuard guard =
      utils::MakeObjGuard(*db_, &utils::dbms::SQLDatabase::RollbackTransaction);

  db_->BeginTransaction();
  if (!DeleteSavedFiles(policy_app_id, device_id)) {
    return false;
  }
  if (!DeleteSavedSubMenu(policy_app_id, device_id)) {
    return false;
  }
  if (!DeleteSavedSubscriptions(policy_app_id, device_id)) {
    return false;
  }
  if (!DeleteSavedCommands(policy_app_id, device_id)) {
    return false;
  }
  if (!DeleteSavedChoiceSet(policy_app_id, device_id)) {
    return false;
  }
  if (!DeleteSavedGlobalProperties(policy_app_id, device_id)) {
    return false;
  }
  if (!DeleteDataFromApplicationTable(policy_app_id, device_id)) {
    return false;
  }
  db_->CommitTransaction();

  guard.Dismiss();
  return true;
}

bool ResumptionDataDB::DeleteSavedFiles(const std::string& policy_app_id,
                                        const std::string& device_id) {
  SDL_LOG_AUTO_TRACE();
  if (!ExecQueryToDeleteData(policy_app_id, device_id, kDeleteFile)) {
    SDL_LOG_WARN("Incorrect delete from file.");
    return false;
  }

  if (!ExecQueryToDeleteData(
          policy_app_id, device_id, kDeleteApplicationFilesArray)) {
    SDL_LOG_WARN("Incorrect delete from applicationFilesArray.");
    return false;
  }
  return true;
}

bool ResumptionDataDB::DeleteSavedSubMenu(const std::string& policy_app_id,
                                          const std::string& device_id) {
  SDL_LOG_AUTO_TRACE();

  if (!ExecQueryToDeleteData(policy_app_id, device_id, kDeleteSubMenu)) {
    SDL_LOG_WARN("Incorrect delete from subMenu.");
    return false;
  }

  if (!ExecQueryToDeleteData(
          policy_app_id, device_id, kDeleteApplicationSubMenuArray)) {
    SDL_LOG_WARN("Incorrect delete from applicationSubMenuArray.");
    return false;
  }
  return true;
}

bool ResumptionDataDB::DeleteSavedSubscriptions(
    const std::string& policy_app_id, const std::string& device_id) {
  SDL_LOG_AUTO_TRACE();

  if (!ExecQueryToDeleteData(
          policy_app_id, device_id, kDeleteApplicationSubscriptionsArray)) {
    SDL_LOG_WARN("Incorrect delete from applicationSubscriptionsArray.");
    return false;
  }
  return true;
}

bool ResumptionDataDB::DeleteUserLocation(const std::string& policy_app_id,
                                          const std::string& device_id) {
  SDL_LOG_AUTO_TRACE();

  if (!ExecQueryToDeleteData(
          policy_app_id, device_id, kDeleteApplicationUserLocation)) {
    SDL_LOG_WARN("Incorrect delete from applicationUserLocation.");
    return false;
  }
  return true;
}

bool ResumptionDataDB::DeleteSavedCommands(const std::string& policy_app_id,
                                           const std::string& device_id) {
  SDL_LOG_AUTO_TRACE();

  if (!ExecQueryToDeleteData(
          policy_app_id, device_id, kDeleteImageFromCommands)) {
    SDL_LOG_WARN("Incorrect delete image from commands.");
    return false;
  }

  if (!ExecQueryToDeleteData(policy_app_id, device_id, kDeleteVrCommands)) {
    SDL_LOG_WARN("Incorrect delete vrcommands from commands.");
    return false;
  }

  if (!ExecQueryToDeleteData(policy_app_id, device_id, kDeleteCommands)) {
    SDL_LOG_WARN("Incorrect delete commands.");
    return false;
  }

  if (!ExecQueryToDeleteData(
          policy_app_id, device_id, kDeleteApplicationCommandsArray)) {
    SDL_LOG_WARN("Incorrect delete applicationCommandsArray.");
    return false;
  }

  return true;
}

bool ResumptionDataDB::DeleteSavedChoiceSet(const std::string& policy_app_id,
                                            const std::string& device_id) {
  SDL_LOG_AUTO_TRACE();

  if (!ExecUnionQueryToDeleteData(
          policy_app_id, device_id, kDeleteImageFromChoiceSet)) {
    SDL_LOG_WARN("Incorrect delete image from choice set");
    return false;
  }

  if (!ExecQueryToDeleteData(
          policy_app_id, device_id, kDeleteVrCommandsFromChoiceSet)) {
    SDL_LOG_WARN("Incorrect delete vrCommands from choice set");
    return false;
  }

  if (!ExecQueryToDeleteData(policy_app_id, device_id, kDeleteChoice)) {
    SDL_LOG_WARN("Incorrect delete choiceSet");
    return false;
  }

  if (!ExecQueryToDeleteData(policy_app_id, device_id, kDeleteChoiceArray)) {
    SDL_LOG_WARN("Incorrect delete from choiceArray");
    return false;
  }

  if (!ExecQueryToDeleteData(
          policy_app_id, device_id, kDeleteApplicationChoiceSet)) {
    SDL_LOG_WARN("Incorrect delete applicationChoiceSet");
    return false;
  }

  if (!ExecQueryToDeleteData(
          policy_app_id, device_id, kDeleteApplicationChoiceSetArray)) {
    SDL_LOG_WARN("Incorrect delete from ApplicationChoiceSetArray");
    return false;
  }

  return true;
}

bool ResumptionDataDB::DeleteSavedGlobalProperties(
    const std::string& policy_app_id, const std::string& device_id) {
  SDL_LOG_AUTO_TRACE();

  if (!ExecUnionQueryToDeleteData(
          policy_app_id, device_id, kDeleteImageFromGlobalProperties)) {
    SDL_LOG_WARN("Incorrect delete image from globalProperties");
    return false;
  }

  if (!ExecQueryToDeleteData(policy_app_id, device_id, kDeletevrHelpItem)) {
    SDL_LOG_WARN("Incorrect delete vrHelpItem");
    return false;
  }

  if (!ExecQueryToDeleteData(
          policy_app_id, device_id, kDeletevrHelpItemArray)) {
    SDL_LOG_WARN("Incorrect delete vrHelpItemArray");
    return false;
  }

  if (!ExecQueryToDeleteData(
          policy_app_id, device_id, kDeleteTableLimitedCharacterList)) {
    SDL_LOG_WARN("Incorrect delete from tableLimitedCharacterList");
    return false;
  }

  if (!ExecQueryToDeleteData(policy_app_id, device_id, kDeleteCharacterArray)) {
    SDL_LOG_WARN("Incorrect delete from characterArray");
    return false;
  }

  if (!ExecUnionQueryToDeleteData(policy_app_id, device_id, kDeleteTTSChunk)) {
    SDL_LOG_WARN("Incorrect delete from TTSChunk");
    return false;
  }

  if (!ExecQueryToDeleteData(
          policy_app_id, device_id, kDeleteHelpTimeoutPromptArray)) {
    SDL_LOG_WARN("Incorrect delete from HelpTimeoutPromptArray");
    return false;
  }

  if (!ExecQueryToDeleteData(
          policy_app_id, device_id, kDeleteGlobalProperties)) {
    SDL_LOG_WARN("Incorrect delete from GlobalProperties");
    return false;
  }

  return true;
}

bool ResumptionDataDB::DeleteDataFromApplicationTable(
    const std::string& policy_app_id, const std::string& device_id) {
  SDL_LOG_AUTO_TRACE();

  if (!ExecQueryToDeleteData(
          policy_app_id, device_id, kDeleteFromApplicationTable)) {
    SDL_LOG_WARN("Incorrect delete data from application table");
    return false;
  }

  return true;
}

bool ResumptionDataDB::ExecQueryToDeleteData(const std::string& policy_app_id,
                                             const std::string& device_id,
                                             const std::string& text_query) {
  SDL_LOG_AUTO_TRACE();
  utils::dbms::SQLQuery query(db());
  bool result = query.Prepare(text_query);
  if (result) {
    query.Bind(0, policy_app_id);
    query.Bind(1, device_id);
    result = query.Exec();
  }
  return result;
}

bool ResumptionDataDB::ExecUnionQueryToDeleteData(
    const std::string& policy_app_id,
    const std::string& device_id,
    const std::string& text_query) {
  SDL_LOG_AUTO_TRACE();
  utils::dbms::SQLQuery query(db());
  bool result = query.Prepare(text_query);
  if (result) {
    query.Bind(0, policy_app_id);
    query.Bind(1, device_id);
    query.Bind(2, policy_app_id);
    query.Bind(3, device_id);
    result = query.Exec();
  }
  return result;
}

bool ResumptionDataDB::ExecInsertImage(
    int64_t& image_primary_key, const smart_objects::SmartObject& image) const {
  SDL_LOG_AUTO_TRACE();
  using namespace app_mngr;
  utils::dbms::SQLQuery count_image_query(db());
  utils::dbms::SQLQuery query(db());
  uint16_t count_image = 0;
  bool result = count_image_query.Prepare(kSelectCountImage);
  if (result) {
    count_image_query.Bind(0, image[strings::value].asString());
    result = count_image_query.Exec();
    if (result) {
      count_image = count_image_query.GetInteger(0);
    }
  }
  if (!result) {
    SDL_LOG_WARN("Problem with preparing or execution count_image_query.");
    return false;
  }
  if (count_image) {
    result = query.Prepare(kSelectPrimaryKeyImage);
    if (result) {
      query.Bind(0, image[strings::value].asString());
      result = query.Exec();
      if (result) {
        image_primary_key = query.GetLongInt(0);
      }
    }
    if (!result) {
      SDL_LOG_WARN(
          "Problem with preparing or execution "
          "query for select primary key of image");
    }
  } else {
    result = query.Prepare(kInsertImage);
    if (result) {
      query.Bind(0, image[strings::image_type].asInt());
      query.Bind(1, image[strings::value].asString());
      result = query.Exec();
      if (result) {
        image_primary_key = query.LastInsertId();
      }
    }
    if (!result) {
      SDL_LOG_WARN(
          "Problem with preparing or execution "
          "query for insert image to image table");
    }
  }
  return result;
}

bool ResumptionDataDB::ExecInsertChoice(
    int64_t choice_set_key,
    const smart_objects::SmartObject& choice_array) const {
  SDL_LOG_AUTO_TRACE();
  using namespace app_mngr;
  utils::dbms::SQLQuery insert_choice(db());

  if (!insert_choice.Prepare(kInsertChoice)) {
    SDL_LOG_WARN("Incorrect preparation insert_choice query");
    return false;
  }
  /* Positions of binding data for "insert_choice":
     field "choiceID" from table "choice" = 0
     field "menuName" from table "choice" = 1
     field "secondaryText" from table "choice" = 2
     field "tertiaryText" from table "choice" = 3
     field "idimage" from table "choice" = 4
     field "idsecondaryImage" from table "choice" = 5*/
  int64_t image_primary_key = 0;
  size_t length_choice_array = choice_array.length();
  for (size_t i = 0; i < length_choice_array; ++i) {
    insert_choice.Bind(0, (choice_array[i][strings::choice_id]).asInt());
    insert_choice.Bind(1, (choice_array[i][strings::menu_name]).asString());

    CustomBind(strings::secondary_text, choice_array[i], insert_choice, 2);
    CustomBind(strings::tertiary_text, choice_array[i], insert_choice, 3);

    if (choice_array[i].keyExists(strings::image)) {
      if (!ExecInsertImage(image_primary_key,
                           choice_array[i][strings::image])) {
        SDL_LOG_WARN("Problem with insert image to choice");
        return false;
      }
      insert_choice.Bind(4, image_primary_key);
    } else {
      insert_choice.Bind(4);
    }
    if (choice_array[i].keyExists(strings::secondary_image)) {
      if (!ExecInsertImage(image_primary_key,
                           choice_array[i][strings::secondary_image])) {
        SDL_LOG_WARN("Problem with insert secondary_image to choice");
        return false;
      }
      insert_choice.Bind(5, image_primary_key);
    } else {
      insert_choice.Bind(5);
    }
    if (!insert_choice.Exec()) {
      SDL_LOG_WARN("Problem with execution insert_choice query");
      return false;
    }
    int64_t choice_primary_key = insert_choice.LastInsertId();

    if ((!ExecInsertVrCommands(choice_primary_key,
                               choice_array[i][strings::vr_commands],
                               kVRCommandFromChoice)) ||
        !insert_choice.Reset()) {
      SDL_LOG_WARN("problemm with add vr commands to choice");
      return false;
    }

    if (!ExecInsertDataToArray(
            choice_set_key, choice_primary_key, kInsertChoiceArray)) {
      SDL_LOG_INFO("Problem with insertion data to choiceArray table");
      return false;
    }
  }
  SDL_LOG_INFO("Choice data were saved to DB successfully");
  return true;
}

bool ResumptionDataDB::ExecInsertVrCommands(
    const int64_t primary_key,
    const smart_objects::SmartObject& vr_commands_array,
    AccessoryVRCommand value) const {
  SDL_LOG_AUTO_TRACE();
  utils::dbms::SQLQuery insert_vr_command(db());

  if (!insert_vr_command.Prepare(kInsertVrCommand)) {
    SDL_LOG_WARN("Incorrect preparation insert_vr_command query");
    return false;
  }
  size_t length_vr_commands = vr_commands_array.length();

  /* Positions of binding data for "insert_vr_command":
     field "vrCommand" from table "vrCommandsArray" = 0
     field "idcommand" from table "vrCommandsArray" = 1
     field "idchoice" from table "vrCommandsArray" = 2*/
  for (size_t i = 0; i < length_vr_commands; ++i) {
    insert_vr_command.Bind(0, vr_commands_array[i].asString());
    if (AccessoryVRCommand::kVRCommandFromCommand == value) {
      insert_vr_command.Bind(1, primary_key);
      insert_vr_command.Bind(2);
    } else if (AccessoryVRCommand::kVRCommandFromChoice == value) {
      insert_vr_command.Bind(1);
      insert_vr_command.Bind(2, primary_key);
    }
    if (!insert_vr_command.Exec() || !insert_vr_command.Reset()) {
      SDL_LOG_WARN("Problem with insert vr_command to DB");
      return false;
    }
  }
  SDL_LOG_INFO("Insertion of Vr command were executed successfully");
  return true;
}

bool ResumptionDataDB::ExecInsertDataToArray(
    int64_t first_primary_key,
    int64_t second_primary_key,
    const std::string& text_query) const {
  SDL_LOG_AUTO_TRACE();
  bool result;
  utils::dbms::SQLQuery query_insert_array(db());
  result = query_insert_array.Prepare(text_query);
  if (result) {
    query_insert_array.Bind(0, first_primary_key);
    query_insert_array.Bind(1, second_primary_key);
    result = query_insert_array.Exec();
  }
  return result;
}

bool ResumptionDataDB::SaveApplicationToDB(
    app_mngr::ApplicationSharedPtr application,
    const std::string& policy_app_id,
    const std::string& device_id) const {
  SDL_LOG_AUTO_TRACE();
  int64_t application_primary_key = 0;
  int64_t global_properties_key = 0;
  db_->BeginTransaction();
  if (!InsertGlobalPropertiesData(GetApplicationGlobalProperties(application),
                                  global_properties_key)) {
    SDL_LOG_WARN("Incorrect insert globalProperties data to DB.");
    db_->RollbackTransaction();
    return false;
  }
  ApplicationParams app(application);
  if (!InsertApplicationData(app,
                             policy_app_id,
                             device_id,
                             &application_primary_key,
                             global_properties_key)) {
    SDL_LOG_WARN("Incorrect insert application data to DB.");
    db_->RollbackTransaction();
    return false;
  }
  if (!InsertFilesData(GetApplicationFiles(application),
                       application_primary_key)) {
    SDL_LOG_WARN("Incorrect insert file data to DB.");
    db_->RollbackTransaction();
    return false;
  }

  if (!InsertSubMenuData(GetApplicationSubMenus(application),
                         application_primary_key)) {
    SDL_LOG_WARN("Incorrect insert submenu data to DB.");
    db_->RollbackTransaction();
    return false;
  }
  if (!InsertCommandsData(GetApplicationCommands(application),
                          application_primary_key)) {
    SDL_LOG_WARN("Incorrect insert commands data to DB.");
    db_->RollbackTransaction();
    return false;
  }
  if (!InsertSubscriptionsData(GetApplicationSubscriptions(application),
                               application_primary_key)) {
    SDL_LOG_WARN("Incorrect insert subscriptions data to DB.");
    db_->RollbackTransaction();
    return false;
  }
  if (!InsertChoiceSetData(GetApplicationInteractionChoiseSets(application),
                           application_primary_key)) {
    SDL_LOG_WARN("Incorrect insert choiceset data to DB.");
    db_->RollbackTransaction();
    return false;
  }
  if (!InsertUserLocationData(application->get_user_location(),
                              application_primary_key)) {
    SDL_LOG_WARN("Incorrect insert user location to DB.");
    db_->RollbackTransaction();
    return false;
  }
  db_->CommitTransaction();
  return true;
}

bool ResumptionDataDB::SaveApplicationToDB(
    const smart_objects::SmartObject& application,
    const std::string& policy_app_id,
    const std::string& device_id) const {
  SDL_LOG_AUTO_TRACE();

  int64_t application_primary_key = 0;
  int64_t global_properties_key = 0;
  db_->BeginTransaction();
  if (!InsertGlobalPropertiesData(application["globalProperties"],
                                  global_properties_key)) {
    SDL_LOG_WARN("Incorrect insert globalProperties data to DB.");
    db_->RollbackTransaction();
    return false;
  }
  if (!InsertApplicationData(application,
                             policy_app_id,
                             device_id,
                             &application_primary_key,
                             global_properties_key)) {
    SDL_LOG_WARN("Incorrect insert application data to DB.");
    db_->RollbackTransaction();
    return false;
  }
  if (!InsertFilesData(application["applicationFiles"],
                       application_primary_key)) {
    SDL_LOG_WARN("Incorrect insert file data to DB.");
    db_->RollbackTransaction();
    return false;
  }

  if (!InsertSubMenuData(application["applicationSubMenus"],
                         application_primary_key)) {
    SDL_LOG_WARN("Incorrect insert submenu data to DB.");
    db_->RollbackTransaction();
    return false;
  }
  if (!InsertCommandsData(application["applicationCommands"],
                          application_primary_key)) {
    SDL_LOG_WARN("Incorrect insert commands data to DB.");
    db_->RollbackTransaction();
    return false;
  }
  if (!InsertSubscriptionsData(application["subscriptions"],
                               application_primary_key)) {
    SDL_LOG_WARN("Incorrect insert subscriptions data to DB.");
    db_->RollbackTransaction();
    return false;
  }
  if (!InsertChoiceSetData(application["applicationChoiceSets"],
                           application_primary_key)) {
    SDL_LOG_WARN("Incorrect insert choiceset data to DB.");
    db_->RollbackTransaction();
    return false;
  }
  if (!InsertUserLocationData(application["userLocation"],
                              application_primary_key)) {
    SDL_LOG_WARN("Incorrect insert userLocation to DB.");
    db_->RollbackTransaction();
    return false;
  }
  db_->CommitTransaction();
  return true;
}

bool ResumptionDataDB::InsertFilesData(const smart_objects::SmartObject& files,
                                       int64_t application_primary_key) const {
  SDL_LOG_AUTO_TRACE();
  using namespace app_mngr;
  using namespace smart_objects;
  const size_t length_files_array = files.length();
  if (0 == length_files_array) {
    SDL_LOG_INFO("Application doesn't contain files");
    return true;
  }

  utils::dbms::SQLQuery query_insert_file(db());

  if (!query_insert_file.Prepare(kInsertToFile)) {
    SDL_LOG_WARN("Problem with verification queries for insertion files");
    return false;
  }
  /* Positions of binding data for "query_insert_file":
     field "fileType" from table "file" = 0
     field "is_download_complete" from table "file" = 1
     field "persistentFile" from table "file" = 2
     field "syncFileName" from table "file" = 3*/
  for (size_t i = 0; i < length_files_array; ++i) {
    query_insert_file.Bind(0, (files[i][strings::file_type]).asInt());
    query_insert_file.Bind(1,
                           (files[i][strings::is_download_complete]).asBool());
    query_insert_file.Bind(2, (files[i][strings::persistent_file]).asBool());
    query_insert_file.Bind(3, (files[i][strings::sync_file_name]).asString());

    if (!query_insert_file.Exec()) {
      SDL_LOG_WARN("Incorrect insertion of files data");
      return false;
    }

    if ((!ExecInsertDataToArray(application_primary_key,
                                query_insert_file.LastInsertId(),
                                kInsertToApplicationFilesArray)) ||
        !query_insert_file.Reset()) {
      SDL_LOG_WARN("Incorrect insertion to application files array");
      return false;
    }
  }

  SDL_LOG_INFO("Files data were inserted successfully to DB");
  return true;
}

bool ResumptionDataDB::InsertSubMenuData(
    const smart_objects::SmartObject& submenus,
    int64_t application_primary_key) const {
  SDL_LOG_AUTO_TRACE();
  using namespace app_mngr;
  using namespace smart_objects;
  const size_t length_submenu_array = submenus.length();
  if (0 == length_submenu_array) {
    SDL_LOG_INFO("Application doesn't contain submenu");
    return true;
  }
  utils::dbms::SQLQuery query_insert_submenu(db());

  if (!query_insert_submenu.Prepare(kInsertToSubMenu)) {
    SDL_LOG_WARN("Problem with verification queries for insertion submenu");
    return false;
  }
  /* Positions of binding data for "query_insert_submenu":
     field "menuID" from table "submenu" = 0
     field "menuName" from table "submenu" = 1
     field "position" from table "submenu" = 2*/
  for (size_t i = 0; i < length_submenu_array; ++i) {
    query_insert_submenu.Bind(0, (submenus[i][strings::menu_id]).asInt());
    query_insert_submenu.Bind(1, (submenus[i][strings::menu_name]).asString());
    CustomBind(strings::position, submenus[i], query_insert_submenu, 2);

    if (!query_insert_submenu.Exec()) {
      SDL_LOG_WARN("Incorrect insertion of submenu data");
      return false;
    }

    if ((!ExecInsertDataToArray(application_primary_key,
                                query_insert_submenu.LastInsertId(),
                                kInsertToApplicationSubMenuArray)) ||
        !query_insert_submenu.Reset()) {
      SDL_LOG_WARN("Incorrect insertion to application submenu array");
      return false;
    }
  }

  SDL_LOG_INFO("Data about submenu were inserted successfully to DB");
  return true;
}

bool ResumptionDataDB::InsertCommandsData(
    const smart_objects::SmartObject& commands,
    int64_t application_primary_key) const {
  SDL_LOG_AUTO_TRACE();
  using namespace app_mngr;
  using namespace smart_objects;
  const size_t length_command_array = commands.length();
  if (0 == length_command_array) {
    SDL_LOG_INFO("Application doesn't contain command");
    return true;
  }
  utils::dbms::SQLQuery query_insert_command(db());
  int64_t image_primary_key = 0;

  if (!query_insert_command.Prepare(kInsertToCommand)) {
    SDL_LOG_WARN("Problem with verification queries for insertion commands");
    return false;
  }
  /* Positions of binding data for "query_insert_command":
     field "cmdID" from table "command" = 0
     field "idimage" from table "command" = 1
     field "menuName" from table "command" = 2
     field "parentID" from table "command" = 3
     field "position" from table "command" = 4*/
  for (size_t i = 0; i < length_command_array; ++i) {
    query_insert_command.Bind(0, commands[i][strings::cmd_id].asInt());
    if (commands[i].keyExists(strings::cmd_icon)) {
      if (!ExecInsertImage(image_primary_key, commands[i][strings::cmd_icon])) {
        SDL_LOG_WARN("Problem with insert command image to DB");
        return false;
      }
      query_insert_command.Bind(1, image_primary_key);
    } else {
      query_insert_command.Bind(1);
    }

    if (commands[i].keyExists(strings::menu_params)) {
      const SmartObject& menu_params = commands[i][strings::menu_params];
      query_insert_command.Bind(2, menu_params[strings::menu_name].asString());

      CustomBind(hmi_request::parent_id, menu_params, query_insert_command, 3);
      CustomBind(strings::position, menu_params, query_insert_command, 4);
    } else {
      query_insert_command.Bind(2);
      query_insert_command.Bind(3);
      query_insert_command.Bind(4);
    }
    if (!query_insert_command.Exec()) {
      SDL_LOG_WARN("Incorrect insertion of command data to DB");
      return false;
    }
    int64_t command_primary_key = query_insert_command.LastInsertId();
    if (commands[i].keyExists(strings::vr_commands)) {
      if (!ExecInsertVrCommands(command_primary_key,
                                commands[i][strings::vr_commands],
                                kVRCommandFromCommand)) {
        return false;
      }
    }
    if ((!ExecInsertDataToArray(application_primary_key,
                                command_primary_key,
                                kInsertApplicationCommandArray)) ||
        !query_insert_command.Reset()) {
      SDL_LOG_WARN("Incorrect insertion to application commands array");
      return false;
    }
  }
  return true;
}

bool ResumptionDataDB::InsertSubscriptionsData(
    const smart_objects::SmartObject& subscriptions,
    int64_t application_primary_key) const {
  SDL_LOG_AUTO_TRACE();
  using namespace app_mngr;
  using namespace smart_objects;

  if (subscriptions.empty()) {
    SDL_LOG_INFO("Application doesn't contain subscriptions");
    return true;
  }
  const SmartObject& btn_sub = subscriptions[strings::application_buttons];
  const SmartObject& vi_sub = subscriptions[strings::application_vehicle_info];
  size_t btn_sub_length = btn_sub.length();
  size_t vi_sub_length = vi_sub.length();
  size_t max_length =
      (btn_sub_length > vi_sub_length) ? btn_sub_length : vi_sub_length;

  utils::dbms::SQLQuery insert_subscriptions(db());
  if (!insert_subscriptions.Prepare(kInsertSubscriptions)) {
    SDL_LOG_WARN(
        "Problem with verification queries for insertion subscriptions");
    return false;
  }
  /* Positions of binding data for "insert_subscriptions":
       field "idApplication" from table "applicationSubscriptionsArray" = 0
       field "vehicleValue" from table "applicationSubscriptionsArray" = 1
       field "ButtonNameValue" from table "applicationSubscriptionsArray" = 2*/
  for (size_t i = 0; i < max_length; ++i) {
    insert_subscriptions.Bind(0, application_primary_key);
    if (i < vi_sub_length) {
      insert_subscriptions.Bind(1, vi_sub[i].asInt());
    } else {
      insert_subscriptions.Bind(1);
    }
    if (i < btn_sub_length) {
      insert_subscriptions.Bind(2, btn_sub[i].asInt());
    } else {
      insert_subscriptions.Bind(2);
    }
    if (!insert_subscriptions.Exec() || !insert_subscriptions.Reset()) {
      SDL_LOG_WARN("Incorrect insertion of buttons to subscriptions");
      return false;
    }
  }
  SDL_LOG_INFO("Subscriptions data were saved successfully");
  return true;
}

bool ResumptionDataDB::InsertChoiceSetData(
    const smart_objects::SmartObject& choicesets,
    int64_t application_primary_key) const {
  SDL_LOG_AUTO_TRACE();
  using namespace app_mngr;
  using namespace smart_objects;

  if (choicesets.empty()) {
    SDL_LOG_INFO("Application doesn't contain choiceSet");
    return true;
  }
  int64_t choice_set_key = 0;
  size_t length_choceset_array = choicesets.length();
  for (size_t i = 0; i < length_choceset_array; ++i) {
    if (!ExecInsertApplicationChoiceSet(choice_set_key, choicesets[i])) {
      return false;
    }

    if (!ExecInsertChoice(choice_set_key, choicesets[i][strings::choice_set])) {
      return false;
    }

    if (!ExecInsertDataToArray(choice_set_key,
                               application_primary_key,
                               kInsertApplicationChoiceSetArray)) {
      SDL_LOG_WARN(
          "Problem with insertion data to"
          " applicationChoiceSetArray table");
      return false;
    }
  }
  SDL_LOG_INFO("Choice set data were saved to DB successfully");
  return true;
}

bool ResumptionDataDB::InsertUserLocationData(
    const smart_objects::SmartObject& user_location,
    int64_t application_primary_key) const {
  SDL_LOG_AUTO_TRACE();
  using namespace app_mngr;
  using namespace smart_objects;

  if (user_location.empty()) {
    SDL_LOG_DEBUG("Application doesn't contain user location");
    return true;
  }

  const auto grid = user_location[rc_rpc_plugin::strings::kGrid];
  const int32_t col = grid[rc_rpc_plugin::strings::kCol].asInt();
  const int32_t row = grid[rc_rpc_plugin::strings::kRow].asInt();
  const int32_t level = grid[rc_rpc_plugin::strings::kLevel].asInt();
  const int32_t colspan = grid[rc_rpc_plugin::strings::kColspan].asInt();
  const int32_t rowspan = grid[rc_rpc_plugin::strings::kRowspan].asInt();
  const int32_t levelspan = grid[rc_rpc_plugin::strings::kLevelspan].asInt();

  utils::dbms::SQLQuery insert_application_user_location(db());
  if (!insert_application_user_location.Prepare(kInsertUserLocation)) {
    SDL_LOG_WARN(
        "Problem with preparation insert "
        "application user location query");
    return false;
  }

  /* Positions of binding data for "insert_application_user_location":
    field "idApplication" from table "applicationUserLocation" = 0
    field "col" from table "applicationUserLocation" = 1
    field "colspan" from table "applicationUserLocation" = 2
    field "level" from table "applicationUserLocation" = 3
    field "levelspan" from table "applicationUserLocation" = 4
    field "row" from table "applicationUserLocation" = 5
    field "rowspan" from table "applicationUserLocation" = 6*/
  insert_application_user_location.Bind(0, application_primary_key);
  insert_application_user_location.Bind(1, col);
  insert_application_user_location.Bind(2, colspan);
  insert_application_user_location.Bind(3, level);
  insert_application_user_location.Bind(4, levelspan);
  insert_application_user_location.Bind(5, row);
  insert_application_user_location.Bind(6, rowspan);

  if (!insert_application_user_location.Exec()) {
    SDL_LOG_WARN("Incorrect insertion of user location");
    return false;
  }

  return true;
}

bool ResumptionDataDB::ExecInsertApplicationChoiceSet(
    int64_t& choice_set_primary_key,
    const smart_objects::SmartObject& choiceset) const {
  SDL_LOG_AUTO_TRACE();
  using namespace app_mngr;

  utils::dbms::SQLQuery insert_application_choice_set(db());
  if (!insert_application_choice_set.Prepare(kInsertApplicationChoiceSet)) {
    SDL_LOG_WARN(
        "Problem with preparation insert "
        "application choice set query");
    return false;
  }
  /* Positions of binding data for "insert_application_choice_set":
     field "grammarID" from table "applicationChoiceSet" = 0
     field "interactionChoiceSetID" from table "applicationChoiceSet" = 1*/
  insert_application_choice_set.Bind(
      0, static_cast<int64_t>(choiceset[strings::grammar_id].asUInt()));
  insert_application_choice_set.Bind(
      1, choiceset[strings::interaction_choice_set_id].asInt());

  if (!insert_application_choice_set.Exec()) {
    SDL_LOG_WARN("Problem with execution insert application choice set query");
    return false;
  }
  choice_set_primary_key = insert_application_choice_set.LastInsertId();
  SDL_LOG_INFO("Application choice data were saved successfully");
  return true;
}

bool ResumptionDataDB::InsertGlobalPropertiesData(
    const smart_objects::SmartObject& global_properties,
    int64_t& global_properties_key) const {
  SDL_LOG_AUTO_TRACE();
  using namespace app_mngr;
  using namespace smart_objects;
  SmartMap::iterator it_begin = global_properties.map_begin();
  SmartMap::iterator it_end = global_properties.map_end();
  bool data_exists = false;
  while (it_begin != it_end) {
    if (SmartType::SmartType_Null != ((it_begin->second).getType())) {
      SDL_LOG_INFO("Global properties contains - " << it_begin->first);
      data_exists = true;
      break;
    }
    ++it_begin;
  }
  if (!data_exists) {
    SDL_LOG_INFO("Application doesn't contain global properties");
    return true;
  }

  utils::dbms::SQLQuery insert_global_properties(db());
  if (!insert_global_properties.Prepare(kInsertGlobalProperties)) {
    SDL_LOG_WARN(
        "Problem with preparation query "
        "insert_global_properties");
    return false;
  }
  /* Positions of binding data for "insert_global_properties":
     field "vrHelpTitle" from table "globalProperties" = 0
     field "menuTitle" from table "globalProperties" = 1
     field "idmenuIcon" from table "globalProperties" = 2
     field "language" from table "globalProperties" = 3
     field "keyboardLayout" from table "globalProperties" = 4
     field "keypressMode" from table "globalProperties" = 5
     field "autoCompleteText" from table "globalProperties" = 6*/

  CustomBind(
      strings::vr_help_title, global_properties, insert_global_properties, 0);
  CustomBind(
      strings::menu_title, global_properties, insert_global_properties, 1);

  if (SmartType::SmartType_Null ==
      global_properties[strings::menu_icon].getType()) {
    insert_global_properties.Bind(2);
  } else {
    int64_t image_key = 0;
    if (ExecInsertImage(image_key, global_properties[strings::menu_icon])) {
      insert_global_properties.Bind(2, image_key);
    } else {
      SDL_LOG_WARN("Problem with insert image to global properties");
      return false;
    }
  }

  if (SmartType::SmartType_Null ==
      global_properties[strings::keyboard_properties].getType()) {
    insert_global_properties.Bind(3);
    insert_global_properties.Bind(4);
    insert_global_properties.Bind(5);
    insert_global_properties.Bind(6);
  } else {
    const SmartObject& kb_prop =
        global_properties[strings::keyboard_properties];

    CustomBind(strings::language, kb_prop, insert_global_properties, 3);
    CustomBind(
        hmi_request::keyboard_layout, kb_prop, insert_global_properties, 4);
    CustomBind(strings::key_press_mode, kb_prop, insert_global_properties, 5);
    CustomBind(
        strings::auto_complete_text, kb_prop, insert_global_properties, 6);
  }
  if (!insert_global_properties.Exec()) {
    SDL_LOG_WARN("Problem with insert data to global properties table");
    return false;
  }

  global_properties_key = insert_global_properties.LastInsertId();
  if ((SmartType::SmartType_Null !=
       global_properties[strings::keyboard_properties].getType()) &&
      (global_properties[strings::keyboard_properties].keyExists(
          strings::limited_character_list))) {
    if (!ExecInsertLimitedCharacters(
            global_properties_key,
            global_properties[strings::keyboard_properties]
                             [strings::limited_character_list])) {
      SDL_LOG_WARN("Problem with insert data to limited_character table");
      return false;
    }
  }

  if (SmartType::SmartType_Null !=
      global_properties[strings::vr_help].getType()) {
    if (!ExecInsertVRHelpItem(global_properties_key,
                              global_properties[strings::vr_help])) {
      SDL_LOG_WARN("Problem with insert data to vrHelpItem table");
      return false;
    }
  }

  if (!ExecInsertHelpTimeoutArray(global_properties, global_properties_key)) {
    SDL_LOG_WARN("Problem with insert data to HelpTimeoutPromptArray table");
    return false;
  }

  SDL_LOG_INFO("Global properties data were saved successfully");
  return true;
}

bool ResumptionDataDB::ExecInsertHelpTimeoutArray(
    const smart_objects::SmartObject& global_properties,
    int64_t global_properties_key) const {
  SDL_LOG_AUTO_TRACE();
  using namespace app_mngr;
  using namespace smart_objects;
  size_t timeout_prompt_length = 0;
  size_t help_prompt_length = 0;

  if (SmartType::SmartType_Null !=
      global_properties[strings::help_prompt].getType()) {
    help_prompt_length = global_properties[strings::help_prompt].length();
  }

  if (SmartType::SmartType_Null !=
      global_properties[strings::timeout_prompt].getType()) {
    timeout_prompt_length = global_properties[strings::timeout_prompt].length();
  }
  if (0 == timeout_prompt_length && 0 == help_prompt_length) {
    SDL_LOG_INFO("Application doesn't HelpPrompt and timoutPrompt data");
    return true;
  }

  utils::dbms::SQLQuery insert_help_prompt_array(db());

  if (!insert_help_prompt_array.Prepare(kInsertHelpTimeoutPromptArray)) {
    SDL_LOG_WARN("Problem with verification query insert_help_prompt_array");
    return false;
  }
  int64_t tts_chunk_key = 0;

  size_t max_length = (timeout_prompt_length > help_prompt_length)
                          ? timeout_prompt_length
                          : help_prompt_length;
  /* Positions of binding data for "insert_help_prompt_array":
     field "idglobalProperties" from table "helpTimeoutPromptArray" = 0
     field "idtimeoutPrompt" from table "helpTimeoutPromptArray" = 1
     field "idhelpPrompt" from table "helpTimeoutPromptArray" = 2*/
  for (size_t i = 0; i < max_length; ++i) {
    insert_help_prompt_array.Bind(0, global_properties_key);
    if (i < timeout_prompt_length) {
      if (!ExecInsertTTSChunks(global_properties[strings::timeout_prompt][i],
                               tts_chunk_key)) {
        SDL_LOG_WARN("Problem with insertion timeoutPrompt's ttsChunk");
        return false;
      }
      insert_help_prompt_array.Bind(1, tts_chunk_key);
    } else {
      insert_help_prompt_array.Bind(1);
    }

    if (i < help_prompt_length) {
      if (!ExecInsertTTSChunks(global_properties[strings::help_prompt][i],
                               tts_chunk_key)) {
        SDL_LOG_WARN("Problem with insertion helpPrompt's ttsChunk");
        return false;
      }
      insert_help_prompt_array.Bind(2, tts_chunk_key);
    } else {
      insert_help_prompt_array.Bind(2);
    }
    if (!insert_help_prompt_array.Exec() || !insert_help_prompt_array.Reset()) {
      SDL_LOG_WARN(
          "Problem with execution or resetting insert_help_prompt_array query");
      return false;
    }
  }
  SDL_LOG_INFO("Data were saved to helpTimeoutPromptArray table");
  return true;
}

bool ResumptionDataDB::ExecInsertTTSChunks(
    const smart_objects::SmartObject& tts_chunk, int64_t& tts_chunk_key) const {
  SDL_LOG_AUTO_TRACE();
  using namespace app_mngr;
  utils::dbms::SQLQuery insert_tts_chunk(db());
  if (!insert_tts_chunk.Prepare(kInsertTTSChunk)) {
    SDL_LOG_WARN("Problem with verification insert_tts_chunk query");
    return false;
  }
  /* Positions of binding data for "insert_tts_chunk":
     field "type" from table "TTSChunk" = 0
     field "text" from table "TTSChunk" = 1*/
  insert_tts_chunk.Bind(0, tts_chunk[strings::type].asInt());
  insert_tts_chunk.Bind(1, tts_chunk[strings::text].asString());
  if (!insert_tts_chunk.Exec()) {
    SDL_LOG_WARN("Problem with execution insert_tts_chunk query");
    return false;
  }
  tts_chunk_key = insert_tts_chunk.LastInsertId();
  SDL_LOG_WARN("TTSChunk was saved successfully");
  return true;
}

bool ResumptionDataDB::ExecInsertLimitedCharacters(
    int64_t global_properties_key,
    const smart_objects::SmartObject& characters_array) const {
  SDL_LOG_AUTO_TRACE();
  using namespace app_mngr;
  utils::dbms::SQLQuery insert_characters(db());
  if (!insert_characters.Prepare(kInsertTableLimitedCharacter)) {
    SDL_LOG_WARN(
        "Problem with preparation query "
        "insert_characters");
    return false;
  }
  size_t length_characters_array = characters_array.length();
  /* Positions of binding data for "insert_characters":
     field "limitedCharacterList" from table "tableLimitedCharacterList" = 0*/
  for (size_t i = 0; i < length_characters_array; ++i) {
    insert_characters.Bind(0, characters_array[i].asString());

    if (!insert_characters.Exec()) {
      SDL_LOG_WARN("Problem with insert data to limited_character table");
      return false;
    }
    if ((!ExecInsertDataToArray(global_properties_key,
                                insert_characters.LastInsertId(),
                                kInsertCharacterArray)) ||
        (!insert_characters.Reset())) {
      SDL_LOG_WARN("Problem with insert data to characterArray table");
      return false;
    }
  }
  SDL_LOG_INFO("Data were saved successfully to limited_character table");
  return true;
}

bool ResumptionDataDB::ExecInsertVRHelpItem(
    int64_t global_properties_key,
    const smart_objects::SmartObject& vrhelp_array) const {
  SDL_LOG_AUTO_TRACE();
  using namespace app_mngr;
  utils::dbms::SQLQuery insert_vrhelp_item(db());
  if (!insert_vrhelp_item.Prepare(kInsertVRHelpItem)) {
    SDL_LOG_WARN("Problem with preparation query insert_vrhelp_item");
    return false;
  }
  int64_t image_primary_key = 0;
  size_t length_vrhelp_array = vrhelp_array.length();
  /* Positions of binding data for "insert_vrhelp_item":
     field "text" from table "vrHelpItem" = 0
     field "position" from table "vrHelpItem" = 1
     field "idimage" from table "vrHelpItem" = 2*/
  for (size_t i = 0; i < length_vrhelp_array; ++i) {
    insert_vrhelp_item.Bind(0, vrhelp_array[i][strings::text].asString());
    insert_vrhelp_item.Bind(1, vrhelp_array[i][strings::position].asInt());
    if (vrhelp_array[i].keyExists(strings::image)) {
      if (!ExecInsertImage(image_primary_key,
                           vrhelp_array[i][strings::image])) {
        SDL_LOG_INFO("Problem with insert image to vrHelpItem table");
        return false;
      }
      insert_vrhelp_item.Bind(2, image_primary_key);
    } else {
      insert_vrhelp_item.Bind(2);
    }

    if (!insert_vrhelp_item.Exec()) {
      SDL_LOG_INFO("Problem with insert data vrHelpItem table");
      return false;
    }

    if ((!ExecInsertDataToArray(global_properties_key,
                                insert_vrhelp_item.LastInsertId(),
                                kInsertVRHelpItemArray)) ||
        (!insert_vrhelp_item.Reset())) {
      SDL_LOG_WARN("Problem with insert data to vrHelpItemArray table");
      return false;
    }
  }
  SDL_LOG_INFO("Data were saved successfully to vrHelpItem array table");
  return true;
}

bool ResumptionDataDB::InsertApplicationData(
    app_mngr::ApplicationSharedPtr application,
    const std::string& policy_app_id,
    const std::string& device_id) {
  SDL_LOG_AUTO_TRACE();
  ApplicationParams app(application);
  return InsertApplicationData(app, policy_app_id, device_id, NULL, 0);
}

bool ResumptionDataDB::InsertApplicationData(
    const ApplicationParams& application,
    const std::string& policy_app_id,
    const std::string& device_id,
    int64_t* application_primary_key,
    int64_t global_properties_key) const {
  SDL_LOG_AUTO_TRACE();
  using namespace app_mngr;
  utils::dbms::SQLQuery query(db());

  if (!application.m_is_valid) {
    SDL_LOG_ERROR("Invalid application params passed.");
    return false;
  }

  const std::string hash = application.m_hash;
  const int64_t grammar_id = application.m_grammar_id;
  const int64_t time_stamp = static_cast<int64_t>(time(NULL));
  const int64_t connection_key = application.m_connection_key;
  const int64_t hmi_app_id = application.m_hmi_app_id;
  const mobile_apis::HMILevel::eType hmi_level = application.m_hmi_level;
  bool is_media_application = application.m_is_media_application;
  bool is_subscribed_for_way_points =
      application_manager_.IsAppSubscribedForWayPoints(*(application.app_ptr));

  if (!query.Prepare(kInsertApplication)) {
    SDL_LOG_WARN(
        "Problem with verification query "
        "for insert to table application");
    return false;
  }

  /* Positions of binding data for "query":
     field "connection_key" from table "application" = 0
     field "grammarID" from table "application" = 1
     field "hashID" from table "application" = 2
     field "hmiAppID" from table "application" = 3
     field "hmiLevel" from table "application" = 4
     field "ign_off_count" from table "application" = 5
     field "timeStamp" from table "application" = 6
     field "idglobalProperties" from table "application" = 7
     field "isMediaApplication" from table "application" = 8
     field "appID" from table "application" = 9
     field "deviceID" from table "application" = 10
     field "isSubscribedForWayPoints" from table "application" = 11*/
  query.Bind(0, connection_key);
  query.Bind(1, grammar_id);
  query.Bind(2, hash);
  query.Bind(3, hmi_app_id);
  query.Bind(4, static_cast<int32_t>(hmi_level));
  query.Bind(5, 0);
  query.Bind(6, time_stamp);
  if (global_properties_key) {
    query.Bind(7, global_properties_key);
  } else {
    query.Bind(7);
  }
  query.Bind(8, is_media_application);
  query.Bind(9, policy_app_id);
  query.Bind(10, device_id);
  query.Bind(11, is_subscribed_for_way_points);

  if (!query.Exec()) {
    SDL_LOG_WARN("Problem with execution query");
    return false;
  }
  if (NULL != application_primary_key) {
    *application_primary_key = query.LastInsertId();
  }
  SDL_LOG_INFO("Data were saved successfully to application table");
  return true;
}

void ResumptionDataDB::CustomBind(const std::string& key,
                                  const smart_objects::SmartObject& so,
                                  utils::dbms::SQLQuery& query,
                                  const int pos) const {
  SDL_LOG_AUTO_TRACE();
  using namespace smart_objects;
  if (so.keyExists(key) && SmartType::SmartType_Null != so[key].getType()) {
    switch (so[key].getType()) {
      case SmartType::SmartType_Integer: {
        query.Bind(pos, so[key].asInt());
        break;
      }
      case SmartType::SmartType_String: {
        query.Bind(pos, so[key].asString());
        break;
      }
      default: {
        SDL_LOG_WARN("Incorrect type");
        break;
      }
    }
  } else {
    query.Bind(pos);
  }
}

bool ResumptionDataDB::PrepareSelectQuery(utils::dbms::SQLQuery& query,
                                          const std::string& policy_app_id,
                                          const std::string& device_id,
                                          const std::string& text_query) const {
  SDL_LOG_AUTO_TRACE();
  if (!query.Prepare(text_query)) {
    SDL_LOG_WARN("Problem with verification query");
    return false;
  }
  query.Bind(0, policy_app_id);
  query.Bind(1, device_id);
  return true;
}

void ResumptionDataDB::UpdateDataOnAwake() {
  SDL_LOG_AUTO_TRACE();

  utils::dbms::SQLQuery query(db());
  if (query.Prepare(kUpdateIgnOffCount)) {
    if (query.Exec()) {
      SDL_LOG_INFO("Values of ignition off counts were updated successfully");
      WriteDb();
    }
  }
}

bool ResumptionDataDB::UpdateApplicationData(
    app_mngr::ApplicationConstSharedPtr application,
    const std::string& policy_app_id,
    const std::string& device_id) {
  SDL_LOG_AUTO_TRACE();
  using namespace app_mngr;
  utils::dbms::SQLQuery query(db());

  const int64_t time_stamp = static_cast<int64_t>(time(NULL));
  const mobile_apis::HMILevel::eType hmi_level =
      application->hmi_level(mobile_apis::PredefinedWindows::DEFAULT_WINDOW);

  if (!query.Prepare(kUpdateApplicationData)) {
    SDL_LOG_WARN(
        "Problem with verification query "
        "for updating some application data");
    return false;
  }

  /* Positions of binding data for "query":
     field "hmiLevel" from table "application" = 0
     field "timeStamp" from table "application" = 1
     field "appID" from table "application" = 2
     field "deviceID" from table "application" = 3*/
  query.Bind(0, static_cast<int32_t>(hmi_level));
  query.Bind(1, time_stamp);
  query.Bind(2, policy_app_id);
  query.Bind(3, device_id);

  if (!query.Exec()) {
    SDL_LOG_WARN("Problem with execution query");
    return false;
  }
  SDL_LOG_INFO("Data were updated successfully in application table");
  return true;
}

void ResumptionDataDB::WriteDb() {
  SDL_LOG_AUTO_TRACE();
  db_->Backup();
}

bool ResumptionDataDB::UpdateGrammarID(const std::string& policy_app_id,
                                       const std::string& device_id,
                                       const uint32_t grammar_id) {
  SDL_LOG_AUTO_TRACE();
  using namespace app_mngr;
  utils::dbms::SQLQuery query(db());

  if (!query.Prepare(kUpdateGrammarID)) {
    SDL_LOG_WARN("Problem with verification query for updating grammar id.");
    return false;
  }

  //  Positions of binding data for "query":
  //  field "grammarID" from table "application" = 0
  //  field "appID" from table "application" = 1
  //  field "deviceID" from table "application" = 2
  query.Bind(0, static_cast<int32_t>(grammar_id));
  query.Bind(1, policy_app_id);
  query.Bind(2, device_id);

  if (!query.Exec()) {
    SDL_LOG_WARN("Problem with execution query");
    return false;
  }
  SDL_LOG_INFO("Data were updated successfully in application table");
  return true;
}

utils::dbms::SQLDatabase* ResumptionDataDB::db() const {
#ifdef __QNX__
  utils::dbms::SQLDatabase* db = new utils::dbms::SQLDatabase(kDatabaseName);
  db->Open();
  return db;
#else
  return db_;
#endif
}

ApplicationParams::ApplicationParams(
    const smart_objects::SmartObject& application)
    : m_hash()
    , m_grammar_id(0)
    , m_connection_key(0)
    , m_hmi_app_id(0)
    , m_hmi_level(mobile_apis::HMILevel::INVALID_ENUM)
    , m_is_media_application(false)
    , m_is_valid(false) {
  using namespace app_mngr::strings;
  if (!application.keyExists(app_id) || !application.keyExists(hash_id) ||
      !application.keyExists(grammar_id) ||
      !application.keyExists(connection_key) ||
      !application.keyExists(hmi_app_id) || !application.keyExists(hmi_level) ||
      !application.keyExists(is_media_application)) {
    return;
  }
  m_is_valid = true;
  m_hash = application[hash_id].asString();
  m_grammar_id = application[grammar_id].asInt();
  m_connection_key = application[connection_key].asInt();
  m_hmi_app_id = application[hmi_app_id].asInt();
  m_hmi_level =
      static_cast<mobile_apis::HMILevel::eType>(application[hmi_level].asInt());
  m_is_media_application = application[is_media_application].asBool();
}

ApplicationParams::ApplicationParams(app_mngr::ApplicationSharedPtr application)
    : m_hash()
    , m_grammar_id(0)
    , m_connection_key(0)
    , m_hmi_app_id(0)
    , m_hmi_level(mobile_apis::HMILevel::INVALID_ENUM)
    , m_is_media_application(false)
    , m_is_valid(false) {
  if (application) {
    m_is_valid = true;
    m_hash = application->curHash();
    m_grammar_id = application->get_grammar_id();
    m_connection_key = application->app_id();
    m_hmi_app_id = application->hmi_app_id();
    m_hmi_level =
        application->hmi_level(mobile_apis::PredefinedWindows::DEFAULT_WINDOW);
    m_is_media_application = application->IsAudioApplication();
    app_ptr = application;
  }
}

}  // namespace resumption