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