Search in sources :

Example 1 with TreeElement

use of org.eclipse.che.api.project.shared.dto.TreeElement in project che by eclipse.

the class ProjectServiceTest method testGetTreeWithDepthAndIncludeFilesNoFiles.

@Test
public void testGetTreeWithDepthAndIncludeFilesNoFiles() throws Exception {
    RegisteredProject myProject = pm.getProject("my_project");
    FolderEntry a = myProject.getBaseFolder().createFolder("a");
    a.createFolder("b/c");
    a.createFolder("x");
    ContainerResponse response = launcher.service(GET, "http://localhost:8080/api/project/tree/my_project/a?depth=100&includeFiles=true", "http://localhost:8080/api", null, null, null);
    assertEquals(response.getStatus(), 200, "Error: " + response.getEntity());
    TreeElement tree = (TreeElement) response.getEntity();
    ItemReference a_node = tree.getNode();
    assertEquals(a_node.getName(), "a");
    List<TreeElement> children = tree.getChildren();
    assertNotNull(children);
    Set<String> names = new LinkedHashSet<>(4);
    for (TreeElement subTree : children) {
        ItemReference _node = subTree.getNode();
        validateFolderLinks(_node);
        String name = _node.getName();
        names.add(name);
        for (TreeElement subSubTree : subTree.getChildren()) {
            ItemReference __node = subSubTree.getNode();
            validateFolderLinks(__node);
            names.add(name + "/" + __node.getName());
        }
    }
    Assert.assertTrue(names.contains("b"));
    Assert.assertTrue(names.contains("x"));
    Assert.assertTrue(names.contains("b/c"));
    Assert.assertFalse(names.contains("x/test.txt"));
}
Also used : ItemReference(org.eclipse.che.api.project.shared.dto.ItemReference) LinkedHashSet(java.util.LinkedHashSet) ContainerResponse(org.everrest.core.impl.ContainerResponse) TreeElement(org.eclipse.che.api.project.shared.dto.TreeElement) Test(org.testng.annotations.Test)

Example 2 with TreeElement

use of org.eclipse.che.api.project.shared.dto.TreeElement in project che by eclipse.

the class ResourceManager method traverse.

private void traverse(TreeElement tree, ResourceVisitor visitor) {
    for (final TreeElement element : tree.getChildren()) {
        final Resource resource = newResourceFrom(element.getNode());
        visitor.visit(resource);
        if (resource instanceof Container) {
            traverse(element, visitor);
        }
    }
}
Also used : Container(org.eclipse.che.ide.api.resources.Container) Resource(org.eclipse.che.ide.api.resources.Resource) TreeElement(org.eclipse.che.api.project.shared.dto.TreeElement)

Example 3 with TreeElement

use of org.eclipse.che.api.project.shared.dto.TreeElement in project che by eclipse.

the class ProjectServiceTest method testGetTreeWithDepthAndIncludeFiles.

@Test
public void testGetTreeWithDepthAndIncludeFiles() throws Exception {
    RegisteredProject myProject = pm.getProject("my_project");
    FolderEntry a = myProject.getBaseFolder().createFolder("a");
    a.createFolder("b/c");
    a.createFolder("x").createFile("test.txt", "test".getBytes(Charset.defaultCharset()));
    ContainerResponse response = launcher.service(GET, "http://localhost:8080/api/project/tree/my_project/a?depth=100&includeFiles=true", "http://localhost:8080/api", null, null, null);
    assertEquals(response.getStatus(), 200, "Error: " + response.getEntity());
    TreeElement tree = (TreeElement) response.getEntity();
    ItemReference a_node = tree.getNode();
    assertEquals(a_node.getName(), "a");
    List<TreeElement> children = tree.getChildren();
    assertNotNull(children);
    Set<String> names = new LinkedHashSet<>(4);
    for (TreeElement subTree : children) {
        ItemReference _node = subTree.getNode();
        validateFolderLinks(_node);
        String name = _node.getName();
        names.add(name);
        for (TreeElement subSubTree : subTree.getChildren()) {
            ItemReference __node = subSubTree.getNode();
            if (__node.getType().equals("folder")) {
                validateFolderLinks(__node);
            } else if (__node.getType().equals("file")) {
                validateFileLinks(__node);
            }
            names.add(name + "/" + __node.getName());
        }
    }
    Assert.assertTrue(names.contains("b"));
    Assert.assertTrue(names.contains("x"));
    Assert.assertTrue(names.contains("b/c"));
    Assert.assertTrue(names.contains("x/test.txt"));
}
Also used : ItemReference(org.eclipse.che.api.project.shared.dto.ItemReference) LinkedHashSet(java.util.LinkedHashSet) ContainerResponse(org.everrest.core.impl.ContainerResponse) TreeElement(org.eclipse.che.api.project.shared.dto.TreeElement) Test(org.testng.annotations.Test)

