Skip to content

Commit

Permalink
Merge pull request #87 from Opetushallitus/OK-733
Browse files Browse the repository at this point in the history
Ok 733
  • Loading branch information
msiukola authored Dec 10, 2024
2 parents b966f5d + 364d4dc commit 358a066
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@
import java.time.LocalDateTime;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import javax.persistence.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

@Repository
public class PoistettuDAOImpl implements PoistettuDAO {
@PersistenceContext private EntityManager entityManager;
private static final Logger LOG = LoggerFactory.getLogger(PoistettuDAOImpl.class);

@Override
public List<Poistettu> findPoistetutHakukohdeViitteet(LocalDateTime start, LocalDateTime end) {
Expand Down Expand Up @@ -100,17 +104,29 @@ public List<Poistettu> findParentValinnanvaiheetFromHistory(Collection<Long> ids

private List<Poistettu> doFindParents(
String tableName, String parentIdFieldName, String tunnisteFieldName, Collection<Long> ids) {
String sqlTemplate =
"""
select distinct on (id) id, %s as parentId, %s as tunniste from %s
where id in (:ids)""";
String sql = String.format(sqlTemplate, parentIdFieldName, tunnisteFieldName, tableName);
Query query = entityManager.createNativeQuery(sql).setParameter("ids", ids);
@SuppressWarnings("unchecked")
List<Object[]> resultList = (List<Object[]>) query.getResultList();
return resultList.stream()
.map(Poistettu::new)
.map(p -> p.setDeletedItself(false))
.collect(Collectors.toList());
try {
Collection<Long> nonNullIds = ids.stream().filter(Objects::nonNull).toList();
String sqlTemplate =
"""
select distinct on (id) id, %s as parentId, %s as tunniste from %s
where id in (:ids)""";
String sql = String.format(sqlTemplate, parentIdFieldName, tunnisteFieldName, tableName);
Query query = entityManager.createNativeQuery(sql).setParameter("ids", nonNullIds);
@SuppressWarnings("unchecked")
List<Object[]> resultList = (List<Object[]>) query.getResultList();
return resultList.stream()
.map(Poistettu::new)
.map(p -> p.setDeletedItself(false))
.collect(Collectors.toList());
} catch (Exception e) {
LOG.error(
"Virhe haettaessa tietoja parametreille {}, {}, {}, {}:",
tableName,
parentIdFieldName,
tunnisteFieldName,
ids,
e);
throw e;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,12 @@ private LocalDateTime parseDateTime(
summary =
"Luo siirtotiedostot annetulla aikavälillä luoduista / muutetuista valintaperusteista hakukohteittain")
public ResponseEntity<String> createByTimeRange(
@Parameter(description = "Alkuaika") @RequestParam(required = false) String startDatetime,
@Parameter(description = "Loppuaika") @RequestParam(required = false) String endDatetime) {
@Parameter(description = "Alkuaika", example = "2024-08-01T00:00:00")
@RequestParam(required = false)
String startDatetime,
@Parameter(description = "Loppuaika", example = "2024-11-13T00:00:00")
@RequestParam(required = false)
String endDatetime) {
LocalDateTime start =
parseDateTime(
startDatetime,
Expand Down

0 comments on commit 358a066

Please sign in to comment.