Search in sources :

Example 71 with ContextualPreference

use of com.epam.pipeline.entity.contextual.ContextualPreference in project cloud-pipeline by epam.

the class ContextualPreferenceManager method upsert.

/**
 * Inserts or updates the given preference.
 *
 * @throws IllegalArgumentException if preference can't be inserted or updated.
 */
@Transactional
public ContextualPreference upsert(final ContextualPreferenceVO preferenceVO) {
    validatePreferenceFields(preferenceVO);
    validatePreferenceTypeAccordingToPreferencesWithTheSameName(preferenceVO);
    Assert.isTrue(preferenceVO.getResource().getLevel() != ContextualPreferenceLevel.SYSTEM, messageHelper.getMessage(MessageConstants.ERROR_SAVE_CONTEXTUAL_PREFERENCE_EXTERNAL_RESOURCE_LEVEL_INVALID));
    final ContextualPreference preference = preferenceFromVO(preferenceVO);
    if (contextualPreferenceHandler.isValid(preference)) {
        return contextualPreferenceDao.upsert(preference);
    } else {
        throw new IllegalArgumentException(messageHelper.getMessage(MessageConstants.ERROR_CONTEXTUAL_PREFERENCE_EXTERNAL_RESOURCE_NOT_FOUND, preference.getResource()));
    }
}
Also used : ContextualPreference(com.epam.pipeline.entity.contextual.ContextualPreference) Transactional(org.springframework.transaction.annotation.Transactional)

Example 72 with ContextualPreference

use of com.epam.pipeline.entity.contextual.ContextualPreference in project cloud-pipeline by epam.

the class DefaultContextualPreferenceReducer method reduce.

@Override
public Optional<ContextualPreference> reduce(final List<ContextualPreference> preferences) {
    if (preferences.isEmpty()) {
        return Optional.empty();
    }
    final ContextualPreference validPreference = preferences.get(0);
    if (preferences.size() == 1) {
        return Optional.of(validPreference);
    }
    final List<ContextualPreference> invalidPreferences = preferences.stream().filter(preference -> !preference.getName().equals(validPreference.getName()) || preference.getResource().getLevel() != validPreference.getResource().getLevel() || preference.getType() != validPreference.getType()).collect(Collectors.toList());
    return invalidPreferences.isEmpty() ? reduceValidPreferences(preferences) : reduceInvalidPreferences(validPreference, invalidPreferences);
}
Also used : ContextualPreference(com.epam.pipeline.entity.contextual.ContextualPreference) List(java.util.List) Slf4j(lombok.extern.slf4j.Slf4j) MessageHelper(com.epam.pipeline.common.MessageHelper) MessageConstants(com.epam.pipeline.common.MessageConstants) Map(java.util.Map) RequiredArgsConstructor(lombok.RequiredArgsConstructor) Optional(java.util.Optional) Collectors(java.util.stream.Collectors) ContextualPreference(com.epam.pipeline.entity.contextual.ContextualPreference)

Aggregations

ContextualPreference (com.epam.pipeline.entity.contextual.ContextualPreference)72 Test (org.junit.Test)67 ContextualPreferenceExternalResource (com.epam.pipeline.entity.contextual.ContextualPreferenceExternalResource)35 AbstractSpringTest (com.epam.pipeline.AbstractSpringTest)10 ContextualPreferenceVO (com.epam.pipeline.controller.vo.ContextualPreferenceVO)4 AllowedInstanceAndPriceTypes (com.epam.pipeline.entity.cluster.AllowedInstanceAndPriceTypes)2 InstanceType (com.epam.pipeline.entity.cluster.InstanceType)2 Transactional (org.springframework.transaction.annotation.Transactional)2 MessageConstants (com.epam.pipeline.common.MessageConstants)1 MessageHelper (com.epam.pipeline.common.MessageHelper)1 Tool (com.epam.pipeline.entity.pipeline.Tool)1 PipelineUser (com.epam.pipeline.entity.user.PipelineUser)1 Role (com.epam.pipeline.entity.user.Role)1 List (java.util.List)1 Map (java.util.Map)1 Optional (java.util.Optional)1 Collectors (java.util.stream.Collectors)1 RequiredArgsConstructor (lombok.RequiredArgsConstructor)1 Slf4j (lombok.extern.slf4j.Slf4j)1