Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
package panda.dao;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import panda.bean.BeanHandler;
import panda.bean.Beans;
import panda.cast.Castors;
import panda.dao.entity.Entity;
import panda.dao.entity.EntityDao;
import panda.dao.entity.EntityField;
import panda.dao.query.DataQuery;
import panda.dao.query.Join;
import panda.dao.query.Query;
import panda.lang.Arrays;
import panda.lang.Asserts;
import panda.lang.Classes;
import panda.lang.Collections;
import panda.lang.Objects;
/**
* !! thread-unsafe !!
*/
public abstract class AbstractDao implements Dao {
protected final DaoClient daoClient;
protected int timeout;
public AbstractDao(DaoClient daoClient) {
this.daoClient = daoClient;
timeout = DEFAULT_TIMEOUT;
}
/**
* @return the DaoClient
*/
@Override
public DaoClient getDaoClient() {
return daoClient;
}
/**
* @return database meta
*/
@Override
public DatabaseMeta meta() {
return daoClient.getDatabaseMeta();
}
/**
* @return the timeout (seconds)
*/
@Override
public int getTimeout() {
return timeout;
}
/**
* @param timeout the timeout (seconds) to set
*/
@Override
public void setTimeout(int timeout) {
this.timeout = timeout;
}
//---------------------------------------------------------------------------------
/**
* @return beans
*/
protected Beans getBeans() {
return daoClient.getBeans();
}
/**
* @return castors
*/
protected Castors getCastors() {
return daoClient.getCastors();
}
//---------------------------------------------------------------------------------
protected void assertTable(String table) {
Asserts.notEmpty(table, "The table is empty");
}
protected void assertTable(Entity> entity) {
assertEntity(entity);
Asserts.notEmpty(entity.getTable(), "The table of [%s] is undefined", entity.getType().toString());
}
protected void assertType(Class> type) {
Asserts.notNull(type, "The type is null");
}
protected void assertEntity(Entity> entity) {
Asserts.notNull(entity, "The entity is null");
}
protected void assertEntityQuery(Query> query) {
assertQuery(query);
Asserts.notNull(query.getEntity(), "This entity of query is null");
}
protected void assertQuery(Query> query) {
Asserts.notNull(query, "The query is null");
}
protected void assertEntity(Entity> entity, Class> type) {
Asserts.notNull(entity, "The entity of [%s] is undefined", type.toString());
}
protected void assertCollection(Collection> col) {
Asserts.notNull(col, "The collection is null");
}
protected void assertObject(Object object) {
Asserts.notNull(object, "The object is null");
}
protected void assertTransaction(Runnable transaction) {
Asserts.notNull(transaction, "The transaction is null");
}
@SuppressWarnings("unchecked")
protected T createEntityData(Entity en) throws DaoException {
try {
return en == null ? (T)(new HashMap()) : Classes.newInstance(en.getType());
}
catch (Exception e) {
throw new DaoException(e);
}
}
//---------------------------------------------------------------------------------
/**
* @param type record type
* @return the entity
*/
@Override
public Entity getEntity(Class type) {
assertType(type);
return daoClient.getEntity(type);
}
/**
* @param type record type
* @return the entity
*/
@Override
public EntityDao getEntityDao(Class type) {
assertType(type);
return new EntityDao(this, type);
}
/**
* @param type record type
* @param param argument used for dynamic table
* @return the entity
*/
@Override
public EntityDao getEntityDao(Class type, Object param) {
assertType(type);
return new EntityDao(this, type, param);
}
/**
* @param query query
* @return the table name of query
*/
@Override
public String getTableName(Query> query) {
return getDaoClient().getTableName(query);
}
/**
* @param entity entity
* @return the table name of entity
*/
@Override
public String getTableName(Entity> entity) {
return getDaoClient().getTableName(entity);
}
/**
* @param entity entity
* @return the view name of entity
*/
@Override
public String getViewName(Entity> entity) {
return getDaoClient().getViewName(entity);
}
//---------------------------------------------------------------------------------
/**
* drop a table if exists
*
* @param type record type
*/
@Override
public void drop(Class> type) {
drop(getEntity(type));
}
/**
* drop a table if exists
*
* @param entity entity
*/
@Override
public abstract void drop(Entity> entity);
/**
* drop a table if exists
*
* @param table table name
*/
@Override
public abstract void drop(String table);
//-------------------------------------------------------------------------
/**
* create table
*
* @param type record type
*/
@Override
public void create(Class> type) {
create(getEntity(type));
}
/**
* create table
*
* @param entity entity
*/
@Override
public abstract void create(Entity> entity);
//-------------------------------------------------------------------------
/**
* create table ddl
*
* @param type record type
*/
@Override
public String ddl(Class> type) {
return ddl(getEntity(type));
}
/**
* create table ddl
*
* @param entity entity
*/
@Override
public abstract String ddl(Entity> entity);
//-------------------------------------------------------------------------
/**
* check a table exists in the data store.
*
* @param table table name
* @return true if the record or the table exists in the data store
*/
@Override
public boolean exists(String table) {
return existsByTable(table);
}
/**
* check a record exists in the data store.
* if the keys is not supplied, then check the table existence.
*
* @param type record type
* @param keys record keys (int, string or java bean with keys)
* @return true if the record or the table exists in the data store
*/
@Override
public boolean exists(Class> type, Object ... keys) {
return existsByKeys(getEntity(type), keys);
}
/**
* check a record exists in the data store.
* if the keys is not supplied, then check the table existence.
*
* @param entity entity
* @param keys record keys (int, string or java bean with keys)
* @return true if the record or the table exists in the data store
*/
@Override
public boolean exists(Entity> entity, Object ... keys) {
assertEntity(entity);
return existsByKeys(entity, keys);
}
/**
* check a record exists in the data store.
* if the query is not supplied, then check the table existence.
*
* @param query query
* @return true if the record or the table exists in the data store
*/
@Override
public boolean exists(Query> query) {
return existsByQuery(cloneQuery(query));
}
/**
* check a table exists in the data store.
*
* @param table table name
* @return true if the record or the table exists in the data store
*/
protected abstract boolean existsByTable(String table);
/**
* check a record exists in the data store.
* if the keys is not supplied, then check the table existence.
*
* @param entity entity
* @param keys record keys (int, string or java bean with keys)
* @return true if the record or the table exists in the data store
*/
protected abstract boolean existsByKeys(Entity> entity, Object ... keys);
/**
* check a record exists in the data store.
* if the query is not supplied, then check the table existence.
*
* @param query query
* @return true if the record or the table exists in the data store
*/
protected abstract boolean existsByQuery(DataQuery> query);
//-------------------------------------------------------------------------
/**
* get a record by the supplied keys
*
* @param type record type
* @param keys record keys (int, string or java bean with keys)
*/
@Override
public T fetch(Class type, Object ... keys) {
return fetchByKeys(getEntity(type), keys);
}
/**
* get a record by the supplied keys
*
* @param entity entity
* @param keys record keys (int, string or java bean with keys)
*/
@Override
public T fetch(Entity entity, Object ... keys) {
assertEntity(entity);
return fetchByKeys(entity, keys);
}
/**
* get a record by the supplied query
*
* @param query query
* @return record
*/
@Override
public T fetch(Query query) {
return fetchByQuery(cloneQuery(query));
}
/**
* get a record by the supplied keys
*
* @param entity entity
* @param keys record keys (int, string or java bean with keys)
* @return record
*/
protected T fetchByKeys(Entity entity, Object ... keys) {
assertEntity(entity);
DataQuery query = createQuery(entity);
queryPrimaryKey(query, keys);
return fetchByQuery(query);
}
/**
* get a record by the supplied query
*
* @param query query
* @return record
*/
protected abstract T fetchByQuery(DataQuery query);
//-------------------------------------------------------------------------
/**
* count all records.
*
* @param type record type
* @return record count
*/
@Override
public long count(Class> type) {
return count(createQuery(type));
}
/**
* count all records.
*
* @param entity entity
* @return record count
*/
@Override
public long count(Entity> entity) {
assertEntity(entity);
return count(createQuery(entity));
}
/**
* count all records.
*
* @param table table name
* @return record count
*/
@Override
public long count(String table) {
return count(createQuery(table));
}
/**
* count records by the supplied query.
*
* @param query query
* @return record count
*/
@Override
public long count(Query> query) {
return countByQuery(cloneQuery(query));
}
/**
* count records by the supplied query.
*
* @param query query
* @return record count
*/
protected abstract long countByQuery(Query> query);
//-------------------------------------------------------------------------
/**
* select all records.
*
* @param type record type
* @return record list
*/
@Override
public List select(Class type) {
return selectByQuery(createQuery(type));
}
/**
* select all records.
*
* @param entity entity
* @return record list
*/
@Override
public List select(Entity entity) {
assertEntity(entity);
return selectByQuery(createQuery(entity));
}
/**
* select all records.
*
* @param table table name
* @return record(a map) list
*/
@Override
public List