Search in sources :

Example 1 with AttributeValue

use of org.eclipse.che.api.project.server.type.AttributeValue in project che by eclipse.

the class ProjectManagerWriteTest method testSettableValueProvider.

@Test
public void testSettableValueProvider() throws Exception {
    assertTrue(((Variable) projectTypeRegistry.getProjectType("settableVPPT").getAttribute("my")).isValueProvided());
    ProjectConfig pc = new NewProjectConfigImpl("/testSettableValueProvider", "settableVPPT", null, "", "", new HashMap<>(), null, null);
    pm.createProject(pc, null);
    RegisteredProject project = pm.getProject("/testSettableValueProvider");
    assertEquals(1, project.getAttributes().size());
    assertEquals("notset", project.getAttributes().get("my").get(0));
    Map<String, List<String>> attributes = new HashMap<>();
    attributes.put("my", new AttributeValue("set").getList());
    pc = new NewProjectConfigImpl("/testSettableValueProvider", "settableVPPT", null, "", "", attributes, null, null);
    pm.updateProject(pc);
    project = pm.getProject("/testSettableValueProvider");
    assertEquals("set", project.getAttributes().get("my").get(0));
}
Also used : NewProjectConfig(org.eclipse.che.api.core.model.project.NewProjectConfig) ProjectConfig(org.eclipse.che.api.core.model.project.ProjectConfig) AttributeValue(org.eclipse.che.api.project.server.type.AttributeValue) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test)

Example 2 with AttributeValue

use of org.eclipse.che.api.project.server.type.AttributeValue in project che by eclipse.

the class ProjectManagerWriteTest method testProvidedAttributesNotSerialized.

@Test
public void testProvidedAttributesNotSerialized() throws Exception {
    Map<String, List<String>> attributes = new HashMap<>();
    attributes.put("pt2-var2", new AttributeValue("test2").getList());
    attributes.put("pt2-var1", new AttributeValue("test1").getList());
    ProjectConfig pc = new NewProjectConfigImpl("/testProvidedAttributesNotSerialized", "pt3", null, "name", "descr", attributes, null, null);
    pm.createProject(pc, null);
    for (ProjectConfig project : workspaceHolder.getProjects()) {
        if (project.getPath().equals("/testProvidedAttributesNotSerialized")) {
            assertNotNull(project.getAttributes().get("pt2-var1"));
            assertNotNull(project.getAttributes().get("pt2-var2"));
            assertNull(project.getAttributes().get("pt2-const1"));
            assertNull(project.getAttributes().get("pt2-provided1"));
        }
    }
}
Also used : NewProjectConfig(org.eclipse.che.api.core.model.project.NewProjectConfig) ProjectConfig(org.eclipse.che.api.core.model.project.ProjectConfig) AttributeValue(org.eclipse.che.api.project.server.type.AttributeValue) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test)

Example 3 with AttributeValue

use of org.eclipse.che.api.project.server.type.AttributeValue in project che by eclipse.

the class ProjectManagerWriteTest method testCreateProjectWithRequiredProvidedAttributeWhenGivenProjectTypeHasGenerator.

@Test
public void testCreateProjectWithRequiredProvidedAttributeWhenGivenProjectTypeHasGenerator() throws Exception {
    final String path = "/testCreateProjectWithRequiredProvidedAttribute";
    final String projectTypeId = "pt3";
    final Map<String, List<String>> attributes = new HashMap<>();
    attributes.put("pt2-var2", new AttributeValue("test").getList());
    final ProjectConfig pc = new NewProjectConfigImpl(path, projectTypeId, null, "name", "descr", attributes, null, null);
    pm.createProject(pc, null);
    final RegisteredProject project = projectRegistry.getProject(path);
    assertEquals(projectTypeId, project.getType());
    assertNotNull(project.getBaseFolder().getChild("file1"));
    assertEquals("pt2-provided1", project.getAttributes().get("pt2-provided1").get(0));
}
Also used : NewProjectConfig(org.eclipse.che.api.core.model.project.NewProjectConfig) ProjectConfig(org.eclipse.che.api.core.model.project.ProjectConfig) AttributeValue(org.eclipse.che.api.project.server.type.AttributeValue) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test)

Example 4 with AttributeValue

use of org.eclipse.che.api.project.server.type.AttributeValue in project che by eclipse.

the class RegisteredProject method initAttributes.

/**
     * Initialize project attributes.
     * Note: the problem with {@link Problem#code} = 13 will be added when a value for some attribute is not initialized
     */
