Commit 8c5a054d authored by PECQUOT's avatar PECQUOT
Browse files

Merge branch 'feature/obsdeb' into develop

# Conflicts:
#	sumaris-core/src/main/java/net/sumaris/core/service/data/AggregatedLandingServiceImpl.java
#	sumaris-core/src/main/java/net/sumaris/core/service/data/LandingServiceImpl.java
parents 6f9d502a 0576f5fe
......@@ -922,6 +922,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());
}
......
......@@ -159,6 +159,7 @@ public abstract class SumarisJpaRepositoryImpl<E extends IEntity<ID>, ID extends
Preconditions.checkNotNull(vo);
E entity;
if (vo.getId() != null) {
// getSessionFactory().getCache().evict(getDomainClass(), vo.getId());
entity = getById(vo.getId());
} else {
entity = createEntity();
......
......@@ -37,6 +37,7 @@ import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.util.ObjectUtils;
import java.beans.PropertyDescriptor;
import java.io.Serializable;
......@@ -466,18 +467,7 @@ public class Beans {
return Arrays.stream(bean.getClass().getDeclaredFields())
.filter(field -> !ArrayUtils.contains(ignoredAttributes, field.getName()))
.allMatch(field -> {
Object property = getProperty(bean, field.getName());
if (property == null)
return true;
if (property instanceof Collection)
return CollectionUtils.isEmpty((Collection<?>) property);
if (property.getClass().isArray())
return ArrayUtils.isEmpty((Object[]) property);
// if (!property.getClass().isPrimitive())
// log.warn(String.format("Unable to determinate if %s is null", property));
return false;
});
.allMatch(field -> ObjectUtils.isEmpty((Object) getProperty(bean, field.getName())));
}
/**
......
......@@ -95,10 +95,10 @@ public interface MeasurementDao {
Map<Integer, String> saveSaleMeasurementsMap(int saleId, Map<Integer, String> sources);
// Expected Sale
List<MeasurementVO> getExpectedSaleMeasurements(int saleId);
Map<Integer, String> getExpectedSaleMeasurementsMap(int saleId);
List<MeasurementVO> saveExpectedSaleMeasurements(int saleId, List<MeasurementVO> sources);
Map<Integer, String> saveExpectedSaleMeasurementsMap(int saleId, Map<Integer, String> sources);
List<MeasurementVO> getExpectedSaleMeasurements(int expectedSaleId);
Map<Integer, String> getExpectedSaleMeasurementsMap(int expectedSaleId);
List<MeasurementVO> saveExpectedSaleMeasurements(int expectedSaleId, List<MeasurementVO> sources);
Map<Integer, String> saveExpectedSaleMeasurementsMap(int expectedSaleId, Map<Integer, String> sources);
// Landing
List<MeasurementVO> saveLandingMeasurements(int landingId, List<MeasurementVO> sources);
......
......@@ -112,6 +112,9 @@ public class MeasurementDaoImpl extends HibernateDaoSupport implements Measureme
// Sale
result.put(SaleMeasurement.class, BeanUtils.getPropertyDescriptor(SaleMeasurement.class, SaleMeasurement.Fields.SALE));
// ExpectedSale
result.put(SaleMeasurement.class, BeanUtils.getPropertyDescriptor(SaleMeasurement.class, SaleMeasurement.Fields.EXPECTED_SALE));
// Landing
result.put(LandingMeasurement.class, BeanUtils.getPropertyDescriptor(LandingMeasurement.class, LandingMeasurement.Fields.LANDING));
......@@ -367,20 +370,20 @@ public class MeasurementDaoImpl extends HibernateDaoSupport implements Measureme
}
@Override
public List<MeasurementVO> getExpectedSaleMeasurements(int saleId) {
public List<MeasurementVO> getExpectedSaleMeasurements(int expectedSaleId) {
return getMeasurementsByParentId(SaleMeasurement.class,
MeasurementVO.class,
SaleMeasurement.Fields.EXPECTED_SALE,
saleId,
expectedSaleId,
SaleMeasurement.Fields.ID
);
}
@Override
public Map<Integer, String> getExpectedSaleMeasurementsMap(int saleId) {
public Map<Integer, String> getExpectedSaleMeasurementsMap(int expectedSaleId) {
return getMeasurementsMapByParentId(SaleMeasurement.class,
SaleMeasurement.Fields.EXPECTED_SALE,
saleId,
expectedSaleId,
null
);
}
......@@ -494,14 +497,14 @@ public class MeasurementDaoImpl extends HibernateDaoSupport implements Measureme
}
@Override
public List<MeasurementVO> saveExpectedSaleMeasurements(int saleId, List<MeasurementVO> sources) {
ExpectedSale parent = getById(ExpectedSale.class, saleId);
public List<MeasurementVO> saveExpectedSaleMeasurements(int expectedSaleId, List<MeasurementVO> sources) {
ExpectedSale parent = getById(ExpectedSale.class, expectedSaleId);
return saveMeasurements(SaleMeasurement.class, sources, parent.getMeasurements(), parent);
}
@Override
public Map<Integer, String> saveExpectedSaleMeasurementsMap(int saleId, Map<Integer, String> sources) {
ExpectedSale parent = getById(ExpectedSale.class, saleId);
public Map<Integer, String> saveExpectedSaleMeasurementsMap(int expectedSaleId, Map<Integer, String> sources) {
ExpectedSale parent = getById(ExpectedSale.class, expectedSaleId);
return saveMeasurementsMap(SaleMeasurement.class, sources, parent.getMeasurements(), parent);
}
......
......@@ -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()));
......
......@@ -128,7 +128,6 @@ public class OperationGroupRepositoryImpl
// Physical gear
if (source.getPhysicalGear() != null) {
target.setPhysicalGearId(source.getPhysicalGear().getId());
target.setPhysicalGear(physicalGearRepository.toVO(source.getPhysicalGear(), DataFetchOptions.builder().withRecorderDepartment(false).build()));
}
// Metier
......@@ -214,14 +213,11 @@ public class OperationGroupRepositoryImpl
}
// Physical gear
if (copyIfNull || source.getPhysicalGearId() != null || (source.getPhysicalGear() != null && source.getPhysicalGear().getId() != null)) {
Integer physicalGearId = source.getPhysicalGearId() != null ? source.getPhysicalGearId() : (
source.getPhysicalGear() != null ? source.getPhysicalGear().getId() : null
);
if (physicalGearId == null) {
if (copyIfNull || source.getPhysicalGearId() != null) {
if (source.getPhysicalGearId() == null) {
target.setPhysicalGear(null);
} else {
target.setPhysicalGear(getReference(PhysicalGear.class, physicalGearId));
target.setPhysicalGear(getReference(PhysicalGear.class, source.getPhysicalGearId()));
}
}
......
......@@ -132,7 +132,7 @@ public class TripRepositoryImpl
landing.setTrip(savedEntity);
landingRepository.save(landing);
}
if (landing.getObservedLocation() == null) {
if (landing.getObservedLocation() != null) {
observedLocationId = landing.getObservedLocation().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);
}
......@@ -28,7 +28,6 @@ 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;
......@@ -69,26 +68,29 @@ public class AggregatedLandingServiceImpl implements AggregatedLandingService {
.withChildrenEntities(false)
.build();
private final LandingService landingService;
private final LandingRepository landingRepository;
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,
LandingRepository landingRepository,
TripRepository tripRepository,
ObservedLocationRepository observedLocationRepository,
ObservedLocationService observedLocationService,
OperationGroupRepository operationGroupRepository,
MeasurementDao measurementDao,
MetierRepository metierRepository,
VesselService vesselService,
ProgramRepository programRepository) {
this.landingService = landingService;
this.landingRepository = landingRepository;
this.tripRepository = tripRepository;
this.observedLocationRepository = observedLocationRepository;
this.observedLocationService = observedLocationService;
this.operationGroupRepository = operationGroupRepository;
this.measurementDao = measurementDao;
this.metierRepository = metierRepository;
......@@ -113,7 +115,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())
......@@ -134,8 +136,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);
});
});
......@@ -145,7 +149,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
......@@ -204,7 +208,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();
......@@ -221,32 +225,30 @@ 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())
.startDate(startDate)
.endDate(endDate)
.build(),
Page.builder()
.offset(0).size(1000)
.build(),
0, 1000, null, null,
defaultFetchOption);
// Create observed location if missing
......@@ -268,8 +270,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 -> {
......@@ -296,6 +297,9 @@ public class AggregatedLandingServiceImpl implements AggregatedLandingService {
// Delete landing
landingRepository.deleteByIds(landingIdsToRemove);
// TODO LP: or use this
//landingIdsToRemove.forEach(landingService::delete);
// Add the observed location to check list
observationIdsToCheck.add(observedLocation.getId());
}
......@@ -326,11 +330,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);
}
......@@ -340,13 +395,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;
......@@ -357,7 +412,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));
}
......@@ -370,8 +425,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);
}
});
}
......@@ -385,8 +440,11 @@ public class AggregatedLandingServiceImpl implements AggregatedLandingService {
// Delete landings
landingRepository.deleteByIds(Beans.collectIds(landings));
// TODO LP or use this:
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));
}
......@@ -405,7 +463,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) {
......@@ -630,7 +688,6 @@ public class AggregatedLandingServiceImpl implements AggregatedLandingService {
PhysicalGearVO physicalGear = new PhysicalGearVO();
physicalGear.setGear(metier.getGear());
physicalGear.setRankOrder(rankOrder);
operationGroup.setPhysicalGear(physicalGear);
return operationGroup;
}
......
......@@ -37,7 +37,10 @@ import net.sumaris.core.event.config.ConfigurationUpdatedEvent;
import net.sumaris.core.event.entity.EntityDeleteEvent;
import net.sumaris.core.event.entity.EntityInsertEvent;
import net.sumaris.core.event.entity.EntityUpdateEvent;
import net.sumaris.core.model.data.*;
import net.sumaris.core.model.data.IMeasurementEntity;
import net.sumaris.core.model.data.Landing;
import net.sumaris.core.model.data.LandingMeasurement;
import net.sumaris.core.model.data.SurveyMeasurement;
import net.sumaris.core.model.referential.pmfm.MatrixEnum;
import net.sumaris.core.service.data.vessel.VesselService;
import net.sumaris.core.service.referential.pmfm.PmfmService;
......
......@@ -61,10 +61,7 @@ import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Service;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;
......@@ -239,6 +236,11 @@ public class TripServiceImpl implements TripService {
operationGroupService.updateUndefinedOperationDates(source.getId(), source.getDepartureDateTime(), source.getReturnDateTime());
}
// Keep source parent information
finalOptions.setLandingId(source.getLandingId());
finalOptions.setObservedLocationId(source.getObservedLocationId());
// Save
TripVO result = tripRepository.save(source);
......@@ -490,28 +492,20 @@ public class TripServiceImpl implements TripService {
}
protected void fillOperationGroupPhysicalGears(List<OperationGroupVO> sources, List<PhysicalGearVO> physicalGears) {
Map<Integer, PhysicalGearVO> physicalGearsById = Beans.splitById(physicalGears);
Multimap<Integer, PhysicalGearVO> physicalGearsByGearId = Beans.splitByNotUniqueProperty(physicalGears, PhysicalGearVO.Fields.GEAR + "." + ReferentialVO.Fields.ID);
// Affect physical gears from savedTrip.getGears, because new oG can have a physicalGear with null id
sources.forEach(source -> {
PhysicalGearVO physicalGear = source.getPhysicalGear();
if (physicalGear == null) {
if (source.getPhysicalGearId() != null) {
physicalGear = physicalGearsById.get(source.getPhysicalGearId());
}
} else if (physicalGear.getId() == null && physicalGear.getGear() != null && physicalGear.getGear().getId() != null) {
// case of new operation group with unsaved physical gear
// try to find it with trip's gears
Collection<PhysicalGearVO> matches = physicalGearsByGearId.get(physicalGear.getGear().getId());
if (CollectionUtils.isNotEmpty(matches)) {
physicalGear = matches.iterator().next();
}
if (source.getPhysicalGearId() == null && source.getMetier() != null && source.getMetier().getGear() != null) {
Collection<PhysicalGearVO> matches = Beans.getList(physicalGearsByGearId.get(source.getMetier().getGear().getId()));
matches.stream()
.filter(physicalGearVO -> Objects.equals(physicalGearVO.getRankOrder(), source.getRankOrderOnPeriod()))
.findFirst()
.ifPresent(physicalGearVO -> source.setPhysicalGearId(physicalGearVO.getId()));
}
// Assert PhysicalGear
Preconditions.checkNotNull(physicalGear, "OperationGroup has no valid PhysicalGear");
source.setPhysicalGear(physicalGear);
Preconditions.checkNotNull(source.getPhysicalGearId(), "OperationGroup has no valid PhysicalGearId");
});
}
......@@ -519,12 +513,11 @@ public class TripServiceImpl implements TripService {
if (options.getWithLanding()) {
// Landing
Preconditions.checkNotNull(trip.getLanding(), "The Landing object must be created first");
boolean createLanding = false;
if (trip.getLanding().getId() != null) {
Integer landingId = Optional.ofNullable(trip.getLandingId()).orElse(options.getLandingId());
if (landingId != null) {
// update update_date on landing
LandingVO landing = landingRepository.get(trip.getLanding().getId());
LandingVO landing = landingRepository.get(landingId);
if (landing.getTripId() == null) {
landing.setTripId(trip.getId());
......@@ -533,22 +526,19 @@ public class TripServiceImpl implements TripService {
landing.setObservers(Beans.getSet(trip.getObservers()));
landingRepository.save(landing);
} else {
// a landing have to be created
createLanding = true;
trip.setLandingId(landing.getId());
}