Search in sources :

Example 1 with Theme

use of io.gravitee.repository.management.model.Theme 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 Theme

use of io.gravitee.repository.management.model.Theme 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)

Example 3 with Theme

use of io.gravitee.repository.management.model.Theme in project gravitee-management-rest-api by gravitee-io.

the class ThemeServiceImpl method findEnabled.

@Override
public ThemeEntity findEnabled() {
    try {
        LOGGER.debug("Find all themes by reference type");
        Optional<Theme> themeEnabled = themeRepository.findByReferenceIdAndReferenceType(GraviteeContext.getCurrentEnvironment(), ThemeReferenceType.ENVIRONMENT.name()).stream().filter(theme -> theme.isEnabled()).findFirst();
        if (themeEnabled.isPresent()) {
            return convert(themeEnabled.get());
        }
        final ThemeEntity theme = new ThemeEntity();
        theme.setId(DEFAULT_THEME_ID);
        theme.setName("Default theme");
        theme.setDefinition(MAPPER.readDefinition(getDefaultDefinition()));
        theme.setLogo(this.getDefaultLogo());
        theme.setOptionalLogo(this.getDefaultOptionalLogo());
        theme.setFavicon(this.getDefaultFavicon());
        return theme;
    } catch (IOException ex) {
        final String error = "Error while trying to get the default theme";
        LOGGER.error(error, ex);
        throw new TechnicalManagementException(error, ex);
    } catch (TechnicalException ex) {
        final String error = "An error occurs while trying to find all themes by reference type";
        LOGGER.error(error, ex);
        throw new TechnicalManagementException(error, ex);
    }
}
Also used : Charset.defaultCharset(java.nio.charset.Charset.defaultCharset) java.util(java.util) InlinePictureEntity(io.gravitee.rest.api.model.InlinePictureEntity) Theme(io.gravitee.repository.management.model.Theme) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) GraviteeContext(io.gravitee.rest.api.service.common.GraviteeContext) MimetypesFileTypeMap(javax.activation.MimetypesFileTypeMap) Value(org.springframework.beans.factory.annotation.Value) TechnicalManagementException(io.gravitee.rest.api.service.exceptions.TechnicalManagementException) DuplicateThemeNameException(io.gravitee.rest.api.service.exceptions.DuplicateThemeNameException) JsonNode(com.fasterxml.jackson.databind.JsonNode) ThemeReferenceType(io.gravitee.repository.management.model.ThemeReferenceType) ThemeNotFoundException(io.gravitee.rest.api.service.exceptions.ThemeNotFoundException) THEME(io.gravitee.repository.management.model.Audit.AuditProperties.THEME) AuditEvent(io.gravitee.repository.management.model.Theme.AuditEvent) Logger(org.slf4j.Logger) PictureEntity(io.gravitee.rest.api.model.PictureEntity) Files(java.nio.file.Files) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) AuditService(io.gravitee.rest.api.service.AuditService) IOException(java.io.IOException) ThemeRepository(io.gravitee.repository.management.api.ThemeRepository) Collectors(java.util.stream.Collectors) File(java.io.File) UrlPictureEntity(io.gravitee.rest.api.model.UrlPictureEntity) ThemeService(io.gravitee.rest.api.service.ThemeService) Component(org.springframework.stereotype.Component) io.gravitee.rest.api.model.theme(io.gravitee.rest.api.model.theme) DatatypeConverter(javax.xml.bind.DatatypeConverter) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) Theme(io.gravitee.repository.management.model.Theme) IOException(java.io.IOException) TechnicalManagementException(io.gravitee.rest.api.service.exceptions.TechnicalManagementException)

Example 4 with Theme

use of io.gravitee.repository.management.model.Theme in project gravitee-management-rest-api by gravitee-io.

the class ThemeServiceTest method shouldGetBackgroundImageUrl.

@Test
public void shouldGetBackgroundImageUrl() throws TechnicalException {
    final Theme theme = mock(Theme.class);
    when(theme.getBackgroundImage()).thenReturn("http://localhost/image");
    when(theme.getId()).thenReturn(THEME_ID);
    when(theme.getName()).thenReturn("NAME");
    when(theme.isEnabled()).thenReturn(true);
    when(theme.getDefinition()).thenReturn(themeServiceImpl.getDefaultDefinition());
    when(themeRepository.findByReferenceIdAndReferenceType(GraviteeContext.getCurrentEnvironment(), ENVIRONMENT.name())).thenReturn(singleton(theme));
    PictureEntity backgroundImage = themeService.getBackgroundImage(THEME_ID);
    assertNotNull(backgroundImage);
    assertTrue(backgroundImage instanceof UrlPictureEntity);
}
Also used : UrlPictureEntity(io.gravitee.rest.api.model.UrlPictureEntity) Theme(io.gravitee.repository.management.model.Theme) InlinePictureEntity(io.gravitee.rest.api.model.InlinePictureEntity) PictureEntity(io.gravitee.rest.api.model.PictureEntity) UrlPictureEntity(io.gravitee.rest.api.model.UrlPictureEntity) Test(org.junit.Test)

Example 5 with Theme

use of io.gravitee.repository.management.model.Theme in project gravitee-management-rest-api by gravitee-io.

the class ThemeServiceTest method shouldGetBackgroundImage.

@Test
public void shouldGetBackgroundImage() throws TechnicalException {
    final Theme theme = mock(Theme.class);
    Mockito.lenient().when(theme.getReferenceType()).thenReturn(ENVIRONMENT.name());
    PictureEntity backgroundImage = themeService.getBackgroundImage(THEME_ID);
    assertNull(backgroundImage);
}
Also used : Theme(io.gravitee.repository.management.model.Theme) InlinePictureEntity(io.gravitee.rest.api.model.InlinePictureEntity) PictureEntity(io.gravitee.rest.api.model.PictureEntity) UrlPictureEntity(io.gravitee.rest.api.model.UrlPictureEntity) Test(org.junit.Test)

Aggregations

Theme (io.gravitee.repository.management.model.Theme)24 Test (org.junit.Test)17 TechnicalException (io.gravitee.repository.exceptions.TechnicalException)10 Date (java.util.Date)10 InlinePictureEntity (io.gravitee.rest.api.model.InlinePictureEntity)8 PictureEntity (io.gravitee.rest.api.model.PictureEntity)8 UrlPictureEntity (io.gravitee.rest.api.model.UrlPictureEntity)8 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)7 DuplicateThemeNameException (io.gravitee.rest.api.service.exceptions.DuplicateThemeNameException)7 ThemeDefinitionMapper (io.gravitee.rest.api.service.impl.ThemeServiceImpl.ThemeDefinitionMapper)7 TechnicalManagementException (io.gravitee.rest.api.service.exceptions.TechnicalManagementException)6 ThemeNotFoundException (io.gravitee.rest.api.service.exceptions.ThemeNotFoundException)6 IOException (java.io.IOException)6 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)5 ThemeRepository (io.gravitee.repository.management.api.ThemeRepository)5 THEME (io.gravitee.repository.management.model.Audit.AuditProperties.THEME)5 io.gravitee.rest.api.model.theme (io.gravitee.rest.api.model.theme)5 GraviteeContext (io.gravitee.rest.api.service.common.GraviteeContext)5 HashSet (java.util.HashSet)5 ImmutableMap (com.google.common.collect.ImmutableMap)4