Skip to content

Commit

Permalink
Merge pull request #930 from digitalservicebund/risdev-6259-deprecate…
Browse files Browse the repository at this point in the history
…d-norm-methods

Adapt timeboundaries-related code to use Regelungstext entity
  • Loading branch information
andreasphil authored Jan 21, 2025
2 parents cbb796c + a8dc2cd commit d03b247
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

/** Controller for norm-related actions. */
/** Controller for listing and managing time boundaries of a regelungstext. */
@RestController
@RequestMapping(
"/api/v1/norms/eli/bund/{agent}/{year}/{naturalIdentifier}/{pointInTime}/{version}/{language}/{subtype}/timeBoundaries"
Expand All @@ -37,7 +37,7 @@ public TimeBoundaryController(
}

/**
* Retrieves time boundaries for a norm based on its ELI with an optional filtering by the eli of
* Retrieves time boundaries for a regelungstext based on its ELI with an optional filtering by the eli of
* an amending law.
*
* <p>The method constructs an ELI from the provided path variables, queries the use case to
Expand All @@ -62,7 +62,7 @@ public ResponseEntity<List<TimeBoundarySchema>> getTimeBoundaries(
)
)
.orElseGet(() ->
loadTimeBoundariesUseCase.loadTimeBoundariesOfNorm(
loadTimeBoundariesUseCase.loadTimeBoundariesFromRegelungstext(
new LoadTimeBoundariesUseCase.Query(eli)
)
)
Expand All @@ -73,7 +73,7 @@ public ResponseEntity<List<TimeBoundarySchema>> getTimeBoundaries(
}

/**
* Updates time boundaries for a norm based on its ELI.
* Updates time boundaries for a regelungstext based on its ELI.
*
* <p>The method constructs an ELI from the provided path variables, queries the use case to
* update time boundaries, and maps the resulting data to {@link TimeBoundarySchema}. If no data
Expand All @@ -95,7 +95,7 @@ public ResponseEntity<List<TimeBoundarySchema>> updateTimeBoundaries(
) {
return ResponseEntity.ok(
updateTimeBoundariesUseCase
.updateTimeBoundariesOfNorm(
.updateTimeBoundariesOfRegelungstext(
new UpdateTimeBoundariesUseCase.Query(
eli,
TimeBoundaryMapper.fromResponseSchema(timeBoundaries)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,24 @@
import java.util.List;

/**
* Interface representing the use case for loading a list of {@link TimeBoundary} of a norm
* Interface representing the use case for loading a list of {@link TimeBoundary} of a regelungstext
* containing passive mods. It will filter out those time boundaries that were introduced by the
* given amending law.
*/
public interface LoadTimeBoundariesAmendedByUseCase {
/**
* Retrieves a list of time boundaries related to the specified norm filtered by the amending law
* Retrieves a list of time boundaries related to the specified regelungstext filtered by the amending law
* eli.
*
* @param query The query containing the ELI (European Legislation Identifier) of the norm.
* @return A list of {@link TimeBoundary} entities related to the specified norm.
* @param query The query containing the ELI (European Legislation Identifier) of the regelungstext.
* @return A list of {@link TimeBoundary} entities related to the specified regelungstext.
*/
List<TimeBoundary> loadTimeBoundariesAmendedBy(Query query);

/**
* A record representing the parameters needed to query time boundaries related to a norm.
* A record representing the parameters needed to query time boundaries related to a regelungstext.
*
* @param eli The ELI used to identify the norm in the query.
* @param eli The ELI used to identify the regelungstext in the query.
* @param amendingLawEli The ELI of the amending law.
*/
record Query(DokumentExpressionEli eli, DokumentExpressionEli amendingLawEli) {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,22 @@

/**
* Interface representing the use case for loading a list of {@link TimeBoundary}. Implementations
* of this interface should provide functionality to load all time boundaries related to a norm
* of this interface should provide functionality to load all time boundaries related to a regelungstext
* based on a given query.
*/
public interface LoadTimeBoundariesUseCase {
/**
* Retrieves a list of time boundaries related to the specified norm based on the provided query.
* Retrieves a list of time boundaries related to the specified regelungstext based on the provided query.
*
* @param query The query containing the ELI (European Legislation Identifier) of the norm.
* @return A list of {@link TimeBoundary} entities related to the specified norm.
* @param query The query containing the ELI (European Legislation Identifier) of the regelungstext.
* @return A list of {@link TimeBoundary} entities related to the specified regelungstext.
*/
List<TimeBoundary> loadTimeBoundariesOfNorm(Query query);
List<TimeBoundary> loadTimeBoundariesFromRegelungstext(Query query);

/**
* A record representing the parameters needed to query time boundaries related to a norm.
* A record representing the parameters needed to query time boundaries related to a regelungstext.
*
* @param eli The ELI used to identify the norm in the query.
* @param eli The ELI used to identify the regelungstext in the query.
*/
record Query(DokumentExpressionEli eli) {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,22 @@

/**
* Interface representing the use case for updating a list of {@link TimeBoundary}. Implementations
* of this interface should provide functionality to update all time boundaries related to a norm
* of this interface should provide functionality to update all time boundaries related to a regelungstext
* based on a given query.
*/
public interface UpdateTimeBoundariesUseCase {
/**
* Updates a list of time boundaries related to the specified norm based on the provided query.
* Updates a list of time boundaries related to the specified regelungstext based on the provided query.
*
* @param query The query containing the ELI (European Legislation Identifier) of the norm.
* @return A list of {@link TimeBoundary} entities related to the specified norm.
* @param query The query containing the ELI (European Legislation Identifier) of the regelungstext.
* @return A list of {@link TimeBoundary} entities related to the specified regelungstext.
*/
List<TimeBoundary> updateTimeBoundariesOfNorm(Query query);
List<TimeBoundary> updateTimeBoundariesOfRegelungstext(Query query);

/**
* A record representing the parameters needed to update time boundaries related to a norm.
* A record representing the parameters needed to update time boundaries related to a regelungstext.
*
* @param eli The ELI used to identify the norm in the query.
* @param eli The ELI used to identify the regelungstext in the query.
* @param timeBoundaries The list of the changed time boundaries.
*/
record Query(DokumentExpressionEli eli, List<TimeBoundaryChangeData> timeBoundaries) {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,26 +30,30 @@ public TimeBoundaryService(LoadNormPort loadNormPort, NormService normService) {
}

/**
* @param query The query containing the ELI (European Legislation Identifier) of the norm.
* @param query The query containing the ELI (European Legislation Identifier) of the regelungstext.
* @return a List of TimeBoundaries
*/
@Override
public List<TimeBoundary> loadTimeBoundariesOfNorm(LoadTimeBoundariesUseCase.Query query) {
public List<TimeBoundary> loadTimeBoundariesFromRegelungstext(
LoadTimeBoundariesUseCase.Query query
) {
return loadNormPort
.loadNorm(new LoadNormPort.Command(query.eli()))
.orElseThrow(() -> new NormNotFoundException(query.eli().toString()))
.getRegelungstext1()
.getTimeBoundaries();
}

@Override
public List<TimeBoundary> loadTimeBoundariesAmendedBy(
LoadTimeBoundariesAmendedByUseCase.Query query
) {
final Norm norm = loadNormPort
final Regelungstext regelungstext = loadNormPort
.loadNorm(new LoadNormPort.Command(query.eli()))
.orElseThrow(() -> new NormNotFoundException(query.eli().toString()));
.orElseThrow(() -> new NormNotFoundException(query.eli().toString()))
.getRegelungstext1();

final List<String> temporalGroupEidAmendedBy = norm
final List<String> temporalGroupEidAmendedBy = regelungstext
.getMeta()
.getOrCreateAnalysis()
.getPassiveModifications()
Expand All @@ -67,25 +71,30 @@ public List<TimeBoundary> loadTimeBoundariesAmendedBy(
.filter(Objects::nonNull)
.toList();

final List<TemporalGroup> temporalGroups = norm
final List<TemporalGroup> temporalGroups = regelungstext
.getMeta()
.getTemporalData()
.getTemporalGroups()
.stream()
.filter(f -> temporalGroupEidAmendedBy.contains(f.getEid()))
.toList();
return norm.getTimeBoundaries(temporalGroups);
return regelungstext.getTimeBoundaries(temporalGroups);
}

/**
* @param query The query containing the ELI (European Legislation Identifier) of the norm.
* @param query The query containing the ELI (European Legislation Identifier) of the regelungstext.
* @return a List of TimeBoundaries
*/
@Override
public List<TimeBoundary> updateTimeBoundariesOfNorm(UpdateTimeBoundariesUseCase.Query query) {
public List<TimeBoundary> updateTimeBoundariesOfRegelungstext(
UpdateTimeBoundariesUseCase.Query query
) {
final Norm norm = loadNormPort
.loadNorm(new LoadNormPort.Command(query.eli()))
.orElseThrow(() -> new NormNotFoundException(query.eli().toString()));

var regelungstext = norm.getRegelungstext1();

// At first time boundaries that shall be deleted need to be selected
// if we would delete first, there are cases where the next possible eId could not be safely
// calculated
Expand All @@ -94,28 +103,31 @@ public List<TimeBoundary> updateTimeBoundariesOfNorm(UpdateTimeBoundariesUseCase
// is being added. Then id3 could not be calculated.
List<TimeBoundaryChangeData> timeBoundariesToDelete = selectTimeBoundariesToDelete(
query.timeBoundaries(),
norm
regelungstext
);

// Add TimeBoundaries where eid is null|empty
addTimeBoundaries(query.timeBoundaries(), norm);
addTimeBoundaries(query.timeBoundaries(), regelungstext);

deleteTimeBoundaries(timeBoundariesToDelete, norm);
deleteTimeBoundaries(timeBoundariesToDelete, regelungstext);

editTimeBoundaries(query.timeBoundaries(), norm);
editTimeBoundaries(query.timeBoundaries(), regelungstext);

Map<DokumentExpressionEli, Norm> result = normService.updateNorm(norm);

return result.get(query.eli()).getTimeBoundaries();
return result.get(query.eli()).getRegelungstext1().getTimeBoundaries();
}

private void editTimeBoundaries(List<TimeBoundaryChangeData> timeBoundaryChangeData, Norm norm) {
private void editTimeBoundaries(
List<TimeBoundaryChangeData> timeBoundaryChangeData,
Regelungstext regelungstext
) {
List<TimeBoundaryChangeData> datesToUpdate = timeBoundaryChangeData
.stream()
.filter(tb -> tb.eid() != null && !tb.eid().isEmpty())
.toList();

List<TimeBoundary> timeBoundariesToUpdate = norm
List<TimeBoundary> timeBoundariesToUpdate = regelungstext
.getTimeBoundaries()
.stream()
.filter(tb ->
Expand All @@ -137,14 +149,14 @@ private void editTimeBoundaries(List<TimeBoundaryChangeData> timeBoundaryChangeD
tb.setEventRefDate(newDate);
});

logChangeDataWithoutCorrespondingEidInXml(norm, datesToUpdate);
logChangeDataWithoutCorrespondingEidInXml(regelungstext, datesToUpdate);
}

private void logChangeDataWithoutCorrespondingEidInXml(
Norm norm,
Regelungstext regelungstext,
List<TimeBoundaryChangeData> datesToUpdate
) {
List<String> timeBoundaryEids = norm
List<String> timeBoundaryEids = regelungstext
.getTimeBoundaries()
.stream()
.map(TimeBoundary::getEventRefEid)
Expand All @@ -165,24 +177,27 @@ private void logChangeDataWithoutCorrespondingEidInXml(
}
}

private void addTimeBoundaries(List<TimeBoundaryChangeData> timeBoundaryChangeData, Norm norm) {
private void addTimeBoundaries(
List<TimeBoundaryChangeData> timeBoundaryChangeData,
Regelungstext regelungstext
) {
timeBoundaryChangeData
.stream()
.filter(tb -> tb.eid() == null || tb.eid().isEmpty())
.map(TimeBoundaryChangeData::date)
.forEach(date -> norm.addTimeBoundary(date, EventRefType.GENERATION));
.forEach(date -> regelungstext.addTimeBoundary(date, EventRefType.GENERATION));
}

private List<TimeBoundaryChangeData> selectTimeBoundariesToDelete(
List<TimeBoundaryChangeData> timeBoundaryChangeData,
Norm norm
Regelungstext regelungstext
) {
List<String> allChangeDateEids = timeBoundaryChangeData
.stream()
.map(TimeBoundaryChangeData::eid)
.toList();

List<String> allEventRefEidsToDelete = norm
List<String> allEventRefEidsToDelete = regelungstext
.getTimeBoundaries()
.stream()
.map(TimeBoundary::getEventRefEid)
Expand All @@ -197,8 +212,8 @@ private List<TimeBoundaryChangeData> selectTimeBoundariesToDelete(

private void deleteTimeBoundaries(
List<TimeBoundaryChangeData> timeBoundariesToDelete,
Norm norm
Regelungstext regelungstext
) {
timeBoundariesToDelete.forEach(norm::deleteTimeBoundary);
timeBoundariesToDelete.forEach(regelungstext::deleteTimeBoundary);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ void getTimeBoundariesReturnsCorrectData() throws Exception {
)
);

when(loadTimeBoundariesUseCase.loadTimeBoundariesOfNorm(any())).thenReturn(timeBoundaries);
when(loadTimeBoundariesUseCase.loadTimeBoundariesFromRegelungstext(any()))
.thenReturn(timeBoundaries);

// When // Then
mockMvc
Expand All @@ -104,7 +105,7 @@ void getTimeBoundariesReturnsCorrectData() throws Exception {
.andExpect(jsonPath("$[0].temporalGroupEid", is("meta-1_geltzeiten-1_geltungszeitgr-1")));

verify(loadTimeBoundariesUseCase, times(1))
.loadTimeBoundariesOfNorm(any(LoadTimeBoundariesUseCase.Query.class));
.loadTimeBoundariesFromRegelungstext(any(LoadTimeBoundariesUseCase.Query.class));
}

@Test
Expand Down Expand Up @@ -206,7 +207,7 @@ void updateTimeBoundariesReturnsSuccess() throws Exception {
)
);

when(updateTimeBoundariesUseCase.updateTimeBoundariesOfNorm(any()))
when(updateTimeBoundariesUseCase.updateTimeBoundariesOfRegelungstext(any()))
.thenReturn(timeBoundaries);

// When // Then
Expand All @@ -227,7 +228,7 @@ void updateTimeBoundariesReturnsSuccess() throws Exception {
.andExpect(jsonPath("$[0].temporalGroupEid", is("meta-1_geltzeiten-1_geltungszeitgr-1")));

verify(updateTimeBoundariesUseCase, times(1))
.updateTimeBoundariesOfNorm(any(UpdateTimeBoundariesUseCase.Query.class));
.updateTimeBoundariesOfRegelungstext(any(UpdateTimeBoundariesUseCase.Query.class));
}

@Test
Expand Down
Loading

0 comments on commit d03b247

Please sign in to comment.