Android Open Source - BaseAndroid Base D B Controller






From Project

Back to project page BaseAndroid.

License

The source code is released under:

MIT License

If you think the Android project BaseAndroid listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/**
 * File: BaseDBController//w w w  .j ava  2 s .  c  om
 * CreationDate: 31/07/13
 * Author: "M. en C. Javier Silva Perez (JSP)"
 * Description: 
 *
 * Class that will be the connection between database and view classes, so it will include functions for inserting,
 * deleting, updating and getting rows in the data base, but will use DAO objects for getting and returning data
 */
package com.cmovil.baseandroid.controller;

import android.database.Cursor;
import android.util.Log;

import com.cmovil.baseandroid.dao.db.BaseDBDAO;
import com.cmovil.baseandroid.dao.db.DBException;
import com.cmovil.baseandroid.dao.db.DatabaseDictionary;
import com.cmovil.baseandroid.model.db.BaseModel;
import com.cmovil.baseandroid.util.KeyDictionary;

import java.lang.reflect.ParameterizedType;
import java.util.LinkedList;
import java.util.List;

/**
 * Class that will be the connection between database and view classes, so it will include generic functions for
 * inserting,
 * deleting, updating and getting rows in the data base, but will use DAO objects for getting and returning data
 *
 * @author "M. en C. Javier Silva Perez (JSP)"
 * @version 1.0
 * @since 19/09/13
 */
public abstract class BaseDBController<T extends BaseModel> {

  BaseDBDAO<T> baseDBDAO;

  /**
   * Constructor
   *
   * @param baseDBDAO
   *   Data base dao to be used in this controller
   */
  protected BaseDBController(BaseDBDAO<T> baseDBDAO) {
    this.baseDBDAO = baseDBDAO;
  }

  /**
   * Columns that will be returned by the sql statements
   *
   * @return A string array with the columns that will be returned in all sql statements
   */
  protected abstract String[] getColumns();

  /**
   * Insert an object to the data base
   *
   * @param insertObject
   *   Object to be get the values to insert
   * @return The id of the inserted row, or -1 if an error occurs while opening the data base
   *
   * @throws com.cmovil.baseandroid.dao.db.DBException
   *   if something goes wrong during SQL statements execution
   */
  public Integer insert(T insertObject) throws DBException {
    return baseDBDAO.insert(insertObject);
  }

  /**
   * Delete an specific row from the selected table in database
   *
   * @param id
   *   Database id to be deleted from the data base table
   * @return the number of rows affected, 0 otherwise. To remove all rows and get a
   * count pass "1" as the whereClause.
   *
   * @throws DBException
   *   if something goes wrong during SQL statements execution
   */
  public Integer delete(Integer id) throws DBException {
    return baseDBDAO.delete(id);
  }

  /**
   * Updates all the fields of the object, this function will update all the columns of the row,
   * so be sure to set the correct values to it
   *
   * @param objectToUpdate
   *   Object with the all the values to update
   * @return the number of rows affected, 0 otherwise. To remove all rows and get a
   * count pass "1" as the whereClause.
   *
   * @throws DBException
   *   if something goes wrong during SQL statements execution
   */
  public Integer update(T objectToUpdate) throws DBException {
    return baseDBDAO.update(objectToUpdate);
  }

  /**
   * Fill up an object with cursor values, the cursor must be valid or exceptions could be thrown
   *
   * @param cursor
   *   Valid cursor for extract object information
   * @return A State object fill up with cursor information
   */
  public abstract T fillUpObject(Cursor cursor);/* {
    State state = new State();
    state.setId(cursor.getInt(cursor.getColumnIndexOrThrow(DatabaseDictionary.State._ID)));
    state.setIdServer(cursor.getInt(cursor.getColumnIndexOrThrow(DatabaseDictionary.State.COLUMN_NAME_ID_SERVER)));
    state.setName(cursor.getString(cursor.getColumnIndexOrThrow(DatabaseDictionary.State.COLUMN_NAME_NAME)));
    return state;
  }*/

  /**
   * Gets the object with the selected id
   *
   * @param id
   *   Id that will be searched in the database
   * @return A {@link com.cmovil.baseandroid.model.db.State} object with the values from the database or an empty
   * object
   * if no results where found
   *
   * @throws com.cmovil.baseandroid.dao.db.DBException
   *   if something goes wrong during SQL statements execution
   */
  public T getById(Integer id) throws DBException {

    Cursor res = baseDBDAO.getById(id, getColumns());
    //If the cursor has at least one element, create the corresponding State object, if not,
    // return an empty object
    if (res != null && res.moveToFirst()) {
      T object = fillUpObject(res);
      res.close();
      return object;

    }
    if (res != null) {
      res.close();
    }

    try {
      //Return an empty instance of the object using the no-parameters constructor
      return (T) ((Class) ((ParameterizedType) this.getClass().getGenericSuperclass())
        .getActualTypeArguments()[0]).newInstance();
    } catch (InstantiationException e) {
      Log.e(KeyDictionary.TAG, "Return error");
    } catch (IllegalAccessException e) {
      Log.e(KeyDictionary.TAG, "Return error");
    }
    return null;

  }

  /**
   * @return A list of all the states of the db
   *
   * @throws com.cmovil.baseandroid.dao.db.DBException
   *   if something goes wrong during SQL statements execution
   */
  public List<T> getAll() throws DBException {
    List<T> res = new LinkedList<T>();


    Cursor cursor = baseDBDAO.getAll(DatabaseDictionary.State.NAME, getColumns(), null);
    //If the cursor has at least one element, create the corresponding State object, if not,
    // return an empty object
    if (cursor != null && cursor.moveToFirst()) {
      do {
        T t = fillUpObject(cursor);
        res.add(t);
      } while (cursor.moveToNext());
      cursor.close();
    }
    if (cursor != null) {
      cursor.close();
    }
    return res;
  }
}




Java Source Code List

com.cmovil.baseandroid.controller.BaseDBController.java
com.cmovil.baseandroid.controller.SampleController.java
com.cmovil.baseandroid.dao.db.BaseDBDAO.java
com.cmovil.baseandroid.dao.db.DBException.java
com.cmovil.baseandroid.dao.db.DatabaseDictionary.java
com.cmovil.baseandroid.dao.db.SampleDAO.java
com.cmovil.baseandroid.dao.db.helper.BaseDatabaseOpenHelper.java
com.cmovil.baseandroid.dao.db.helper.SampleOpenHelper.java
com.cmovil.baseandroid.dao.ws.BaseMessageWS.java
com.cmovil.baseandroid.dao.ws.InvalidResponseException.java
com.cmovil.baseandroid.dao.ws.WSClient.java
com.cmovil.baseandroid.model.db.BaseModel.java
com.cmovil.baseandroid.model.db.State.java
com.cmovil.baseandroid.model.ws.MessageErrorCode.java
com.cmovil.baseandroid.model.ws.Parser.java
com.cmovil.baseandroid.util.CMUtils.java
com.cmovil.baseandroid.util.CustomCatalogComparator.java
com.cmovil.baseandroid.util.KeyDictionary.java
com.cmovil.baseandroid.view.BaseActionBarActivity.java
com.cmovil.baseandroid.view.BaseDrawerActivity.java
com.cmovil.baseandroid.view.SplashActivity.java