Search in sources :

Example 1 with ResourceComponent

use of org.apache.dolphinscheduler.api.dto.resources.ResourceComponent in project dolphinscheduler by apache.

the class ResourceTreeVisitor method getResourceComponent.

/**
 * get resource component by resource
 * @param resource resource
 * @return resource component
 */
private static ResourceComponent getResourceComponent(Resource resource) {
    ResourceComponent tempResourceComponent;
    if (resource.isDirectory()) {
        tempResourceComponent = new Directory();
    } else {
        tempResourceComponent = new FileLeaf();
    }
    tempResourceComponent.setName(resource.getAlias());
    tempResourceComponent.setFullName(resource.getFullName().replaceFirst("/", ""));
    tempResourceComponent.setId(resource.getId());
    tempResourceComponent.setPid(resource.getPid());
    tempResourceComponent.setIdValue(resource.getId(), resource.isDirectory());
    tempResourceComponent.setDescription(resource.getDescription());
    tempResourceComponent.setType(resource.getType());
    return tempResourceComponent;
}
Also used : ResourceComponent(org.apache.dolphinscheduler.api.dto.resources.ResourceComponent) FileLeaf(org.apache.dolphinscheduler.api.dto.resources.FileLeaf) Directory(org.apache.dolphinscheduler.api.dto.resources.Directory)

Example 2 with ResourceComponent

use of org.apache.dolphinscheduler.api.dto.resources.ResourceComponent in project dolphinscheduler by apache.

the class ResourcesService method authorizeResourceTree.

/**
 * list all file
 *
 * @param loginUser login user
 * @param userId user id
 * @return unauthorized result code
 */
public Map<String, Object> authorizeResourceTree(User loginUser, Integer userId) {
    Map<String, Object> result = new HashMap<>();
    if (checkAdmin(loginUser, result)) {
        return result;
    }
    List<Resource> resourceList = resourcesMapper.queryResourceExceptUserId(userId);
    List<ResourceComponent> list;
    if (CollectionUtils.isNotEmpty(resourceList)) {
        Visitor visitor = new ResourceTreeVisitor(resourceList);
        list = visitor.visit().getChildren();
    } else {
        list = new ArrayList<>(0);
    }
    result.put(Constants.DATA_LIST, list);
    putMsg(result, Status.SUCCESS);
    return result;
}
Also used : ResourceTreeVisitor(org.apache.dolphinscheduler.api.dto.resources.visitor.ResourceTreeVisitor) Visitor(org.apache.dolphinscheduler.api.dto.resources.visitor.Visitor) ResourceComponent(org.apache.dolphinscheduler.api.dto.resources.ResourceComponent) ResourceTreeVisitor(org.apache.dolphinscheduler.api.dto.resources.visitor.ResourceTreeVisitor)

Example 3 with ResourceComponent

use of org.apache.dolphinscheduler.api.dto.resources.ResourceComponent in project dolphinscheduler by apache.

the class UsersService method updateUser.

/**
 * updateProcessInstance user
 *
 * @param loginUser
 * @param userId user id
 * @param userName user name
 * @param userPassword user password
 * @param email email
 * @param tenantId tennat id
 * @param phone phone
 * @param queue  queue
 * @return update result code
 * @throws Exception exception
 */
public Map<String, Object> updateUser(User loginUser, int userId, String userName, String userPassword, String email, int tenantId, String phone, String queue) throws Exception {
    Map<String, Object> result = new HashMap<>(5);
    result.put(Constants.STATUS, false);
    if (check(result, !hasPerm(loginUser, userId), Status.USER_NO_OPERATION_PERM)) {
        return result;
    }
    User user = userMapper.selectById(userId);
    if (user == null) {
        putMsg(result, Status.USER_NOT_EXIST, userId);
        return result;
    }
    if (StringUtils.isNotEmpty(userName)) {
        if (!CheckUtils.checkUserName(userName)) {
            putMsg(result, Status.REQUEST_PARAMS_NOT_VALID_ERROR, userName);
            return result;
        }
        User tempUser = userMapper.queryByUserNameAccurately(userName);
        if (tempUser != null && tempUser.getId() != userId) {
            putMsg(result, Status.USER_NAME_EXIST);
            return result;
        }
        user.setUserName(userName);
    }
    if (StringUtils.isNotEmpty(userPassword)) {
        if (!CheckUtils.checkPassword(userPassword)) {
            putMsg(result, Status.REQUEST_PARAMS_NOT_VALID_ERROR, userPassword);
            return result;
        }
        user.setUserPassword(EncryptionUtils.getMd5(userPassword));
    }
    if (StringUtils.isNotEmpty(email)) {
        if (!CheckUtils.checkEmail(email)) {
            putMsg(result, Status.REQUEST_PARAMS_NOT_VALID_ERROR, email);
            return result;
        }
        user.setEmail(email);
    }
    if (StringUtils.isNotEmpty(phone) && !CheckUtils.checkPhone(phone)) {
        putMsg(result, Status.REQUEST_PARAMS_NOT_VALID_ERROR, phone);
        return result;
    }
    user.setPhone(phone);
    user.setQueue(queue);
    Date now = new Date();
    user.setUpdateTime(now);
    // if user switches the tenant, the user's resources need to be copied to the new tenant
    if (user.getTenantId() != tenantId) {
        Tenant oldTenant = tenantMapper.queryById(user.getTenantId());
        // query tenant
        Tenant newTenant = tenantMapper.queryById(tenantId);
        if (newTenant != null) {
            // if hdfs startup
            if (PropertyUtils.getResUploadStartupState() && oldTenant != null) {
                String newTenantCode = newTenant.getTenantCode();
                String oldResourcePath = HadoopUtils.getHdfsResDir(oldTenant.getTenantCode());
                String oldUdfsPath = HadoopUtils.getHdfsUdfDir(oldTenant.getTenantCode());
                // if old tenant dir exists
                if (HadoopUtils.getInstance().exists(oldResourcePath)) {
                    String newResourcePath = HadoopUtils.getHdfsResDir(newTenantCode);
                    String newUdfsPath = HadoopUtils.getHdfsUdfDir(newTenantCode);
                    // file resources list
                    List<Resource> fileResourcesList = resourceMapper.queryResourceList(null, userId, ResourceType.FILE.ordinal());
                    if (CollectionUtils.isNotEmpty(fileResourcesList)) {
                        ResourceTreeVisitor resourceTreeVisitor = new ResourceTreeVisitor(fileResourcesList);
                        ResourceComponent resourceComponent = resourceTreeVisitor.visit();
                        copyResourceFiles(resourceComponent, oldResourcePath, newResourcePath);
                    }
                    // udf resources
                    List<Resource> udfResourceList = resourceMapper.queryResourceList(null, userId, ResourceType.UDF.ordinal());
                    if (CollectionUtils.isNotEmpty(udfResourceList)) {
                        ResourceTreeVisitor resourceTreeVisitor = new ResourceTreeVisitor(udfResourceList);
                        ResourceComponent resourceComponent = resourceTreeVisitor.visit();
                        copyResourceFiles(resourceComponent, oldUdfsPath, newUdfsPath);
                    }
                    // Delete the user from the old tenant directory
                    String oldUserPath = HadoopUtils.getHdfsUserDir(oldTenant.getTenantCode(), userId);
                    HadoopUtils.getInstance().delete(oldUserPath, true);
                } else {
                    // if old tenant dir not exists , create
                    createTenantDirIfNotExists(oldTenant.getTenantCode());
                }
                if (HadoopUtils.getInstance().exists(HadoopUtils.getHdfsTenantDir(newTenant.getTenantCode()))) {
                    // create user in the new tenant directory
                    String newUserPath = HadoopUtils.getHdfsUserDir(newTenant.getTenantCode(), user.getId());
                    HadoopUtils.getInstance().mkdir(newUserPath);
                } else {
                    // if new tenant dir not exists , create
                    createTenantDirIfNotExists(newTenant.getTenantCode());
                }
            }
        }
        user.setTenantId(tenantId);
    }
    // updateProcessInstance user
    userMapper.updateById(user);
    putMsg(result, Status.SUCCESS);
    return result;
}
Also used : ResourceComponent(org.apache.dolphinscheduler.api.dto.resources.ResourceComponent) ResourceTreeVisitor(org.apache.dolphinscheduler.api.dto.resources.visitor.ResourceTreeVisitor)

Example 4 with ResourceComponent

use of org.apache.dolphinscheduler.api.dto.resources.ResourceComponent in project dolphinscheduler by apache.

the class ResourceTreeVisitor method setChildren.

/**
 * set children
 * @param id    id
 * @param list  resource list
 * @return resource component list
 */
public static List<ResourceComponent> setChildren(int id, List<Resource> list) {
    List<ResourceComponent> childList = new ArrayList<>();
    for (Resource resource : list) {
        if (id == resource.getPid()) {
            ResourceComponent tempResourceComponent = getResourceComponent(resource);
            childList.add(tempResourceComponent);
        }
    }
    for (ResourceComponent resourceComponent : childList) {
        resourceComponent.setChildren(setChildren(resourceComponent.getId(), list));
    }
    if (childList.size() == 0) {
        return new ArrayList<>();
    }
    return childList;
}
Also used : ResourceComponent(org.apache.dolphinscheduler.api.dto.resources.ResourceComponent) ArrayList(java.util.ArrayList) Resource(org.apache.dolphinscheduler.dao.entity.Resource)

Example 5 with ResourceComponent

use of org.apache.dolphinscheduler.api.dto.resources.ResourceComponent in project dolphinscheduler by apache.

the class ResourceTreeVisitor method visit.

/**
 * visit
 * @return resoruce component
 */
public ResourceComponent visit() {
    ResourceComponent rootDirectory = new Directory();
    for (Resource resource : resourceList) {
        // judge whether is root node
        if (rootNode(resource)) {
            ResourceComponent tempResourceComponent = getResourceComponent(resource);
            rootDirectory.add(tempResourceComponent);
            tempResourceComponent.setChildren(setChildren(tempResourceComponent.getId(), resourceList));
        }
    }
    return rootDirectory;
}
Also used : ResourceComponent(org.apache.dolphinscheduler.api.dto.resources.ResourceComponent) Resource(org.apache.dolphinscheduler.dao.entity.Resource) Directory(org.apache.dolphinscheduler.api.dto.resources.Directory)

Aggregations

ResourceComponent (org.apache.dolphinscheduler.api.dto.resources.ResourceComponent)6 Resource (org.apache.dolphinscheduler.dao.entity.Resource)3 ArrayList (java.util.ArrayList)2 Directory (org.apache.dolphinscheduler.api.dto.resources.Directory)2 ResourceTreeVisitor (org.apache.dolphinscheduler.api.dto.resources.visitor.ResourceTreeVisitor)2 FileLeaf (org.apache.dolphinscheduler.api.dto.resources.FileLeaf)1 Visitor (org.apache.dolphinscheduler.api.dto.resources.visitor.Visitor)1 Test (org.junit.Test)1