class ContextualPreferenceManager {

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

}