package com.jqmobile.core.server.db.dao;
import com.jqmobile.core.orm.ORM;
import com.jqmobile.core.orm.exception.ORMException;
import com.jqmobile.core.orm.exception.ORMNotDBTableException;
import com.jqmobile.core.utils.TypeArgFinder;
import com.jqmobile.core.utils.plain.Log;
public class SimpleDAO<T> extends DAO implements ISimpleDao<T>{
private Class<T> beanClass;
private ORM<T> orm;
@Override
public boolean create(T t) throws ORMException {
return 1==getORM().insert(t);
}
@Override
public boolean update(T t) throws ORMException {
return 1==getORM().update(t);
}
@Override
public boolean delete(String recid) throws ORMException {
return 1==getORM().delete(recid);
}
@Override
public T find(String recid) {
try {
return getORM().find(recid);
} catch (ORMException e) {
Log.getLog(getClass()).e(e);
return null;
}
}
protected ORM<T> getORM() throws ORMNotDBTableException, ORMException{
if(null == orm) orm = super.getORM(getBeanClass());
return orm;
}
@SuppressWarnings("unchecked")
protected Class<T> getBeanClass(){
if(null == beanClass){
beanClass = (Class<T>) TypeArgFinder.find(this.getClass(), SimpleDAO.class, 0);
}
return beanClass;
}
public void setBeanClass(Class<T> c){
this.beanClass = c;
}
@Override
public boolean put(T t) throws ORMException {
if(!update(t)){
return create(t);
}
return true;
}
}