Search in sources :

Example 1 with DuplicateThemeNameException

use of io.gravitee.rest.api.service.exceptions.DuplicateThemeNameException in project gravitee-management-rest-api by gravitee-io.

the class ThemeServiceImpl method update.

@Override
public ThemeEntity update(final UpdateThemeEntity updateThemeEntity) {
    try {
        final Optional<Theme> themeOptional = themeRepository.findById(updateThemeEntity.getId());
        if (themeOptional.isPresent()) {
            final Theme theme = new Theme(themeOptional.get());
            if (this.findByName(theme.getName(), theme.getId()).isPresent()) {
                throw new DuplicateThemeNameException(theme.getName());
            }
            theme.setEnabled(updateThemeEntity.isEnabled());
            final Date now = new Date();
            theme.setUpdatedAt(now);
            theme.setReferenceType(ThemeReferenceType.ENVIRONMENT.name());
            theme.setReferenceId(GraviteeContext.getCurrentEnvironment());
            if (updateThemeEntity.getName() != null) {
                theme.setName(updateThemeEntity.getName());
            }
            theme.setDefinition(MAPPER.writeValueAsString(updateThemeEntity.getDefinition()));
            if (updateThemeEntity.getLogo() != null) {
                theme.setLogo(updateThemeEntity.getLogo());
            } else {
                theme.setLogo(this.getDefaultLogo());
            }
            theme.setBackgroundImage(updateThemeEntity.getBackgroundImage());
            if (updateThemeEntity.getOptionalLogo() != null) {
                theme.setOptionalLogo(updateThemeEntity.getOptionalLogo());
            } else {
                theme.setOptionalLogo(this.getDefaultOptionalLogo());
            }
            if (updateThemeEntity.getFavicon() != null) {
                theme.setFavicon(updateThemeEntity.getFavicon());
            } else {
                theme.setFavicon(this.getDefaultFavicon());
            }
            final ThemeEntity savedTheme = convert(themeRepository.update(theme));
            auditService.createEnvironmentAuditLog(Collections.singletonMap(THEME, theme.getId()), THEME_UPDATED, new Date(), themeOptional.get(), theme);
            return savedTheme;
        } else {
            final NewThemeEntity newTheme = new NewThemeEntity();
            newTheme.setName(updateThemeEntity.getName());
            newTheme.setDefinition(updateThemeEntity.getDefinition());
            newTheme.setBackgroundImage(updateThemeEntity.getBackgroundImage());
            newTheme.setLogo(updateThemeEntity.getLogo());
            newTheme.setOptionalLogo(updateThemeEntity.getOptionalLogo());
            newTheme.setFavicon(updateThemeEntity.getFavicon());
            newTheme.setEnabled(updateThemeEntity.isEnabled());
            return create(newTheme);
        }
    } catch (TechnicalException | JsonProcessingException ex) {
        final String error = "An error occurred while trying to update theme " + updateThemeEntity;
        LOGGER.error(error, ex);
        throw new TechnicalManagementException(error, ex);
    }
}
Also used : TechnicalException(io.gravitee.repository.exceptions.TechnicalException) Theme(io.gravitee.repository.management.model.Theme) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) DuplicateThemeNameException(io.gravitee.rest.api.service.exceptions.DuplicateThemeNameException) TechnicalManagementException(io.gravitee.rest.api.service.exceptions.TechnicalManagementException)

Example 2 with DuplicateThemeNameException

use of io.gravitee.rest.api.service.exceptions.DuplicateThemeNameException in project gravitee-management-rest-api by gravitee-io.

the class ThemeServiceImpl method create.

@Override
public ThemeEntity create(final NewThemeEntity themeEntity) {
    // First we prevent the duplicate name
    try {
        if (this.findByName(themeEntity.getName(), null).isPresent()) {
            throw new DuplicateThemeNameException(themeEntity.getName());
        }
        Theme theme = themeRepository.create(convert(themeEntity));
        auditService.createEnvironmentAuditLog(Collections.singletonMap(THEME, theme.getId()), THEME_CREATED, theme.getCreatedAt(), null, theme);
        return convert(theme);
    } catch (TechnicalException ex) {
        final String error = "An error occurred while trying to create theme " + themeEntity;
        LOGGER.error(error, ex);
        throw new TechnicalManagementException(error, ex);
    }
}
Also used : TechnicalException(io.gravitee.repository.exceptions.TechnicalException) Theme(io.gravitee.repository.management.model.Theme) DuplicateThemeNameException(io.gravitee.rest.api.service.exceptions.DuplicateThemeNameException) TechnicalManagementException(io.gravitee.rest.api.service.exceptions.TechnicalManagementException)

Aggregations

TechnicalException (io.gravitee.repository.exceptions.TechnicalException)2 Theme (io.gravitee.repository.management.model.Theme)2 DuplicateThemeNameException (io.gravitee.rest.api.service.exceptions.DuplicateThemeNameException)2 TechnicalManagementException (io.gravitee.rest.api.service.exceptions.TechnicalManagementException)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1