Skip to content

Commit

Permalink
Rename norms table to dokumente and add RegelungstextMapper
Browse files Browse the repository at this point in the history
RISDEV-6261
  • Loading branch information
malte-laukoetter committed Jan 21, 2025
1 parent a4198d8 commit cbb796c
Show file tree
Hide file tree
Showing 28 changed files with 773 additions and 553 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,16 @@
import org.hibernate.type.SqlTypes;

/**
* Data Transfer Object (DTO) class representing a norm entity. This class is annotated with Lombok
* annotations for generating getters, setters, constructors, and builder methods.
* Data Transfer Object (DTO) class representing a dokument of a norm.
*/
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Builder(toBuilder = true)
@Entity
@Table(name = "norms")
public class NormDto {
@Table(name = "dokumente")
public class DokumentDto {

@Id
@GeneratedValue
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,5 @@ public class ReleaseDto {
)
)
@Builder.Default
private List<NormDto> norms = new ArrayList<>();
private List<DokumentDto> norms = new ArrayList<>();
}
Original file line number Diff line number Diff line change
@@ -1,42 +1,52 @@
package de.bund.digitalservice.ris.norms.adapter.output.database.mapper;

import de.bund.digitalservice.ris.norms.adapter.output.database.dto.NormDto;
import de.bund.digitalservice.ris.norms.adapter.output.database.dto.DokumentDto;
import de.bund.digitalservice.ris.norms.domain.entity.Norm;
import de.bund.digitalservice.ris.norms.domain.entity.Regelungstext;
import de.bund.digitalservice.ris.norms.utils.XmlMapper;
import java.util.Collection;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import javax.annotation.Nonnull;

