Search in sources :

Example 21 with FunctionException

use of org.eclipse.che.api.promises.client.FunctionException in project che by eclipse.

the class ResourceManager method getRemoteResources.

Promise<Resource[]> getRemoteResources(final Container container, final int depth, boolean includeFiles) {
    checkArgument(depth > -2, "Invalid depth");
    if (depth == DEPTH_ZERO) {
        return promises.resolve(NO_RESOURCES);
    }
    int depthToReload = depth;
    final Optional<Resource[]> descendants = store.getAll(container.getLocation());
    if (depthToReload != -1 && descendants.isPresent()) {
        for (Resource resource : descendants.get()) {
            if (resource.getLocation().segmentCount() - container.getLocation().segmentCount() > depth) {
                depthToReload = resource.getLocation().segmentCount() - container.getLocation().segmentCount();
            }
        }
    }
    return ps.getTree(container.getLocation(), depthToReload, includeFiles).then(new Function<TreeElement, Resource[]>() {

        @Override
        public Resource[] apply(TreeElement tree) throws FunctionException {
            class Visitor implements ResourceVisitor {

                Resource[] resources;

                //size of total items
                private int size = 0;

                //step to increase resource array
                private int incStep = 50;

                private Visitor() {
                    this.resources = NO_RESOURCES;
                }

                @Override
                public void visit(Resource resource) {
                    if (resource.getResourceType() == PROJECT) {
                        final Optional<ProjectConfigDto> optionalConfig = findProjectConfigDto(resource.getLocation());
                        if (optionalConfig.isPresent()) {
                            final Optional<ProblemProjectMarker> optionalMarker = getProblemMarker(optionalConfig.get());
                            if (optionalMarker.isPresent()) {
                                resource.addMarker(optionalMarker.get());
                            }
                        }
                    }
                    if (size > resources.length - 1) {
                        //check load factor and increase resource array
                        resources = copyOf(resources, resources.length + incStep);
                    }
                    resources[size++] = resource;
                }
            }
            final Visitor visitor = new Visitor();
            traverse(tree, visitor);
            return copyOf(visitor.resources, visitor.size);
        }
    }).then(new Function<Resource[], Resource[]>() {

        @Override
        public Resource[] apply(Resource[] reloaded) throws FunctionException {
            Resource[] result = new Resource[0];
            if (descendants.isPresent()) {
                Resource[] outdated = descendants.get();
                final Resource[] removed = removeAll(outdated, reloaded, false);
                for (Resource resource : removed) {
                    store.dispose(resource.getLocation(), false);
                    eventBus.fireEvent(new ResourceChangedEvent(new ResourceDeltaImpl(resource, REMOVED)));
                }
                final Resource[] updated = removeAll(outdated, reloaded, true);
                for (Resource resource : updated) {
                    store.register(resource);
                    eventBus.fireEvent(new ResourceChangedEvent(new ResourceDeltaImpl(resource, UPDATED)));
                    final Optional<Resource> registered = store.getResource(resource.getLocation());
                    if (registered.isPresent()) {
                        result = Arrays.add(result, registered.get());
                    }
                }
                final Resource[] added = removeAll(reloaded, outdated, false);
                for (Resource resource : added) {
                    store.register(resource);
                    eventBus.fireEvent(new ResourceChangedEvent(new ResourceDeltaImpl(resource, ADDED)));
                    final Optional<Resource> registered = store.getResource(resource.getLocation());
                    if (registered.isPresent()) {
                        result = Arrays.add(result, registered.get());
                    }
                }
            } else {
                for (Resource resource : reloaded) {
                    store.register(resource);
                    eventBus.fireEvent(new ResourceChangedEvent(new ResourceDeltaImpl(resource, ADDED)));
                    final Optional<Resource> registered = store.getResource(resource.getLocation());
                    if (registered.isPresent()) {
                        result = Arrays.add(result, registered.get());
                    }
                }
            }
            return result;
        }
    });
}
Also used : Optional(com.google.common.base.Optional) NewProjectConfigDto(org.eclipse.che.api.workspace.shared.dto.NewProjectConfigDto) ProjectConfigDto(org.eclipse.che.api.workspace.shared.dto.ProjectConfigDto) Resource(org.eclipse.che.ide.api.resources.Resource) ProblemProjectMarker(org.eclipse.che.ide.api.resources.Project.ProblemProjectMarker) FunctionException(org.eclipse.che.api.promises.client.FunctionException) TreeElement(org.eclipse.che.api.project.shared.dto.TreeElement) Function(org.eclipse.che.api.promises.client.Function) ResourceChangedEvent(org.eclipse.che.ide.api.resources.ResourceChangedEvent)

