Search in sources :

Example 6 with Attribute

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

the class ProjectTypeDef method resolveSources.

public ProjectTypeResolution resolveSources(FolderEntry projectFolder) {
    Map<String, Value> matchAttrs = new HashMap<>();
    for (Map.Entry<String, Attribute> entry : attributes.entrySet()) {
        Attribute attr = entry.getValue();
        String name = entry.getKey();
        if (attr.isVariable()) {
            Variable var = (Variable) attr;
            ValueProviderFactory factory = var.getValueProviderFactory();
            if (factory != null) {
                Value value;
                String errorMessage = "";
                try {
                    value = new AttributeValue(factory.newInstance(projectFolder).getValues(name));
                } catch (ValueStorageException e) {
                    value = null;
                    errorMessage = e.getLocalizedMessage();
                }
                if (value == null || value.isEmpty()) {
                    if (var.isRequired()) {
                        // this PT is not match
                        errorMessage = errorMessage.isEmpty() ? format("Value for required attribute %s is not initialized", name) : errorMessage;
                        return new DefaultResolution(id, errorMessage);
                    }
                } else {
                    // add one more matched attribute
                    matchAttrs.put(name, value);
                }
            }
        }
    }
    return new DefaultResolution(id, matchAttrs, true);
}
Also used : Maps.newHashMap(com.google.common.collect.Maps.newHashMap) HashMap(java.util.HashMap) Attribute(org.eclipse.che.api.core.model.project.type.Attribute) Value(org.eclipse.che.api.core.model.project.type.Value) Maps.newHashMap(com.google.common.collect.Maps.newHashMap) Map(java.util.Map) HashMap(java.util.HashMap)

Example 7 with Attribute

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

the class ProjectTypeRegistry method initRecursively.

/**
     * initializes all the attributes defined in myType and its ancestors recursively
     *
     * @param myType
     * @param typeId
     *         temporary type for recursive (started with initial type)
     * @throws ProjectTypeConstraintException
     */
private final void initRecursively(ProjectTypeDef myType, String typeId) throws ProjectTypeConstraintException {
    ProjectTypeDef type = validatedData.get(typeId);
    for (String superTypeId : type.getParents()) {
        myType.addAncestor(superTypeId);
        ProjectTypeDef supertype = validatedData.get(superTypeId);
        for (Attribute attr : supertype.getAttributes()) {
            // check attribute names
            for (Attribute attr2 : myType.getAttributes()) {
                if (attr.getName().equals(attr2.getName()) && !attr.getProjectType().equals(attr2.getProjectType())) {
                    throw new ProjectTypeConstraintException("Attribute name conflict. Project type " + myType.getId() + " could not be registered as attribute declaration " + attr.getName() + " is duplicated in its ancestor(s).");
                }
            }
            myType.addAttributeDefinition(attr);
        }
        initRecursively(myType, superTypeId);
    }
}
Also used : Attribute(org.eclipse.che.api.core.model.project.type.Attribute)

Example 8 with Attribute

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

the class ProjectTypes method reset.

/**
     * Reset project types and atrributes after initialization
     * in case when some attributes are not valid
     * (for instance required attributes are not initialized)
     *
     * @param attributesToDel - invalid attributes
     */
void reset(Set<Attribute> attributesToDel) {
    Set<String> ptsToDel = new HashSet<>();
    for (Attribute attr : attributesToDel) {
        ptsToDel.add(attr.getProjectType());
    }
    Set<String> attrNamesToDel = new HashSet<>();
    for (String pt : ptsToDel) {
        ProjectTypeDef typeDef = all.get(pt);
        for (Attribute attrDef : typeDef.getAttributes()) {
            attrNamesToDel.add(attrDef.getName());
        }
    }
    // remove project types
    for (String typeId : ptsToDel) {
        this.all.remove(typeId);
        if (this.primary.getId().equals(typeId)) {
            this.primary = ProjectTypeRegistry.BASE_TYPE;
            this.all.put(ProjectTypeRegistry.BASE_TYPE.getId(), ProjectTypeRegistry.BASE_TYPE);
        } else {
            mixins.remove(typeId);
        }
    }
    // remove attributes
    for (String attr : attrNamesToDel) {
        this.attributeDefs.remove(attr);
    }
}
Also used : Attribute(org.eclipse.che.api.core.model.project.type.Attribute) ProjectTypeDef(org.eclipse.che.api.project.server.type.ProjectTypeDef) HashSet(java.util.HashSet)

Example 9 with Attribute

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

the class ProjectTypes method addTransient.

void addTransient(FolderEntry projectFolder) {
    for (ProjectTypeDef pt : projectTypeRegistry.getProjectTypes()) {
        // NOTE: Only mixable types allowed
        if (pt.isMixable() && !pt.isPersisted() && pt.resolveSources(projectFolder).matched()) {
            all.put(pt.getId(), pt);
            mixins.put(pt.getId(), pt);
            for (Attribute attr : pt.getAttributes()) {
                final String attrName = attr.getName();
                if (attributeDefs.containsKey(attrName)) {
                    problems.add(new Problem(ATTRIBUTE_NAME_PROBLEM, format("Attribute name conflict. Duplicated attributes detected for %s. " + "Attribute %s declared in %s already declared in %s. Skipped.", projectPath, attrName, pt.getId(), attributeDefs.get(attrName).getProjectType())));
                }
                attributeDefs.put(attrName, attr);
            }
        }
    }
}
Also used : Attribute(org.eclipse.che.api.core.model.project.type.Attribute) ProjectTypeDef(org.eclipse.che.api.project.server.type.ProjectTypeDef) Problem(org.eclipse.che.api.project.server.RegisteredProject.Problem)

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