Commit 0576f5fe authored by PECQUOT's avatar PECQUOT
Browse files

[fix] VesselDaoImpl: use ProgramEnum.SIH instead of getConfig().getVesselDefaultProgramLabel()

[fix] SaleTypeEnum
[fix] AggregatedLandingService: use services if available
[enh] add AggregatedLandingService.deleteAll
parent 0b2e4e83
......@@ -904,6 +904,10 @@ public class SumarisConfiguration extends PropertyPlaceholderConfigurer {
return applicationConfig.getOptionAsBoolean(SumarisConfigurationOption.ENABLE_SAMPLE_UNIQUE_TAG.getKey());
}
/**
* Prefer ProgramEnum.SIH.getLabel()
*/
@Deprecated
public String getVesselDefaultProgramLabel() {
return applicationConfig.getOption(SumarisConfigurationOption.VESSEL_DEFAULT_PROGRAM_LABEL.getKey());
}
......
......@@ -33,6 +33,7 @@ import net.sumaris.core.dao.technical.Daos;
import net.sumaris.core.dao.technical.SortDirection;
import net.sumaris.core.dao.technical.hibernate.HibernateDaoSupport;
import net.sumaris.core.model.administration.programStrategy.Program;
import net.sumaris.core.model.administration.programStrategy.ProgramEnum;
import net.sumaris.core.model.data.Vessel;
import net.sumaris.core.model.data.VesselFeatures;
import net.sumaris.core.model.data.VesselRegistrationPeriod;
......@@ -539,7 +540,7 @@ public class VesselDaoImpl extends HibernateDaoSupport implements VesselDao {
// Default program
if (copyIfNull && target.getProgram() == null) {
String defaultProgramLabel = getConfig().getVesselDefaultProgramLabel();
String defaultProgramLabel = ProgramEnum.SIH.getLabel(); //getConfig().getVesselDefaultProgramLabel();
ProgramVO defaultProgram = StringUtils.isNotBlank(defaultProgramLabel) ? programRepository.getByLabel(defaultProgramLabel) : null;
if (defaultProgram != null && defaultProgram.getId() != null) {
target.setProgram(getReference(Program.class, defaultProgram.getId()));
......
......@@ -30,7 +30,7 @@ import java.util.Arrays;
/**
* @author peck7 on 13/05/2020.
*/
@EntityEnum(entity = SaleType.class)
@EntityEnum(entity = SaleType.class, joinAttributes = SaleType.Fields.LABEL)
public enum SaleTypeEnum implements Serializable {
OTHER(4, "Other");
......
......@@ -41,6 +41,8 @@ public interface AggregatedLandingService {
@Transactional(readOnly = true)
List<AggregatedLandingVO> findAll(AggregatedLandingFilterVO filter);
List<AggregatedLandingVO> saveAllByObservedLocationId(AggregatedLandingFilterVO filter, List<AggregatedLandingVO> aggregatedLandings);
List<AggregatedLandingVO> saveAll(AggregatedLandingFilterVO filter, List<AggregatedLandingVO> aggregatedLandings);
void deleteAll(AggregatedLandingFilterVO filter, List<Integer> vesselSnapshotIds);
}
......@@ -30,8 +30,6 @@ import com.google.common.collect.Multimap;
import lombok.extern.slf4j.Slf4j;
import net.sumaris.core.dao.administration.programStrategy.ProgramRepository;
import net.sumaris.core.dao.data.MeasurementDao;
import net.sumaris.core.dao.data.landing.LandingRepository;
import net.sumaris.core.dao.data.observedLocation.ObservedLocationRepository;
import net.sumaris.core.dao.data.operation.OperationGroupRepository;
import net.sumaris.core.dao.data.trip.TripRepository;
import net.sumaris.core.dao.referential.metier.MetierRepository;
......@@ -70,26 +68,26 @@ public class AggregatedLandingServiceImpl implements AggregatedLandingService {
.withChildrenEntities(false)
.build();
private final LandingRepository landingRepository;
private final LandingService landingService;
private final TripRepository tripRepository;
private final ObservedLocationRepository observedLocationRepository;
private final ObservedLocationService observedLocationService;
private final OperationGroupRepository operationGroupRepository;
private final MeasurementDao measurementDao;
private final MetierRepository metierRepository;
private final VesselService vesselService;
private final ProgramRepository programRepository;
public AggregatedLandingServiceImpl(LandingRepository landingRepository,
public AggregatedLandingServiceImpl(LandingService landingService,
TripRepository tripRepository,
ObservedLocationRepository observedLocationRepository,
ObservedLocationService observedLocationService,
OperationGroupRepository operationGroupRepository,
MeasurementDao measurementDao,
MetierRepository metierRepository,
VesselService vesselService,
ProgramRepository programRepository) {
this.landingRepository = landingRepository;
this.landingService = landingService;
this.tripRepository = tripRepository;
this.observedLocationRepository = observedLocationRepository;
this.observedLocationService = observedLocationService;
this.operationGroupRepository = operationGroupRepository;
this.measurementDao = measurementDao;
this.metierRepository = metierRepository;
......@@ -114,7 +112,7 @@ public class AggregatedLandingServiceImpl implements AggregatedLandingService {
List<AggregatedLandingVO> result = new ArrayList<>();
// Get observations
List<ObservedLocationVO> observedLocations = observedLocationRepository.findAll(
List<ObservedLocationVO> observedLocations = observedLocationService.findAll(
ObservedLocationFilterVO.builder()
.programLabel(filter.getProgramLabel())
.locationId(filter.getLocationId())
......@@ -122,7 +120,7 @@ public class AggregatedLandingServiceImpl implements AggregatedLandingService {
.endDate(endDate)
.build(),
0, 1000, null, null,
defaultFetchOption).getContent();
defaultFetchOption);
ConcurrentHashMap<VesselSnapshotVO, Map<Date, List<LandingVO>>> landingsByBateByVessel = new ConcurrentHashMap<>();
observedLocations.parallelStream().forEach(observedLocation -> {
......@@ -135,8 +133,10 @@ public class AggregatedLandingServiceImpl implements AggregatedLandingService {
if (landing.getDateTime() == null)
throw new SumarisTechnicalException(String.format("The landing date is missing for landing id=%s", landing.getId()));
Map<Date, List<LandingVO>> landingsByDate = landingsByBateByVessel.computeIfAbsent(landing.getVesselSnapshot(), x -> new HashMap<>());
landingsByDate.computeIfAbsent(Dates.resetTime(landing.getDateTime()), x -> new ArrayList<>()).add(landing);
landingsByBateByVessel
.computeIfAbsent(landing.getVesselSnapshot(), x -> new HashMap<>())
.computeIfAbsent(Dates.resetTime(landing.getDateTime()), x -> new ArrayList<>())
.add(landing);
});
});
......@@ -146,7 +146,7 @@ public class AggregatedLandingServiceImpl implements AggregatedLandingService {
AggregatedLandingVO aggregatedLanding = new AggregatedLandingVO();
result.add(aggregatedLanding);
aggregatedLanding.setVesselSnapshot(vessel);
aggregatedLanding.setId(vessel.getId());
// aggregatedLanding.setId(vessel.getId());
Map<Date, List<LandingVO>> landingsByDate = landingsByBateByVessel.get(vessel);
// Iterate days
......@@ -205,7 +205,7 @@ public class AggregatedLandingServiceImpl implements AggregatedLandingService {
}
@Override
public List<AggregatedLandingVO> saveAllByObservedLocationId(AggregatedLandingFilterVO filter, List<AggregatedLandingVO> aggregatedLandings) {
public List<AggregatedLandingVO> saveAll(AggregatedLandingFilterVO filter, List<AggregatedLandingVO> aggregatedLandings) {
long start = System.currentTimeMillis();
......@@ -222,23 +222,23 @@ public class AggregatedLandingServiceImpl implements AggregatedLandingService {
.forEach(activity -> Preconditions.checkArgument(activity.getDate().equals(Dates.resetTime(activity.getDate())))));
// Load VesselSnapshot Entity
aggregatedLandings.parallelStream().forEach(aggregatedLanding -> aggregatedLanding.setVesselSnapshot(vesselService.getSnapshotByIdAndDate(aggregatedLanding.getVesselSnapshot().getId(), null)));
aggregatedLandings.parallelStream()
.forEach(aggregatedLanding -> aggregatedLanding.setVesselSnapshot(vesselService.getSnapshotByIdAndDate(aggregatedLanding.getVesselSnapshot().getId(), null)));
// Collect aggregated landings by date
Map<Date, Multimap<Integer, VesselActivityVO>> aggregatedLandingsByDate = new HashMap<>();
aggregatedLandings
.forEach(aggregatedLanding -> aggregatedLanding.getVesselActivities()
.forEach(activity -> {
Multimap<Integer, VesselActivityVO> activityByVessel = aggregatedLandingsByDate.computeIfAbsent(activity.getDate(), x -> ArrayListMultimap.create());
activityByVessel.put(aggregatedLanding.getVesselSnapshot().getId(), activity);
}));
.forEach(activity -> aggregatedLandingsByDate
.computeIfAbsent(activity.getDate(), x -> ArrayListMultimap.create())
.put(aggregatedLanding.getVesselSnapshot().getId(), activity)));
// Get parent observed location
ObservedLocationVO parent = observedLocationRepository.get(filter.getObservedLocationId());
ObservedLocationVO parent = observedLocationService.get(filter.getObservedLocationId());
// Get existing observations
final Date startDate = Dates.resetTime(filter.getStartDate());
final Date endDate = Dates.lastSecondOfTheDay(filter.getEndDate());
List<ObservedLocationVO> observedLocations = observedLocationRepository.findAll(
List<ObservedLocationVO> observedLocations = observedLocationService.findAll(
ObservedLocationFilterVO.builder()
.programLabel(filter.getProgramLabel())
.locationId(filter.getLocationId())
......@@ -246,7 +246,7 @@ public class AggregatedLandingServiceImpl implements AggregatedLandingService {
.endDate(endDate)
.build(),
0, 1000, null, null,
defaultFetchOption).getContent();
defaultFetchOption);
// Create observed location if missing
Set<Date> existingDates = observedLocations.stream().map(ObservedLocationVO::getStartDateTime).map(Dates::resetTime).collect(Collectors.toSet());
......@@ -267,8 +267,7 @@ public class AggregatedLandingServiceImpl implements AggregatedLandingService {
Multimap<Integer, VesselActivityVO> activityByVesselId = aggregatedLandingsByDate.get(date);
ObservedLocationVO observedLocation = observedLocationByDate.remove(date);
ListMultimap<Integer, LandingVO> landingsByVesselId = ArrayListMultimap.create();//Beans.splitByNotUniqueProperty(existingLandings, LandingVO.Fields.VESSEL_SNAPSHOT);
List<LandingVO> existingLandings = getLandings(observedLocation.getId());
existingLandings.forEach(landing -> landingsByVesselId.put(landing.getVesselSnapshot().getId(), landing));
getLandings(observedLocation.getId()).forEach(landing -> landingsByVesselId.put(landing.getVesselSnapshot().getId(), landing));
// Iterate over vessel's activities for the date
activityByVesselId.keySet().forEach(vesselId -> {
......@@ -289,7 +288,7 @@ public class AggregatedLandingServiceImpl implements AggregatedLandingService {
}
if (!landingIdsToRemove.isEmpty()) {
// Delete remaining landings
landingRepository.deleteByIdIn(landingIdsToRemove);
landingIdsToRemove.forEach(landingService::delete);
// Add the observed location to check list
observationIdsToCheck.add(observedLocation.getId());
}
......@@ -320,11 +319,62 @@ public class AggregatedLandingServiceImpl implements AggregatedLandingService {
return aggregatedLandings;
}
@Override
public void deleteAll(AggregatedLandingFilterVO filter, List<Integer> vesselSnapshotIds) {
long start = System.currentTimeMillis();
Preconditions.checkNotNull(filter);
Preconditions.checkNotNull(filter.getObservedLocationId()); // not really used
Preconditions.checkNotNull(filter.getStartDate());
Preconditions.checkNotNull(filter.getEndDate());
Preconditions.checkNotNull(filter.getLocationId());
Preconditions.checkArgument(StringUtils.isNotBlank(filter.getProgramLabel()));
if (CollectionUtils.isEmpty(vesselSnapshotIds)) {
return;
}
// Get existing observations
final Date startDate = Dates.resetTime(filter.getStartDate());
final Date endDate = Dates.lastSecondOfTheDay(filter.getEndDate());
List<ObservedLocationVO> observedLocations = observedLocationService.findAll(
ObservedLocationFilterVO.builder()
.programLabel(filter.getProgramLabel())
.locationId(filter.getLocationId())
.startDate(startDate)
.endDate(endDate)
.build(),
0, 1000, null, null,
defaultFetchOption);
observedLocations.forEach(observedLocation -> vesselSnapshotIds.forEach(vesselSnapshotId -> {
List<LandingVO> landingsToDelete = getLandings(observedLocation.getId(), vesselSnapshotId);
landingsToDelete.stream().map(LandingVO::getId).forEach(landingService::delete);
}));
updateOrDeleteEmptyObservedLocations(Beans.collectIds(observedLocations));
if (log.isDebugEnabled()) {
log.debug(String.format("Deleting done in %s ms", System.currentTimeMillis() - start));
}
}
/* protected methods */
private List<LandingVO> getLandings(int observedLocationId) {
return landingRepository.findAll(
return landingService.findAll(
LandingFilterVO.builder().observedLocationId(observedLocationId).build(),
null,
defaultFetchOption);
}
private List<LandingVO> getLandings(int observedLocationId, int vesselId) {
return landingService.findAll(
LandingFilterVO.builder().observedLocationId(observedLocationId).vesselId(vesselId).build(),
null,
defaultFetchOption);
}
......@@ -334,13 +384,13 @@ public class AggregatedLandingServiceImpl implements AggregatedLandingService {
DataBeans.setDefaultRecorderDepartment(landing, parent.getRecorderDepartment());
DataBeans.setDefaultRecorderPerson(landing, parent.getRecorderPerson());
LandingVO savedLanding = landingRepository.save(landing);
LandingVO savedLanding = landingService.save(landing);
if (landing.getMeasurementValues() != null)
measurementDao.saveLandingMeasurementsMap(savedLanding.getId(), landing.getMeasurementValues());
}
private void updateOrDeleteEmptyObservedLocations(Set<Integer> observationIds) {
private void updateOrDeleteEmptyObservedLocations(Collection<Integer> observationIds) {
if (CollectionUtils.isEmpty(observationIds)) return;
......@@ -351,7 +401,7 @@ public class AggregatedLandingServiceImpl implements AggregatedLandingService {
if (CollectionUtils.isEmpty(landings)) {
// No landing, delete observation
observedLocationRepository.deleteById(observationId);
observedLocationService.delete(observationId);
if (log.isDebugEnabled()) {
log.debug(String.format("Delete observation (id=%s) without landing", observationId));
}
......@@ -364,8 +414,8 @@ public class AggregatedLandingServiceImpl implements AggregatedLandingService {
} else {
// Just Update
ObservedLocationVO observedLocation = observedLocationRepository.get(observationId);
observedLocationRepository.save(observedLocation);
ObservedLocationVO observedLocation = observedLocationService.get(observationId);
observedLocationService.save(observedLocation, null);
}
});
}
......@@ -377,10 +427,10 @@ public class AggregatedLandingServiceImpl implements AggregatedLandingService {
landings = getLandings(observedLocationId);
}
// Delete landings
landingRepository.deleteByIdIn(Beans.collectIds(landings));
Beans.collectIds(landings).forEach(landingService::delete);
// Delete observed location
observedLocationRepository.deleteById(observedLocationId);
observedLocationService.delete(observedLocationId);
if (log.isDebugEnabled()) {
log.debug(String.format("Delete observation (id=%s) with empty landings", observedLocationId));
}
......@@ -399,7 +449,7 @@ public class AggregatedLandingServiceImpl implements AggregatedLandingService {
log.debug(String.format("Create observed location from %s to %s, program: %s, location: %s",
observedLocation.getStartDateTime(), observedLocation.getEndDateTime(), observedLocation.getProgram().getLabel(), observedLocation.getLocation().getLabel()));
}
return observedLocationRepository.save(observedLocation);
return observedLocationService.save(observedLocation, null);
}
private boolean createOrUpdateLandings(ObservedLocationVO observedLocation, List<LandingVO> landings, List<Integer> landingIdsToRemove, Integer vesselId, VesselActivityVO activity) {
......
......@@ -1065,12 +1065,19 @@ public class DataGraphQLService {
@GraphQLArgument(name = "aggregatedLandings") List<AggregatedLandingVO> aggregatedLandings,
@GraphQLEnvironment ResolutionEnvironment env
) {
List<AggregatedLandingVO> result = aggregatedLandingService.saveAllByObservedLocationId(filter, aggregatedLandings);
List<AggregatedLandingVO> result = aggregatedLandingService.saveAll(filter, aggregatedLandings);
fillVesselSnapshot(result, GraphQLUtils.fields(env));
return result;
}
@GraphQLMutation(name = "deleteAggregatedLandings", description = "Delete many aggregated landings")
public void deleteAggregatedLandings(
@GraphQLArgument(name = "filter") AggregatedLandingFilterVO filter,
@GraphQLArgument(name = "vesselSnapshotIds") List<Integer> vesselSnapshotIds
) {
aggregatedLandingService.deleteAll(filter, vesselSnapshotIds);
}
/* -- Measurements -- */
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment