summaryrefslogtreecommitdiff
path: root/src/components/application_manager/include/application_manager/resumption/resumption_data_db.h
blob: a4614d4f2c4027d9788d5d296aeed6b3196576d5 (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
/*
 * 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.
 */

#ifndef SRC_COMPONENTS_APPLICATION_MANAGER_INCLUDE_APPLICATION_MANAGER_RESUMPTION_RESUMPTION_DATA_DB_H_
#define SRC_COMPONENTS_APPLICATION_MANAGER_INCLUDE_APPLICATION_MANAGER_RESUMPTION_RESUMPTION_DATA_DB_H_
#include "application_manager/resumption/resumption_data.h"

#include "utils/sqlite_wrapper/sql_database.h"
#include "utils/sqlite_wrapper/sql_query.h"

namespace resumption {

/**
 * @brief Points what structure contains vr commands
 */
enum AccessoryVRCommand { kVRCommandFromChoice = 0, kVRCommandFromCommand };

struct ApplicationParams {
  ApplicationParams(const smart_objects::SmartObject& application);
  ApplicationParams(app_mngr::ApplicationSharedPtr application);

  std::string m_hash;
  int64_t m_grammar_id;
  int64_t m_connection_key;
  int64_t m_hmi_app_id;
  mobile_apis::HMILevel::eType m_hmi_level;
  bool m_is_media_application;
  bool m_is_valid;
  app_mngr::ApplicationSharedPtr app_ptr;
};

/**
 * @brief Show should database be saved in a disk file or in memory
 */
enum DbStorage { In_Memory_Storage = 0, In_File_Storage };

/**
 * @brief class contains logic for representation application data in
 * data base
 */
class ResumptionDataDB : public ResumptionData {
 public:
  /**
   * @brief Constructor of ResumptionDataDB
   * @param db_storage show database should be saved in a disk file or in memory
   */
  ResumptionDataDB(
      DbStorage db_storage,
      const application_manager::ApplicationManager& application_manager);

  /**
   * @brief allows to destroy ResumptionDataDB object
   */
  virtual ~ResumptionDataDB();

  /**
   * @brief Creates or opens DB and initialize it
   * @return false if DB doesn't initialize
   * otherwise returns true
   */
  virtual bool Init();

  /**
   * @brief Save application persistent info for future resuming to db
   * @param application is application which need to be saved
   */
  void SaveApplication(app_mngr::ApplicationSharedPtr application) OVERRIDE;
  /**
   * @brief Checks if saved data of applications have hmi app id
   * @param hmi_app_id - hmi application id
   * @return true if exist, otherwise false
   */
  virtual bool IsHMIApplicationIdExist(uint32_t hmi_app_id) const;
  /**
   * @brief Retrieves HMI app ID for the given mobile app ID
   * and device ID from stored data.
   * @param policy_app_id - mobile application id
   * @param device_id - contains id of device on which is running application
   * @return HMI app ID
   */
  virtual uint32_t GetHMIApplicationID(const std::string& policy_app_id,
                                       const std::string& device_id) const;

  void IncrementGlobalIgnOnCounter() OVERRIDE;

  uint32_t GetGlobalIgnOnCounter() const OVERRIDE;

  void ResetGlobalIgnOnCount() OVERRIDE;

  /**
   * @brief Increments ignition counter for all registered applications
   * and remember ign_off time stamp
   */
  void IncrementIgnOffCount() FINAL;

  /**
   * @brief Decrements ignition counter for all registered applications
   */
  void DecrementIgnOffCount() FINAL;

  /**
   * @brief Retrieves hash ID for the given mobile app ID
   * and device ID from stored information.
   * @param policy_app_id - mobile application id
   * @param device_id - contains id of device on which is running application
   * @param hash_id - parameter which will contain HASH id from saved
   * application
   * @return TRUE if application will be found in saved data otherwise
   * returns FALSE
   */
  virtual bool GetHashId(const std::string& policy_app_id,
                         const std::string& device_id,
                         std::string& hash_id) const;

  /**
   * @brief Retrieves data of saved application for the given mobile app ID
   * and device ID
   * @param policy_app_id - mobile application id
   * @param device_id - contains id of device on which is running application
   * @param saved_app - parameter which will contain data of saved application
   * @return TRUE if application will be found in saved data otherwise
   * returns FALSE
   */
  virtual bool GetSavedApplication(const std::string& policy_app_id,
                                   const std::string& device_id,
                                   smart_objects::SmartObject& saved_app) const;

  /**
   * @brief Remove application from list of saved applications
   * @param policy_app_id application witch need to be removed
   * @param device_id - contains id of device on which is running application
   * @return return true, if success, otherwise return false
   */
  virtual bool RemoveApplicationFromSaved(const std::string& policy_app_id,
                                          const std::string& device_id);

  /**
   * @brief Get the last ignition off time from LastState
   * @return the last ignition off time from LastState
   */
  virtual uint32_t GetIgnOffTime() const;

  /**
   * @brief Checks if saved data have application
   * @param policy_app_id - mobile application id
   * @param device_id - contains id of device on which is running application
   * @return 0 if saved data contains application otherwise returns -1
   */
  virtual ssize_t IsApplicationSaved(const std::string& policy_app_id,
                                     const std::string& device_id) const;

  /**
   * @brief Retrieves data from saved application
   * @param  will contain data for resume_ctrl
   */
  virtual void GetDataForLoadResumeData(
      smart_objects::SmartObject& saved_data) const;

  /**
   * @brief Updates HMI level of saved application
   * @param policy_app_id - mobile application id
   * @param device_id - contains id of device on which is running application
   * @param hmi_level - contains hmi level for saved application
   */
  virtual void UpdateHmiLevel(const std::string& policy_app_id,
                              const std::string& device_id,
                              mobile_apis::HMILevel::eType hmi_level);

  /**
   * @brief Re-creates and re-init DB
   * @return true if success, otherwise - false
   */
  bool RefreshDB() const;

  /**
   * @brief Gets all the data from DB to memory
   * @param data Object which holds resumption data
   * @return true if success, otherwise - false
   */
  bool GetAllData(smart_objects::SmartObject& data) const;

  /**
   * @brief Saves all the data to DB
   * @param data Object, which holds resumption data
   * @return true if success, otherwise - false
   */
  bool SaveAllData(const smart_objects::SmartObject& data);

  /**
   * @brief Checks, if DB version is actual to current schema
   * @return true if success, otherwise - false
   */
  bool IsDBVersionActual() const;

  /**
   * @brief Updates DB version accordingly to current schema
   * @return true if success, otherwise - false
   */
  bool UpdateDBVersion() const;

  bool DropAppDataResumption(const std::string& device_id,
                             const std::string& app_id) OVERRIDE;
  /**
   * @brief Write json resumption info to file system
   */
  void Persist() OVERRIDE;

 protected:
  /**
   * @brief returns pointer to data base
   */
  utils::dbms::SQLDatabase* db() const;

 private:
  /**
   * @brief Calculates DB version from current schema
   * @return version
   */
  const int32_t GetDBVersion() const;

  /**
   * @brief Retrieves hmi level from db
   * @param policy_app_id - mobile application id
   * @param device_id - contains id of device on which is running application
   * @param hmi_level - will contains hmi level for saved application
   * @return true if application with mobile id and device id has hmi level
   * otherwise returns false
   */
  bool SelectHMILevel(const std::string& policy_app_id,
                      const std::string& device_id,
                      int& hmi_level) const;
  /**
   * @brief Checks existence HMI id in DB
   * @param hmi_app_id - HMI id
   * return true if hmiID is same with saved hmiID otherwise returns false
   */
  bool CheckExistenceHMIId(uint32_t hmi_app_id) const;

  /**
   * @brief Select HMI id from saved application
   * @param policy_app_id - mobile application id
   * @param device_id - contains id of device on which is running application
   * @param hmi_id - will contains hmi id from saved application
   */
  void SelectHMIId(const std::string& policy_app_id,
                   const std::string& device_id,
                   uint32_t& hmi_id) const;

  /**
   * @brief Select hash id from saved application
   * @param policy_app_id - mobile application id
   * @param device_id - contains id of device on which is running application
   * @param hash_id - will contains hash id from saved application
   */
  bool SelectHashId(const std::string& policy_app_id,
                    const std::string& device_id,
                    std::string& hash_id) const;

  /**
   * @brief Select Ign off time
   * @return Ign off time from saved data
   */
  uint32_t SelectIgnOffTime() const;

  /**
   * @brief Checks existence application in DB
   * @param policy_app_id - mobile application id
   * @param device_id - contains id of device on which is running application
   * @param application_exist will contain true if restored data contain
   * application
   * otherwise will contain false
   * return if we have problem with access to DB returns FALSE otherwise
   * returns TRUE
   */
  bool CheckExistenceApplication(const std::string& policy_app_id,
                                 const std::string& device_id,
                                 bool& application_exist) const;

  /**
   * @brief Retrieves data from saved application
   * @param will contain data for resume_ctrl
   */
  void SelectDataForLoadResumeData(
      smart_objects::SmartObject& saved_data) const;

  /**
   * @brief Deletes saved application from db
   * @param policy_app_id - mobile application id
   * @param device_id - contains id of device on which is running application
   * @return true if application data was deleted otherwise returns
   * false
   */
  bool DeleteSavedApplication(const std::string& policy_app_id,
                              const std::string& device_id);

  /**
   * @brief Deletes file from saved application
   * @param policy_app_id - mobile application id
   * @param device_id - contains id of device on which is running application
   * @return true if data was deleted otherwise returns
   * false
   */
  bool DeleteSavedFiles(const std::string& policy_app_id,
                        const std::string& device_id);

  /**
   * @brief Deletes submenu from saved application
   * @param policy_app_id - mobile application id
   * @param device_id - contains id of device on which is running application
   * @return true if data was deleted otherwise returns
   * false
   */
  bool DeleteSavedSubMenu(const std::string& policy_app_id,
                          const std::string& device_id);

  /**
   * @brief Deletes subscriptions from saved application
   * @param policy_app_id - mobile application id
   * @param device_id - contains id of device on which is running application
   * @return true if data was deleted otherwise returns
   * false
   */
  bool DeleteSavedSubscriptions(const std::string& policy_app_id,
                                const std::string& device_id);

  /**
   * @brief Deletes userLocation from saved application
   * @param policy_app_id - mobile application id
   * @param device_id - contains id of device on which is running application
   * @return true if data was deleted otherwise returns
   * false
   */
  bool DeleteUserLocation(const std::string& policy_app_id,
                          const std::string& device_id);

  /**
   * @brief Deletes commands from saved application
   * @param policy_app_id - mobile application id
   * @param device_id - contains id of device on which is running application
   * @return true if data was deleted otherwise returns
   * false
   */
  bool DeleteSavedCommands(const std::string& policy_app_id,
                           const std::string& device_id);

  /**
   * @brief Deletes choice set from saved application
   * @param policy_app_id - mobile application id
   * @param device_id - contains id of device on which is running application
   * @return true if data was deleted otherwise returns
   * false
   */
  bool DeleteSavedChoiceSet(const std::string& policy_app_id,
                            const std::string& device_id);

  /**
   * @brief Deletes global properties from saved application
   * @param policy_app_id - mobile application id
   * @param device_id - contains id of device on which is running application
   * @return true if data was deleted otherwise returns
   * false
   */
  bool DeleteSavedGlobalProperties(const std::string& policy_app_id,
                                   const std::string& device_id);
  /**
   * @brief Deletes data from application table
   * @param policy_app_id - mobile application id
   * @param device_id - contains id of device on which is running application
   * @return true if data was deleted otherwise returns
   * false
   */
  bool DeleteDataFromApplicationTable(const std::string& policy_app_id,
                                      const std::string& device_id);

  /**
   * @brief Saves files data to DB
   * @param files contains data for saving
   * @param application_primary_key - primary key from DB table application
   * @return true if data was saved successfully otherwise returns
   * false
   */
  bool InsertFilesData(const smart_objects::SmartObject& files,
                       int64_t application_primary_key) const;

  /**
   * @brief Saves submenu data to DB
   * @param submenus contains data for saving
   * @param application_primary_key - primary key from DB table application
   * @return true if data was saved successfully otherwise returns
   * false
   */
  bool InsertSubMenuData(const smart_objects::SmartObject& submenus,
                         int64_t application_primary_key) const;

  /**
   * @brief Saves commands data to DB
   * @param commands contains data for saving
   * @param application_primary_key - primary key from DB table application
   * @return true if data was saved successfully otherwise returns
   * false
   */
  bool InsertCommandsData(const smart_objects::SmartObject& commands,
                          int64_t application_primary_key) const;

  /**
   * @brief Saves subscriptions data to DB
   * @param subscriptions contains data for saving
   * @param application_primary_key - primary key from DB table application
   * @return true if data was saved successfully otherwise returns
   * false
   */
  bool InsertSubscriptionsData(const smart_objects::SmartObject& subscriptions,
                               int64_t application_primary_key) const;

  /**
   * @brief Saves choice set data to DB
   * @param choicesets contains data for saving
   * @param application_primary_key - primary key from DB table application
   * @return true if data was saved successfully otherwise returns
   * false
   */
  bool InsertChoiceSetData(const smart_objects::SmartObject& choicesets,
                           int64_t application_primary_key) const;

  /**
   * @brief Saves user location data to DB
   * @param user_location contains data for saving
   * @param application_primary_key - primary key from DB table application
   * @return true if data was saved successfully otherwise returns
   * false
   */
  bool InsertUserLocationData(const smart_objects::SmartObject& user_location,
                              int64_t application_primary_key) const;

  /**
   * @brief Saves globalProperties data to DB
   * @param global_properties contains data for saving
   * @param global_properties_key - will contain primary key from global
   * properties table
   * @return true if data was saved successfully otherwise returns
   * false
   */
  bool InsertGlobalPropertiesData(
      const smart_objects::SmartObject& global_properties,
      int64_t& global_properties_key) const;

  /**
   * @brief Saves application data to DB
   * @param application contains data for saving
   * @param policy_app_id - mobile application id
   * @param device_id - contains id of device on which is running application
   * @return true if application data was saved successfully
   * otherwise returns false
   */
  bool SaveApplicationToDB(app_mngr::ApplicationSharedPtr application,
                           const std::string& policy_app_id,
                           const std::string& device_id) const;

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

  /**
   * @brief Updates ignition of count on saved applications after onAwake
   * notification
   */
  void UpdateDataOnAwake();

  /**
   * @brief Execute query for delete data of application
   * @param policy_app_id - mobile application id
   * @param device_id - contains id of device on which is running application
   * @param text_query - contains text of query
   * @return true if query was run successfully otherwise returns
   * false
   */
  bool ExecQueryToDeleteData(const std::string& policy_app_id,
                             const std::string& device_id,
                             const std::string& text_query);

  /**
   * @brief Execute union query for delete data of application
   * @param policy_app_id - mobile application id
   * @param device_id - contains id of device on which is running application
   * @param text_query - contains text of query
   * @return true if query was run successfully otherwise returns
   * false
   */
  bool ExecUnionQueryToDeleteData(const std::string& policy_app_id,
                                  const std::string& device_id,
                                  const std::string& text_query);

  /**
   * @brief Execute query in order to insert image to DB
   * @param image_primary_key - will contain primary key from image table
   * @param image contains data for saving to DB
   * @return true if query was run successfully otherwise returns
   * false
   */
  bool ExecInsertImage(int64_t& image_primary_key,
                       const smart_objects::SmartObject& image) const;

  /**
   * @brief Execute query in order to insert choice to DB
   * @param choice_set_key - contain primary key from
   * applicationChoiceSet table
   * @param choice_array contains data for saving to DB
   * @return true if query was run successfully otherwise returns
   * false
   */
  bool ExecInsertChoice(int64_t choice_set_key,
                        const smart_objects::SmartObject& choice_array) const;

  /**
   * @brief Execute query in order to insert vr commands to DB
   * @param primary_key - will contain primary key from command table or
   * choice table
   * @param vr_commands_array contains data for saving to DB
   * @param value indicates which structure contains vrcommands
   * @return true if query was run successfully otherwise returns
   * false
   */
  bool ExecInsertVrCommands(const int64_t primary_key,
                            const smart_objects::SmartObject& vr_commands_array,
                            AccessoryVRCommand value) const;

  /**
   * @brief Execute query in order to insert choice set data to DB
   * @param choice_set_primary_key - will contain primary key from
   * applicationChoiceSet table
   * @param choiceset contains data for saving to DB
   * @return true if query was run successfully otherwise returns
   * false
   */
  bool ExecInsertApplicationChoiceSet(
      int64_t& choice_set_primary_key,
      const smart_objects::SmartObject& choiceset) const;

  /**
   * @brief combines primary key from several table to new table
   * @param first_primary_key - will contain primary key from first DB table
   * @param second_primary_key - will contain primary key from second DB table
   * @param text_query - contains text of query
   * @return true if query was run successfully otherwise returns
   * false
   */
  bool ExecInsertDataToArray(int64_t first_primary_key,
                             int64_t second_primary_key,
                             const std::string& text_query) const;

  /**
   * @brief Execute query in order to insert characters array to DB
   * @param global_properties_key contains primary key from globalproperties
   * table
   * @param characters_array contains data for saving to DB
   * @return true if query was run successfully otherwise returns
   * false
   */
  bool ExecInsertLimitedCharacters(
      int64_t global_properties_key,
      const smart_objects::SmartObject& characters_array) const;

  /**
   * @brief Execute query in order to insert vr help item array to DB
   * @param global_properties_key contains primary key from globalproperties
   * table
   * @param vrhelp_array contains data for saving to DB
   * @return true if query was run successfully otherwise returns
   * false
   */
  bool ExecInsertVRHelpItem(
      int64_t global_properties_key,
      const smart_objects::SmartObject& vrhelp_array) const;

  /**
   * @brief Execute query in order to insert data to ttsChunk table
   * @param tts_chunk contains data for saving to DB
   * @param tts_chunk_key will contain primary key from ttsChunk table
   * @return true if query was run successfully otherwise returns
   * false
   */
  bool ExecInsertTTSChunks(const smart_objects::SmartObject& tts_chunk,
                           int64_t& tts_chunk_key) const;

  /**
   * @brief Execute query in order to insert data to helpTimeoutPromptArray
   * @param global_properties contains data for saving to DB
   * @param global_properties_key contains primary key from globalproperties
   * table
   * @return true if query was run successfully otherwise returns
   * false
   */
  bool ExecInsertHelpTimeoutArray(
      const smart_objects::SmartObject& global_properties,
      int64_t global_properties_key) const;

  /**
   * @brief Execute query in order to insert or update data in application table
   * @param application contains data for saving to DB
   * @param policy_app_id contains mobile application id of application
   * @param device_id contains id of device on which is running application
   * @param application_primary_key will contain primary key from application
   * table
   * @param global_properties_key contains primary key from globalproperties
   * table
   * @return true if query was run successfully otherwise returns
   * false
   */
  bool 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;

  /**
   * @brief Calls InsertApplicationData method
   * @param application contains data for saving to DB
   * @param policy_app_id contains mobile application id of application
   * @param device_id contains id of device on which is running application
   * @return true if InsertApplicationData works successfully, otherwise
   * returns false;
   */
  bool InsertApplicationData(app_mngr::ApplicationSharedPtr application,
                             const std::string& policy_app_id,
                             const std::string& device_id);

  /**
   * @brief Delete application data where ign_off_count >= application_lifes
   * @param application_lifes contains amount ign off
   * @return true if query was run successfully otherwise returns
   * false
   */
  bool DeleteAppWithIgnCount(const int application_lifes);

  /**
   * @brief Selects data from file table
   * @param policy_app_id contains mobile application id of application
   * @param device_id contains id of device on which is running application
   * @param saved_app will contain data from file table
   * @return true if query was run successfully otherwise returns
   * false
   */
  bool SelectFilesData(const std::string& policy_app_id,
                       const std::string& device_id,
                       smart_objects::SmartObject& saved_app) const;

  /**
   * @brief Selects data from subMenu table
   * @param policy_app_id contains mobile application id of application
   * @param device_id contains id of device on which is running application
   * @param saved_app will contain data from subMenu table
   * @return true if query was run successfully otherwise returns
   * false
   */
  bool SelectSubmenuData(const std::string& policy_app_id,
                         const std::string& device_id,
                         smart_objects::SmartObject& saved_app) const;

  /**
   * @brief Selects data from commands table
   * @param policy_app_id contains mobile application id of application
   * @param device_id contains id of device on which is running application
   * @param saved_app will contain commands
   * @return true if query was run successfully otherwise returns
   * false
   */
  bool SelectCommandData(const std::string& policy_app_id,
                         const std::string& device_id,
                         smart_objects::SmartObject& saved_app) const;

  /**
   * @brief Selects data from applicationSubscriptionsArray table
   * @param policy_app_id contains mobile application id of application
   * @param device_id contains id of device on which is running application
   * @param saved_app will contain subscriptions
   * @return true if query was run successfully otherwise returns
   * false
   */
  bool SelectSubscriptionsData(const std::string& policy_app_id,
                               const std::string& device_id,
                               smart_objects::SmartObject& saved_app) const;

  /**
   * @brief Selects data from applicationUserLocation table
   * @param policy_app_id contains mobile application id of application
   * @param device_id contains id of device on which is running application
   * @param saved_app contains userLocation
   * @return true if query has been executed successfully otherwise returns
   * false
   */
  bool SelectUserLocationData(const std::string& policy_app_id,
                              const std::string& device_id,
                              smart_objects::SmartObject& saved_app) const;

  /**
   * @brief Selects choice set data from DB
   * @param policy_app_id contains mobile application id of application
   * @param device_id contains id of device on which is running application
   * @param saved_app will contain choiceSets
   * @return true if query was run successfully otherwise returns
   * false
   */
  bool SelectChoiceSetData(const std::string& policy_app_id,
                           const std::string& device_id,
                           smart_objects::SmartObject& saved_app) const;

  /**
   * @brief Selects global properties data from DB
   * @param policy_app_id contains mobile application id of application
   * @param device_id contains id of device on which is running application
   * @param saved_app will contain global properties
   * @return true if query was run successfully otherwise returns
   * false
   */
  bool SelectGlobalPropertiesData(const std::string& policy_app_id,
                                  const std::string& device_id,
                                  smart_objects::SmartObject& saved_app) const;

  /**
   * @brief Selects image data from DB
   * @param image_key primary key from image table
   * @param image will contain image data from DB
   * @return true if query was run successfully otherwise returns
   * false
   */
  bool SelectImageData(int64_t image_key,
                       smart_objects::SmartObject& image) const;

  /**
   * @brief Selects ttsChunk data from DB
   * @param tts_chunk_key primary key from ttsChunk table
   * @param tts_chunk will contain ttsChunk data from DB
   * @return true if query was run successfully otherwise returns
   * false
   */
  bool SelectTTSChunkData(int64_t tts_chunk_key,
                          smart_objects::SmartObject& tts_chunk) const;

  /**
   * @brief Selects VR help items data from DB
   * @param global_properties_key primary key from global properties table
   * @param global_properties will contain VR help items
   * @return true if query was run successfully otherwise returns
   * false
   */
  bool SelectVrHelpItemsData(
      int64_t global_properties_key,
      smart_objects::SmartObject& global_properties) const;

  /**
   * @brief Selects table limited character data from DB
   * @param global_properties_key primary key from global properties table
   * @param keyboard_properties will contain table limited character
   * @return true if query was run successfully otherwise returns
   * false
   */
  bool SelectCharactersData(
      int64_t global_properties_key,
      smart_objects::SmartObject& keyboard_properties) const;

  /**
   * @brief Selects data from application table of DB
   * @param policy_app_id contains mobile application id of application
   * @param device_id contains id of device on which is running application
   * @param saved_app will contain data from application table
   * @return true if query was run successfully otherwise returns
   * false
   */
  bool SelectDataFromAppTable(const std::string& policy_app_id,
                              const std::string& device_id,
                              smart_objects::SmartObject& saved_app) const;

  /**
   * @brief Selects count from array table
   * @param count_item will contain amount item in table
   * @param text_query - contains text of query
   * @param policy_app_id contains mobile application id of application
   * @param device_id contains id of device on which is running application
   * @return true if query was run successfully otherwise returns
   * false
   */
  bool SelectCountFromArray(uint32_t& count_item,
                            const std::string& text_query,
                            const std::string& policy_app_id,
                            const std::string& device_id) const;

  /**
   * @brief Binds data from smart object with query
   * @param key contains key from smart object
   * @param so contains data which needs to bind with query
   * @param query object of query for DB
   * @param pos contains position for binding query
   */
  void CustomBind(const std::string& key,
                  const smart_objects::SmartObject& so,
                  utils::dbms::SQLQuery& query,
                  const int pos) const;

  /**
   * @brief prepare and bind the same type query
   * @param query object of query for DB
   * @param policy_app_id contains mobile application id of application
   * @param device_id contains id of device on which is running application
   * @param text_query - contains text of query
   * @return true if query was binded successfully otherwise returns
   * false
   */
  bool PrepareSelectQuery(utils::dbms::SQLQuery& query,
                          const std::string& policy_app_id,
                          const std::string& device_id,
                          const std::string& text_query) const;

  /**
   * @brief Updates HMI level and time stamp in application table
   * @param application contains data for saving
   * @param policy_app_id - mobile application id
   * @param device_id - contains id of device on which is running application
   * @return true if application data were updated successfully
   * otherwise returns false
   */
  bool UpdateApplicationData(app_mngr::ApplicationConstSharedPtr application,
                             const std::string& policy_app_id,
                             const std::string& device_id);

  /**
   * @brief Writes data to DB after update
   */
  void WriteDb();

  /**
   * @brief Updates grammarID for application
   * @param policy_app_id Application ID
   * @param device_id Device ID
   * @param grammar_id GrammarID
   * @return true if succedeed, otherwise - false
   */
  bool UpdateGrammarID(const std::string& policy_app_id,
                       const std::string& device_id,
                       const uint32_t grammar_id);

  DISALLOW_COPY_AND_ASSIGN(ResumptionDataDB);

  utils::dbms::SQLDatabase* db_;
  mutable sync_primitives::RecursiveLock resumption_lock_;
};
}  // namespace resumption

#endif  // SRC_COMPONENTS_APPLICATION_MANAGER_INCLUDE_APPLICATION_MANAGER_RESUMPTION_RESUMPTION_DATA_DB_H_