Advertisement
vergepuppeter

Untuk mu zul

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