Search in sources :

Example 1 with UserInterfaceDalErrorException

use of org.mx.dal.error.UserInterfaceDalErrorException in project main by JohnPeng739.

the class AccountManageServiceCommonImpl method saveAccount.

/**
 * {@inheritDoc}
 *
 * @see AccountManageService#saveAccount(AccountInfo)
 */
@Override
public Account saveAccount(AccountInfo accountInfo) {
    if (accountInfo == null) {
        throw new UserInterfaceSystemErrorException(UserInterfaceSystemErrorException.SystemErrors.SYSTEM_ILLEGAL_PARAM);
    }
    try {
        String accountId = accountInfo.getAccountId();
        Account account;
        if (!StringUtils.isBlank(accountId)) {
            account = accessor.getById(accountId, Account.class);
            if (account == null) {
                if (logger.isErrorEnabled()) {
                    logger.error(String.format("The Account entity[%s] not found.", accountId));
                }
                throw new UserInterfaceRbacErrorException(UserInterfaceRbacErrorException.RbacErrors.ACCOUNT_NOT_FOUND);
            }
        // 这里不允许修改密码,密码必须通过另外途径进行修改
        } else {
            String password = accountInfo.getPassword();
            if (StringUtils.isBlank(password)) {
                password = "ds110119";
            }
            account = EntityFactory.createEntity(Account.class);
            account.setPassword(DigestUtils.md5(password));
        }
        account.setCode(accountInfo.getCode());
        if (StringUtils.isBlank(accountInfo.getOwnerId())) {
            if (!"admin".equals(accountInfo.getCode())) {
                throw new UserInterfaceRbacErrorException(UserInterfaceRbacErrorException.RbacErrors.ACCOUNT_NOALLOCATE_USER);
            }
        } else {
            User owner = accessor.getById(accountInfo.getOwnerId(), User.class);
            if (owner == null) {
                throw new UserInterfaceRbacErrorException(UserInterfaceRbacErrorException.RbacErrors.USER_NOT_FOUND);
            }
            account.setOwner(owner);
            account.setName(owner.getFullName());
        }
        account.setDesc(accountInfo.getDesc());
        if (account.getRoles() != null && !account.getRoles().isEmpty()) {
            account.getRoles().clear();
        }
        for (String roleId : accountInfo.getRoleIds()) {
            Role role = accessor.getById(roleId, Role.class);
            if (role == null) {
                throw new UserInterfaceRbacErrorException(UserInterfaceRbacErrorException.RbacErrors.ROLE_NOT_FOUND);
            }
            account.getRoles().add(role);
        }
        account.setValid(accountInfo.isValid());
        account = this.save(account);
        if (operateLogService != null) {
            operateLogService.writeLog(String.format("保存账户[code=%s, name=%s]成功。", account.getCode(), account.getName()));
        }
        return account;
    } catch (UserInterfaceDalErrorException ex) {
        if (logger.isErrorEnabled()) {
            logger.error(ex);
        }
        throw new UserInterfaceDalErrorException(UserInterfaceDalErrorException.DalErrors.DB_OPERATE_FAIL);
    } catch (NoSuchAlgorithmException ex) {
        if (logger.isErrorEnabled()) {
            logger.error(ex);
        }
        throw new UserInterfaceRbacErrorException(UserInterfaceRbacErrorException.RbacErrors.ACCOUNT_DIGEST_PASSWORD_FAIL);
    }
}
Also used : Role(org.mx.comps.rbac.dal.entity.Role) Account(org.mx.comps.rbac.dal.entity.Account) UserInterfaceRbacErrorException(org.mx.comps.rbac.error.UserInterfaceRbacErrorException) User(org.mx.comps.rbac.dal.entity.User) UserInterfaceDalErrorException(org.mx.dal.error.UserInterfaceDalErrorException) UserInterfaceSystemErrorException(org.mx.error.UserInterfaceSystemErrorException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException)

Example 2 with UserInterfaceDalErrorException

use of org.mx.dal.error.UserInterfaceDalErrorException in project main by JohnPeng739.

the class ElasticAccessorRest method search.

private <T extends Base> SearchResponse search(List<GeneralAccessor.ConditionTuple> tuples, Class<T> clazz, Pagination pagination) throws UserInterfaceDalErrorException {
    SearchSourceBuilder builder = new SearchSourceBuilder();
    if (tuples == null || tuples.isEmpty()) {
        builder.query(QueryBuilders.matchAllQuery());
    } else {
        BoolQueryBuilder query = QueryBuilders.boolQuery();
        tuples.forEach(tuple -> query.must(QueryBuilders.termQuery(tuple.field, tuple.value)));
        builder.query(query);
    }
    if (pagination != null) {
        builder.from((pagination.getPage() - 1) * pagination.getSize());
        builder.size(pagination.getSize());
    }
    SearchRequest request = new SearchRequest(index);
    request.types(clazz.getName());
    request.source(builder);
    try {
        SearchResponse response = client.search(request);
        return response;
    } catch (Exception ex) {
        if (logger.isErrorEnabled()) {
            logger.error("Search fail from elastic.", ex);
        }
        throw new UserInterfaceDalErrorException(UserInterfaceDalErrorException.DalErrors.DB_OPERATE_FAIL);
    }
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) UserInterfaceDalErrorException(org.mx.dal.error.UserInterfaceDalErrorException) ElasticsearchStatusException(org.elasticsearch.ElasticsearchStatusException) IOException(java.io.IOException) UserInterfaceDalErrorException(org.mx.dal.error.UserInterfaceDalErrorException) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 3 with UserInterfaceDalErrorException

