Skip to content

Commit

Permalink
fix: location order
Browse files Browse the repository at this point in the history
  • Loading branch information
Angular2Guy committed Aug 2, 2024
1 parent 63871b7 commit a4359af
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.graphql.data.method.annotation.Argument;
import org.springframework.graphql.data.method.annotation.BatchMapping;
import org.springframework.graphql.data.method.annotation.MutationMapping;
Expand All @@ -31,30 +30,22 @@
import ch.xxx.maps.domain.model.dto.PolygonDto;
import ch.xxx.maps.domain.model.dto.RingDto;
import ch.xxx.maps.domain.model.entity.CompanySite;
import ch.xxx.maps.domain.model.entity.CompanySiteRepository;
import ch.xxx.maps.domain.model.entity.LocationRepository;
import ch.xxx.maps.domain.model.entity.PolygonRepository;
import ch.xxx.maps.domain.model.entity.RingRepository;
import ch.xxx.maps.usecase.mapper.EntityDtoMapper;
import ch.xxx.maps.usecase.service.CompanySiteService;
import graphql.schema.DataFetchingEnvironment;
import jakarta.persistence.EntityManager;

@Controller
public class CompanySiteController {
private static final Logger LOGGER = LoggerFactory.getLogger(CompanySite.class);
private final CompanySiteService companySiteService;
private final EntityDtoMapper entityDtoMapper;
private final ApplicationContext applicationContext;

private record Selections(boolean withPolygons, boolean withRings, boolean withLocations) {
}

public CompanySiteController(CompanySiteService companySiteService, EntityDtoMapper entityDtoMapper,
ApplicationContext applicationContext) {
public CompanySiteController(CompanySiteService companySiteService, EntityDtoMapper entityDtoMapper) {
this.companySiteService = companySiteService;
this.entityDtoMapper = entityDtoMapper;
this.applicationContext = applicationContext;
}

@QueryMapping
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,35 +68,6 @@ public Collection<CompanySite> findCompanySiteByTitleAndYear(String title, Long
title = title.trim().toLowerCase();
List<CompanySite> companySites = this.companySiteRepository.findByTitleFromTo(title, beginOfYear, endOfYear)
.stream().peek(this.entityManager::detach).toList();
// companySites = addEntities(withPolygons, withRings, withLocations, companySites);
return companySites;
}



private List<CompanySite> addEntities(boolean withPolygons, boolean withRings, boolean withLocations,
List<CompanySite> companySites) {
if (withPolygons) {
Map<Long, List<Polygon>> fetchPolygons = this.fetchPolygonEntitys(companySites);
Map<Long, List<Ring>> fetchRings = !withRings ? Map.of()
: this.fetchRings(fetchPolygons.values().stream().flatMap(List::stream).toList());
Map<Long, List<Location>> fetchLocations = !withLocations ? Map.of()
: this.fetchLocations(fetchRings.values().stream().flatMap(List::stream).toList());
companySites.forEach(myCompanySite -> {
myCompanySite.setPolygons(new HashSet<>(fetchPolygons.getOrDefault(myCompanySite.getId(), List.of())));
if (withRings) {
myCompanySite.getPolygons().forEach(myPolygon -> {
myPolygon.setRings(new HashSet<>(fetchRings.getOrDefault(myPolygon.getId(), List.of())));
if (withLocations) {
myPolygon.getRings().forEach(myRing -> {
myRing.setLocations(
new HashSet<>(fetchLocations.getOrDefault(myRing.getId(), List.of())));
});
}
});
}
});
}
return companySites;
}

Expand All @@ -108,7 +79,6 @@ public Optional<CompanySite> findCompanySiteById(Long id) {
public Optional<CompanySite> findCompanySiteByIdDetached(Long id, boolean withPolygons, boolean withRings,
boolean withLocations) {
return Optional.ofNullable(id).flatMap(myId -> this.companySiteRepository.findById(myId)).stream()
// .peek(myCompanySite -> this.addEntities(withPolygons, withRings, withLocations, List.of(myCompanySite)))
.findFirst();
}

Expand Down Expand Up @@ -208,7 +178,7 @@ public LinkedHashMap<RingDto, List<Location>> fetchLocationDtos(List<RingDto> ri
.findAllByRingIds(ringDtos.stream().map(RingDto::getId).collect(Collectors.toList())).stream()
.peek(this.entityManager::detach).toList();
return ringDtos.stream()
.map(myRi -> Map.entry(myRi, findLocations(locations, myRi.getId())))
.map(myRi -> Map.entry(myRi, findLocations(locations, myRi.getId())))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> {throw new RuntimeException();}, LinkedHashMap::new));
}

Expand Down Expand Up @@ -236,6 +206,7 @@ public Map<Long, List<Location>> fetchLocations(List<Ring> rings) {

private List<Location> findLocations(List<Location> locations, Long myRiId) {
return locations.stream().filter(myLocation -> myLocation.getRing().getId().equals(myRiId))
.sorted((a,b) -> a.getOrderId().compareTo(b.getOrderId()))
.collect(Collectors.toList());
}
}

0 comments on commit a4359af

Please sign in to comment.