Skip to content

Commit

Permalink
feat: fix data import tenant (#18)
Browse files Browse the repository at this point in the history
* fix: theme import

* fix: import tenant case

* fix: code smells
  • Loading branch information
andrejpetras authored Dec 7, 2023
1 parent a09c914 commit 8551193
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import io.github.onecx.theme.domain.models.Theme_;

@ApplicationScoped
@Transactional(Transactional.TxType.NOT_SUPPORTED)
public class ThemeDAO extends AbstractDAO<Theme> {

// https://hibernate.atlassian.net/browse/HHH-16830#icft=HHH-16830
Expand Down
Original file line number Diff line number Diff line change
@@ -1,37 +1,32 @@
package io.github.onecx.theme.domain.di;

import java.util.function.Consumer;
import java.util.stream.Collectors;

import jakarta.inject.Inject;
import jakarta.transaction.Transactional;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.tkit.quarkus.context.ApplicationContext;
import org.tkit.quarkus.context.Context;
import org.tkit.quarkus.dataimport.DataImport;
import org.tkit.quarkus.dataimport.DataImportConfig;
import org.tkit.quarkus.dataimport.DataImportService;

import com.fasterxml.jackson.databind.ObjectMapper;

import gen.io.github.onecx.theme.di.v1.model.DataImportDTOV1;
import io.github.onecx.theme.domain.daos.ThemeDAO;
import io.github.onecx.theme.domain.di.mappers.DataImportMapperV1;
import gen.io.github.onecx.theme.di.v1.model.DataImportThemeDTOV1;

@DataImport("theme")
public class ThemeDataImportServiceV1 implements DataImportService {

private static final Logger log = LoggerFactory.getLogger(ThemeDataImportServiceV1.class);

@Inject
ThemeDAO themeDAO;

@Inject
ObjectMapper objectMapper;

@Inject
DataImportMapperV1 mapper;
ThemeImportService importService;

@Override
@Transactional(Transactional.TxType.REQUIRES_NEW)
Expand Down Expand Up @@ -72,25 +67,11 @@ public void importData(DataImportConfig config) {
public void cleanInsert(DataImportDTOV1 data) {

// clean data
themeDAO.deleteAll();

data.getThemes().forEach((themeName, dto) -> {

try {
var ctx = Context.builder()
.principal("data-import")
.tenantId(dto.getTenantId())
.build();

ApplicationContext.start(ctx);
// import themes
var theme = mapper.importTheme(dto);
theme.setName(themeName);
themeDAO.create(theme);
} finally {
ApplicationContext.close();
}
});
var tenants = data.getThemes().values().stream().map(DataImportThemeDTOV1::getTenantId).collect(Collectors.toSet());
tenants.forEach(x -> importService.deleteAllByTenantId(x));

// import themes
data.getThemes().forEach((name, dto) -> importService.importTheme(name, dto));

}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package io.github.onecx.theme.domain.di;

import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import jakarta.transaction.Transactional;

import org.tkit.quarkus.context.ApplicationContext;
import org.tkit.quarkus.context.Context;

import gen.io.github.onecx.theme.di.v1.model.DataImportThemeDTOV1;
import io.github.onecx.theme.domain.daos.ThemeDAO;
import io.github.onecx.theme.domain.di.mappers.DataImportMapperV1;

@ApplicationScoped
@Transactional(Transactional.TxType.NOT_SUPPORTED)
public class ThemeImportService {

@Inject
ThemeDAO dao;

@Inject
DataImportMapperV1 mapper;

@Transactional(Transactional.TxType.REQUIRES_NEW)
public void importTheme(String name, DataImportThemeDTOV1 dto) {
try {
var ctx = Context.builder()
.principal("data-import")
.tenantId(dto.getTenantId())
.build();

ApplicationContext.start(ctx);
// import themes
var theme = mapper.importTheme(dto);
theme.setName(name);
dao.create(theme);
} finally {
ApplicationContext.close();
}
}

@Transactional(Transactional.TxType.REQUIRES_NEW)
public void deleteAllByTenantId(String tenantId) {
try {
var ctx = Context.builder()
.principal("data-import")
.tenantId(tenantId)
.build();

ApplicationContext.start(ctx);
dao.deleteAll();
} finally {
ApplicationContext.close();
}
}
}
40 changes: 40 additions & 0 deletions src/test/resources/import/theme-import.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,46 @@
"inline-menu-border-color": " #e4e4e4"
}
}
},
"Theme3": {
"tenantId": "default1",
"description": "Theme3 basic color scheme",
"properties": {
"topbar": {
"topbar-bg-color": " rgb(18, 171, 219)",
"topbar-item-text-color": " #ffffff",
"topbar-text-color": " #ffffff",
"topbar-left-bg-color": " #ececec",
"topbar-item-text-hover-bg-color": " rgba(255, 255, 255, 0.12)",
"topbar-menu-button-bg-color": " rgb(255, 0, 68)",
"logo-color": " rgb(18, 171, 219)",
"danger-button-bg": "#D32F2F",
"info-message-bg": "#b3e5fc",
"success-message-bg": "#c8e6c9",
"warning-message-bg": "#ffecb3",
"error-message-bg": "#ffcdd2"
},
"general": {
"text-color": " rgba(0, 0, 0, 0.87)",
"text-secondary-color": " rgba(0, 0, 0, 0.6)",
"primary-color": " rgb(18, 171, 219)",
"secondary-color": " #ee4400",
"body-bg-color": " #f7f7f7",
"content-bg-color": " #ffffff",
"content-alt-bg-color": "",
"overlay-content-bg-color": " #ffffff",
"hover-bg-color": " rgba(0, 0, 0, 0.04)",
"solid-surface-text-color": " #ffffff",
"divider-color": " #e4e4e4"
},
"sidebar": {
"menu-text-color": " #657380",
"menu-bg-color": " #fdfeff",
"menu-item-text-color": " #515c66",
"menu-item-hover-bg-color": " rgba(0, 0, 0, 0.04)",
"inline-menu-border-color": " #e4e4e4"
}
}
}
}
}

0 comments on commit 8551193

Please sign in to comment.