Search in sources :

Example 1 with Function

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

the class Delayer method trigger.

public Promise<T> trigger(Task<T> task, int delay) {
    this.task = task;
    cancelTimer();
    if (completionPromise == null) {
        completionPromise = Promises.create(new Executor.ExecutorBody<T>() {

            @Override
            public void apply(ResolveFunction<T> resolve, RejectFunction reject) {
                resolveFunction = resolve;
            }
        }).thenPromise(new Function<T, Promise<T>>() {

            @Override
            public Promise<T> apply(T arg) throws FunctionException {
                completionPromise = null;
                resolveFunction = null;
                Task<T> t = Delayer.this.task;
                Delayer.this.task = null;
                return Promises.resolve(t.run());
            }
        });
    }
    timer = new Timer() {

        @Override
        public void run() {
            timer = null;
            resolveFunction.apply(null);
        }
    };
    timer.schedule(delay);
    return completionPromise;
}
Also used : Function(org.eclipse.che.api.promises.client.Function) RejectFunction(org.eclipse.che.api.promises.client.js.RejectFunction) ResolveFunction(org.eclipse.che.api.promises.client.js.ResolveFunction) Executor(org.eclipse.che.api.promises.client.js.Executor) Timer(com.google.gwt.user.client.Timer) RejectFunction(org.eclipse.che.api.promises.client.js.RejectFunction)

Example 2 with Function

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

the class ResourceManager method importProject.

protected Promise<Project> importProject(final Project.ProjectRequest importRequest) {
    checkArgument(checkProjectName(importRequest.getBody().getName()), "Invalid project name");
    checkNotNull(importRequest.getBody().getSource(), "Null source configuration occurred");
    final Path path = Path.valueOf(importRequest.getBody().getPath());
    return findResource(path, true).thenPromise(new Function<Optional<Resource>, Promise<Project>>() {

        @Override
        public Promise<Project> apply(final Optional<Resource> resource) throws FunctionException {
            final SourceStorage sourceStorage = importRequest.getBody().getSource();
            final SourceStorageDto sourceStorageDto = dtoFactory.createDto(SourceStorageDto.class).withType(sourceStorage.getType()).withLocation(sourceStorage.getLocation()).withParameters(sourceStorage.getParameters());
            return ps.importProject(path, sourceStorageDto).thenPromise(new Function<Void, Promise<Project>>() {

                @Override
                public Promise<Project> apply(Void ignored) throws FunctionException {
                    return ps.getProject(path).then(new Function<ProjectConfigDto, Project>() {

                        @Override
                        public Project apply(ProjectConfigDto config) throws FunctionException {
                            cachedConfigs = add(cachedConfigs, config);
                            Resource project = resourceFactory.newProjectImpl(config, ResourceManager.this);
                            checkState(project != null, "Failed to locate imported project's configuration");
                            store.register(project);
                            eventBus.fireEvent(new ResourceChangedEvent(new ResourceDeltaImpl(project, (resource.isPresent() ? UPDATED : ADDED) | DERIVED)));
                            return (Project) project;
                        }
                    });
                }
            });
        }
    });
}
Also used : Path(org.eclipse.che.ide.resource.Path) 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) FunctionException(org.eclipse.che.api.promises.client.FunctionException) Promise(org.eclipse.che.api.promises.client.Promise) Function(org.eclipse.che.api.promises.client.Function) Project(org.eclipse.che.ide.api.resources.Project) SourceStorage(org.eclipse.che.api.core.model.project.SourceStorage) SourceStorageDto(org.eclipse.che.api.workspace.shared.dto.SourceStorageDto) ResourceChangedEvent(org.eclipse.che.ide.api.resources.ResourceChangedEvent)

Example 3 with Function

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

the class ResourceManager method search.

