Search in sources :

Example 6 with Function

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

the class ResourceManager method onExternalDeltaMoved.

private Promise<Void> onExternalDeltaMoved(final ResourceDelta delta) {
    //search resource to remove at first
    return findResource(delta.getFromPath(), true).thenPromise(new Function<Optional<Resource>, Promise<Void>>() {

        @Override
        public Promise<Void> apply(final Optional<Resource> toRemove) throws FunctionException {
            if (!toRemove.isPresent()) {
                return promises.resolve(null);
            }
            store.dispose(delta.getFromPath(), true);
            return findResource(delta.getToPath(), true).then(new Function<Optional<Resource>, Void>() {

                @Override
                public Void apply(final Optional<Resource> resource) throws FunctionException {
                    if (resource.isPresent() && toRemove.isPresent()) {
                        Resource intercepted = resource.get();
                        if (!store.getResource(intercepted.getLocation()).isPresent()) {
                            store.register(intercepted);
                        }
                        eventBus.fireEvent(new ResourceChangedEvent(new ResourceDeltaImpl(intercepted, toRemove.get(), ADDED | MOVED_FROM | MOVED_TO | DERIVED)));
                    }
                    return null;
                }
            });
        }
    });
}
Also used : Promise(org.eclipse.che.api.promises.client.Promise) Function(org.eclipse.che.api.promises.client.Function) Optional(com.google.common.base.Optional) Resource(org.eclipse.che.ide.api.resources.Resource) FunctionException(org.eclipse.che.api.promises.client.FunctionException) ResourceChangedEvent(org.eclipse.che.ide.api.resources.ResourceChangedEvent)

Example 7 with Function

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

the class ProjectImporter method doImport.

private Promise<Project> doImport(final Path path, final SourceStorage sourceStorage) {
    final ProjectNotificationSubscriber subscriber = subscriberFactory.createSubscriber();
    subscriber.subscribe(path.lastSegment());
    MutableProjectConfig importConfig = new MutableProjectConfig();
    importConfig.setPath(path.toString());
    importConfig.setSource(sourceStorage);
    return appContext.getWorkspaceRoot().importProject().withBody(importConfig).send().thenPromise(new Function<Project, Promise<Project>>() {

        @Override
        public Promise<Project> apply(Project project) throws FunctionException {
            subscriber.onSuccess();
            return projectResolver.resolve(project);
        }
    }).catchErrorPromise(new Function<PromiseError, Promise<Project>>() {

        @Override
        public Promise<Project> apply(PromiseError exception) throws FunctionException {
            subscriber.onFailure(exception.getCause().getMessage());
            switch(getErrorCode(exception.getCause())) {
                case UNABLE_GET_PRIVATE_SSH_KEY:
                    throw new IllegalStateException(localizationConstant.importProjectMessageUnableGetSshKey());
                case UNAUTHORIZED_SVN_OPERATION:
                    return recallImportWithCredentials(sourceStorage, path);
                case UNAUTHORIZED_GIT_OPERATION:
                    final Map<String, String> attributes = ExceptionUtils.getAttributes(exception.getCause());
                    final String providerName = attributes.get(PROVIDER_NAME);
                    final String authenticateUrl = attributes.get(AUTHENTICATE_URL);
                    if (!Strings.isNullOrEmpty(providerName) && !Strings.isNullOrEmpty(authenticateUrl)) {
                        return authUserAndRecallImport(providerName, authenticateUrl, path, sourceStorage, subscriber);
                    } else {
                        throw new IllegalStateException(localizationConstant.oauthFailedToGetAuthenticatorText());
                    }
                default:
                    throw new IllegalStateException(exception.getCause());
            }
        }
    });
}
Also used : Function(org.eclipse.che.api.promises.client.Function) Project(org.eclipse.che.ide.api.resources.Project) Promise(org.eclipse.che.api.promises.client.Promise) MutableProjectConfig(org.eclipse.che.ide.api.project.MutableProjectConfig) ProjectNotificationSubscriber(org.eclipse.che.ide.api.project.wizard.ProjectNotificationSubscriber) PromiseError(org.eclipse.che.api.promises.client.PromiseError) FunctionException(org.eclipse.che.api.promises.client.FunctionException) Map(java.util.Map)

Example 8 with Function

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

the class ProjectResolver method resolve.

