Skip to content

Commit

Permalink
feat: add template import (#53)
Browse files Browse the repository at this point in the history
* feat: add template import

* fix: sonar issues
  • Loading branch information
andrejpetras authored May 13, 2024
1 parent c4cc82a commit 6b4f1ba
Show file tree
Hide file tree
Showing 20 changed files with 521 additions and 303 deletions.
10 changes: 6 additions & 4 deletions docs/modules/onecx-theme-svc/pages/onecx-theme-svc-docs.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,18 @@ include::onecx-theme-svc-attributes.adoc[opts=optional]
quarkus.datasource.db-kind=postgresql
quarkus.datasource.jdbc.max-size=30
quarkus.datasource.jdbc.min-size=10
quarkus.native.resources.includes=import/template.json
quarkus.hibernate-orm.database.generation=validate
quarkus.hibernate-orm.multitenant=DISCRIMINATOR
quarkus.hibernate-orm.jdbc.timezone=UTC
quarkus.liquibase.migrate-at-start=true
quarkus.liquibase.validate-on-migrate=true
tkit.dataimport.enabled=false
tkit.dataimport.configurations.theme.file=dev-data.import.json
tkit.dataimport.configurations.theme.metadata.operation=CLEAN_INSERT
tkit.dataimport.configurations.theme.enabled=false
tkit.dataimport.configurations.theme.stop-at-error=true
tkit.dataimport.configurations.template.file=import/template.json
tkit.dataimport.configurations.template.metadata.tenants=default
tkit.dataimport.configurations.template.class-path=true
tkit.dataimport.configurations.template.enabled=false
tkit.dataimport.configurations.template.stop-at-error=true
tkit.rs.context.tenant-id.enabled=true
%prod.quarkus.datasource.jdbc.url=${DB_URL:jdbc:postgresql://postgresdb:5432/onecx-theme?sslmode=disable}
%prod.quarkus.datasource.username=${DB_USER:onecx-theme}
Expand Down
64 changes: 32 additions & 32 deletions docs/modules/onecx-theme-svc/pages/onecx-theme-svc-extensions.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -12,140 +12,140 @@ h| Version

| https://onecx.github.io/docs/onecx-quarkus/current/onecx-quarkus/onecx-core.html[Link]
|
| 0.17.0
| 0.18.0

| onecx-tenant

| https://onecx.github.io/docs/onecx-quarkus/current/onecx-quarkus/onecx-tenant.html[Link]
| https://github.com/onecx/onecx-quarkus/blob/0.17.0/docs/modules/onecx-quarkus/pages/includes/onecx-tenant.adoc[Link]
| 0.17.0
| https://github.com/onecx/onecx-quarkus/blob/0.18.0/docs/modules/onecx-quarkus/pages/includes/onecx-tenant.adoc[Link]
| 0.18.0

| tkit-quarkus-rest-context

| https://1000kit.github.io/tkit-quarkus/current/tkit-quarkus/tkit-quarkus-rest-context.html[Link]
| https://github.com/1000kit/tkit-quarkus/blob/2.22.0/docs/modules/tkit-quarkus/pages/includes/tkit-quarkus-rest-context.adoc[Link]
| 2.22.0
| https://github.com/1000kit/tkit-quarkus/blob/2.23.0/docs/modules/tkit-quarkus/pages/includes/tkit-quarkus-rest-context.adoc[Link]
| 2.23.0

| tkit-quarkus-jpa-tenant

| https://1000kit.github.io/tkit-quarkus/current/tkit-quarkus/tkit-quarkus-jpa-tenant.html[Link]
| https://github.com/1000kit/tkit-quarkus/blob/2.22.0/docs/modules/tkit-quarkus/pages/includes/tkit-quarkus-jpa-tenant.adoc[Link]
| 2.22.0
| https://github.com/1000kit/tkit-quarkus/blob/2.23.0/docs/modules/tkit-quarkus/pages/includes/tkit-quarkus-jpa-tenant.adoc[Link]
| 2.23.0

| tkit-quarkus-data-import

| https://1000kit.github.io/tkit-quarkus/current/tkit-quarkus/tkit-quarkus-data-import.html[Link]
| https://github.com/1000kit/tkit-quarkus/blob/2.22.0/docs/modules/tkit-quarkus/pages/includes/tkit-quarkus-data-import.adoc[Link]
| 2.22.0
| https://github.com/1000kit/tkit-quarkus/blob/2.23.0/docs/modules/tkit-quarkus/pages/includes/tkit-quarkus-data-import.adoc[Link]
| 2.23.0

| tkit-quarkus-jpa

| https://1000kit.github.io/tkit-quarkus/current/tkit-quarkus/tkit-quarkus-jpa.html[Link]
| https://github.com/1000kit/tkit-quarkus/blob/2.22.0/docs/modules/tkit-quarkus/pages/includes/tkit-quarkus-jpa.adoc[Link]
| 2.22.0
| https://github.com/1000kit/tkit-quarkus/blob/2.23.0/docs/modules/tkit-quarkus/pages/includes/tkit-quarkus-jpa.adoc[Link]
| 2.23.0

| tkit-quarkus-log-cdi

| https://1000kit.github.io/tkit-quarkus/current/tkit-quarkus/tkit-quarkus-log-cdi.html[Link]
| https://github.com/1000kit/tkit-quarkus/blob/2.22.0/docs/modules/tkit-quarkus/pages/includes/tkit-quarkus-log-cdi.adoc[Link]
| 2.22.0
| https://github.com/1000kit/tkit-quarkus/blob/2.23.0/docs/modules/tkit-quarkus/pages/includes/tkit-quarkus-log-cdi.adoc[Link]
| 2.23.0

| tkit-quarkus-log-rs

| https://1000kit.github.io/tkit-quarkus/current/tkit-quarkus/tkit-quarkus-log-rs.html[Link]
| https://github.com/1000kit/tkit-quarkus/blob/2.22.0/docs/modules/tkit-quarkus/pages/includes/tkit-quarkus-log-rs.adoc[Link]
| 2.22.0
| https://github.com/1000kit/tkit-quarkus/blob/2.23.0/docs/modules/tkit-quarkus/pages/includes/tkit-quarkus-log-rs.adoc[Link]
| 2.23.0

| tkit-quarkus-log-json

| https://1000kit.github.io/tkit-quarkus/current/tkit-quarkus/tkit-quarkus-log-json.html[Link]
| https://github.com/1000kit/tkit-quarkus/blob/2.22.0/docs/modules/tkit-quarkus/pages/includes/tkit-quarkus-log-json.adoc[Link]
| 2.22.0
| https://github.com/1000kit/tkit-quarkus/blob/2.23.0/docs/modules/tkit-quarkus/pages/includes/tkit-quarkus-log-json.adoc[Link]
| 2.23.0

| tkit-quarkus-rest

| https://1000kit.github.io/tkit-quarkus/current/tkit-quarkus/tkit-quarkus-rest.html[Link]
| https://github.com/1000kit/tkit-quarkus/blob/2.22.0/docs/modules/tkit-quarkus/pages/includes/tkit-quarkus-rest.adoc[Link]
| 2.22.0
| https://github.com/1000kit/tkit-quarkus/blob/2.23.0/docs/modules/tkit-quarkus/pages/includes/tkit-quarkus-rest.adoc[Link]
| 2.23.0

| quarkus-arc

| https://quarkus.io/guides/cdi-reference[Link]
| https://github.com/quarkusio/quarkusio.github.io/blob/develop/_generated-doc/latest/config/quarkus-arc.adoc[Link]
| 3.9.3
| 3.9.4
| quarkus-liquibase
| https://quarkus.io/guides/liquibase[Link]
| https://github.com/quarkusio/quarkusio.github.io/blob/develop/_generated-doc/latest/config/quarkus-liquibase.adoc[Link]
| 3.9.3
| 3.9.4
| quarkus-smallrye-health
| https://quarkus.io/guides/smallrye-health[Link]
| https://github.com/quarkusio/quarkusio.github.io/blob/develop/_generated-doc/latest/config/quarkus-smallrye-health.adoc[Link]
| 3.9.3
| 3.9.4
| quarkus-micrometer-registry-prometheus
| https://quarkus.io/guides/telemetry-micrometer[Link]
| https://github.com/quarkusio/quarkusio.github.io/blob/develop/_generated-doc/latest/config/quarkus-micrometer-registry-prometheus.adoc[Link]
| 3.9.3
| 3.9.4
| quarkus-hibernate-orm
| https://quarkus.io/guides/hibernate-orm[Link]
| https://github.com/quarkusio/quarkusio.github.io/blob/develop/_generated-doc/latest/config/quarkus-hibernate-orm.adoc[Link]
| 3.9.3
| 3.9.4
| quarkus-rest
| https://quarkus.io/guides/rest[Link]
| https://github.com/quarkusio/quarkusio.github.io/blob/develop/_generated-doc/latest/config/quarkus-rest.adoc[Link]
| 3.9.3
| 3.9.4
| quarkus-rest-jackson
| https://quarkus.io/guides/rest-json[Link]
|
| 3.9.3
| 3.9.4
| quarkus-jdbc-postgresql
| https://quarkus.io/guides/datasource[Link]
| https://github.com/quarkusio/quarkusio.github.io/blob/develop/_generated-doc/latest/config/quarkus-jdbc-postgresql.adoc[Link]
| 3.9.3
| 3.9.4
| quarkus-smallrye-openapi
| https://quarkus.io/guides/openapi-swaggerui[Link]
| https://github.com/quarkusio/quarkusio.github.io/blob/develop/_generated-doc/latest/config/quarkus-smallrye-openapi.adoc[Link]
| 3.9.3
| 3.9.4
| quarkus-hibernate-validator
| https://quarkus.io/guides/validation[Link]
| https://github.com/quarkusio/quarkusio.github.io/blob/develop/_generated-doc/latest/config/quarkus-hibernate-validator.adoc[Link]
| 3.9.3
| 3.9.4
| quarkus-opentelemetry
| https://quarkus.io/guides/opentelemetry[Link]
| https://github.com/quarkusio/quarkusio.github.io/blob/develop/_generated-doc/latest/config/quarkus-opentelemetry.adoc[Link]
| 3.9.3
| 3.9.4
| quarkus-container-image-docker
| https://quarkus.io/guides/container-image[Link]
| https://github.com/quarkusio/quarkusio.github.io/blob/develop/_generated-doc/latest/config/quarkus-container-image-docker.adoc[Link]
| 3.9.3
| 3.9.4
| quarkus-smallrye-context-propagation
|
|
| 3.9.3
| 3.9.4
|===
12 changes: 6 additions & 6 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>org.tkit.onecx</groupId>
<artifactId>onecx-quarkus3-parent</artifactId>
<version>0.48.0</version>
<version>0.49.0</version>
</parent>

<artifactId>onecx-theme-svc</artifactId>
Expand Down Expand Up @@ -187,15 +187,15 @@
</configuration>
</execution>
<execution>
<id>di-theme-v1</id>
<id>template</id>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<inputSpec>src/main/openapi/di-theme-v1.yaml</inputSpec>
<apiPackage>gen.org.tkit.onecx.theme.di.v1</apiPackage>
<modelPackage>gen.org.tkit.onecx.theme.di.v1.model</modelPackage>
<modelNameSuffix>DTOV1</modelNameSuffix>
<inputSpec>src/main/openapi/onecx-theme-di-template.yaml</inputSpec>
<apiPackage>gen.org.tkit.onecx.theme.di.template</apiPackage>
<modelPackage>gen.org.tkit.onecx.theme.di.template.model</modelPackage>
<modelNameSuffix>DTO</modelNameSuffix>
<skipValidateSpec>true</skipValidateSpec>
<generateApis>false</generateApis>
</configuration>
Expand Down
19 changes: 19 additions & 0 deletions src/main/java/org/tkit/onecx/theme/domain/daos/ImageDAO.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package org.tkit.onecx.theme.domain.daos;

import java.util.List;
import java.util.Set;

import jakarta.enterprise.context.ApplicationScoped;
import jakarta.persistence.NoResultException;
import jakarta.transaction.Transactional;
Expand Down Expand Up @@ -67,8 +70,24 @@ public void deleteQueryByRefIdAndRefType(String refId, RefTypeDTO refType) throw
}
}

