Search in sources :

Example 1 with Attribute

use of org.eclipse.che.api.core.model.project.type.Attribute in project che by eclipse.

the class DtoConverter method asDto.

/** Converts {@link ProjectTypeDef} to {@link ProjectTypeDto}. */
public static ProjectTypeDto asDto(ProjectTypeDef projectType) {
    final List<AttributeDto> typeAttributes = new ArrayList<>();
    for (Attribute attr : projectType.getAttributes()) {
        ValueDto valueDto = newDto(ValueDto.class);
        if (attr.getValue() != null) {
            valueDto.withList(attr.getValue().getList());
        }
        typeAttributes.add(newDto(AttributeDto.class).withName(attr.getName()).withDescription(attr.getDescription()).withRequired(attr.isRequired()).withVariable(attr.isVariable()).withValue(valueDto));
    }
    return newDto(ProjectTypeDto.class).withId(projectType.getId()).withDisplayName(projectType.getDisplayName()).withPrimaryable(projectType.isPrimaryable()).withMixable(projectType.isMixable()).withParents(projectType.getParents()).withAncestors(projectType.getAncestors()).withAttributes(typeAttributes);
}
Also used : AttributeDto(org.eclipse.che.api.project.shared.dto.AttributeDto) ValueDto(org.eclipse.che.api.project.shared.dto.ValueDto) ProjectTypeDto(org.eclipse.che.api.project.shared.dto.ProjectTypeDto) Attribute(org.eclipse.che.api.core.model.project.type.Attribute) ArrayList(java.util.ArrayList)

Example 2 with Attribute

use of org.eclipse.che.api.core.model.project.type.Attribute in project che by eclipse.

the class ProjectTypeRegistry method overrideFactories.

private final void overrideFactories(ProjectTypeDef myType) throws ProjectTypeConstraintException {
    for (Map.Entry<String, ValueProviderFactory> entry : myType.factoriesToOverride.entrySet()) {
        Attribute old = myType.getAttribute(entry.getKey());
        if (old == null || !old.isVariable()) {
            throw new ProjectTypeConstraintException("Can not override Value Provider Factory. Variable not defined: " + myType.getId() + ":" + entry.getKey());
        }
        myType.attributes.put(old.getName(), new Variable(old.getId(), old.getName(), old.getDescription(), old.isRequired(), entry.getValue()));
    }
}
Also used : Attribute(org.eclipse.che.api.core.model.project.type.Attribute) HashMap(java.util.HashMap) Map(java.util.Map)

Example 3 with Attribute

use of org.eclipse.che.api.core.model.project.type.Attribute in project che by eclipse.

the class ProjectTypeRegistry method isNameValid.

/**
     * Checks if incoming Project Type definition has valid ID (Pattern.compile("[^a-zA-Z0-9-_.]")
     * and display name (should not be null or empty)
     *
     * @param type
     * @return true if valid
     */
private boolean isNameValid(ProjectTypeDef type) {
    boolean valid = true;
    if (type.getId() == null || type.getId().isEmpty() || NAME_PATTERN.matcher(type.getId()).find()) {
        LOG.error("Could not register Project Type ID is null or invalid (only Alphanumeric, dash, point and underscore allowed): " + type.getClass().getName());
        valid = false;
    }
    if (type.getDisplayName() == null || type.getDisplayName().isEmpty()) {
        LOG.error("Could not register Project Type with null or empty display name: " + type.getId());
        valid = false;
    }
    for (Attribute attr : type.getAttributes()) {
        // ID spelling (no spaces, only alphanumeric)
        if (NAME_PATTERN.matcher(attr.getName()).find()) {
            LOG.error("Could not register Project Type with invalid attribute Name (only Alphanumeric, dash and underscore allowed): " + attr.getClass().getName() + " ID: '" + attr.getId() + "'");
            valid = false;
        }
    }
    return valid;
}
Also used : Attribute(org.eclipse.che.api.core.model.project.type.Attribute)

Example 4 with Attribute

use of org.eclipse.che.api.core.model.project.type.Attribute 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 Attribute

use of org.eclipse.che.api.core.model.project.type.Attribute in project che by eclipse.

the class ProjectServiceTest method addMockedProjectConfigDto.

private void addMockedProjectConfigDto(org.eclipse.che.api.project.server.type.ProjectTypeDef myProjectType, String projectName) throws ForbiddenException, ServerException, NotFoundException, ConflictException {
    final ProjectConfigDto testProjectConfigMock = mock(ProjectConfigDto.class);
    when(testProjectConfigMock.getPath()).thenReturn("/" + projectName);
    when(testProjectConfigMock.getName()).thenReturn(projectName);
    when(testProjectConfigMock.getDescription()).thenReturn("my test project");
    when(testProjectConfigMock.getType()).thenReturn("my_project_type");
    when(testProjectConfigMock.getSource()).thenReturn(DtoFactory.getInstance().createDto(SourceStorageDto.class));
    //        when(testProjectConfigMock.getModules()).thenReturn(modules);
    //        when(testProjectConfigMock.findModule(anyString())).thenReturn(testProjectConfigMock);
    Map<String, List<String>> attr = new HashMap<>();
    for (Attribute attribute : myProjectType.getAttributes()) {
        attr.put(attribute.getName(), attribute.getValue().getList());
    }
    when(testProjectConfigMock.getAttributes()).thenReturn(attr);
    projects.add(testProjectConfigMock);
    pm.createProject(testProjectConfigMock, null);
}
Also used : SourceStorageDto(org.eclipse.che.api.workspace.shared.dto.SourceStorageDto) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) Attribute(org.eclipse.che.api.core.model.project.type.Attribute) 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)

Aggregations

Attribute (org.eclipse.che.api.core.model.project.type.Attribute)9 HashMap (java.util.HashMap)4 Map (java.util.Map)3 ArrayList (java.util.ArrayList)2 ProjectTypeDef (org.eclipse.che.api.project.server.type.ProjectTypeDef)2 Maps.newHashMap (com.google.common.collect.Maps.newHashMap)1 Collections.singletonList (java.util.Collections.singletonList)1 HashSet (java.util.HashSet)1 LinkedHashMap (java.util.LinkedHashMap)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 Value (org.eclipse.che.api.core.model.project.type.Value)1 Problem (org.eclipse.che.api.project.server.RegisteredProject.Problem)1 AttributeValue (org.eclipse.che.api.project.server.type.AttributeValue)1 ValueProvider (org.eclipse.che.api.project.server.type.ValueProvider)1 ValueStorageException (org.eclipse.che.api.project.server.type.ValueStorageException)1 Variable (org.eclipse.che.api.project.server.type.Variable)1 AttributeDto (org.eclipse.che.api.project.shared.dto.AttributeDto)1 ProjectTypeDto (org.eclipse.che.api.project.shared.dto.ProjectTypeDto)1 ValueDto (org.eclipse.che.api.project.shared.dto.ValueDto)1