Search in sources :

Example 1 with AbstractSuggestionEntry

use of alien4cloud.model.common.AbstractSuggestionEntry in project alien4cloud by alien4cloud.

the class SuggestionService method checkPropertyConstraints.

private void checkPropertyConstraints(String prefix, Class<? extends AbstractInheritableToscaType> type, String elementId, String propertyName, List<PropertyConstraint> constraints, ParsingContext context) {
    if (constraints != null && !constraints.isEmpty()) {
        for (PropertyConstraint propertyConstraint : constraints) {
            if (propertyConstraint instanceof EqualConstraint) {
                EqualConstraint equalConstraint = (EqualConstraint) propertyConstraint;
                String valueToCheck = equalConstraint.getEqual();
                if (checkProperty(prefix, propertyName, valueToCheck, type, elementId, context) == null) {
                    createSuggestionEntry(ElasticSearchDAO.TOSCA_ELEMENT_INDEX, CapabilityType.class, Sets.newHashSet(valueToCheck), elementId, propertyName);
                }
            } else if (propertyConstraint instanceof ValidValuesConstraint) {
                ValidValuesConstraint validValuesConstraint = (ValidValuesConstraint) propertyConstraint;
                if (validValuesConstraint.getValidValues() != null && !validValuesConstraint.getValidValues().isEmpty()) {
                    AbstractSuggestionEntry foundSuggestion = null;
                    for (String valueToCheck : validValuesConstraint.getValidValues()) {
                        foundSuggestion = checkProperty(prefix, propertyName, valueToCheck, type, elementId, context);
                        if (foundSuggestion == null) {
                            // No suggestion exists don't need to check any more for other values
                            break;
                        }
                    }
                    if (foundSuggestion == null) {
                        createSuggestionEntry(ElasticSearchDAO.TOSCA_ELEMENT_INDEX, CapabilityType.class, Sets.newHashSet(validValuesConstraint.getValidValues()), elementId, propertyName);
                    }
                }
            }
        }
    }
}
Also used : PropertyConstraint(org.alien4cloud.tosca.model.definitions.PropertyConstraint) CapabilityType(org.alien4cloud.tosca.model.types.CapabilityType) ValidValuesConstraint(org.alien4cloud.tosca.model.definitions.constraints.ValidValuesConstraint) AbstractSuggestionEntry(alien4cloud.model.common.AbstractSuggestionEntry) EqualConstraint(org.alien4cloud.tosca.model.definitions.constraints.EqualConstraint)

Example 2 with AbstractSuggestionEntry

use of alien4cloud.model.common.AbstractSuggestionEntry in project alien4cloud by alien4cloud.

the class SuggestionService method addSuggestionValueToSuggestionEntry.

public void addSuggestionValueToSuggestionEntry(String suggestionId, String newValue) {
    AbstractSuggestionEntry suggestion = alienDAO.findById(AbstractSuggestionEntry.class, suggestionId);
    if (suggestion == null) {
        throw new NotFoundException("Suggestion entry [" + suggestionId + "] cannot be found");
    }
    // TODO: should check the format of new value
    if (suggestion.getSuggestions().contains(newValue)) {
        return;
    }
    suggestion.getSuggestions().add(newValue);
    alienDAO.save(suggestion);
}
Also used : AbstractSuggestionEntry(alien4cloud.model.common.AbstractSuggestionEntry) NotFoundException(alien4cloud.exception.NotFoundException)

Example 3 with AbstractSuggestionEntry

use of alien4cloud.model.common.AbstractSuggestionEntry in project alien4cloud by alien4cloud.

the class SuggestionService method checkProperty.

private AbstractSuggestionEntry checkProperty(String nodePrefix, String propertyName, String propertyTextValue, Class<? extends AbstractInheritableToscaType> type, String elementId, ParsingContext context) {
    AbstractSuggestionEntry suggestionEntry = getSuggestionEntry(ElasticSearchDAO.TOSCA_ELEMENT_INDEX, type.getSimpleName().toLowerCase(), elementId, propertyName);
    if (suggestionEntry != null) {
        PriorityQueue<SuggestionService.MatchedSuggestion> similarValues = getJaroWinklerMatchedSuggestions(suggestionEntry.getSuggestions(), propertyTextValue, 0.8);
        if (!similarValues.isEmpty()) {
            // Has some similar values in the system already
            SuggestionService.MatchedSuggestion mostMatched = similarValues.poll();
            if (!mostMatched.getValue().equals(propertyTextValue)) {
                // If user has entered a property value not the same as the most matched in the system
                ParsingErrorLevel level;
                if (mostMatched.getPriority() == 1.0) {
                    // It's really identical if we take out all white spaces and lower / upper case
                    level = ParsingErrorLevel.WARNING;
                } else {
                    // It's pretty similar
                    level = ParsingErrorLevel.INFO;
                    // Add suggestion anyway
                    addSuggestionValueToSuggestionEntry(suggestionEntry.getId(), propertyTextValue);
                }
                context.getParsingErrors().add(new ParsingError(level, ErrorCode.POTENTIAL_BAD_PROPERTY_VALUE, null, null, null, null, "At path [" + nodePrefix + "." + propertyName + "] existing value [" + mostMatched.getValue() + "] is very similar to [" + propertyTextValue + "]"));
            }
        } else {
            // Not similar add suggestion
            addSuggestionValueToSuggestionEntry(suggestionEntry.getId(), propertyTextValue);
        }
    }
    return suggestionEntry;
}
Also used : ParsingErrorLevel(alien4cloud.tosca.parser.ParsingErrorLevel) ParsingError(alien4cloud.tosca.parser.ParsingError) AbstractSuggestionEntry(alien4cloud.model.common.AbstractSuggestionEntry)

Aggregations

AbstractSuggestionEntry (alien4cloud.model.common.AbstractSuggestionEntry)3 NotFoundException (alien4cloud.exception.NotFoundException)1 ParsingError (alien4cloud.tosca.parser.ParsingError)1 ParsingErrorLevel (alien4cloud.tosca.parser.ParsingErrorLevel)1 PropertyConstraint (org.alien4cloud.tosca.model.definitions.PropertyConstraint)1 EqualConstraint (org.alien4cloud.tosca.model.definitions.constraints.EqualConstraint)1 ValidValuesConstraint (org.alien4cloud.tosca.model.definitions.constraints.ValidValuesConstraint)1 CapabilityType (org.alien4cloud.tosca.model.types.CapabilityType)1