Search in sources :

Example 6 with UserInterfaceDalErrorException

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);
    }
}
Also used : TextQuery(org.springframework.data.mongodb.core.query.TextQuery) Query(org.springframework.data.mongodb.core.query.Query) UserInterfaceDalErrorException(org.mx.dal.error.UserInterfaceDalErrorException) TextCriteria(org.springframework.data.mongodb.core.query.TextCriteria) Criteria(org.springframework.data.mongodb.core.query.Criteria)

Example 7 with UserInterfaceDalErrorException

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);
    }
}
Also used : Pagination(org.mx.dal.Pagination) TextQuery(org.springframework.data.mongodb.core.query.TextQuery) Query(org.springframework.data.mongodb.core.query.Query) UserInterfaceDalErrorException(org.mx.dal.error.UserInterfaceDalErrorException)

Example 8 with UserInterfaceDalErrorException

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);
    }
}
Also used : Query(javax.persistence.Query) UserInterfaceDalErrorException(org.mx.dal.error.UserInterfaceDalErrorException) Transactional(org.springframework.transaction.annotation.Transactional)

Example 9 with UserInterfaceDalErrorException

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);
    }
}
Also used : TextQuery(org.springframework.data.mongodb.core.query.TextQuery) Query(org.springframework.data.mongodb.core.query.Query) TextCriteria(org.springframework.data.mongodb.core.query.TextCriteria) UserInterfaceDalErrorException(org.mx.dal.error.UserInterfaceDalErrorException) TextQuery(org.springframework.data.mongodb.core.query.TextQuery)

Aggregations

UserInterfaceDalErrorException (org.mx.dal.error.UserInterfaceDalErrorException)9 IOException (java.io.IOException)3 Query (org.springframework.data.mongodb.core.query.Query)3 TextQuery (org.springframework.data.mongodb.core.query.TextQuery)3 TextCriteria (org.springframework.data.mongodb.core.query.TextCriteria)2 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 ArrayList (java.util.ArrayList)1 Query (javax.persistence.Query)1 ElasticsearchStatusException (org.elasticsearch.ElasticsearchStatusException)1 CreateIndexRequest (org.elasticsearch.action.admin.indices.create.CreateIndexRequest)1 OpenIndexRequest (org.elasticsearch.action.admin.indices.open.OpenIndexRequest)1 DeleteRequest (org.elasticsearch.action.delete.DeleteRequest)1 IndexRequest (org.elasticsearch.action.index.IndexRequest)1 SearchRequest (org.elasticsearch.action.search.SearchRequest)1 SearchResponse (org.elasticsearch.action.search.SearchResponse)1 UpdateRequest (org.elasticsearch.action.update.UpdateRequest)1 BoolQueryBuilder (org.elasticsearch.index.query.BoolQueryBuilder)1 SearchSourceBuilder (org.elasticsearch.search.builder.SearchSourceBuilder)1 Account (org.mx.comps.rbac.dal.entity.Account)1 Role (org.mx.comps.rbac.dal.entity.Role)1