use of org.mx.dal.error.UserInterfaceDalErrorException in project main by JohnPeng739.

the class ElasticAccessorRest method remove.

/**
 * {@inheritDoc}
 *
 * @see ElasticAccessor#remove(Base, boolean)
 */
@Override
public <T extends Base> T remove(T t, boolean logicRemove) throws UserInterfaceDalErrorException {
    if (logicRemove) {
        // 逻辑删除
        t.setValid(false);
        return save(t);
    } else {
        // 物理删除
        t = getById(t.getId(), (Class<T>) t.getClass());
        if (t == null) {
            throw new UserInterfaceDalErrorException(UserInterfaceDalErrorException.DalErrors.ENTITY_NOT_FOUND);
        }
        DeleteRequest request = new DeleteRequest(index, t.getClass().getName(), t.getId());
        try {
            client.delete(request);
            return t;
        } catch (IOException ex) {
            if (logger.isErrorEnabled()) {
                logger.error("Delete the data from elastic fail.", ex);
            }
            throw new UserInterfaceDalErrorException(UserInterfaceDalErrorException.DalErrors.DB_OPERATE_FAIL);
        }
    }
}
Also used : UserInterfaceDalErrorException(org.mx.dal.error.UserInterfaceDalErrorException) IOException(java.io.IOException) DeleteRequest(org.elasticsearch.action.delete.DeleteRequest)

Example 4 with UserInterfaceDalErrorException

use of org.mx.dal.error.UserInterfaceDalErrorException in project main by JohnPeng739.

the class ElasticAccessorRest method save.

/**
 * {@inheritDoc}
 *
 * @see ElasticAccessor#save(Base)
 */
@Override
public <T extends Base> T save(T t) throws UserInterfaceDalErrorException {
    try {
        Class<T> clazz = (Class<T>) t.getClass();
        boolean isNew;
        if (StringUtils.isBlank(t.getId())) {
            // 新数据
            t.setId(DigestUtils.uuid());
            t.setCreatedTime(System.currentTimeMillis());
            isNew = true;
        } else {
            T check = getById(t.getId(), clazz);
            if (check != null) {
                // 修改数据
                t.setCreatedTime(check.getCreatedTime());
                if (check instanceof BaseDict) {
                    ((BaseDict) t).setCode(((BaseDict) check).getCode());
                }
                isNew = false;
            } else {
                isNew = true;
            }
        }
        t.setUpdatedTime(System.currentTimeMillis());
        t.setOperator(sessionDataStore.getCurrentUserCode());
        if (isNew) {
            IndexRequest request = new IndexRequest(index, t.getClass().getName(), t.getId());
            request.source(JSON.toJSONString(t), XContentType.JSON);
            client.index(request);
        } else {
            UpdateRequest request = new UpdateRequest(index, t.getClass().getName(), t.getId());
            request.doc(JSON.toJSONString(t), XContentType.JSON);
            client.update(request);
        }
        return getById(t.getId(), (Class<T>) t.getClass());
    } catch (IOException ex) {
        if (logger.isErrorEnabled()) {
            logger.error("Save the data into elastic fail.", ex);
        }
        throw new UserInterfaceDalErrorException(UserInterfaceDalErrorException.DalErrors.DB_OPERATE_FAIL);
    }
}
Also used : BaseDict(org.mx.dal.entity.BaseDict) UpdateRequest(org.elasticsearch.action.update.UpdateRequest) UserInterfaceDalErrorException(org.mx.dal.error.UserInterfaceDalErrorException) IOException(java.io.IOException) OpenIndexRequest(org.elasticsearch.action.admin.indices.open.OpenIndexRequest) IndexRequest(org.elasticsearch.action.index.IndexRequest) CreateIndexRequest(org.elasticsearch.action.admin.indices.create.CreateIndexRequest)

Example 5 with UserInterfaceDalErrorException

use of org.mx.dal.error.UserInterfaceDalErrorException in project main by JohnPeng739.

the class GeneralAccessorImpl method list.

/**
 * {@inheritDoc}
 *
 * @see GeneralAccessor#list(Class, boolean)
 */
@Override
public <T extends Base> List<T> list(Class<T> clazz, boolean isValid) {
    try {
        if (clazz.isInterface()) {
            clazz = EntityFactory.getEntityClass(clazz);
        }
        List<T> result;
        if (isValid) {
            List<ConditionTuple> tuples = new ArrayList<>();
            tuples.add(new ConditionTuple("valid", true));
            result = find(tuples, clazz);
        } else {
            result = template.findAll(clazz);
        }
        if (logger.isDebugEnabled()) {
            logger.debug(String.format("List %d entity[%s].", result.size(), clazz.getName()));
        }
        return result;
    } catch (ClassNotFoundException ex) {
        throw new UserInterfaceDalErrorException(UserInterfaceDalErrorException.DalErrors.ENTITY_INSTANCE_FAIL);
    }
}
Also used : UserInterfaceDalErrorException(org.mx.dal.error.UserInterfaceDalErrorException) ArrayList(java.util.ArrayList)

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