Example 4 with TreeElement

use of org.eclipse.che.api.project.shared.dto.TreeElement 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 5 with TreeElement

use of org.eclipse.che.api.project.shared.dto.TreeElement in project che by eclipse.

the class ProjectServiceTest method testGetTreeWithDepth.

@Test
public void testGetTreeWithDepth() throws Exception {
    RegisteredProject myProject = pm.getProject("my_project");
    FolderEntry a = myProject.getBaseFolder().createFolder("a");
    a.createFolder("b/c");
    a.createFolder("x/y");
    a.createFile("test.txt", "test".getBytes(Charset.defaultCharset()));
    ContainerResponse response = launcher.service(GET, "http://localhost:8080/api/project/tree/my_project/a?depth=2", "http://localhost:8080/api", null, null, null);
    assertEquals(response.getStatus(), 200, "Error: " + response.getEntity());
    TreeElement tree = (TreeElement) response.getEntity();
    ItemReference a_node = tree.getNode();
    assertEquals(a_node.getName(), "a");
    List<TreeElement> children = tree.getChildren();
    assertNotNull(children);
    Set<String> names = new LinkedHashSet<>(4);
    for (TreeElement subTree : children) {
        ItemReference _node = subTree.getNode();
        validateFolderLinks(_node);
        String name = _node.getName();
        names.add(name);
        for (TreeElement subSubTree : subTree.getChildren()) {
            ItemReference __node = subSubTree.getNode();
            validateFolderLinks(__node);
            names.add(name + "/" + __node.getName());
        }
    }
    Assert.assertTrue(names.contains("b"));
    Assert.assertTrue(names.contains("x"));
    Assert.assertTrue(names.contains("b/c"));
    Assert.assertTrue(names.contains("x/y"));
}
Also used : ItemReference(org.eclipse.che.api.project.shared.dto.ItemReference) LinkedHashSet(java.util.LinkedHashSet) ContainerResponse(org.everrest.core.impl.ContainerResponse) TreeElement(org.eclipse.che.api.project.shared.dto.TreeElement) Test(org.testng.annotations.Test)

Aggregations

TreeElement (org.eclipse.che.api.project.shared.dto.TreeElement)6 LinkedHashSet (java.util.LinkedHashSet)4 ItemReference (org.eclipse.che.api.project.shared.dto.ItemReference)4 ContainerResponse (org.everrest.core.impl.ContainerResponse)4 Test (org.testng.annotations.Test)4 Resource (org.eclipse.che.ide.api.resources.Resource)2 Optional (com.google.common.base.Optional)1 Function (org.eclipse.che.api.promises.client.Function)1 FunctionException (org.eclipse.che.api.promises.client.FunctionException)1 NewProjectConfigDto (org.eclipse.che.api.workspace.shared.dto.NewProjectConfigDto)1 ProjectConfigDto (org.eclipse.che.api.workspace.shared.dto.ProjectConfigDto)1 Container (org.eclipse.che.ide.api.resources.Container)1 ProblemProjectMarker (org.eclipse.che.ide.api.resources.Project.ProblemProjectMarker)1 ResourceChangedEvent (org.eclipse.che.ide.api.resources.ResourceChangedEvent)1