content: /*
   *
   *  *    Copyright (C) 2019 Amit Shekhar
   *  *    Copyright (C) 2011 Android Open Source Project
   *  *
   *  *    Licensed under the Apache License, Version 2.0 (the "License");
   *  *    you may not use this file except in compliance with the License.
   *  *    You may obtain a copy of the License at
   *  *
   *  *        http://www.apache.org/licenses/LICENSE-2.0
   *  *
   *  *    Unless required by applicable law or agreed to in writing, software
   *  *    distributed under the License is distributed on an "AS IS" BASIS,
   *  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   *  *    See the License for the specific language governing permissions and
   *  *    limitations under the License.
   *
   */
  
  package com.sample.database;
  
  import android.content.ContentValues;
  import android.content.Context;
  import android.database.Cursor;
  import android.database.DatabaseUtils;
  import android.database.sqlite.SQLiteDatabase;
  import android.database.sqlite.SQLiteOpenHelper;
  
  import java.util.ArrayList;
  import java.util.Calendar;
  
  /**
   * Created by amitshekhar on 16/11/16.
   */
  public class ContactDBHelper extends SQLiteOpenHelper {
  
      public static final String DATABASE_NAME = "Contact.db";
      public static final String CONTACTS_TABLE_NAME = "contacts";
      public static final String CONTACTS_COLUMN_ID = "id";
      public static final String CONTACTS_COLUMN_NAME = "name";
      public static final String CONTACTS_COLUMN_EMAIL = "email";
      public static final String CONTACTS_COLUMN_STREET = "street";
      public static final String CONTACTS_COLUMN_CITY = "place";
      public static final String CONTACTS_COLUMN_PHONE = "phone";
      public static final String CONTACTS_CREATED_AT = "createdAt";
  
      public ContactDBHelper(Context context) {
          super(context, DATABASE_NAME, null, 1);
      }
  
      @Override
      public void onCreate(SQLiteDatabase db) {
          // TODO Auto-generated method stub
          db.execSQL(
                  "create table contacts " +
                          "(id integer primary key, name text, phone text, email text, street text, place text, createdAt integer)"
          );
      }
  
      @Override
      public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
          // TODO Auto-generated method stub
          db.execSQL("DROP TABLE IF EXISTS contacts");
          onCreate(db);
      }
  
      public boolean insertContact(String name, String phone, String email, String street, String place) {
          SQLiteDatabase db = this.getWritableDatabase();
          ContentValues contentValues = new ContentValues();
          contentValues.put("name", name);
          contentValues.put("phone", phone);
          contentValues.put("email", email);
          contentValues.put("street", street);
          contentValues.put("place", place);
          contentValues.put(CONTACTS_CREATED_AT, Calendar.getInstance().getTimeInMillis());
          db.insert("contacts", null, contentValues);
          return true;
      }
  
      public Cursor getData(int id) {
          SQLiteDatabase db = this.getReadableDatabase();
          Cursor res = db.rawQuery("select * from contacts where id=" + id + "", null);
          return res;
      }
  
      public int numberOfRows() {
          SQLiteDatabase db = this.getReadableDatabase();
          int numRows = (int) DatabaseUtils.queryNumEntries(db, CONTACTS_TABLE_NAME);
          return numRows;
      }
  
      public boolean updateContact(Integer id, String name, String phone, String email, String street, String place) {
          SQLiteDatabase db = this.getWritableDatabase();
          ContentValues contentValues = new ContentValues();
          contentValues.put("name", name);
          contentValues.put("phone", phone);
          contentValues.put("email", email);
          contentValues.put("street", street);
          contentValues.put("place", place);
          db.update("contacts", contentValues, "id = ? ", new String[]{Integer.toString(id)});
          return true;
      }
  
      public Integer deleteContact(Integer id) {
          SQLiteDatabase db = this.getWritableDatabase();
          return db.delete("contacts",
                  "id = ? ",
                  new String[]{Integer.toString(id)});
      }
  
      public ArrayList<String> getAllCotacts() {
          ArrayList<String> arrayList = new ArrayList<>();
  
          //hp = new HashMap();
          SQLiteDatabase db = this.getReadableDatabase();
          Cursor res = db.rawQuery("select * from contacts", null);
          res.moveToFirst();
  
          while (!res.isAfterLast()) {
              arrayList.add(res.getString(res.getColumnIndex(CONTACTS_COLUMN_NAME)));
              res.moveToNext();
          }
          return arrayList;
      }
  
      public int count() {
          SQLiteDatabase db = getReadableDatabase();
          Cursor cursor = db.rawQuery("select * from contacts", null);
          if (cursor != null && cursor.getCount() > 0) {
              cursor.moveToFirst();
              return cursor.getInt(0);
          } else {
              return 0;
          }
      }
  }
  