Search in sources :

Example 16 with Theme

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

the class ThemeServiceImpl method updateDefaultTheme.

@Override
public void updateDefaultTheme() {
    try {
        final Set<Theme> themes = themeRepository.findByReferenceIdAndReferenceType(GraviteeContext.getCurrentEnvironment(), ThemeReferenceType.ENVIRONMENT.name());
        String defaultDefinition = this.getDefaultDefinition();
        if (themes != null && !themes.isEmpty()) {
            themes.forEach(theme -> {
                if (!MAPPER.isSame(defaultDefinition, theme.getDefinition())) {
                    try {
                        ThemeDefinition mergeDefinition = MAPPER.merge(defaultDefinition, theme.getDefinition());
                        Theme themeUpdate = new Theme(theme);
                        themeUpdate.setDefinition(MAPPER.writeValueAsString(mergeDefinition));
                        theme.setUpdatedAt(new Date());
                        this.themeRepository.update(themeUpdate);
                        auditService.createEnvironmentAuditLog(Collections.singletonMap(THEME, theme.getId()), THEME_UPDATED, new Date(), theme, themeUpdate);
                    } catch (IOException ex) {
                        final String error = "Error while trying to merge default theme from the definition path: " + themesPath + DEFAULT_THEME_PATH + " with theme " + theme.toString();
                        LOGGER.error(error, ex);
                    } catch (TechnicalException ex) {
                        final String error = "Error while trying to update theme after merge with default" + theme.toString();
                        LOGGER.error(error, ex);
                    }
                }
            });
        }
    } catch (TechnicalException ex) {
        LOGGER.error("An error occurs while trying to find all themes", ex);
    }
}
Also used : TechnicalException(io.gravitee.repository.exceptions.TechnicalException) Theme(io.gravitee.repository.management.model.Theme) IOException(java.io.IOException)

Example 17 with Theme

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

the class ThemeServiceImpl method findByIdWithoutConvert.

private Theme findByIdWithoutConvert(String themeId) {
    try {
        LOGGER.debug("Find theme by ID: {}", themeId);
        Optional<Theme> optTheme = themeRepository.findById(themeId);
        if (!optTheme.isPresent()) {
            throw new ThemeNotFoundException(themeId);
        }
        Theme theme = optTheme.get();
        if (!theme.getReferenceId().equals(GraviteeContext.getCurrentEnvironment())) {
            LOGGER.warn("Theme is not in current environment " + GraviteeContext.getCurrentEnvironment() + " actual:" + theme.getReferenceId());
            throw new ThemeNotFoundException(themeId);
        }
        return theme;
    } catch (TechnicalException ex) {
        LOGGER.error("An error occurs while trying to find theme by ID", ex);
        throw new TechnicalManagementException("An error occurs while trying to find theme by ID", ex);
    }
}
Also used : TechnicalException(io.gravitee.repository.exceptions.TechnicalException) ThemeNotFoundException(io.gravitee.rest.api.service.exceptions.ThemeNotFoundException) Theme(io.gravitee.repository.management.model.Theme) TechnicalManagementException(io.gravitee.rest.api.service.exceptions.TechnicalManagementException)

Example 18 with Theme

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

the class ThemeServiceImpl method convert.

private Theme convert(NewThemeEntity themeEntity) {
    try {
        final Date now = new Date();
        final Theme theme = new Theme();
        theme.setId(DEFAULT_THEME_ID);
        theme.setCreatedAt(now);
        theme.setUpdatedAt(now);
        theme.setReferenceId(GraviteeContext.getCurrentEnvironment());
        theme.setReferenceType(ThemeReferenceType.ENVIRONMENT.name());
        theme.setLogo(themeEntity.getLogo());
        theme.setName(themeEntity.getName());
        theme.setDefinition(MAPPER.writeValueAsString(themeEntity.getDefinition()));
        theme.setEnabled(themeEntity.isEnabled());
        theme.setBackgroundImage(themeEntity.getBackgroundImage());
        theme.setOptionalLogo(themeEntity.getOptionalLogo());
        theme.setFavicon(themeEntity.getFavicon());
        return theme;
    } catch (JsonProcessingException e) {
        throw new TechnicalManagementException("Cannot convert new theme entity", e);
    }
}
Also used : Theme(io.gravitee.repository.management.model.Theme) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) TechnicalManagementException(io.gravitee.rest.api.service.exceptions.TechnicalManagementException)

Example 19 with Theme

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

the class ThemeServiceImpl method delete.

@Override
public void delete(String themeId) {
    try {
        Optional<Theme> themeOptional = themeRepository.findById(themeId);
        if (themeOptional.isPresent()) {
            themeRepository.delete(themeId);
            auditService.createEnvironmentAuditLog(Collections.singletonMap(THEME, themeId), THEME_DELETED, new Date(), null, themeOptional.get());
        }
    } catch (TechnicalException ex) {
        final String error = "An error occurs while trying to delete theme " + themeId;
        LOGGER.error(error, ex);
        throw new TechnicalManagementException(error, ex);
    }
}
Also used : TechnicalException(io.gravitee.repository.exceptions.TechnicalException) Theme(io.gravitee.repository.management.model.Theme) TechnicalManagementException(io.gravitee.rest.api.service.exceptions.TechnicalManagementException)

Example 20 with Theme

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

the class ThemeServiceTest method shouldThrowThemeNotFoundExceptionWhenThemeIsNotInDefaultEnv.

@Test(expected = ThemeNotFoundException.class)
public void shouldThrowThemeNotFoundExceptionWhenThemeIsNotInDefaultEnv() throws TechnicalException, JsonProcessingException {
    ThemeDefinitionMapper definitionMapper = new ThemeDefinitionMapper();
    ThemeDefinition themeDefinition = new ThemeDefinition();
    themeDefinition.setData(Collections.EMPTY_LIST);
    String definition = definitionMapper.writeValueAsString(themeDefinition);
    final Theme theme = mock(Theme.class);
    when(theme.getReferenceId()).thenReturn("NOT-DEFAULT");
    when(themeRepository.findById(THEME_ID)).thenReturn(of(theme));
    final ThemeEntity themeEntity = themeService.findById(THEME_ID);
    assertEquals(THEME_ID, themeEntity.getId());
    assertEquals("NAME", themeEntity.getName());
    assertEquals(definition, definitionMapper.writeValueAsString(themeEntity.getDefinition()));
    assertEquals(new Date(1), themeEntity.getCreatedAt());
    assertEquals(new Date(2), themeEntity.getUpdatedAt());
}
Also used : Theme(io.gravitee.repository.management.model.Theme) ThemeDefinitionMapper(io.gravitee.rest.api.service.impl.ThemeServiceImpl.ThemeDefinitionMapper) Date(java.util.Date) 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