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;
}
});
}
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();
}
});
}
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));
}
}
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);
}
}
Aggregations