Search in sources :

Example 1 with BaseService

use of com.diboot.core.service.BaseService in project diboot by dibo-software.

the class BaseJwtRealm method doGetAuthenticationInfo.

/**
 * 获取认证信息
 * @param token
 * @return
 * @throws AuthenticationException
 */
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
    BaseJwtAuthToken jwtToken = (BaseJwtAuthToken) token;
    String authAccount = (String) jwtToken.getPrincipal();
    if (V.isEmpty(authAccount)) {
        throw new AuthenticationException("无效的用户标识");
    } else {
        // 获取认证方式
        AuthService authService = AuthServiceFactory.getAuthService(jwtToken.getAuthType());
        if (authService == null) {
            jwtToken.clearAuthtoken();
            throw new AuthenticationException("认证类型: " + jwtToken.getAuthType() + " 的AccountAuthService未实现!");
        }
        IamAccount account = authService.getAccount(jwtToken);
        // 登录失败则抛出相关异常
        if (account == null) {
            jwtToken.clearAuthtoken();
            throw new AuthenticationException("用户账号或密码错误!");
        }
        // 获取当前user对象并缓存
        BaseLoginUser loginUser = null;
        BaseService userService = ContextHelper.getBaseServiceByEntity(jwtToken.getUserTypeClass());
        if (userService != null) {
            loginUser = (BaseLoginUser) userService.getEntity(account.getUserId());
        } else {
            throw new AuthenticationException("用户 " + jwtToken.getUserTypeClass().getName() + " 相关的Service未定义!");
        }
        if (loginUser == null) {
            throw new AuthenticationException("用户不存在");
        }
        loginUser.setAuthToken(jwtToken.getAuthtoken());
        IamExtensible iamExtensible = getIamUserRoleService().getIamExtensible();
        if (iamExtensible != null) {
            LabelValue extentionObj = iamExtensible.getUserExtentionObj(jwtToken.getUserTypeClass().getSimpleName(), account.getUserId(), jwtToken.getExtObj());
            if (extentionObj != null) {
                loginUser.setExtentionObj(extentionObj);
            }
        }
        // 清空当前用户缓存
        this.clearCachedAuthorizationInfo(IamSecurityUtils.getSubject().getPrincipals());
        return new SimpleAuthenticationInfo(loginUser, jwtToken.getCredentials(), this.getName());
    }
}
Also used : IamAccount(com.diboot.iam.entity.IamAccount) IamExtensible(com.diboot.iam.auth.IamExtensible) LabelValue(com.diboot.core.vo.LabelValue) SimpleAuthenticationInfo(org.apache.shiro.authc.SimpleAuthenticationInfo) AuthenticationException(org.apache.shiro.authc.AuthenticationException) BaseLoginUser(com.diboot.iam.entity.BaseLoginUser) AuthService(com.diboot.iam.auth.AuthService) BaseService(com.diboot.core.service.BaseService)

Example 2 with BaseService

use of com.diboot.core.service.BaseService in project diboot by dibo-software.

the class BaseServiceImpl method createEntityAndRelatedEntities.

@Override
@Transactional(rollbackFor = Exception.class)
public <RE, R> boolean createEntityAndRelatedEntities(T entity, List<RE> relatedEntities, ISetter<RE, R> relatedEntitySetter) {
    boolean success = createEntity(entity);
    if (!success) {
        log.warn("新建Entity失败: {}", entity.toString());
        return false;
    }
    if (V.isEmpty(relatedEntities)) {
        return true;
    }
    Class relatedEntityClass = relatedEntities.get(0).getClass();
    // 获取主键
    Object pkValue = getPrimaryKeyValue(entity);
    String attributeName = BeanUtils.convertToFieldName(relatedEntitySetter);
    // 填充关联关系
    relatedEntities.forEach(relatedEntity -> BeanUtils.setProperty(relatedEntity, attributeName, pkValue));
    // 获取关联对象对应的Service
    BaseService relatedEntityService = ContextHelper.getBaseServiceByEntity(relatedEntityClass);
    if (relatedEntityService != null) {
        return relatedEntityService.createEntities(relatedEntities);
    } else {
        // 查找mapper
        BaseMapper mapper = ContextHelper.getBaseMapperByEntity(entity.getClass());
        // 新增关联,无service只能循环插入
        for (RE relation : relatedEntities) {
            mapper.insert(relation);
        }
        return true;
    }
}
Also used : BaseMapper(com.baomidou.mybatisplus.core.mapper.BaseMapper) BaseService(com.diboot.core.service.BaseService) Transactional(org.springframework.transaction.annotation.Transactional)

