82 lines
3.2 KiB
Java
82 lines
3.2 KiB
Java
package SuMoC.backend.trip;
|
|
|
|
import SuMoC.backend.h3.H3Service;
|
|
import SuMoC.backend.trip.dtos.NewTripDTO;
|
|
import SuMoC.backend.trip.dtos.NewTripSegmentDTO;
|
|
import SuMoC.backend.trip.dtos.RegularTripDetectionDTO;
|
|
import SuMoC.backend.trip.dtos.TripDTO;
|
|
import SuMoC.backend.trip.models.Trip;
|
|
import org.mapstruct.Mapper;
|
|
import org.mapstruct.Mapping;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
import java.util.Arrays;
|
|
import java.util.HashMap;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
import java.util.stream.Collectors;
|
|
|
|
@Mapper(componentModel = "spring", uses = {TripSegmentMapper.class, H3Service.class})
|
|
public abstract class TripMapper {
|
|
|
|
@Autowired
|
|
protected H3Service h3service;
|
|
|
|
@Mapping(source = "id", target = "tripId")
|
|
@Mapping(source = "user.id", target = "userId")
|
|
public abstract TripDTO toTripDTO(Trip trip);
|
|
|
|
public abstract List<TripDTO> toTripDTOs(List<Trip> trips);
|
|
|
|
@Mapping(source = "tripSegments", target = "tripSegments")
|
|
@Mapping(target = "startH3Cell", expression = "java(calculateH3CellStart(newTripDTO))")
|
|
@Mapping(target = "endH3Cell", expression = "java(calculateH3CellEnd(newTripDTO))")
|
|
@Mapping(target = "mainModality", expression = "java(mainModalityByDistance(newTripDTO))")
|
|
public abstract Trip toTrip(NewTripDTO newTripDTO);
|
|
|
|
@Mapping(source = "id", target = "tripId")
|
|
@Mapping(source = "user.id", target = "userId")
|
|
@Mapping(target = "startH3CellKRingNeighboors", expression = "java(calculateStartH3KRingNeighboords(trip))")
|
|
@Mapping(target = "endH3CellKRingNeighboors", expression = "java(calculateEndtH3KRingNeighboords(trip))")
|
|
public abstract RegularTripDetectionDTO toRegularTripDetectionDTO(Trip trip);
|
|
|
|
public abstract List<RegularTripDetectionDTO> toRegularTripDetectionDTOS(List<Trip> trips);
|
|
|
|
protected String calculateH3CellStart(NewTripDTO newTripDTO) {
|
|
return h3service.latLngToCellForRoute(newTripDTO.getStartLatitude(), newTripDTO.getStartLongitude());
|
|
}
|
|
|
|
protected String calculateH3CellEnd(NewTripDTO newTripDTO) {
|
|
return h3service.latLngToCellForRoute(newTripDTO.getEndLatitude(), newTripDTO.getEndLongitude());
|
|
}
|
|
|
|
protected List<String> calculateStartH3KRingNeighboords(Trip trip) {
|
|
return h3service.getRoutePatternNeighbors(trip.getStartH3Cell());
|
|
}
|
|
|
|
protected List<String> calculateEndtH3KRingNeighboords(Trip trip) {
|
|
return h3service.getRoutePatternNeighbors(trip.getEndH3Cell());
|
|
}
|
|
|
|
protected Modality mainModalityByDistance(NewTripDTO newTripDTO) {
|
|
HashMap<Modality, Float> map = Arrays.stream(Modality.values())
|
|
.collect(Collectors.toMap(
|
|
modality -> modality,
|
|
modality -> 0f,
|
|
(a, b) -> a,
|
|
HashMap::new
|
|
));
|
|
|
|
for (NewTripSegmentDTO segmentDTO : newTripDTO.getTripSegments()) {
|
|
Modality modality = segmentDTO.getModality();
|
|
map.put(modality, map.get(modality) + segmentDTO.getDistanceMeters());
|
|
}
|
|
|
|
return map.entrySet().stream()
|
|
.max(Map.Entry.comparingByValue())
|
|
.map(Map.Entry::getKey)
|
|
.orElse(Modality.IDLE);
|
|
|
|
}
|
|
}
|