protected Promise<Resource[]> search(final Container container, String fileMask, String contentMask) {
    QueryExpression queryExpression = new QueryExpression();
    if (!isNullOrEmpty(contentMask)) {
        queryExpression.setText(contentMask);
    }
    if (!isNullOrEmpty(fileMask)) {
        queryExpression.setName(fileMask);
    }
    if (!container.getLocation().isRoot()) {
        queryExpression.setPath(container.getLocation().toString());
    }
    return ps.search(queryExpression).thenPromise(new Function<List<ItemReference>, Promise<Resource[]>>() {

        @Override
        public Promise<Resource[]> apply(final List<ItemReference> references) throws FunctionException {
            if (references.isEmpty()) {
                return promises.resolve(NO_RESOURCES);
            }
            int maxDepth = 0;
            final Path[] paths = new Path[references.size()];
            for (int i = 0; i < paths.length; i++) {
                final Path path = Path.valueOf(references.get(i).getPath());
                paths[i] = path;
                if (path.segmentCount() > maxDepth) {
                    maxDepth = path.segmentCount();
                }
            }
            return getRemoteResources(container, maxDepth, true).then(new Function<Resource[], Resource[]>() {

                @Override
                public Resource[] apply(Resource[] resources) throws FunctionException {
                    Resource[] filtered = NO_RESOURCES;
                    Path[] mutablePaths = paths;
                    outer: for (Resource resource : resources) {
                        if (resource.getResourceType() != FILE) {
                            continue;
                        }
                        for (int i = 0; i < mutablePaths.length; i++) {
                            Path path = mutablePaths[i];
                            if (path.segmentCount() == resource.getLocation().segmentCount() && path.equals(resource.getLocation())) {
                                Resource[] tmpFiltered = copyOf(filtered, filtered.length + 1);
                                tmpFiltered[filtered.length] = resource;
                                filtered = tmpFiltered;
                                //reduce the size of mutablePaths by removing already checked item
                                int size = mutablePaths.length;
                                int numMoved = mutablePaths.length - i - 1;
                                if (numMoved > 0) {
                                    arraycopy(mutablePaths, i + 1, mutablePaths, i, numMoved);
                                }
                                mutablePaths = copyOf(mutablePaths, --size);
                                continue outer;
                            }
                        }
                    }
                    return filtered;
                }
            });
        }
    });
}
Also used : Path(org.eclipse.che.ide.resource.Path) Resource(org.eclipse.che.ide.api.resources.Resource) FunctionException(org.eclipse.che.api.promises.client.FunctionException) ItemReference(org.eclipse.che.api.project.shared.dto.ItemReference) Promise(org.eclipse.che.api.promises.client.Promise) Function(org.eclipse.che.api.promises.client.Function) List(java.util.List) ArrayList(java.util.ArrayList) QueryExpression(org.eclipse.che.ide.api.project.QueryExpression)

Example 4 with Function

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

the class ResourceManager method update.

/**
     * Update state of specific properties in project and save this state on the server.
     * As the result method should return the {@link Promise} with new {@link Project} object.
     * <p/>
     * During the update method have to iterate on children of updated resource and if any of
     * them has changed own type, e.g. folder -> project, project -> folder, specific event
     * has to be fired.
     * <p/>
     * Method is not intended to be called in third party components. It is the service method
     * for {@link Project}.
     *
     * @param path
     *         the path to project which should be updated
     * @param request
     *         the update request
     * @return the {@link Promise} with new {@link Project} object.
     * @see ResourceChangedEvent
     * @see ProjectRequest
     * @see Project#update()
     * @since 4.4.0
     */
protected Promise<Project> update(final Path path, final ProjectRequest request) {
    final ProjectConfig projectConfig = request.getBody();
    final SourceStorage source = projectConfig.getSource();
    final SourceStorageDto sourceDto = dtoFactory.createDto(SourceStorageDto.class);
    if (source != null) {
        sourceDto.setLocation(source.getLocation());
        sourceDto.setType(source.getType());
        sourceDto.setParameters(source.getParameters());
    }
    final ProjectConfigDto dto = dtoFactory.createDto(ProjectConfigDto.class).withName(projectConfig.getName()).withPath(path.toString()).withDescription(projectConfig.getDescription()).withType(projectConfig.getType()).withMixins(projectConfig.getMixins()).withAttributes(projectConfig.getAttributes()).withSource(sourceDto);
    return ps.updateProject(dto).thenPromise(new Function<ProjectConfigDto, Promise<Project>>() {

        @Override
        public Promise<Project> apply(ProjectConfigDto reference) throws FunctionException {
            /* Note: After update, project may become to be other type,
                   e.g. blank -> java or maven, or ant, or etc. And this may
                   cause sub-project creations. Simultaneously on the client
                   side there is outdated information about sub-projects, so
                   we need to get updated project list. */
            //dispose outdated resource
            final Optional<Resource> outdatedResource = store.getResource(path);
            checkState(outdatedResource.isPresent(), "Outdated resource wasn't found");
            final Resource resource = outdatedResource.get();
            checkState(resource instanceof Container, "Outdated resource is not a container");
            Container container = (Container) resource;
            if (resource instanceof Folder) {
                Container parent = resource.getParent();
                checkState(parent != null, "Parent of the resource wasn't found");
                container = parent;
            }
            return synchronize(container).then(new Function<Resource[], Project>() {

                @Override
                public Project apply(Resource[] synced) throws FunctionException {
                    final Optional<Resource> updatedProject = store.getResource(path);
                    checkState(updatedProject.isPresent(), "Updated resource is not present");
                    checkState(updatedProject.get().isProject(), "Updated resource is not a project");
                    eventBus.fireEvent(new ResourceChangedEvent(new ResourceDeltaImpl(updatedProject.get(), UPDATED)));
                    return (Project) updatedProject.get();
                }
            });
        }
    });
}
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) FunctionException(org.eclipse.che.api.promises.client.FunctionException) Resource(org.eclipse.che.ide.api.resources.Resource) Folder(org.eclipse.che.ide.api.resources.Folder) ProjectConfig(org.eclipse.che.api.core.model.project.ProjectConfig) MutableProjectConfig(org.eclipse.che.ide.api.project.MutableProjectConfig) NewProjectConfig(org.eclipse.che.api.core.model.project.NewProjectConfig) Promise(org.eclipse.che.api.promises.client.Promise) Function(org.eclipse.che.api.promises.client.Function) Project(org.eclipse.che.ide.api.resources.Project) SourceStorage(org.eclipse.che.api.core.model.project.SourceStorage) Container(org.eclipse.che.ide.api.resources.Container) SourceStorageDto(org.eclipse.che.api.workspace.shared.dto.SourceStorageDto) ResourceChangedEvent(org.eclipse.che.ide.api.resources.ResourceChangedEvent)