Example 22 with FunctionException

use of org.eclipse.che.api.promises.client.FunctionException in project che by eclipse.

the class ResourceManager method findResource.

private Promise<Optional<Resource>> findResource(final Path absolutePath, boolean quiet) {
    //search resource in local cache
    final Optional<Resource> optionalCachedResource = store.getResource(absolutePath);
    if (optionalCachedResource.isPresent()) {
        return promises.resolve(optionalCachedResource);
    }
    //request from server
    final Path projectPath = Path.valueOf(absolutePath.segment(0)).makeAbsolute();
    final Optional<Resource> optProject = store.getResource(projectPath);
    final boolean isPresent = optProject.isPresent();
    checkState(isPresent || quiet, "Resource with path '" + projectPath + "' doesn't exists");
    if (!isPresent) {
        return promises.resolve(Optional.<Resource>absent());
    }
    final Resource project = optProject.get();
    checkState(project.getResourceType() == PROJECT, "Resource with path '" + projectPath + "' isn't a project");
    final int seekDepth = absolutePath.segmentCount() - 1;
    return getRemoteResources((Container) project, seekDepth, true).then(new Function<Resource[], Optional<Resource>>() {

        @Override
        public Optional<Resource> apply(Resource[] resources) throws FunctionException {
            for (Resource resource : resources) {
                if (absolutePath.equals(resource.getLocation())) {
                    return of(resource);
                }
            }
            return absent();
        }
    });
}
Also used : Path(org.eclipse.che.ide.resource.Path) Container(org.eclipse.che.ide.api.resources.Container) Optional(com.google.common.base.Optional) Resource(org.eclipse.che.ide.api.resources.Resource) FunctionException(org.eclipse.che.api.promises.client.FunctionException)

Example 23 with FunctionException

use of org.eclipse.che.api.promises.client.FunctionException in project che by eclipse.

the class ProjectWizard method complete.

/** {@inheritDoc} */
@Override
public void complete(@NotNull final CompleteCallback callback) {
    if (mode == CREATE) {
        appContext.getWorkspaceRoot().newProject().withBody(dataObject).send().then(onComplete(callback)).catchError(onFailure(callback));
    } else if (mode == UPDATE) {
        appContext.getWorkspaceRoot().getContainer(Path.valueOf(dataObject.getPath())).then(new Operation<Optional<Container>>() {

            @Override
            public void apply(Optional<Container> optContainer) throws OperationException {
                checkState(optContainer.isPresent(), "Failed to update non existed path");
                final Container container = optContainer.get();
                if (container.getResourceType() == PROJECT) {
                    ((Project) container).update().withBody(dataObject).send().then(onComplete(callback)).catchError(onFailure(callback));
                } else if (container.getResourceType() == FOLDER) {
                    ((Folder) container).toProject().withBody(dataObject).send().then(onComplete(callback)).catchError(onFailure(callback));
                }
            }
        });
    } else if (mode == IMPORT) {
        appContext.getWorkspaceRoot().newProject().withBody(dataObject).send().thenPromise(new Function<Project, Promise<Project>>() {

            @Override
            public Promise<Project> apply(Project project) throws FunctionException {
                return project.update().withBody(dataObject).send();
            }
        }).then(addCommands(callback)).catchError(onFailure(callback));
    }
}
Also used : Project(org.eclipse.che.ide.api.resources.Project) Promise(org.eclipse.che.api.promises.client.Promise) Container(org.eclipse.che.ide.api.resources.Container) Optional(com.google.common.base.Optional) FunctionException(org.eclipse.che.api.promises.client.FunctionException) Operation(org.eclipse.che.api.promises.client.Operation)