public List<String> findRefIdRefTypesByRefId(Set<String> refIds) {
try {
var cb = this.getEntityManager().getCriteriaBuilder();
var cq = cb.createQuery(String.class);
var root = cq.from(Image.class);
cq.select(cb.concat(root.get(Image_.REF_ID), root.get(Image_.REF_TYPE)));
cq.where(root.get(Image_.REF_ID).in(refIds));
return this.getEntityManager().createQuery(cq).getResultList();

} catch (Exception ex) {
throw new DAOException(ErrorKeys.FIND_REF_TYPES_BY_REF_ID, ex);
}
}

public enum ErrorKeys {

FIND_REF_TYPES_BY_REF_ID,

FAILED_TO_DELETE_BY_REF_ID_QUERY,

FIND_ENTITY_BY_REF_ID_REF_TYPE_FAILED,
Expand Down
16 changes: 16 additions & 0 deletions src/main/java/org/tkit/onecx/theme/domain/daos/ThemeDAO.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.tkit.onecx.theme.domain.daos;

import java.util.List;
import java.util.Set;
import java.util.stream.Stream;

Expand Down Expand Up @@ -54,6 +55,20 @@ public Stream<Theme> findThemeByNames(Set<String> themeNames) {
}
}

public List<String> findNamesByThemeByNames(Set<String> themeNames) {
try {
var cb = this.getEntityManager().getCriteriaBuilder();
var cq = cb.createQuery(String.class);
var root = cq.from(Theme.class);
cq.select(root.get(Theme_.name));
cq.where(root.get(Theme_.name).in(themeNames));
return this.getEntityManager().createQuery(cq).getResultList();

} catch (Exception ex) {
throw new DAOException(ErrorKeys.ERROR_FIND_NAMES_BY_NAMES, ex);
}
}