public Promise<Project> resolve(final Project project) {
    return project.resolve().thenPromise(new Function<List<SourceEstimation>, Promise<Project>>() {

        @Override
        public Promise<Project> apply(List<SourceEstimation> estimations) throws FunctionException {
            if (estimations == null || estimations.isEmpty()) {
                return promiseProvider.resolve(project);
            }
            final List<String> primeTypes = newArrayList();
            for (SourceEstimation estimation : estimations) {
                if (projectTypeRegistry.getProjectType(estimation.getType()).isPrimaryable()) {
                    primeTypes.add(estimation.getType());
                }
            }
            final MutableProjectConfig config = new MutableProjectConfig(project);
            final SourceStorage source = project.getSource();
            if (source != null && source.getParameters() != null && source.getParameters().containsKey("keepDir")) {
                config.setType(Constants.BLANK_ID);
            } else if (primeTypes.isEmpty()) {
                return promiseProvider.resolve(project);
            } else if (primeTypes.size() == 1) {
                config.setType(primeTypes.get(0));
            } else {
                config.setType(Constants.BLANK_ID);
                projectWizard.show(config);
                return promiseProvider.resolve(project);
            }
            return project.update().withBody(config).send();
        }
    }).catchErrorPromise(new Function<PromiseError, Promise<Project>>() {

        @Override
        public Promise<Project> apply(PromiseError error) throws FunctionException {
            Log.warn(ProjectResolver.class, error.getMessage());
            return promiseProvider.resolve(project);
        }
    });
}
Also used : MutableProjectConfig(org.eclipse.che.ide.api.project.MutableProjectConfig) FunctionException(org.eclipse.che.api.promises.client.FunctionException) Function(org.eclipse.che.api.promises.client.Function) Project(org.eclipse.che.ide.api.resources.Project) Promise(org.eclipse.che.api.promises.client.Promise) SourceStorage(org.eclipse.che.api.core.model.project.SourceStorage) PromiseError(org.eclipse.che.api.promises.client.PromiseError) SourceEstimation(org.eclipse.che.api.project.shared.dto.SourceEstimation) List(java.util.List) Lists.newArrayList(com.google.common.collect.Lists.newArrayList)

Example 9 with Function

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

the class CopyPasteManager method moveResource.

private Promise<Void> moveResource(final Resource resource, final Path destination) {
    //simple move without overwriting
    return resource.move(destination).thenPromise(new Function<Resource, Promise<Void>>() {

        @Override
        public Promise<Void> apply(Resource resource) throws FunctionException {
            eventBus.fireEvent(new RevealResourceEvent(resource));
            return promises.resolve(null);
        }
    }).catchErrorPromise(new Function<PromiseError, Promise<Void>>() {

        @Override
        public Promise<Void> apply(final PromiseError error) throws FunctionException {
            //resource may already exists
            if (error.getMessage().contains("exists")) {
                //create dialog with overwriting option
                return createFromAsyncRequest(new RequestCall<Void>() {

                    @Override
                    public void makeCall(final AsyncCallback<Void> callback) {
                        //handle overwrite operation
                        final ConfirmCallback overwrite = new ConfirmCallback() {

                            @Override
                            public void accepted() {
                                //copy with overwriting
                                resource.move(destination, true).then(new Operation<Resource>() {

                                    @Override
                                    public void apply(Resource ignored) throws OperationException {
                                        callback.onSuccess(null);
                                    }
                                }).catchError(new Operation<PromiseError>() {

                                    @Override
                                    public void apply(PromiseError error) throws OperationException {
                                        callback.onFailure(error.getCause());
                                    }
                                });
                            }
                        };
                        //skip this resource
                        final ConfirmCallback skip = new ConfirmCallback() {

                            @Override
                            public void accepted() {
                                callback.onSuccess(null);
                            }
                        };
                        //change destination name
                        final ConfirmCallback rename = new ConfirmCallback() {

                            @Override
                            public void accepted() {
                                dialogFactory.createInputDialog("Enter new name", "Enter new name", new InputCallback() {

                                    @Override
                                    public void accepted(String value) {
                                        final Path newPath = destination.parent().append(value);
                                        moveResource(resource, newPath).then(new Operation<Void>() {

                                            @Override
                                            public void apply(Void result) throws OperationException {
                                                callback.onSuccess(result);
                                            }
                                        }).catchError(new Operation<PromiseError>() {

                                            @Override
                                            public void apply(PromiseError error) throws OperationException {
                                                callback.onFailure(error.getCause());
                                            }
                                        });
                                    }
                                }, new CancelCallback() {

                                    @Override
                                    public void cancelled() {
                                    }
                                }).show();
                            }
                        };
                        dialogFactory.createChoiceDialog("Error", error.getMessage(), "Overwrite", "Skip", "Change Name", overwrite, skip, rename).show();
                    }
                });
            } else {
                //notify user about failed copying
                notificationManager.notify("Error moving resource", error.getMessage(), FAIL, FLOAT_MODE);
                return promises.resolve(null);
            }
        }
    });
}
Also used : Path(org.eclipse.che.ide.resource.Path) ConfirmCallback(org.eclipse.che.ide.api.dialogs.ConfirmCallback) RequestCall(org.eclipse.che.api.promises.client.callback.AsyncPromiseHelper.RequestCall) InputCallback(org.eclipse.che.ide.api.dialogs.InputCallback) AsyncCallback(com.google.gwt.user.client.rpc.AsyncCallback) Resource(org.eclipse.che.ide.api.resources.Resource) FunctionException(org.eclipse.che.api.promises.client.FunctionException) Operation(org.eclipse.che.api.promises.client.Operation) Function(org.eclipse.che.api.promises.client.Function) Promise(org.eclipse.che.api.promises.client.Promise) PromiseError(org.eclipse.che.api.promises.client.PromiseError) CancelCallback(org.eclipse.che.ide.api.dialogs.CancelCallback) RevealResourceEvent(org.eclipse.che.ide.resources.reveal.RevealResourceEvent) OperationException(org.eclipse.che.api.promises.client.OperationException)

