Search in sources :

Example 6 with ProjectConfigDto

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

the class ProjectServiceTest method testCreateProject.

@Test
public void testCreateProject() throws Exception {
    final String projectName = "new_project";
    final String projectType = "testCreateProject";
    phRegistry.register(createProjectHandlerFor(projectName, projectType));
    Map<String, List<String>> headers = new HashMap<>();
    headers.put("Content-Type", singletonList(APPLICATION_JSON));
    ProjectTypeDef pt = new ProjectTypeDef("testCreateProject", "my project type", true, false) {

        {
            addConstantDefinition("new_project_attribute", "attr description", "to be or not to be");
        }
    };
    ptRegistry.registerProjectType(pt);
    Map<String, List<String>> attributeValues = new LinkedHashMap<>();
    attributeValues.put("new_project_attribute", singletonList("to be or not to be"));
    final ProjectConfigDto newProjectConfig = DtoFactory.getInstance().createDto(ProjectConfigDto.class).withPath("/new_project").withName(projectName).withDescription("new project").withType(projectType).withAttributes(attributeValues).withSource(DtoFactory.getInstance().createDto(SourceStorageDto.class));
    projects.add(newProjectConfig);
    ContainerResponse response = launcher.service(POST, "http://localhost:8080/api/project", "http://localhost:8080/api", headers, DtoFactory.getInstance().toJson(newProjectConfig).getBytes(Charset.defaultCharset()), null);
    assertEquals(response.getStatus(), 200, "Error: " + response.getEntity());
    ProjectConfigDto result = (ProjectConfigDto) response.getEntity();
    assertNotNull(result);
    assertEquals(result.getName(), projectName);
    assertEquals(result.getPath(), "/new_project");
    assertEquals(result.getDescription(), newProjectConfig.getDescription());
    assertEquals(result.getType(), newProjectConfig.getType());
    assertEquals(result.getType(), projectType);
    Map<String, List<String>> attributes = result.getAttributes();
    assertNotNull(attributes);
    assertEquals(attributes.size(), 1);
    assertEquals(attributes.get("new_project_attribute"), singletonList("to be or not to be"));
    validateProjectLinks(result);
    RegisteredProject project = pm.getProject("new_project");
    assertNotNull(project);
    //ProjectConfig config = project.getConfig();
    assertEquals(project.getDescription(), newProjectConfig.getDescription());
    assertEquals(project.getProjectType().getId(), newProjectConfig.getType());
    String attributeVal = project.getAttributeEntries().get("new_project_attribute").getString();
    assertNotNull(attributeVal);
    assertEquals(attributeVal, "to be or not to be");
    assertNotNull(project.getBaseFolder().getChild("a"));
    assertNotNull(project.getBaseFolder().getChild("b"));
    assertNotNull(project.getBaseFolder().getChild("test.txt"));
}
Also used : SourceStorageDto(org.eclipse.che.api.workspace.shared.dto.SourceStorageDto) ContainerResponse(org.everrest.core.impl.ContainerResponse) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) ProjectConfigDto(org.eclipse.che.api.workspace.shared.dto.ProjectConfigDto) Collections.singletonList(java.util.Collections.singletonList) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList) ProjectTypeDef(org.eclipse.che.api.project.server.type.ProjectTypeDef) LinkedHashMap(java.util.LinkedHashMap) Test(org.testng.annotations.Test)

Example 7 with ProjectConfigDto

use of org.eclipse.che.api.workspace.shared.dto.ProjectConfigDto 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 8 with ProjectConfigDto

use of org.eclipse.che.api.workspace.shared.dto.ProjectConfigDto 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 9 with ProjectConfigDto

use of org.eclipse.che.api.workspace.shared.dto.ProjectConfigDto 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)

Example 10 with ProjectConfigDto

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

the class ProjectServiceTest method testUpdateProjectInvalidPath.

@Test
public void testUpdateProjectInvalidPath() throws Exception {
    Map<String, List<String>> headers = new HashMap<>();
    headers.put("Content-Type", singletonList(APPLICATION_JSON));
    Map<String, List<String>> attributeValues = new LinkedHashMap<>();
    attributeValues.put("my_attribute", singletonList("to be or not to be"));
    ProjectConfigDto descriptor = DtoFactory.getInstance().createDto(ProjectConfigDto.class).withType("my_project_type").withDescription("updated project").withAttributes(attributeValues);
    ContainerResponse response = launcher.service(PUT, "http://localhost:8080/api/project/my_project_invalid", "http://localhost:8080/api", headers, DtoFactory.getInstance().toJson(descriptor).getBytes(Charset.defaultCharset()), null);
    assertEquals(response.getStatus(), 404);
}
Also used : ContainerResponse(org.everrest.core.impl.ContainerResponse) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) ProjectConfigDto(org.eclipse.che.api.workspace.shared.dto.ProjectConfigDto) Collections.singletonList(java.util.Collections.singletonList) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList) LinkedHashMap(java.util.LinkedHashMap) Test(org.testng.annotations.Test)

Aggregations

ProjectConfigDto (org.eclipse.che.api.workspace.shared.dto.ProjectConfigDto)30 Test (org.testng.annotations.Test)14 List (java.util.List)13 ArrayList (java.util.ArrayList)12 HashMap (java.util.HashMap)10 LinkedList (java.util.LinkedList)10 ContainerResponse (org.everrest.core.impl.ContainerResponse)10 Collections.singletonList (java.util.Collections.singletonList)9 LinkedHashMap (java.util.LinkedHashMap)7 Resource (org.eclipse.che.ide.api.resources.Resource)6 NewProjectConfigDto (org.eclipse.che.api.workspace.shared.dto.NewProjectConfigDto)5 SourceStorageDto (org.eclipse.che.api.workspace.shared.dto.SourceStorageDto)5 WorkspaceConfigDto (org.eclipse.che.api.workspace.shared.dto.WorkspaceConfigDto)5 ResourceChangedEvent (org.eclipse.che.ide.api.resources.ResourceChangedEvent)5 Optional (com.google.common.base.Optional)4 WorkspaceDto (org.eclipse.che.api.workspace.shared.dto.WorkspaceDto)4 ApiOperation (io.swagger.annotations.ApiOperation)3 ApiResponses (io.swagger.annotations.ApiResponses)3 Function (org.eclipse.che.api.promises.client.Function)3 FunctionException (org.eclipse.che.api.promises.client.FunctionException)3