use of org.mx.dal.error.UserInterfaceDalErrorException in project main by JohnPeng739.
the class GeneralAccessorImpl method find.
/**
* {@inheritDoc}
*
* @see GeneralAccessor#find(List, Class)
*/
@Override
public <T extends Base> List<T> find(List<ConditionTuple> tuples, Class<T> clazz) throws UserInterfaceDalErrorException {
try {
if (clazz.isInterface()) {
clazz = EntityFactory.getEntityClass(clazz);
}
Query query;
Criteria cd;
switch(tuples.size()) {
case 0:
query = new Query();
break;
case 1:
ConditionTuple tuple = tuples.get(0);
cd = where(tuple.field).is(tuple.value);
query = query(cd);
break;
default:
cd = where(tuples.get(0).field).is(tuples.get(0).value);
for (int index = 1; index < tuples.size(); index++) {
tuple = tuples.get(index);
cd.and(tuple.field).is(tuple.value);
}
query = query(cd);
break;
}
return template.find(query, clazz);
} catch (ClassNotFoundException ex) {
if (logger.isErrorEnabled()) {
logger.error(String.format("Condition find entity[%s] fail, condition: %s.", clazz.getName(), StringUtils.merge(tuples, ",")), ex);
}
throw new UserInterfaceDalErrorException(UserInterfaceDalErrorException.DalErrors.ENTITY_INSTANCE_FAIL);
}
}
use of org.mx.dal.error.UserInterfaceDalErrorException in project main by JohnPeng739.
the class GeneralAccessorImpl method list.
/**
* {@inheritDoc}
*
* @see GeneralAccessor#list(Pagination, Class, boolean)
*/
@Override
public <T extends Base> List<T> list(Pagination pagination, Class<T> clazz, boolean isValid) {
if (pagination == null) {
pagination = new Pagination();
}
try {
if (clazz.isInterface()) {
clazz = EntityFactory.getEntityClass(clazz);
}
pagination.setTotal((int) count(clazz, isValid));
int skip = (pagination.getPage() - 1) * pagination.getSize();
int limit = pagination.getSize();
if (isValid) {
return template.find(query(where("valid").is(true)).skip(skip).limit(limit), clazz);
} else {
return template.find(new Query().skip(skip).limit(limit), clazz);
}
} catch (ClassNotFoundException ex) {
throw new UserInterfaceDalErrorException(UserInterfaceDalErrorException.DalErrors.ENTITY_INSTANCE_FAIL);
}
}
use of org.mx.dal.error.UserInterfaceDalErrorException in project main by JohnPeng739.
the class GeneralAccessorImpl method count.
/**
* {@inheritDoc}
*
* @see GeneralAccessor#count(Class, boolean)
*/
@Transactional(readOnly = true)
@Override
public <T extends Base> long count(Class<T> clazz, boolean isValid) throws UserInterfaceDalErrorException {
try {
if (clazz.isInterface()) {
clazz = EntityFactory.getEntityClass(clazz);
}
Query query = entityManager.createQuery(String.format("SELECT COUNT(entity) FROM %s entity %s", clazz.getName(), isValid ? "WHERE entity.valid = TRUE" : ""));
long count = (long) query.getSingleResult();
if (logger.isDebugEnabled()) {
logger.debug(String.format("Count %d %s entity[%s].", count, isValid ? "valid" : "", clazz.getName()));
}
return count;
} catch (ClassNotFoundException ex) {
throw new UserInterfaceDalErrorException(UserInterfaceDalErrorException.DalErrors.ENTITY_INSTANCE_FAIL);
}
}
use of org.mx.dal.error.UserInterfaceDalErrorException in project main by JohnPeng739.
the class GeneralAccessorImpl method search.
/**
* {@inheritDoc}
*
* @see GeneralTextSearchAccessor#search(Pagination, List, boolean, Class)
*/
@Override
public <T extends Base> List<T> search(Pagination pagination, List<String> contents, boolean valid, Class<T> clazz) {
try {
if (clazz.isInterface()) {
clazz = EntityFactory.getEntityClass(clazz);
}
Query query = new TextQuery(new TextCriteria().matchingAny(contents.toArray(new String[0])));
if (valid) {
query.addCriteria(where("valid").is(true));
}
if (pagination != null) {
pagination.setTotal((int) template.count(query, clazz));
query.skip((pagination.getPage() - 1) * pagination.getSize());
query.limit(pagination.getSize());
}
return template.find(query, clazz);
} catch (ClassNotFoundException ex) {
if (logger.isErrorEnabled()) {
logger.error(String.format("Text search entity[%s] fail, condition: %s.", clazz.getName(), StringUtils.merge(contents, ",")), ex);
}
throw new UserInterfaceDalErrorException(UserInterfaceDalErrorException.DalErrors.ENTITY_INSTANCE_FAIL);
}
}
Aggregations