Commit 217962f5 authored by LAVENIER's avatar LAVENIER
Browse files

[fix] Vessel: add default filter value (program and date)

parent 66b652cc
......@@ -22,6 +22,7 @@ package net.sumaris.core.dao.data.vessel;
* #L%
*/
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import net.sumaris.core.dao.data.DataRepositoryImpl;
import net.sumaris.core.dao.referential.ReferentialDao;
......@@ -73,7 +74,9 @@ public class VesselSnapshotRepositoryImpl
}
@Override
public Page<VesselSnapshotVO> findAll(VesselFilterVO filter, Pageable pageable, VesselFetchOptions fetchOptions) {
public Page<VesselSnapshotVO> findAll(@NonNull VesselFilterVO filter,
@NonNull Pageable pageable,
@NonNull VesselFetchOptions fetchOptions) {
CriteriaBuilder builder = this.getEntityManager().getCriteriaBuilder();
CriteriaQuery<Tuple> criteriaQuery = builder.createTupleQuery();
......@@ -115,8 +118,9 @@ public class VesselSnapshotRepositoryImpl
}
@Override
public List<VesselSnapshotVO> findAll(VesselFilterVO filter, net.sumaris.core.dao.technical.Page page,
VesselFetchOptions fetchOptions) {
public List<VesselSnapshotVO> findAll(@NonNull VesselFilterVO filter,
@NonNull net.sumaris.core.dao.technical.Page page,
@NonNull VesselFetchOptions fetchOptions) {
CriteriaBuilder builder = this.getEntityManager().getCriteriaBuilder();
CriteriaQuery<Tuple> criteriaQuery = builder.createTupleQuery();
......@@ -153,7 +157,7 @@ public class VesselSnapshotRepositoryImpl
}
@Override
public Specification<VesselFeatures> toSpecification(VesselFilterVO filter, VesselFetchOptions fetchOptions) {
public Specification<VesselFeatures> toSpecification(@NonNull VesselFilterVO filter, VesselFetchOptions fetchOptions) {
return super.toSpecification(filter, fetchOptions)
// IDs
.and(id(filter.getVesselFeaturesId()))
......
......@@ -24,6 +24,7 @@ package net.sumaris.core.service.data.vessel;
import com.google.common.base.Preconditions;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import net.sumaris.core.dao.data.MeasurementDao;
import net.sumaris.core.dao.data.vessel.VesselFeaturesRepository;
......@@ -68,38 +69,21 @@ public class VesselServiceImpl implements VesselService {
protected MeasurementDao measurementDao;
@Override
public List<VesselSnapshotVO> findAllSnapshots(VesselFilterVO filter,
public List<VesselSnapshotVO> findAllSnapshots(@NonNull VesselFilterVO filter,
net.sumaris.core.dao.technical.Page page,
VesselFetchOptions fetchOptions) {
filter = VesselFilterVO.nullToEmpty(filter);
// If expected a date: use today
if (filter.getStartDate() == null && filter.getEndDate() == null) {
filter.setDate(new Date());
}
return vesselSnapshotRepository.findAll(filter, page, fetchOptions);
}
@Override
public Long countSnapshotsByFilter(VesselFilterVO filter) {
filter = VesselFilterVO.nullToEmpty(filter);
// If expected a date: use today
if (filter.getStartDate() == null && filter.getEndDate() == null) {
filter.setDate(new Date());
}
public Long countSnapshotsByFilter(@NonNull VesselFilterVO filter) {
return vesselSnapshotRepository.count(filter);
}
@Override
public List<VesselVO> findAll(VesselFilterVO filter,
net.sumaris.core.dao.technical.Page page,
VesselFetchOptions fetchOptions) {
filter = VesselFilterVO.nullToEmpty(filter);
public List<VesselVO> findAll(@NonNull VesselFilterVO filter,
@NonNull net.sumaris.core.dao.technical.Page page,
@NonNull VesselFetchOptions fetchOptions) {
// If expected a date: use today
boolean needDate = fetchOptions.isWithVesselFeatures() || fetchOptions.isWithVesselRegistrationPeriod();
......@@ -114,8 +98,8 @@ public class VesselServiceImpl implements VesselService {
}
@Override
public Long countByFilter(VesselFilterVO filter) {
return vesselRepository.count(VesselFilterVO.nullToEmpty(filter));
public Long countByFilter(@NonNull VesselFilterVO filter) {
return vesselRepository.count(filter);
}
@Override
......
......@@ -50,7 +50,7 @@ public class VesselFetchOptions implements IDataFetchOptions {
private boolean withVesselRegistrationPeriod = true;
@Builder.Default
private boolean withBasePortLocation = true;
private boolean withBasePortLocation = false;
@Override
public boolean isWithObservers() {
......
......@@ -53,6 +53,7 @@ import org.springframework.security.access.AccessDeniedException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.Date;
import java.util.List;
import java.util.Set;
......@@ -89,7 +90,9 @@ public class VesselGraphQLService {
sort = sort.replaceFirst(VesselVO.Fields.STATUS_ID, StringUtils.doting(Vessel.Fields.STATUS, Status.Fields.ID));
}
return vesselService.findAll(restrictVesselFilter(filter),
filter = fillVesselFilterDefaults(filter);
return vesselService.findAll(filter,
Page.builder()
.offset(offset)
.size(size)
......@@ -110,15 +113,22 @@ public class VesselGraphQLService {
@GraphQLArgument(name = "sortDirection", defaultValue = "asc") String direction,
@GraphQLEnvironment ResolutionEnvironment env
) {
// Add restriction to filter (e.g. program=SIH)
// and fill (or fix) dates
filter = fillVesselFilterDefaults(filter);
// Compute fetch options
VesselFetchOptions fetchOptions = getSnapshotFetchOptions(GraphQLUtils.fields(env));
return vesselService.findAllSnapshots(
restrictVesselFilter(filter),
filter,
Page.builder()
.offset(offset)
.size(size)
.sortBy(sort)
.sortDirection(SortDirection.fromString(direction))
.build(),
getSnapshotFetchOptions(GraphQLUtils.fields(env))
fetchOptions
);
}
......@@ -126,14 +136,22 @@ public class VesselGraphQLService {
@Transactional(readOnly = true)
@IsUser
public long countVesselSnapshots(@GraphQLArgument(name = "filter") VesselFilterVO filter) {
return vesselService.countSnapshotsByFilter(restrictVesselFilter(filter));
// Add restriction to filter (e.g. program=SIH)
// and fill (or fix) dates
filter = fillVesselFilterDefaults(filter);
return vesselService.countSnapshotsByFilter(filter);
}
@GraphQLQuery(name = "vesselsCount", description = "Get total vessels count")
@Transactional(readOnly = true)
@IsUser
public long countVessels(@GraphQLArgument(name = "filter") VesselFilterVO filter) {
return vesselService.countByFilter(restrictVesselFilter(filter));
// Add restriction to filter (e.g. program=SIH)
// and fill (or fix) dates
filter = fillVesselFilterDefaults(filter);
return vesselService.countByFilter(filter);
}
@GraphQLQuery(name = "vessel", description = "Get a vessel")
......@@ -284,15 +302,32 @@ public class VesselGraphQLService {
}
/**
* Restrict to vessel, depending of user access rights
* If need, restrict vessel program (to SIH), and dates (to today)
* @param filter
*/
protected VesselFilterVO restrictVesselFilter(VesselFilterVO filter) {
protected VesselFilterVO fillVesselFilterDefaults(VesselFilterVO filter) {
filter = VesselFilterVO.nullToEmpty(filter);
// Filter on SIH program, when empty or not an admin
if (StringUtils.isBlank(filter.getProgramLabel()) || !authService.isAdmin()) {
filter.setProgramLabel(ProgramEnum.SIH.getLabel());
}
// If expected a date: use today (at 0h0min)
if (filter.getStartDate() == null && filter.getEndDate() == null) {
filter.setDate(Dates.resetTime(new Date()));
}
// Reset hour in date (0h0min - to limit cache key changes)
else {
if (filter.getStartDate() != null) {
filter.setStartDate(Dates.resetTime(filter.getStartDate()));
}
if (filter.getEndDate() != null) {
filter.setEndDate(Dates.resetTime(filter.getEndDate()));
}
}
return filter;
}
......
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