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()));
}
}
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);
}
Aggregations