Example 3 with BaseService

use of com.diboot.core.service.BaseService in project diboot by dibo-software.

the class BaseServiceImpl method deleteEntityAndRelatedEntities.

@Override
@Transactional(rollbackFor = Exception.class)
public <RE, R> boolean deleteEntityAndRelatedEntities(Serializable id, Class<RE> relatedEntityClass, ISetter<RE, R> relatedEntitySetter) {
    boolean success = deleteEntity(id);
    if (!success) {
        log.warn("删除Entity失败: {}", id);
        return false;
    }
    // 获取关联对象对应的Service
    BaseService relatedEntityService = ContextHelper.getBaseServiceByEntity(relatedEntityClass);
    if (relatedEntityService == null) {
        log.error("未能识别到Entity: {} 的Service实现,请检查!", relatedEntityClass.getName());
        return false;
    }
    // 获取主键的关联属性
    String attributeName = BeanUtils.convertToFieldName(relatedEntitySetter);
    QueryWrapper<RE> queryWrapper = new QueryWrapper<RE>().eq(S.toSnakeCase(attributeName), id);
    // 删除关联子表数据
    return relatedEntityService.deleteEntities(queryWrapper);
}
Also used : BaseService(com.diboot.core.service.BaseService) Transactional(org.springframework.transaction.annotation.Transactional)

Example 4 with BaseService

use of com.diboot.core.service.BaseService in project diboot by dibo-software.

the class BaseServiceImpl method createOrUpdateN2NRelations.