Example 24 with FunctionException

use of org.eclipse.che.api.promises.client.FunctionException in project che by eclipse.

the class DeleteResourceManager method delete.

/**
     * Deletes the given resources and its descendants in the standard manner from file system.
     * Method requires a confirmation from the user before resource will be removed.
     *
     * @param needConfirmation
     *         true if confirmation is need
     * @param resources
     *         the resources to delete
     * @return the {@link Promise} with void if removal has successfully completed
     * @see #delete(Resource...)
     */
public Promise<Void> delete(boolean needConfirmation, Resource... resources) {
    checkArgument(resources != null, "Null resource occurred");
    checkArgument(resources.length > 0, "No resources were provided to remove");
    final Resource[] filtered = filterDescendants(resources);
    if (!needConfirmation) {
        Promise<?>[] deleteAll = new Promise<?>[resources.length];
        for (int i = 0; i < resources.length; i++) {
            deleteAll[i] = resources[i].delete();
        }
        return promiseProvider.all(deleteAll).then(new Function<JsArrayMixed, Void>() {

            @Override
            public Void apply(JsArrayMixed arg) throws FunctionException {
                return null;
            }
        });
    }
    List<Resource> projectsList = newArrayList();
    for (Resource resource : filtered) {
        if (resource.getResourceType() == PROJECT) {
            projectsList.add(resource);
        }
    }
    Resource[] projects = projectsList.toArray(new Resource[projectsList.size()]);
    if (projectsList.isEmpty()) {
        //if no project were found in nodes list
        return promptUserToDelete(filtered);
    } else if (projects.length < filtered.length) {
        //inform user that we can't delete mixed list of the nodes
        return promiseProvider.reject(JsPromiseError.create(localization.mixedProjectDeleteMessage()));
    } else {
        //delete only project nodes
        return promptUserToDelete(projects);
    }
}
Also used : Promise(org.eclipse.che.api.promises.client.Promise) Resource(org.eclipse.che.ide.api.resources.Resource) FunctionException(org.eclipse.che.api.promises.client.FunctionException) JsArrayMixed(com.google.gwt.core.client.JsArrayMixed)

Aggregations

FunctionException (org.eclipse.che.api.promises.client.FunctionException)24 Promise (org.eclipse.che.api.promises.client.Promise)15 Resource (org.eclipse.che.ide.api.resources.Resource)12 Function (org.eclipse.che.api.promises.client.Function)11 Optional (com.google.common.base.Optional)9 Project (org.eclipse.che.ide.api.resources.Project)8 List (java.util.List)7 PromiseError (org.eclipse.che.api.promises.client.PromiseError)7 Path (org.eclipse.che.ide.resource.Path)6 Operation (org.eclipse.che.api.promises.client.Operation)5 ResourceChangedEvent (org.eclipse.che.ide.api.resources.ResourceChangedEvent)5 ArrayList (java.util.ArrayList)4 NewProjectConfigDto (org.eclipse.che.api.workspace.shared.dto.NewProjectConfigDto)4 ProjectConfigDto (org.eclipse.che.api.workspace.shared.dto.ProjectConfigDto)4 MutableProjectConfig (org.eclipse.che.ide.api.project.MutableProjectConfig)4 SourceStorage (org.eclipse.che.api.core.model.project.SourceStorage)3 TextDocumentPositionParamsDTO (org.eclipse.che.api.languageserver.shared.lsapi.TextDocumentPositionParamsDTO)3 OperationException (org.eclipse.che.api.promises.client.OperationException)3 AsyncCallback (com.google.gwt.user.client.rpc.AsyncCallback)2 Set (java.util.Set)2