Advertisement
vergepuppeter

Notification DBHElper

Jun 10th, 2016
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 19.43 KB | None | 0 0
  1. package app.seesharpsolutions.com.iscity.Utils;
  2.  
  3. import android.content.ContentValues;
  4. import android.content.Context;
  5. import android.database.Cursor;
  6. import android.database.sqlite.SQLiteDatabase;
  7. import android.database.sqlite.SQLiteOpenHelper;
  8. import java.util.ArrayList;
  9. import app.seesharpsolutions.com.iscity.Model.Notification;
  10.  
  11.  
  12. public class DatabaseHandler extends SQLiteOpenHelper{
  13.     // All Static variables
  14.     // Database Version
  15.     private static final int DATABASE_VERSION = 1;
  16.  
  17.     // Database Name
  18.     private static final String DATABASE_NAME = "NotificationManager";
  19.  
  20.     // Contacts table name
  21.     private static final String TABLE_CONTACTS = "notifications";
  22.  
  23.     // Contacts Table Columns names
  24.     private static final String KEY_ID = "id";
  25.     private static final String KEY_TITLE = "title";
  26.     private static final String KEY_MESSAGE = "message";
  27.     private static final String KEY_TIME = "time";
  28.     private static final String KEY_DATE = "date";
  29.     private static final String KEY_HASCHECK = "hascheck";
  30.     private static final String KEY_TYPE = "type";
  31.     //
  32.     //    private int id;
  33.     //    private String title;
  34.     //    private String message;
  35.     //    private String time;
  36.     //    private String date;
  37.     //    private boolean hasCheck;
  38.     //
  39.     public DatabaseHandler(Context context) {
  40.         //super(context, DATABASE_NAME, null, DATABASE_VERSION);
  41.         super(context, DATABASE_NAME, null, DATABASE_VERSION);
  42.     }
  43.  
  44.     // Creating Tables
  45.     @Override
  46.     public void onCreate(SQLiteDatabase db) {
  47.         String CREATE_CONTACTS_TABLE = "CREATE TABLE IF NOT EXISTS " + TABLE_CONTACTS + "("
  48.                 + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + KEY_TITLE + " TEXT,"
  49.                 + KEY_MESSAGE + " TEXT," + KEY_TIME+" TEXT,"+KEY_DATE+" TEXT," + KEY_HASCHECK + " INTEGER," + KEY_TYPE + " TEXT)";
  50.         db.execSQL(CREATE_CONTACTS_TABLE);
  51.     }
  52.  
  53.     // Upgrading database
  54.     @Override
  55.     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  56.         // Drop older table if existed
  57.         db.execSQL("DROP TABLE IF EXISTS " + TABLE_CONTACTS);
  58.  
  59.         // Create tables again
  60.         onCreate(db);
  61.     }
  62.    
  63.     public void addNotification(Notification notification) {
  64.         SQLiteDatabase db = this.getWritableDatabase();
  65.      
  66.         ContentValues values = new ContentValues();
  67.         values.put(KEY_TITLE, notification.getTitle());
  68.         values.put(KEY_MESSAGE, notification.getMessage());
  69.         values.put(KEY_TIME, notification.getTime());
  70.         values.put(KEY_DATE, notification.getDate());
  71.         values.put(KEY_HASCHECK, notification.getHasCheck());
  72.         values.put(KEY_TYPE, notification.getType());
  73.  
  74.         // Inserting Row
  75.         db.insert(TABLE_CONTACTS, null, values);
  76.         db.close(); // Closing database connection
  77.     }
  78.    
  79.     public Notification getNotification(int id) {
  80.         SQLiteDatabase db = this.getReadableDatabase();
  81.      
  82.         Cursor cursor = db.query(TABLE_CONTACTS, new String[] { KEY_ID,
  83.                 KEY_TITLE, KEY_MESSAGE, KEY_TIME, KEY_DATE, KEY_HASCHECK, KEY_TYPE }, KEY_ID + "=?",
  84.                 new String[] { String.valueOf(id) }, null, null, null, null);
  85.         if (cursor != null)
  86.             cursor.moveToFirst();
  87.      
  88.         Notification notifications = new Notification(cursor.getInt(0),
  89.                 cursor.getString(1), cursor.getString(2), cursor.getString(3), cursor.getString(4), cursor.getInt(5), cursor.getString(6));
  90.         // return contact
  91.         cursor.close();
  92.         db.close();
  93.         return notifications;
  94.     }
  95.    
  96.     public ArrayList<Notification> getAllNotification() {
  97.         ArrayList<Notification> notificationsList = new ArrayList<Notification>();
  98.         // Select All Query
  99.         String selectQuery = "SELECT  * FROM " + TABLE_CONTACTS;
  100.      
  101.         SQLiteDatabase db = this.getWritableDatabase();
  102.         Cursor cursor = db.rawQuery(selectQuery, null);
  103.      
  104.         // looping through all rows and adding to list
  105.         if (cursor.moveToFirst()) {
  106.             do {
  107.                 Notification notifications = new Notification();
  108.                 notifications.setId(cursor.getInt(0));
  109.                 notifications.setTitle(cursor.getString(1));
  110.                 notifications.setMessage(cursor.getString(2));
  111.                 notifications.setTime(cursor.getString(3));
  112.                 notifications.setDate(cursor.getString(4));
  113.                 notifications.setHasCheck(cursor.getInt(5));
  114.                 notifications.setType(cursor.getString(6));
  115.                 // Adding contact to list
  116.                 notificationsList.add(notifications);
  117.             } while (cursor.moveToNext());
  118.         }
  119.      
  120.         // return contact list
  121.         cursor.close();
  122.         db.close();
  123.         return notificationsList;
  124.     }
  125.  
  126.     public ArrayList<Notification> getAllCompNotificationGroup() {
  127.         ArrayList<Notification> notificationsList = new ArrayList<Notification>();
  128.         // Select All Query
  129.         String selectQuery = "SELECT MAX("+KEY_ID+"),"+
  130.         KEY_TITLE+","+KEY_MESSAGE+","+KEY_TIME+","+KEY_DATE+","+KEY_HASCHECK+","+KEY_TYPE+" FROM " + TABLE_CONTACTS + " WHERE type LIKE " + "'com' GROUP BY "+KEY_TITLE+" ORDER BY "+KEY_ID+" DESC";
  131.  
  132.         SQLiteDatabase db = this.getWritableDatabase();
  133.         Cursor cursor = db.rawQuery(selectQuery, null);
  134.  
  135.         // looping through all rows and adding to list
  136.         if (cursor.moveToFirst()) {
  137.             do {
  138.                 Notification notifications = new Notification();
  139.                 notifications.setId(cursor.getInt(0));
  140.                 notifications.setTitle(cursor.getString(1));
  141.                 notifications.setMessage(cursor.getString(2));
  142.                 notifications.setTime(cursor.getString(3));
  143.                 notifications.setDate(cursor.getString(4));
  144.                 notifications.setHasCheck(cursor.getInt(5));
  145.                 notifications.setType(cursor.getString(6));
  146.                 // Adding contact to list
  147.                 notificationsList.add(notifications);
  148.             } while (cursor.moveToNext());
  149.         }
  150.  
  151.         // return contact list
  152.         cursor.close();
  153.         db.close();
  154.         return notificationsList;
  155.     }
  156.  
  157.     public ArrayList<Notification> getAllComNotificationByCategory(String text) {
  158.         ArrayList<Notification> notificationsList = new ArrayList<Notification>();
  159.         // Select All Query
  160.         String selectQuery = "SELECT  * FROM " + TABLE_CONTACTS + " WHERE type LIKE " + "'com' AND "+KEY_TITLE+" LIKE '%"+text+"%' ORDER BY "+ KEY_ID + " DESC";
  161.  
  162.         SQLiteDatabase db = this.getWritableDatabase();
  163.         Cursor cursor = db.rawQuery(selectQuery, null);
  164.  
  165.         // looping through all rows and adding to list
  166.         if (cursor.moveToFirst()) {
  167.             do {
  168.                 Notification notifications = new Notification();
  169.                 notifications.setId(cursor.getInt(0));
  170.                 notifications.setTitle(cursor.getString(1));
  171.                 notifications.setMessage(cursor.getString(2));
  172.                 notifications.setTime(cursor.getString(3));
  173.                 notifications.setDate(cursor.getString(4));
  174.                 notifications.setHasCheck(cursor.getInt(5));
  175.                 notifications.setType(cursor.getString(6));
  176.                 // Adding contact to list
  177.                 notificationsList.add(notifications);
  178.             } while (cursor.moveToNext());
  179.         }
  180.  
  181.         // return contact list
  182.         cursor.close();
  183.         db.close();
  184.         return notificationsList;
  185.     }
  186.  
  187.     public ArrayList<Notification> getAllMemNotificationByCategory(String text) {
  188.         ArrayList<Notification> notificationsList = new ArrayList<Notification>();
  189.         // Select All Query
  190.         String selectQuery = "SELECT  * FROM " + TABLE_CONTACTS + " WHERE type LIKE " + "'mem' AND "+KEY_TITLE+" LIKE '%"+text+"%' ORDER BY "+ KEY_ID + " DESC";
  191.         SQLiteDatabase db = this.getWritableDatabase();
  192.         Cursor cursor = db.rawQuery(selectQuery, null);
  193.  
  194.         // looping through all rows and adding to list
  195.         if (cursor.moveToFirst()) {
  196.             do {
  197.                 Notification notifications = new Notification();
  198.                 notifications.setId(cursor.getInt(0));
  199.                 notifications.setTitle(cursor.getString(1));
  200.                 notifications.setMessage(cursor.getString(2));
  201.                 notifications.setTime(cursor.getString(3));
  202.                 notifications.setDate(cursor.getString(4));
  203.                 notifications.setHasCheck(cursor.getInt(5));
  204.                 notifications.setType(cursor.getString(6));
  205.                 // Adding contact to list
  206.                 notificationsList.add(notifications);
  207.             } while (cursor.moveToNext());
  208.         }
  209.         // return contact list
  210.         cursor.close();
  211.         db.close();
  212.         return notificationsList;
  213.     }
  214.  
  215.  
  216.     public ArrayList<Notification> getAllMemNotificationGroup() {
  217.         ArrayList<Notification> notificationsList = new ArrayList<Notification>();
  218.         // Select All Query
  219.         String selectQuery = "SELECT MAX("+KEY_ID+"),"+
  220.                                 KEY_TITLE+","+KEY_MESSAGE+","+KEY_TIME+","+KEY_DATE+","+KEY_HASCHECK+","+KEY_TYPE+" FROM " + TABLE_CONTACTS + " WHERE type LIKE " + "'mem' GROUP BY "+KEY_TITLE+" ORDER BY "+KEY_ID+" DESC";
  221.         SQLiteDatabase db = this.getWritableDatabase();
  222.         Cursor cursor = db.rawQuery(selectQuery, null);
  223.  
  224.         // looping through all rows and adding to list
  225.         if (cursor.moveToFirst()) {
  226.             do {
  227.                 Notification notifications = new Notification();
  228.                 notifications.setId(cursor.getInt(0));
  229.                 notifications.setTitle(cursor.getString(1));
  230.                 notifications.setMessage(cursor.getString(2));
  231.                 notifications.setTime(cursor.getString(3));
  232.                 notifications.setDate(cursor.getString(4));
  233.                 notifications.setHasCheck(cursor.getInt(5));
  234.                 notifications.setType(cursor.getString(6));
  235.                 // Adding contact to list
  236.                 notificationsList.add(notifications);
  237.             } while (cursor.moveToNext());
  238.         }
  239.         // return contact list
  240.         cursor.close();
  241.         db.close();
  242.         return notificationsList;
  243.     }
  244.  
  245.     public ArrayList<Notification> getAllCompNotification() {
  246.         ArrayList<Notification> notificationsList = new ArrayList<Notification>();
  247.         // Select All Query
  248.         String selectQuery = "SELECT "+KEY_ID+","+
  249.                 KEY_TITLE+","+KEY_MESSAGE+","+KEY_TIME+","+KEY_DATE+","+KEY_HASCHECK+","+KEY_TYPE+" FROM " + TABLE_CONTACTS + " WHERE type LIKE " + "'com' ORDER BY "+KEY_ID+" DESC";
  250.  
  251.         SQLiteDatabase db = this.getWritableDatabase();
  252.         Cursor cursor = db.rawQuery(selectQuery, null);
  253.  
  254.         // looping through all rows and adding to list
  255.         if (cursor.moveToFirst()) {
  256.             do {
  257.                 Notification notifications = new Notification();
  258.                 notifications.setId(cursor.getInt(0));
  259.                 notifications.setTitle(cursor.getString(1));
  260.                 notifications.setMessage(cursor.getString(2));
  261.                 notifications.setTime(cursor.getString(3));
  262.                 notifications.setDate(cursor.getString(4));
  263.                 notifications.setHasCheck(cursor.getInt(5));
  264.                 notifications.setType(cursor.getString(6));
  265.                 // Adding contact to list
  266.                 notificationsList.add(notifications);
  267.             } while (cursor.moveToNext());
  268.         }
  269.  
  270.         // return contact list
  271.         cursor.close();
  272.         db.close();
  273.         return notificationsList;
  274.     }
  275.  
  276.     public ArrayList<Notification> getAllMemNotification() {
  277.         ArrayList<Notification> notificationsList = new ArrayList<Notification>();
  278.         // Select All Query
  279.         String selectQuery = "SELECT "+KEY_ID+","+
  280.                 KEY_TITLE+","+KEY_MESSAGE+","+KEY_TIME+","+KEY_DATE+","+KEY_HASCHECK+","+KEY_TYPE+" FROM " + TABLE_CONTACTS + " WHERE type LIKE " + "'mem' ORDER BY "+KEY_ID+" DESC";
  281.         SQLiteDatabase db = this.getWritableDatabase();
  282.         Cursor cursor = db.rawQuery(selectQuery, null);
  283.  
  284.         // looping through all rows and adding to list
  285.         if (cursor.moveToFirst()) {
  286.             do {
  287.                 Notification notifications = new Notification();
  288.                 notifications.setId(cursor.getInt(0));
  289.                 notifications.setTitle(cursor.getString(1));
  290.                 notifications.setMessage(cursor.getString(2));
  291.                 notifications.setTime(cursor.getString(3));
  292.                 notifications.setDate(cursor.getString(4));
  293.                 notifications.setHasCheck(cursor.getInt(5));
  294.                 notifications.setType(cursor.getString(6));
  295.                 // Adding contact to list
  296.                 notificationsList.add(notifications);
  297.             } while (cursor.moveToNext());
  298.         }
  299.         // return contact list
  300.         cursor.close();
  301.         db.close();
  302.         return notificationsList;
  303.     }
  304.    
  305.     public int getNotificationCount() {
  306.         int count;
  307.         String countQuery = "SELECT  * FROM " + TABLE_CONTACTS;
  308.         SQLiteDatabase db = this.getReadableDatabase();
  309.         Cursor cursor = db.rawQuery(countQuery, null);
  310.         count = cursor.getCount();
  311.         cursor.close();
  312.         db.close();
  313.         // return count
  314.         return count;
  315.     }
  316.  
  317.     public int getComNotificationCountUncheckWithTile(String title) {
  318.         int count;
  319.         String countQuery = "SELECT  * FROM " + TABLE_CONTACTS+" WHERE "+KEY_TITLE+" LIKE '%"+title+"%' AND type LIKE 'com' AND "+KEY_HASCHECK+" = 0";
  320.         SQLiteDatabase db = this.getReadableDatabase();
  321.         Cursor cursor = db.rawQuery(countQuery, null);
  322.         count = cursor.getCount();
  323.         cursor.close();
  324.         db.close();
  325.         // return count
  326.         return count;
  327.     }
  328.  
  329.     public int getMemNotificationCountUncheckWithTile(String title) {
  330.         int count;
  331.         String countQuery = "SELECT  * FROM " + TABLE_CONTACTS+" WHERE "+KEY_TITLE+" LIKE '%"+title+"%' AND type LIKE 'mem' AND "+KEY_HASCHECK+" = 0";
  332.         SQLiteDatabase db = this.getReadableDatabase();
  333.         Cursor cursor = db.rawQuery(countQuery, null);
  334.         count = cursor.getCount();
  335.         cursor.close();
  336.         db.close();
  337.         // return count
  338.         return count;
  339.     }
  340.  
  341.     public int getComNotificationCount() {
  342.         int count;
  343.         String countQuery = "SELECT * FROM " + TABLE_CONTACTS + " WHERE type LIKE " + "'com' AND "+KEY_HASCHECK+" = 0";
  344.         SQLiteDatabase db = this.getReadableDatabase();
  345.         Cursor cursor = db.rawQuery(countQuery, null);
  346.         count = cursor.getCount();
  347.         cursor.close();
  348.         db.close();
  349.         // return count
  350.         return count;
  351.     }
  352.  
  353.     public int getComNotificationCountUncheck() {
  354.         int count;
  355.         String countQuery = "SELECT  * FROM " + TABLE_CONTACTS + " WHERE type LIKE " + "'com'" + " AND " + KEY_HASCHECK + " = 0";
  356.         SQLiteDatabase db = this.getReadableDatabase();
  357.         Cursor cursor = db.rawQuery(countQuery, null);
  358.         count = cursor.getCount();
  359.         cursor.close();
  360.         db.close();
  361.         // return count
  362.         return count;
  363.     }
  364.  
  365.  
  366.     public int getMemNotificationCountUncheck() {
  367.         int count;
  368.         String countQuery = "SELECT  * FROM " + TABLE_CONTACTS + " WHERE type LIKE " + "'mem'" + " AND " + KEY_HASCHECK + " = 0";
  369.         SQLiteDatabase db = this.getReadableDatabase();
  370.         Cursor cursor = db.rawQuery(countQuery, null);
  371.         count = cursor.getCount();
  372.         cursor.close();
  373.         db.close();
  374.         // return count
  375.         return count;
  376.     }
  377.  
  378.     public int getMemNotificationCount() {
  379.         int count;
  380.         String countQuery = "SELECT  * FROM " + TABLE_CONTACTS + " WHERE type LIKE " + "'mem'";
  381.         SQLiteDatabase db = this.getReadableDatabase();
  382.         Cursor cursor = db.rawQuery(countQuery, null);
  383.         count = cursor.getCount();
  384.         cursor.close();
  385.         db.close();
  386.         // return count
  387.         return count;
  388.     }
  389.    
  390.     public int updateNotification(Notification notifications) {
  391.         SQLiteDatabase db = this.getWritableDatabase();
  392.      
  393.         ContentValues values = new ContentValues();
  394.         values.put(KEY_TITLE, notifications.getTitle());
  395.         values.put(KEY_MESSAGE, notifications.getMessage());
  396.         values.put(KEY_TIME, notifications.getTime());
  397.         values.put(KEY_DATE, notifications.getDate());
  398.         values.put(KEY_HASCHECK, notifications.getHasCheck());
  399.         values.put(KEY_TYPE, notifications.getType());
  400.  
  401.         // updating row
  402.         int row = db.update(TABLE_CONTACTS, values, KEY_ID + " = ?",
  403.                 new String[] { String.valueOf(notifications.getId()) });
  404.  
  405.         db.close();
  406.         return row;
  407.     }
  408.    
  409.     public void deleteNotification(Notification notifications) {
  410.         SQLiteDatabase db = this.getWritableDatabase();
  411.         db.delete(TABLE_CONTACTS, KEY_ID + " = ?",
  412.                 new String[] { String.valueOf(notifications.getId()) });
  413.         db.close();
  414.     }
  415.  
  416. //    public void deleteNotification(String title) {
  417. //        SQLiteDatabase db = this.getWritableDatabase();
  418. //        db.delete(TABLE_CONTACTS, KEY_TITLE + " = ?",
  419. //                new String[] { title });
  420. //        db.close();
  421. //    }
  422.     public void deleteNotification(String id) {
  423.         SQLiteDatabase db = this.getWritableDatabase();
  424.         db.delete(TABLE_CONTACTS, KEY_ID + " = ?",
  425.                 new String[] { id });
  426.         db.close();
  427.     }
  428.  
  429.     public void deleteNotificationbytitle(String title) {
  430.         SQLiteDatabase db = this.getWritableDatabase();
  431.         db.delete(TABLE_CONTACTS, KEY_TITLE + " = ?",
  432.                 new String[] { title });
  433.         db.close();
  434.     }
  435.    
  436.     public void deleteAll()
  437.     {
  438.         SQLiteDatabase db= this.getWritableDatabase();
  439.         db.delete(TABLE_CONTACTS, null, null);
  440.         db.close();
  441.     }
  442.    
  443.     public void deleteTable(Context context)
  444.     {
  445.         //SQLiteDatabase db= this.getWritableDatabase();
  446.         //db.delete(TABLE_CONTACTS, null, null);
  447.         //db.execSQL("DROP TABLE IF EXISTS " + TABLE_CONTACTS);
  448.         //db.close();
  449.         context.deleteDatabase(DATABASE_NAME);
  450.     }
  451.    
  452.     public boolean checkDbExist()
  453.     {
  454.         SQLiteDatabase db= this.getReadableDatabase();
  455.         Cursor cursor = db.rawQuery("select DISTINCT tbl_name from sqlite_master where tbl_name = '"+TABLE_CONTACTS+"'", null);
  456.        
  457.         boolean flag = false;
  458.         if(cursor != null) {
  459.             if(cursor.getCount()>0)
  460.             {
  461.                 flag = true;
  462.             }
  463.             else
  464.             {
  465.                 flag = false;
  466.             }
  467.            cursor.close();
  468.         }
  469.         return flag;
  470.     }
  471.    
  472.     public Notification getLastNotification()
  473.     {
  474.         Notification notifications = null;
  475.         String query = "SELECT * FROM " +TABLE_CONTACTS;
  476.         SQLiteDatabase db = this.getReadableDatabase();
  477.         Cursor cursor = db.rawQuery(query, null);
  478.         if (cursor != null)
  479.         {
  480.             cursor.moveToLast();
  481.             //notification = new Notification(Integer.parseInt(cursor.getString(0)),
  482.             //    cursor.getString(2), cursor.getString(3), cursor.getString(1), cursor.getString(4));
  483.         }
  484.  
  485.         cursor.close();
  486.         db.close();
  487.         return notifications;
  488.     }
  489. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement