public void moveResource()

in server/src/main/java/com/epam/aidial/core/server/service/ResourceOperationService.java [43:93]


    public void moveResource(ResourceDescriptor source, ResourceDescriptor destination, boolean overwriteIfExists) {
        if (source.isFolder() || destination.isFolder()) {
            throw new IllegalArgumentException("Moving folders is not supported");
        }

        String sourceResourceUrl = source.getUrl();
        String destinationResourceUrl = destination.getUrl();

        if (!resourceService.hasResource(source)) {
            throw new IllegalArgumentException("Source resource %s does not exist".formatted(sourceResourceUrl));
        }

        if (!ALLOWED_RESOURCES.contains(source.getType())) {
            throw new IllegalStateException("Unsupported type: " + source.getType());
        }

        if (destination.getType() == APPLICATION) {
            applicationService.copyApplication(source, destination, overwriteIfExists, app -> {
                if (ApplicationService.isActive(app)) {
                    throw new HttpException(HttpStatus.CONFLICT, "Application must be stopped: " + source.getUrl());
                }
            });
        } else {
            boolean copied = resourceService.copyResource(source, destination, overwriteIfExists);
            if (!copied) {
                throw new IllegalArgumentException("Can't move resource %s to %s, because destination resource already exists"
                        .formatted(sourceResourceUrl, destinationResourceUrl));
            }
        }

        if (source.isPrivate()) {
            String bucketName = source.getBucketName();
            String bucketLocation = source.getBucketLocation();
            boolean isSameBucket = source.getBucketName().equals(destination.getBucketName());

            if (isSameBucket) {
                invitationService.moveResource(bucketName, bucketLocation, source, destination);
                shareService.moveSharedAccess(bucketName, bucketLocation, source, destination);
            } else {
                Map<ResourceDescriptor, Set<ResourceAccessType>> resources = Map.of(source, ResourceAccessType.ALL);
                invitationService.cleanUpPermissions(bucketName, bucketLocation, resources);
                shareService.revokeSharedAccess(bucketName, bucketLocation, resources);
            }
        }

        if (destination.getType() == APPLICATION) {
            applicationService.deleteApplication(source, EtagHeader.ANY);
        } else {
            resourceService.deleteResource(source, EtagHeader.ANY);
        }
    }