@Override
@Transactional(rollbackFor = Exception.class)
public <R> boolean createOrUpdateN2NRelations(SFunction<R, ?> driverIdGetter, Object driverId, SFunction<R, ?> followerIdGetter, List<? extends Serializable> followerIdList, Consumer<QueryWrapper<R>> queryConsumer, Consumer<R> setConsumer) {
    if (driverId == null) {
        throw new InvalidUsageException("主动ID值不能为空!");
    }
    if (followerIdList == null) {
        log.debug("从动对象ID集合为null,不做关联关系更新处理");
        return false;
    }
    // 从getter中获取class和fieldName
    LambdaMeta lambdaMeta = LambdaUtils.extract(driverIdGetter);
    Class<R> middleTableClass = (Class<R>) lambdaMeta.getInstantiatedClass();
    EntityInfoCache entityInfo = BindingCacheManager.getEntityInfoByClass(middleTableClass);
    if (entityInfo == null) {
        throw new InvalidUsageException("未找到 " + middleTableClass.getName() + " 的 Service 或 Mapper 定义!");
    }
    boolean isExistPk = entityInfo.getIdColumn() != null;
    // 获取主动从动字段名
    String driverFieldName = PropertyNamer.methodToProperty(lambdaMeta.getImplMethodName());
    String followerFieldName = convertGetterToFieldName(followerIdGetter);
    String driverColumnName = entityInfo.getColumnByField(driverFieldName);
    String followerColumnName = entityInfo.getColumnByField(followerFieldName);
    // 查询已有关联
    QueryWrapper<R> selectOld = new QueryWrapper<R>().eq(driverColumnName, driverId);
    if (queryConsumer != null) {
        queryConsumer.accept(selectOld);
    }
    if (isExistPk) {
        selectOld.select(entityInfo.getIdColumn(), followerColumnName);
    } else {
        selectOld.select(followerColumnName);
    }
    List<Map<String, Object>> oldMap;
    IService<R> iService = entityInfo.getService();
    BaseMapper<R> baseMapper = entityInfo.getBaseMapper();
    if (iService != null) {
        oldMap = iService.listMaps(selectOld);
    } else {
        oldMap = baseMapper.selectMaps(selectOld);
    }
    // 删除失效关联
    List<Serializable> delIds = new ArrayList<>();
    for (Map<String, Object> map : oldMap) {
        if (V.notEmpty(followerIdList) && followerIdList.remove((Serializable) map.get(followerColumnName))) {
            continue;
        }
        delIds.add((Serializable) map.get(isExistPk ? entityInfo.getIdColumn() : followerColumnName));
    }
    if (!delIds.isEmpty()) {
        if (isExistPk) {
            if (iService != null) {
                iService.removeByIds(delIds);
            } else {
                baseMapper.deleteBatchIds(delIds);
            }
        } else {
            QueryWrapper<R> delOld = new QueryWrapper<R>().eq(driverColumnName, driverId).in(entityInfo.getColumnByField(followerFieldName), delIds);
            if (queryConsumer != null) {
                queryConsumer.accept(selectOld);
            }
            if (iService != null) {
                iService.remove(delOld);
            } else if (!delIds.isEmpty()) {
                baseMapper.delete(delOld);
            }
        }
    }
    // 新增关联
    if (V.notEmpty(followerIdList)) {
        List<R> n2nRelations = new ArrayList<>(followerIdList.size());
        try {
            for (Serializable followerId : followerIdList) {
                R relation = middleTableClass.newInstance();
                BeanUtils.setProperty(relation, driverFieldName, driverId);
                BeanUtils.setProperty(relation, followerFieldName, followerId);
                if (setConsumer != null) {
                    setConsumer.accept(relation);
                }
                n2nRelations.add(relation);
            }
        } catch (Exception e) {
            throw new BusinessException(Status.FAIL_EXCEPTION, e);
        }
        if (iService != null) {
            if (iService instanceof BaseService) {
                ((BaseService<R>) iService).createEntities(n2nRelations);
            } else {
                iService.saveBatch(n2nRelations);
            }
        } else {
            // 新增关联,无service只能循环插入
            for (R relation : n2nRelations) {
                baseMapper.insert(relation);
            }
        }
    }
    return true;
}
Also used : Serializable(java.io.Serializable) LambdaQueryWrapper(com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper) QueryWrapper(com.baomidou.mybatisplus.core.conditions.query.QueryWrapper) DynamicJoinQueryWrapper(com.diboot.core.binding.query.dynamic.DynamicJoinQueryWrapper) InvalidUsageException(com.diboot.core.exception.InvalidUsageException) BusinessException(com.diboot.core.exception.BusinessException) LambdaMeta(com.baomidou.mybatisplus.core.toolkit.support.LambdaMeta) BusinessException(com.diboot.core.exception.BusinessException) EntityInfoCache(com.diboot.core.binding.parser.EntityInfoCache) BaseService(com.diboot.core.service.BaseService) InvalidUsageException(com.diboot.core.exception.InvalidUsageException) Transactional(org.springframework.transaction.annotation.Transactional)

Example 5 with BaseService

use of com.diboot.core.service.BaseService in project diboot by dibo-software.

the class BaseServiceImpl method updateEntityAndRelatedEntities.

@Override
@Transactional(rollbackFor = Exception.class)
public <RE, R> boolean updateEntityAndRelatedEntities(T entity, List<RE> relatedEntities, ISetter<RE, R> relatedEntitySetter) {
    boolean success = updateEntity(entity);
    if (!success) {
        log.warn("更新Entity失败: {}", entity.toString());
        return false;
    }
    // 获取关联entity的类
    Class relatedEntityClass;
    if (V.notEmpty(relatedEntities)) {
        relatedEntityClass = BeanUtils.getTargetClass(relatedEntities.get(0));
    } else {
        try {
            relatedEntityClass = Class.forName(BeanUtils.getSerializedLambda(relatedEntitySetter).getImplClass().replaceAll("/", "."));
        } catch (Exception e) {
            log.warn("无法识别关联Entity的Class: {}", e.getMessage());
            return false;
        }
    }
    // 获取关联对象对应的Service
    BaseService relatedEntityService = ContextHelper.getBaseServiceByEntity(relatedEntityClass);
    if (relatedEntityService == null) {
        log.error("未能识别到Entity: {} 的Service实现,请检查!", relatedEntityClass.getName());
        return false;
    }
    // 获取主键
    Object pkValue = getPrimaryKeyValue(entity);
    String attributeName = BeanUtils.convertToFieldName(relatedEntitySetter);
    // 获取原 关联entity list
    QueryWrapper<RE> queryWrapper = new QueryWrapper();
    queryWrapper.eq(S.toSnakeCase(attributeName), pkValue);
    List<RE> oldRelatedEntities = relatedEntityService.getEntityList(queryWrapper);
    // 遍历更新关联对象
    Set relatedEntityIds = new HashSet();
    if (V.notEmpty(relatedEntities)) {
        // 新建 修改 删除
        List<RE> newRelatedEntities = new ArrayList<>();
        for (RE relatedEntity : relatedEntities) {
            BeanUtils.setProperty(relatedEntity, attributeName, pkValue);
            Object relPkValue = getPrimaryKeyValue(relatedEntity);
            if (V.notEmpty(relPkValue)) {
                relatedEntityService.updateEntity(relatedEntity);
            } else {
                newRelatedEntities.add(relatedEntity);
            }
            relatedEntityIds.add(relPkValue);
        }
        relatedEntityService.createEntities(newRelatedEntities);
    }
    // 遍历已有关联对象
    if (V.notEmpty(oldRelatedEntities)) {
        List deleteRelatedEntityIds = new ArrayList();
        for (RE relatedEntity : oldRelatedEntities) {
            Object relPkValue = getPrimaryKeyValue(relatedEntity);
            if (!relatedEntityIds.contains(relPkValue)) {
                deleteRelatedEntityIds.add(relPkValue);
            }
        }
        relatedEntityService.deleteEntities(deleteRelatedEntityIds);
    }
    return true;
}
Also used : LambdaQueryWrapper(com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper) QueryWrapper(com.baomidou.mybatisplus.core.conditions.query.QueryWrapper) DynamicJoinQueryWrapper(com.diboot.core.binding.query.dynamic.DynamicJoinQueryWrapper) InvalidUsageException(com.diboot.core.exception.InvalidUsageException) BusinessException(com.diboot.core.exception.BusinessException) BaseService(com.diboot.core.service.BaseService) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

BaseService (com.diboot.core.service.BaseService)9 QueryWrapper (com.baomidou.mybatisplus.core.conditions.query.QueryWrapper)4 Transactional (org.springframework.transaction.annotation.Transactional)4 LambdaQueryWrapper (com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper)3 BusinessException (com.diboot.core.exception.BusinessException)3 LabelValue (com.diboot.core.vo.LabelValue)3 EntityInfoCache (com.diboot.core.binding.parser.EntityInfoCache)2 DynamicJoinQueryWrapper (com.diboot.core.binding.query.dynamic.DynamicJoinQueryWrapper)2 InvalidUsageException (com.diboot.core.exception.InvalidUsageException)2 BaseMapper (com.baomidou.mybatisplus.core.mapper.BaseMapper)1 Wrappers (com.baomidou.mybatisplus.core.toolkit.Wrappers)1 LambdaMeta (com.baomidou.mybatisplus.core.toolkit.support.LambdaMeta)1 IService (com.baomidou.mybatisplus.extension.service.IService)1 Binder (com.diboot.core.binding.Binder)1 QueryBuilder (com.diboot.core.binding.QueryBuilder)1 BindingCacheManager (com.diboot.core.binding.cache.BindingCacheManager)1 PropInfo (com.diboot.core.binding.parser.PropInfo)1 Cons (com.diboot.core.config.Cons)1 AttachMoreDTO (com.diboot.core.dto.AttachMoreDTO)1 ValidList (com.diboot.core.entity.ValidList)1