private void initAttributes() {
    // we take only defined attributes, others ignored
    for (Map.Entry<String, Attribute> entry : types.getAttributeDefs().entrySet()) {
        final Attribute definition = entry.getValue();
        final String name = entry.getKey();
        AttributeValue value = new AttributeValue(config.getAttributes().get(name));
        if (!definition.isVariable()) {
            // constant, value always assumed as stated in definition
            attributes.put(name, definition.getValue());
        } else {
            // variable
            final Variable variable = (Variable) definition;
            // value provided
            if (variable.isValueProvided()) {
                final ValueProvider valueProvider = variable.getValueProviderFactory().newInstance(folder);
                if (folder != null) {
                    try {
                        if (!valueProvider.isSettable() || value.isEmpty()) {
                            // get provided value
                            value = new AttributeValue(valueProvider.getValues(name));
                        } else {
                            // set provided (not empty) value
                            valueProvider.setValues(name, value.getList());
                        }
                    } catch (ValueStorageException e) {
                        final Problem problem = new Problem(ATTRIBUTE_NAME_PROBLEM, format("Value for attribute %s is not initialized, caused by: %s", variable.getId(), e.getLocalizedMessage()));
                        this.problems.add(problem);
                    }
                } else {
                    continue;
                }
            }
            if (value.isEmpty() && variable.isRequired()) {
                final Problem problem = new Problem(ATTRIBUTE_NAME_PROBLEM, "Value for required attribute is not initialized " + variable.getId());
                this.problems.add(problem);
            //throw new ProjectTypeConstraintException("Value for required attribute is not initialized " + variable.getId());
            }
            if (!value.isEmpty()) {
                this.attributes.put(name, value);
            }
        }
    }
}
Also used : AttributeValue(org.eclipse.che.api.project.server.type.AttributeValue) Variable(org.eclipse.che.api.project.server.type.Variable) Attribute(org.eclipse.che.api.core.model.project.type.Attribute) ValueStorageException(org.eclipse.che.api.project.server.type.ValueStorageException) ValueProvider(org.eclipse.che.api.project.server.type.ValueProvider) HashMap(java.util.HashMap) Map(java.util.Map)

Example 5 with AttributeValue

use of org.eclipse.che.api.project.server.type.AttributeValue in project che by eclipse.

the class ArchetypeGenerationStrategy method generateProject.

@Override
public void generateProject(final Path projectPath, Map<String, AttributeValue> attributes, Map<String, String> options) throws ForbiddenException, ConflictException, ServerException {
    AttributeValue artifactId = attributes.get(ARTIFACT_ID);
    AttributeValue groupId = attributes.get(GROUP_ID);
    AttributeValue version = attributes.get(VERSION);
    if (groupId == null || artifactId == null || version == null) {
        throw new ServerException("Missed some required attribute (groupId, artifactId or version)");
    }
    String archetypeGroupId = null;
    String archetypeArtifactId = null;
    String archetypeVersion = null;
    String archetypeRepository = null;
    Map<String, String> archetypeProperties = new HashMap<>();
    //TODO: remove prop 'type' now it use only for detecting generation strategy
    options.remove("type");
    for (Entry<String, String> entry : options.entrySet()) {
        switch(entry.getKey()) {
            case "archetypeGroupId":
                archetypeGroupId = entry.getValue();
                break;
            case "archetypeArtifactId":
                archetypeArtifactId = entry.getValue();
                break;
            case "archetypeVersion":
                archetypeVersion = entry.getValue();
                break;
            case "archetypeRepository":
                archetypeRepository = entry.getValue();
                break;
            default:
                archetypeProperties.put(entry.getKey(), entry.getValue());
        }
    }
    if (isNullOrEmpty(archetypeGroupId) || isNullOrEmpty(archetypeArtifactId) || isNullOrEmpty(archetypeVersion)) {
        throw new ServerException("Missed some required option (archetypeGroupId, archetypeArtifactId or archetypeVersion)");
    }
    MavenArchetype mavenArchetype = new MavenArchetypeImpl(archetypeGroupId, archetypeArtifactId, archetypeVersion, archetypeRepository, archetypeProperties);
    final MavenArtifact mavenArtifact = new MavenArtifact();
    mavenArtifact.setGroupId(getFirst(groupId.getList(), projectPath.getName()));
    mavenArtifact.setArtifactId(getFirst(artifactId.getList(), projectPath.getName()));
    mavenArtifact.setVersion(getFirst(version.getList(), DEFAULT_VERSION));
    archetypeGenerator.generateFromArchetype(vfs.getRoot().toIoFile(), mavenArchetype, mavenArtifact);
}
Also used : AttributeValue(org.eclipse.che.api.project.server.type.AttributeValue) ServerException(org.eclipse.che.api.core.ServerException) MavenArchetypeImpl(org.eclipse.che.plugin.maven.generator.archetype.MavenArchetypeImpl) HashMap(java.util.HashMap) MavenArchetype(org.eclipse.che.plugin.maven.shared.MavenArchetype) MavenArtifact(org.eclipse.che.ide.maven.tools.MavenArtifact)

Aggregations

AttributeValue (org.eclipse.che.api.project.server.type.AttributeValue)16 HashMap (java.util.HashMap)11 ArrayList (java.util.ArrayList)8 List (java.util.List)8 Test (org.junit.Test)8 NewProjectConfig (org.eclipse.che.api.core.model.project.NewProjectConfig)7 ProjectConfig (org.eclipse.che.api.core.model.project.ProjectConfig)5 File (java.io.File)2 Map (java.util.Map)2 ServerException (org.eclipse.che.api.core.ServerException)2 VirtualFile (org.eclipse.che.api.vfs.VirtualFile)2 NewProjectConfigDto (org.eclipse.che.api.workspace.shared.dto.NewProjectConfigDto)2 SourceStorageDto (org.eclipse.che.api.workspace.shared.dto.SourceStorageDto)2 IOException (java.io.IOException)1 Path (java.nio.file.Path)1 PathMatcher (java.nio.file.PathMatcher)1 HashSet (java.util.HashSet)1 LinkedHashSet (java.util.LinkedHashSet)1 Set (java.util.Set)1 TimeoutException (java.util.concurrent.TimeoutException)1