-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: migration, add export and import service
- Loading branch information
1 parent
86e46b3
commit f916abb
Showing
17 changed files
with
692 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
src/main/java/io/github/onecx/theme/rs/exim/v1/controllers/ExportImportRestControllerV1.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package io.github.onecx.theme.rs.exim.v1.controllers; | ||
|
||
import java.util.stream.Collectors; | ||
|
||
import jakarta.enterprise.context.ApplicationScoped; | ||
import jakarta.inject.Inject; | ||
import jakarta.transaction.Transactional; | ||
import jakarta.validation.ConstraintViolationException; | ||
import jakarta.ws.rs.Path; | ||
import jakarta.ws.rs.core.Response; | ||
|
||
import org.jboss.resteasy.reactive.RestResponse; | ||
import org.jboss.resteasy.reactive.server.ServerExceptionMapper; | ||
|
||
import gen.io.github.onecx.theme.rs.exim.v1.ThemesExportImportApi; | ||
import gen.io.github.onecx.theme.rs.exim.v1.model.*; | ||
import io.github.onecx.theme.domain.daos.ThemeDAO; | ||
import io.github.onecx.theme.domain.models.Theme; | ||
import io.github.onecx.theme.rs.exim.v1.mappers.ExportImportExceptionMapperV1; | ||
import io.github.onecx.theme.rs.exim.v1.mappers.ExportImportMapperV1; | ||
|
||
@Path("/exim/v1/themes") | ||
@ApplicationScoped | ||
@Transactional(Transactional.TxType.NOT_SUPPORTED) | ||
public class ExportImportRestControllerV1 implements ThemesExportImportApi { | ||
|
||
@Inject | ||
ThemeDAO dao; | ||
@Inject | ||
ExportImportExceptionMapperV1 exceptionMapper; | ||
|
||
@Inject | ||
ExportImportMapperV1 mapper; | ||
|
||
@Override | ||
public Response exportThemes(EximExportRequestDTOV1 request) { | ||
var themes = dao.findThemeByNames(request.getNames()); | ||
|
||
var data = themes.collect(Collectors.toMap(Theme::getName, theme -> theme)); | ||
|
||
if (data.isEmpty()) { | ||
return Response.status(Response.Status.NOT_FOUND).build(); | ||
} | ||
return Response.ok(mapper.create(data)).build(); | ||
} | ||
|
||
@Override | ||
@Transactional(Transactional.TxType.REQUIRES_NEW) | ||
public Response importThemes(EximImportRequestDTOV1 request) { | ||
var keys = request.getThemes().keySet(); | ||
var themes = dao.findThemeByNames(keys); | ||
var map = themes.collect(Collectors.toMap(Theme::getName, theme -> theme)); | ||
|
||
EximImportResultThemesDTOV1 items = new EximImportResultThemesDTOV1(); | ||
|
||
request.getThemes().forEach((name, dto) -> { | ||
|
||
var theme = map.get(name); | ||
if (theme == null) { | ||
|
||
theme = mapper.create(dto); | ||
theme.setName(name); | ||
dao.create(theme); | ||
items.put(name, mapper.create(EximThemeResultStatusDTOV1.CREATED)); | ||
|
||
} else { | ||
|
||
mapper.update(dto, theme); | ||
dao.update(theme); | ||
items.put(name, mapper.create(EximThemeResultStatusDTOV1.UPDATE)); | ||
} | ||
}); | ||
|
||
return Response.ok(mapper.create(request, items)).build(); | ||
} | ||
|
||
@ServerExceptionMapper | ||
public RestResponse<EximRestExceptionDTOV1> exception(Exception ex) { | ||
return exceptionMapper.exception(ex); | ||
} | ||
|
||
@ServerExceptionMapper | ||
public RestResponse<EximRestExceptionDTOV1> constraint(ConstraintViolationException ex) { | ||
return exceptionMapper.constraint(ex); | ||
} | ||
} |
69 changes: 69 additions & 0 deletions
69
src/main/java/io/github/onecx/theme/rs/exim/v1/mappers/ExportImportExceptionMapperV1.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package io.github.onecx.theme.rs.exim.v1.mappers; | ||
|
||
import java.util.List; | ||
import java.util.Set; | ||
|
||
import jakarta.validation.ConstraintViolation; | ||
import jakarta.validation.ConstraintViolationException; | ||
import jakarta.validation.Path; | ||
import jakarta.ws.rs.core.Response; | ||
|
||
import org.jboss.resteasy.reactive.RestResponse; | ||
import org.mapstruct.Mapper; | ||
import org.mapstruct.Mapping; | ||
import org.tkit.quarkus.jpa.exceptions.DAOException; | ||
import org.tkit.quarkus.rs.mappers.OffsetDateTimeMapper; | ||
|
||
import gen.io.github.onecx.theme.rs.exim.v1.model.*; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
@Slf4j | ||
@Mapper(uses = { OffsetDateTimeMapper.class }) | ||
public abstract class ExportImportExceptionMapperV1 { | ||
|
||
public RestResponse<EximRestExceptionDTOV1> constraint(ConstraintViolationException ex) { | ||
log.error("Processing theme export import rest controller error: {}", ex.getMessage()); | ||
|
||
var dto = exception("CONSTRAINT_VIOLATIONS", ex.getMessage()); | ||
dto.setValidations(createErrorValidationResponse(ex.getConstraintViolations())); | ||
return RestResponse.status(Response.Status.BAD_REQUEST, dto); | ||
} | ||
|
||
public RestResponse<EximRestExceptionDTOV1> exception(Exception ex) { | ||
log.error("Processing theme export import rest controller error: {}", ex.getMessage()); | ||
|
||
if (ex instanceof DAOException de) { | ||
return RestResponse.status(Response.Status.BAD_REQUEST, | ||
exception(de.getMessageKey().name(), ex.getMessage(), de.parameters)); | ||
} | ||
return RestResponse.status(Response.Status.INTERNAL_SERVER_ERROR, | ||
exception("UNDEFINED_ERROR_CODE", ex.getMessage())); | ||
|
||
} | ||
|
||
@Mapping(target = "removeParametersItem", ignore = true) | ||
@Mapping(target = "namedParameters", ignore = true) | ||
@Mapping(target = "removeNamedParametersItem", ignore = true) | ||
@Mapping(target = "parameters", ignore = true) | ||
@Mapping(target = "validations", ignore = true) | ||
@Mapping(target = "removeValidationsItem", ignore = true) | ||
public abstract EximRestExceptionDTOV1 exception(String errorCode, String message); | ||
|
||
@Mapping(target = "removeParametersItem", ignore = true) | ||
@Mapping(target = "namedParameters", ignore = true) | ||
@Mapping(target = "removeNamedParametersItem", ignore = true) | ||
@Mapping(target = "validations", ignore = true) | ||
@Mapping(target = "removeValidationsItem", ignore = true) | ||
public abstract EximRestExceptionDTOV1 exception(String errorCode, String message, List<Object> parameters); | ||
|
||
public abstract List<EximValidationConstraintDTOV1> createErrorValidationResponse( | ||
Set<ConstraintViolation<?>> constraintViolation); | ||
|
||
@Mapping(target = "parameter", source = "propertyPath") | ||
@Mapping(target = "message", source = "message") | ||
public abstract EximValidationConstraintDTOV1 createError(ConstraintViolation<?> constraintViolation); | ||
|
||
public String mapPath(Path path) { | ||
return path.toString(); | ||
} | ||
} |
83 changes: 83 additions & 0 deletions
83
src/main/java/io/github/onecx/theme/rs/exim/v1/mappers/ExportImportMapperV1.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package io.github.onecx.theme.rs.exim.v1.mappers; | ||
|
||
import java.time.OffsetDateTime; | ||
import java.util.Map; | ||
|
||
import jakarta.inject.Inject; | ||
|
||
import org.mapstruct.Mapper; | ||
import org.mapstruct.Mapping; | ||
import org.mapstruct.MappingTarget; | ||
import org.mapstruct.Named; | ||
import org.tkit.quarkus.rs.mappers.OffsetDateTimeMapper; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
|
||
import gen.io.github.onecx.theme.rs.exim.v1.model.*; | ||
import io.github.onecx.theme.domain.models.Theme; | ||
|
||
@Mapper(uses = { OffsetDateTimeMapper.class }) | ||
public abstract class ExportImportMapperV1 { | ||
|
||
@Inject | ||
ObjectMapper objectMapper; | ||
|
||
@Mapping(target = "id", source = "request.id") | ||
@Mapping(target = "themes", source = "themes") | ||
public abstract EximImportResultDTOV1 create(EximImportRequestDTOV1 request, EximImportResultThemesDTOV1 themes); | ||
|
||
public abstract EximThemeResultDTOV1 create(EximThemeResultStatusDTOV1 status); | ||
|
||
public EximImportRequestDTOV1 create(Map<String, Theme> data) { | ||
if (data == null) { | ||
return null; | ||
} | ||
EximImportRequestDTOV1 result = new EximImportRequestDTOV1(); | ||
result.setCreated(OffsetDateTime.now()); | ||
result.setThemes(map(data)); | ||
return result; | ||
} | ||
|
||
public abstract EximImportThemesDTOV1 map(Map<String, Theme> data); | ||
|
||
public abstract EximThemeDTOV1 map(Theme theme); | ||
|
||
@Mapping(target = "id", ignore = true) | ||
@Mapping(target = "name", ignore = true) | ||
@Mapping(target = "creationDate", ignore = true) | ||
@Mapping(target = "creationUser", ignore = true) | ||
@Mapping(target = "modificationDate", ignore = true) | ||
@Mapping(target = "modificationUser", ignore = true) | ||
@Mapping(target = "controlTraceabilityManual", ignore = true) | ||
@Mapping(target = "modificationCount", ignore = true) | ||
@Mapping(target = "persisted", ignore = true) | ||
@Mapping(target = "properties", qualifiedByName = "properties") | ||
public abstract void update(EximThemeDTOV1 dto, @MappingTarget Theme entity); | ||
|
||
@Mapping(target = "id", ignore = true) | ||
@Mapping(target = "name", ignore = true) | ||
@Mapping(target = "creationDate", ignore = true) | ||
@Mapping(target = "creationUser", ignore = true) | ||
@Mapping(target = "modificationDate", ignore = true) | ||
@Mapping(target = "modificationUser", ignore = true) | ||
@Mapping(target = "controlTraceabilityManual", ignore = true) | ||
@Mapping(target = "modificationCount", ignore = true) | ||
@Mapping(target = "persisted", ignore = true) | ||
@Mapping(target = "properties", qualifiedByName = "properties") | ||
public abstract Theme create(EximThemeDTOV1 dto); | ||
|
||
@Named("properties") | ||
public String mapToString(Object properties) { | ||
|
||
if (properties == null) { | ||
return null; | ||
} | ||
|
||
try { | ||
return objectMapper.writeValueAsString(properties); | ||
} catch (JsonProcessingException e) { | ||
return null; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.