public PageResult<Theme> findThemesByCriteria(ThemeSearchCriteria criteria) {
try {
var cb = this.getEntityManager().getCriteriaBuilder();
Expand Down Expand Up @@ -115,6 +130,7 @@ public enum ErrorKeys {
ERROR_FIND_THEMES_BY_CRITERIA,
ERROR_FIND_ALL_THEME_INFO,
ERROR_FIND_ALL_THEME_PAGE,
ERROR_FIND_NAMES_BY_NAMES,
ERROR_FIND_THEME_BY_NAMES,
ERROR_FIND_THEME_BY_NAME,
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package org.tkit.onecx.theme.domain.di;

import java.util.List;
import java.util.Map;

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

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.tkit.onecx.theme.domain.di.mappers.TemplateImportMapper;
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.org.tkit.onecx.theme.di.template.model.TemplateImportDTO;
import gen.org.tkit.onecx.theme.di.template.model.TemplateThemeDTO;

@DataImport("template")
public class TemplateImportService implements DataImportService {

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

@Inject
ObjectMapper objectMapper;

@Inject
TemplateImportMapper mapper;

@Inject
ThemeImportService importService;

@Override
@Transactional(Transactional.TxType.REQUIRES_NEW)
public void importData(DataImportConfig config) {
log.info("Import theme from configuration {}", config);
try {
List<String> tenants = List.of();
var tmp = config.getMetadata().get("tenants");
if (tmp != null) {
tenants = List.of(tmp.split(","));
}

if (tenants.isEmpty()) {
log.warn("No tenants defined for the templates");
return;
}

TemplateImportDTO data = objectMapper.readValue(config.getData(), TemplateImportDTO.class);

if (data.getThemes() == null || data.getThemes().isEmpty()) {
log.warn("Import configuration key {} does not contains any JSON data to import", config.getKey());
return;
}

// execute the import
importThemes(tenants, data.getThemes());
} catch (Exception ex) {
throw new ImportException(ex.getMessage(), ex);
}
}

public void importThemes(List<String> tenants, Map<String, TemplateThemeDTO> data) {

// create list of theme names to import
var names = data.keySet();

tenants.forEach(tenant -> {
// load names from the database
var existingData = importService.getData(tenant, names);

// filter and create themes to import
var themes = mapper.create(existingData, data);
var images = mapper.createImage(existingData, data);

// create themes in database for tenant
importService.importTheme(tenant, themes, images);
});
}

public static class ImportException extends RuntimeException {

public ImportException(String message, Throwable ex) {
super(message, ex);
}
}
}
Loading

0 comments on commit 6b4f1ba

Please sign in to comment.