Example 5 with Function

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

the class ResourceManager method createProject.

Promise<Project> createProject(final Project.ProjectRequest createRequest) {
    checkArgument(checkProjectName(createRequest.getBody().getName()), "Invalid project name");
    checkArgument(typeRegistry.getProjectType(createRequest.getBody().getType()) != null, "Invalid project type");
    final Path path = Path.valueOf(createRequest.getBody().getPath());
    return findResource(path, true).thenPromise(new Function<Optional<Resource>, Promise<Project>>() {

        @Override
        public Promise<Project> apply(Optional<Resource> resource) throws FunctionException {
            if (resource.isPresent()) {
                if (resource.get().isProject()) {
                    throw new IllegalStateException("Project already exists");
                } else if (resource.get().isFile()) {
                    throw new IllegalStateException("File can not be converted to project");
                }
                return update(path, createRequest);
            }
            final MutableProjectConfig projectConfig = (MutableProjectConfig) createRequest.getBody();
            final List<NewProjectConfig> projectConfigList = projectConfig.getProjects();
            projectConfigList.add(asDto(projectConfig));
            final List<NewProjectConfigDto> configDtoList = asDto(projectConfigList);
            return ps.createBatchProjects(configDtoList).thenPromise(new Function<List<ProjectConfigDto>, Promise<Project>>() {

                @Override
                public Promise<Project> apply(final List<ProjectConfigDto> configList) throws FunctionException {
                    return ps.getProjects().then(new Function<List<ProjectConfigDto>, Project>() {

                        @Override
                        public Project apply(List<ProjectConfigDto> updatedConfiguration) throws FunctionException {
                            //cache new configs
                            cachedConfigs = updatedConfiguration.toArray(new ProjectConfigDto[updatedConfiguration.size()]);
                            for (ProjectConfigDto projectConfigDto : configList) {
                                if (projectConfigDto.getPath().equals(path.toString())) {
                                    final Project newResource = resourceFactory.newProjectImpl(projectConfigDto, ResourceManager.this);
                                    store.register(newResource);
                                    eventBus.fireEvent(new ResourceChangedEvent(new ResourceDeltaImpl(newResource, ADDED | DERIVED)));
                                    return newResource;
                                }
                            }
                            throw new IllegalStateException("Created project is not found");
                        }
                    });
                }
            });
        }
    });
}
Also used : Path(org.eclipse.che.ide.resource.Path) MutableProjectConfig(org.eclipse.che.ide.api.project.MutableProjectConfig) 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) FunctionException(org.eclipse.che.api.promises.client.FunctionException) Promise(org.eclipse.che.api.promises.client.Promise) Function(org.eclipse.che.api.promises.client.Function) Project(org.eclipse.che.ide.api.resources.Project) List(java.util.List) ArrayList(java.util.ArrayList) ResourceChangedEvent(org.eclipse.che.ide.api.resources.ResourceChangedEvent)

Aggregations

Function (org.eclipse.che.api.promises.client.Function)17 FunctionException (org.eclipse.che.api.promises.client.FunctionException)11 Promise (org.eclipse.che.api.promises.client.Promise)9 Resource (org.eclipse.che.ide.api.resources.Resource)9 PromiseError (org.eclipse.che.api.promises.client.PromiseError)8 Optional (com.google.common.base.Optional)6 OperationException (org.eclipse.che.api.promises.client.OperationException)5 Project (org.eclipse.che.ide.api.resources.Project)5 ResourceChangedEvent (org.eclipse.che.ide.api.resources.ResourceChangedEvent)5 Path (org.eclipse.che.ide.resource.Path)5 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 ArrayList (java.util.ArrayList)3 List (java.util.List)3 Map (java.util.Map)3 SourceStorage (org.eclipse.che.api.core.model.project.SourceStorage)3 AsyncCallback (com.google.gwt.user.client.rpc.AsyncCallback)2 Operation (org.eclipse.che.api.promises.client.Operation)2 RequestCall (org.eclipse.che.api.promises.client.callback.AsyncPromiseHelper.RequestCall)2