Search in sources :

Example 1 with ServiceException

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);
    }
}
Also used : ServiceException(com.duangframework.core.exceptions.ServiceException) MongoUpdate(com.duangframework.mongodb.common.MongoUpdate) MongoQuery(com.duangframework.mongodb.common.MongoQuery) ServiceException(com.duangframework.core.exceptions.ServiceException) EmptyNullException(com.duangframework.core.exceptions.EmptyNullException)

Example 2 with ServiceException

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;
}
Also used : ServiceException(com.duangframework.core.exceptions.ServiceException) MongoQuery(com.duangframework.mongodb.common.MongoQuery) ServiceException(com.duangframework.core.exceptions.ServiceException) EmptyNullException(com.duangframework.core.exceptions.EmptyNullException)

Example 3 with ServiceException

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);
}
Also used : HeadDto(com.duangframework.core.common.dto.result.HeadDto) ServiceException(com.duangframework.core.exceptions.ServiceException) IEnums(com.duangframework.core.common.enums.IEnums) ReturnDto(com.duangframework.core.common.dto.result.ReturnDto) JSONObject(com.alibaba.fastjson.JSONObject)

Example 4 with ServiceException

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;
}
Also used : ServiceException(com.duangframework.core.exceptions.ServiceException) RuleParam(com.duangframework.rule.entity.RuleParam) EmptyNullException(com.duangframework.core.exceptions.EmptyNullException) KieSession(org.kie.api.runtime.KieSession) RuleResult(com.duangframework.rule.entity.RuleResult) RuleNameEndsWithAgendaFilter(org.drools.core.base.RuleNameEndsWithAgendaFilter)

Example 5 with ServiceException

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);
            }
        }
    }
}
Also used : Field(java.lang.reflect.Field) ServiceException(com.duangframework.core.exceptions.ServiceException)

Aggregations

ServiceException (com.duangframework.core.exceptions.ServiceException)6 EmptyNullException (com.duangframework.core.exceptions.EmptyNullException)4 MongoQuery (com.duangframework.mongodb.common.MongoQuery)2 JSONObject (com.alibaba.fastjson.JSONObject)1 HeadDto (com.duangframework.core.common.dto.result.HeadDto)1 ReturnDto (com.duangframework.core.common.dto.result.ReturnDto)1 IEnums (com.duangframework.core.common.enums.IEnums)1 MongoUpdate (com.duangframework.mongodb.common.MongoUpdate)1 RuleParam (com.duangframework.rule.entity.RuleParam)1 RuleResult (com.duangframework.rule.entity.RuleResult)1 Field (java.lang.reflect.Field)1 RuleNameEndsWithAgendaFilter (org.drools.core.base.RuleNameEndsWithAgendaFilter)1 KieSession (org.kie.api.runtime.KieSession)1