Search in sources :

Example 1 with Variable

use of org.eclipse.che.api.project.server.type.Variable 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)

Aggregations

HashMap (java.util.HashMap)1 Map (java.util.Map)1 Attribute (org.eclipse.che.api.core.model.project.type.Attribute)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