use of com.duangframework.core.exceptions.ServiceException in project duangframework by tcrct.
the class CurdService method delete.
/**
* 删除记录
*/
public boolean delete(String id) throws ServiceException {
if (!ToolsKit.isValidDuangId(id)) {
throw new ServiceException("it is not ObjectId");
}
MongoQuery<T> mongoQuery = new MongoQuery<>();
mongoQuery.eq(IdEntity.ID_FIELD, id);
MongoUpdate<T> mongoUpdate = new MongoUpdate<>();
mongoUpdate.set(IdEntity.STATUS_FIELD, IdEntity.STATUS_FIELD_DELETE);
try {
long count = getMongoDao().update(mongoQuery, mongoUpdate);
if (count > 0 && (getCacheDao() != null)) {
getCacheDao().delete(entityClass, id);
}
return true;
} catch (Exception e) {
throw new ServiceException("delete id[" + id + "] record is fail: " + e.getMessage(), e);
}
}
use of com.duangframework.core.exceptions.ServiceException in project duangframework by tcrct.
the class CurdService method findById.
/**
* 查找记录
*/
public <T> T findById(String id) throws ServiceException {
if (!ToolsKit.isValidDuangId(id)) {
throw new ServiceException("it is not ObjectId");
}
T recordObj = null;
try {
if (getCacheDao() != null) {
recordObj = (T) getCacheDao().findById(id, getEntityClass());
if (ToolsKit.isNotEmpty(recordObj)) {
return recordObj;
}
}
MongoQuery<T> mongoQuery = new MongoQuery<>();
mongoQuery.eq(IdEntity.ID_FIELD, id);
recordObj = (T) getMongoDao().findOne(mongoQuery);
if (ToolsKit.isNotEmpty(recordObj)) {
if (getCacheDao() != null) {
getCacheDao().save((IdEntity) recordObj);
}
}
} catch (Exception e) {
throw new ServiceException("findById id[" + id + "] record is fail: " + e.getMessage(), e);
}
return recordObj;
}
use of com.duangframework.core.exceptions.ServiceException in project duangframework by tcrct.
the class BaseController method returnFailJson.
/**
* 返回错误信息到客户端
*
* @param ex
* 自定义ServiceException异常
*/
protected void returnFailJson(Exception ex) {
String message = ex.getMessage();
int code = IEnums.IENUMS_FAIL_CODE;
if (ex instanceof ServiceException) {
ServiceException se = (ServiceException) ex;
IEnums enums = se.getEnums();
if (ToolsKit.isEmpty(enums)) {
code = ToolsKit.isEmpty(se.getCode()) ? IEnums.IENUMS_FAIL_CODE : se.getCode();
message = ToolsKit.isEmpty(se.getMessage()) ? IEnums.IENUMS_FAIL_MESSAGE : se.getMessage();
} else {
code = enums.getCode();
message = enums.getMessage();
}
ToolsKit.console("returnFail:" + se.getStackTrace()[0].getClassName() + "-->" + se.getStackTrace()[0].getMethodName() + "-->" + se.getStackTrace()[0].getLineNumber() + ":" + message + ":" + request.getRequestURI() + ":" + JSON.toJSONString(getAllParams()));
} else {
logger.warn(ex.getMessage(), ex);
}
ReturnDto<Map<String, Object>> dto = new ReturnDto<Map<String, Object>>();
HeadDto head = ToolsKit.getThreadLocalDto();
if (ToolsKit.isEmpty(head)) {
head = new HeadDto();
head.setUri(request.getRequestURI());
}
head.setRet(code);
head.setMsg(message);
dto.setHead(head);
dto.setData(getAllParams());
returnJson(dto);
}
use of com.duangframework.core.exceptions.ServiceException in project duangframework by tcrct.
the class RuleFactory method execute.
/**
* 执行规则验证
* @param ruleParams
* @return
*/
public static RuleResult execute(List<RuleParam> ruleParams) {
if (ToolsKit.isEmpty(ruleParams)) {
throw new EmptyNullException("ruleParams is null");
}
if (ToolsKit.isEmpty(kieSessionHolder)) {
throw new EmptyNullException("RuleFactory.kieSessionHolder is null");
}
KieSession kieSession = kieSessionHolder.kieSession();
RuleResult ruleResult = new RuleResult(200, "success");
try {
for (RuleParam ruleParam : ruleParams) {
Map<String, Object> ruleParamsMap = ruleParam.toMap();
kieSession.insert(ruleParamsMap);
int ruleFiredCount = kieSession.fireAllRules(new RuleNameEndsWithAgendaFilter(ruleParam.getRuleName()));
if (ruleFiredCount <= 0) {
throw new ServiceException().setMessage("verification [" + ruleParam.getRuleName() + "] is not pass!");
}
}
} catch (ServiceException e) {
ruleResult.setCode(500);
ruleResult.setMessage(e.getMessage());
logger.warn(e.getMessage(), e);
}
kieSession.destroy();
return ruleResult;
}
use of com.duangframework.core.exceptions.ServiceException in project duangframework by tcrct.
the class IocHelper method ioc.
public static void ioc(Class<?> beanClass) throws Exception {
Field[] fields = beanClass.getDeclaredFields();
for (Field field : fields) {
if (field.isAnnotationPresent(Import.class)) {
Class<?> fieldTypeClass = field.getType();
if (fieldTypeClass.equals(beanClass)) {
throw new ServiceException(beanClass.getSimpleName() + " Can't Not Import From already!");
}
Object iocObj = BeanUtils.getBean(fieldTypeClass, beanClass);
if (ToolsKit.isNotEmpty(iocObj)) {
field.setAccessible(true);
field.set(BeanUtils.getBean(beanClass, null), iocObj);
}
}
}
}
Aggregations