/** Mapper class for converting between {@link NormDto} and {@link Norm}. */
/** Mapper class for converting between {@link DokumentDto} and {@link Norm}. */
public class NormMapper {

// Private constructor to hide the implicit public one and prevent instantiation
private NormMapper() {}

/**
* Maps a {@link NormDto} to the domain {@link Norm}.
* Maps multiple {@link DokumentDto}s to the domain {@link Norm}.
*
* @param normDto The input {@link NormDto} to be mapped.
* @return A new {@link Norm} mapped from the input {@link NormDto}.
* @param dokumentDtos The input {@link DokumentDto}s to be mapped.
* @return A new {@link Norm} mapped from the input {@link DokumentDto}.
*/
public static Norm mapToDomain(final NormDto normDto) {
return Norm
.builder()
.regelungstexte(Set.of(new Regelungstext(XmlMapper.toDocument(normDto.getXml()))))
.publishState(normDto.getPublishState())
.build();
public static Optional<Norm> mapToDomain(@Nonnull final Collection<DokumentDto> dokumentDtos) {
if (dokumentDtos.isEmpty()) {
return Optional.empty();
}

return Optional.of(
Norm
.builder()
.regelungstexte(
dokumentDtos.stream().map(RegelungstextMapper::mapToDomain).collect(Collectors.toSet())
)
.publishState(dokumentDtos.stream().findFirst().get().getPublishState())
.build()
);
}

/**
* Maps a domain {@link Norm} to {@link NormDto}.
* Maps a domain {@link Norm} to {@link DokumentDto}s for each of the dokuments of the norm.
*
* @param norm The input {@link Norm} to be mapped.
* @return A new {@link NormDto} mapped from the input {@link Norm}.
* @return A new {@link DokumentDto}s mapped from the input {@link Norm}.
*/
public static NormDto mapToDto(final Norm norm) {
return NormDto
.builder()
.xml(XmlMapper.toString(norm.getDocument()))
.publishState(norm.getPublishState())
.build();
public static Set<DokumentDto> mapToDtos(final Norm norm) {
return norm
.getRegelungstexte()
.stream()
.map(regelungstext -> RegelungstextMapper.mapToDto(regelungstext, norm.getPublishState()))
.collect(Collectors.toSet());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package de.bund.digitalservice.ris.norms.adapter.output.database.mapper;

import de.bund.digitalservice.ris.norms.adapter.output.database.dto.DokumentDto;
import de.bund.digitalservice.ris.norms.domain.entity.Norm;
import de.bund.digitalservice.ris.norms.domain.entity.NormPublishState;
import de.bund.digitalservice.ris.norms.domain.entity.Regelungstext;
import de.bund.digitalservice.ris.norms.utils.XmlMapper;

/** Mapper class for converting between {@link DokumentDto} and {@link Regelungstext}. */
public class RegelungstextMapper {

// Private constructor to hide the implicit public one and prevent instantiation
private RegelungstextMapper() {}

/**
* Maps a {@link DokumentDto} to the domain {@link Norm}.
*
* @param dokumentDto The input {@link DokumentDto} to be mapped.
* @return A new {@link Regelungstext} mapped from the input {@link DokumentDto}.
*/
public static Regelungstext mapToDomain(final DokumentDto dokumentDto) {
return new Regelungstext(XmlMapper.toDocument(dokumentDto.getXml()));
}

/**
* Maps a domain {@link Regelungstext} to {@link DokumentDto}.
*
* @param regelungstext The input {@link Regelungstext} to be mapped.
* @param publishState The publishState of the {@link Norm} associated with the {@link Regelungstext}. For now this is still stored on every dokument.
* @return A new {@link DokumentDto} mapped from the input {@link Regelungstext}.
*/
public static DokumentDto mapToDto(
final Regelungstext regelungstext,
final NormPublishState publishState
) {
return DokumentDto
.builder()
.xml(XmlMapper.toString(regelungstext.getDocument()))
.publishState(publishState)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import de.bund.digitalservice.ris.norms.adapter.output.database.dto.ReleaseDto;
import de.bund.digitalservice.ris.norms.domain.entity.Release;
import java.util.List;
import java.util.Optional;

/** Mapper class for converting between {@link ReleaseDto} and {@link Release}. */
public class ReleaseMapper {
Expand All @@ -19,7 +21,15 @@ public static Release mapToDomain(final ReleaseDto releaseDto) {
return Release
.builder()
.releasedAt(releaseDto.getReleasedAt())
.publishedNorms(releaseDto.getNorms().stream().map(NormMapper::mapToDomain).toList())
.publishedNorms(
releaseDto
.getNorms()
.stream()
.map(List::of)
.map(NormMapper::mapToDomain)
.flatMap(Optional::stream)
.toList()
)
.build();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package de.bund.digitalservice.ris.norms.adapter.output.database.repository;

import de.bund.digitalservice.ris.norms.adapter.output.database.dto.NormDto;
import de.bund.digitalservice.ris.norms.adapter.output.database.dto.DokumentDto;
import de.bund.digitalservice.ris.norms.domain.entity.NormPublishState;
import jakarta.transaction.Transactional;
import java.util.List;
Expand All @@ -12,50 +12,50 @@
import org.springframework.stereotype.Repository;

/**
* Repository interface for interacting with the database and managing {@link NormDto} entities.
* Repository interface for interacting with the database and managing {@link DokumentDto} entities.
* This interface extends {@link JpaRepository} and focuses on operations related to {@link
* NormDto}.
* DokumentDto}.
*/
@Repository
public interface NormRepository extends JpaRepository<NormDto, UUID> {
public interface DokumentRepository extends JpaRepository<DokumentDto, UUID> {
/**
* Finds a {@link NormDto} by its expression ELI (European Legislation Identifier).
* Finds a {@link DokumentDto} by its expression ELI (European Legislation Identifier).
* It takes the newest manifestation if multiple exist.
*
* @param expressionEli The ELI to search for.
* @return An {@link Optional} containing the found {@link NormDto} if exists, or empty if not found.
* @return An {@link Optional} containing the found {@link DokumentDto} if exists, or empty if not found.
*/
Optional<NormDto> findFirstByEliDokumentExpressionOrderByEliDokumentManifestationDesc(
Optional<DokumentDto> findFirstByEliDokumentExpressionOrderByEliDokumentManifestationDesc(
final String expressionEli
);

/**
* Finds a {@link NormDto} by its manifestation ELI (European Legislation Identifier).
* Finds a {@link DokumentDto} by its manifestation ELI (European Legislation Identifier).
*
* @param manifestationEli The ELI to search for.
* @return An {@link Optional} containing the found {@link NormDto} if exists, or empty if not found.
* @return An {@link Optional} containing the found {@link DokumentDto} if exists, or empty if not found.
*/
Optional<NormDto> findByEliDokumentManifestation(final String manifestationEli);
Optional<DokumentDto> findByEliDokumentManifestation(final String manifestationEli);

/**
* Finds a {@link NormDto} by its GUID.
* Finds a {@link DokumentDto} by its GUID.
* It takes the newest manifestation if multiple norms with the same guid exist.
*
* @param guid The GUID to search for.
* @return An {@link Optional} containing the found {@link NormDto} if exists, or empty if not found.
* @return An {@link Optional} containing the found {@link DokumentDto} if exists, or empty if not found.
*/
Optional<NormDto> findFirstByGuidOrderByEliDokumentManifestation(final UUID guid);
Optional<DokumentDto> findFirstByGuidOrderByEliDokumentManifestation(final UUID guid);

/**
* Deletes all {@link NormDto} with the given GUID
* Deletes all {@link DokumentDto} with the given GUID
*
* @param guid The GUID to search for.
*/
@Transactional
void deleteAllByGuid(final UUID guid);

/**
* Deletes a {@link NormDto} by its manifestation ELI (European Legislation Identifier) if it is in the given publish state.
* Deletes a {@link DokumentDto} by its manifestation ELI (European Legislation Identifier) if it is in the given publish state.
*
* @param manifestationEli The ELI to search for.
* @param publishState The publishState to search for.
Expand All @@ -66,7 +66,7 @@ void deleteByEliDokumentManifestationAndPublishState(
);

/**
* Deletes all {@link NormDto} of the given work ELI that are in the given publish state.
* Deletes all {@link DokumentDto} of the given work ELI that are in the given publish state.
* @param workEli The ELI to search for.
* @param publishState The publishState to search for.
*/
Expand All @@ -76,12 +76,12 @@ void deleteAllByEliDokumentWorkAndPublishState(
);

/**
* Retrieves the ids of all {@link NormDto} with a specific {@link NormPublishState}.
* Retrieves the ids of all {@link DokumentDto} with a specific {@link NormPublishState}.
*
* @param normPublishState the publish state to filter the norms by (e.g., {@link NormPublishState#QUEUED_FOR_PUBLISH})
* @return a {@link List} of {@link UUID}s of the {@link NormDto}s matching the specified publish state
* @return a {@link List} of {@link UUID}s of the {@link DokumentDto}s matching the specified publish state
*/
@Query("SELECT n.id FROM NormDto n WHERE n.publishState = :publishState")
@Query("SELECT n.id FROM DokumentDto n WHERE n.publishState = :publishState")
List<UUID> findNormIdsByPublishState(
@Param("publishState") final NormPublishState normPublishState
);
Expand Down
Loading

0 comments on commit cbb796c

Please sign in to comment.