Last active
August 29, 2015 14:01
-
-
Save coffeearmy/392b894bc91a33ea51ee to your computer and use it in GitHub Desktop.
EntityHandler for Green Dao
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.lucentia.ahorrarelec.data; | |
import java.util.List; | |
import android.content.Context; | |
import android.database.sqlite.SQLiteDatabase; | |
import de.greenrobot.dao.AbstractDao; | |
import de.greenrobot.dao.Property; | |
import de.greenrobot.dao.query.WhereCondition; | |
public abstract class DataHandlerBase<T> { | |
private SQLiteDatabase db; | |
private DaoMaster mDaoMaster; | |
private DaoSession mDaoSession; | |
private AbstractDao<T, Long> mAbstractDao; | |
@SuppressWarnings("unchecked") | |
public DataHandlerBase(Context c, T entity) { | |
// Create Schema | |
DevOpenHelper helper = new DaoMaster.DevOpenHelper(c, "elec-db-v1", | |
null); | |
try { | |
db = helper.getWritableDatabase(); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
mDaoMaster = new DaoMaster(db); | |
mDaoSession = mDaoMaster.newSession(); | |
//Create Dao of the T Object | |
mAbstractDao= (AbstractDao<T, Long>) mDaoSession.getDao(entity.getClass()); | |
} | |
//BD operations | |
//Insert | |
/** Return ID inserted*/ | |
public long insertItem(T entity){ | |
return mAbstractDao.insert(entity); | |
} | |
//Update | |
public void updateItem(T entity){ | |
mAbstractDao.update(entity); | |
} | |
//Delete | |
public void deleteItem(T entity){ | |
mAbstractDao.delete(entity); | |
} | |
//Refresh | |
public void refreshItem(T entity){ | |
mAbstractDao.refresh(entity); | |
} | |
//Select one | |
public T getItem(Long id){ | |
//Prevent to get a value from cache | |
mAbstractDao.refresh(mAbstractDao.load(id)); | |
return mAbstractDao.load(id); | |
} | |
//Get All | |
public List<T> getAllItems(T entity){ | |
mDaoSession.clear(); | |
return mAbstractDao.loadAll(); | |
} | |
//Parameterized Query | |
public List<T> getItems(WhereCondition condition,Property orderby){ | |
return mAbstractDao.queryBuilder() | |
.where(condition) | |
.orderDesc(orderby) | |
.list(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ItemHandlerImpl extends DataHandlerBase<Item> { | |
private static ItemHandlerImpl mItemHandler; | |
public ElectHandlerImpl(Context c, Electrodomesticos entity) { | |
super(c, entity); | |
} | |
public static ItemHandlerImpl getInstance(Context c){ | |
if(mItemHandler ==null){ | |
mItemHandler = new ItemHandlerImpl(c,new Item()); | |
} | |
return mItemHandler; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment