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);
}
}
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);
}
}
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);
}
}
}
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);
}
}
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);
}
}
Aggregations