Example 10 with Function

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

the class CopyPasteManager method copyResource.

private Promise<Void> copyResource(final Resource resource, final Path destination) {
    //simple copy without overwriting
    return resource.copy(destination).thenPromise(new Function<Resource, Promise<Void>>() {

        @Override
        public Promise<Void> apply(Resource resource) throws FunctionException {
            eventBus.fireEvent(new RevealResourceEvent(resource));
            return promises.resolve(null);
        }
    }).catchErrorPromise(new Function<PromiseError, Promise<Void>>() {

        @Override
        public Promise<Void> apply(final PromiseError error) throws FunctionException {
            //resource may already exists
            if (error.getMessage().contains("exists")) {
                //create dialog with overwriting option
                return createFromAsyncRequest(new RequestCall<Void>() {

                    @Override
                    public void makeCall(final AsyncCallback<Void> callback) {
                        //handle overwrite operation
                        final ConfirmCallback overwrite = new ConfirmCallback() {

                            @Override
                            public void accepted() {
                                //copy with overwriting
                                resource.copy(destination, true).then(new Operation<Resource>() {

                                    @Override
                                    public void apply(Resource ignored) throws OperationException {
                                        callback.onSuccess(null);
                                    }
                                }).catchError(new Operation<PromiseError>() {

                                    @Override
                                    public void apply(PromiseError error) throws OperationException {
                                        callback.onFailure(error.getCause());
                                    }
                                });
                            }
                        };
                        //skip this resource
                        final ConfirmCallback skip = new ConfirmCallback() {

                            @Override
                            public void accepted() {
                                callback.onSuccess(null);
                            }
                        };
                        //change destination name
                        final ConfirmCallback rename = new ConfirmCallback() {

                            @Override
                            public void accepted() {
                                dialogFactory.createInputDialog("Enter new name", "Enter new name", new InputCallback() {

                                    @Override
                                    public void accepted(String value) {
                                        final Path newPath = destination.parent().append(value);
                                        copyResource(resource, newPath).then(new Operation<Void>() {

                                            @Override
                                            public void apply(Void result) throws OperationException {
                                                callback.onSuccess(result);
                                            }
                                        }).catchError(new Operation<PromiseError>() {

                                            @Override
                                            public void apply(PromiseError error) throws OperationException {
                                                callback.onFailure(error.getCause());
                                            }
                                        });
                                    }
                                }, new CancelCallback() {

                                    @Override
                                    public void cancelled() {
                                    }
                                }).show();
                            }
                        };
                        dialogFactory.createChoiceDialog("Error", error.getMessage(), "Overwrite", "Skip", "Change Name", overwrite, skip, rename).show();
                    }
                });
            } else {
                //notify user about failed copying
                notificationManager.notify("Error copying resource", error.getMessage(), FAIL, FLOAT_MODE);
                return promises.resolve(null);
            }
        }
    });
}
Also used : Path(org.eclipse.che.ide.resource.Path) ConfirmCallback(org.eclipse.che.ide.api.dialogs.ConfirmCallback) RequestCall(org.eclipse.che.api.promises.client.callback.AsyncPromiseHelper.RequestCall) InputCallback(org.eclipse.che.ide.api.dialogs.InputCallback) AsyncCallback(com.google.gwt.user.client.rpc.AsyncCallback) Resource(org.eclipse.che.ide.api.resources.Resource) FunctionException(org.eclipse.che.api.promises.client.FunctionException) Operation(org.eclipse.che.api.promises.client.Operation) Function(org.eclipse.che.api.promises.client.Function) Promise(org.eclipse.che.api.promises.client.Promise) PromiseError(org.eclipse.che.api.promises.client.PromiseError) CancelCallback(org.eclipse.che.ide.api.dialogs.CancelCallback) RevealResourceEvent(org.eclipse.che.ide.resources.reveal.RevealResourceEvent) OperationException(org.eclipse.che.api.promises.client.OperationException)

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