Commit 4627ae40 authored by LAVENIER's avatar LAVENIER
Browse files

[fix] Add a vessel filter (where clause) on program

parent 8ab1aa08
...@@ -316,75 +316,84 @@ public class VesselDaoImpl extends HibernateDaoSupport implements VesselDao { ...@@ -316,75 +316,84 @@ public class VesselDaoImpl extends HibernateDaoSupport implements VesselDao {
/* -- protected methods -- */ /* -- protected methods -- */
private <R> TypedQuery<R> createVesselQuery(CriteriaBuilder builder, CriteriaQuery<R> query, private <R> TypedQuery<R> createVesselQuery(CriteriaBuilder cb, CriteriaQuery<R> query,
Root<Vessel> vesselRoot, Root<Vessel> vesselRoot,
Join<Vessel, VesselFeatures> featuresJoin, Join<Vessel, VesselFeatures> featuresJoin,
Join<Vessel, VesselRegistrationPeriod> vrpJoin, Join<Vessel, VesselRegistrationPeriod> vrpJoin,
VesselFilterVO filter) { VesselFilterVO filter) {
if (filter != null) { if (filter != null) {
Join<Vessel, Program> programJoin = vesselRoot.join(Vessel.Fields.PROGRAM, JoinType.INNER);
// Apply vessel Filter // Apply vessel Filter
ParameterExpression<Date> dateParam = builder.parameter(Date.class); ParameterExpression<String> programParam = cb.parameter(String.class);
ParameterExpression<Integer> vesselIdParam = builder.parameter(Integer.class); ParameterExpression<Date> dateParam = cb.parameter(Date.class);
ParameterExpression<Integer> vesselFeaturesIdParam = builder.parameter(Integer.class); ParameterExpression<Integer> vesselIdParam = cb.parameter(Integer.class);
ParameterExpression<String> searchNameParam = builder.parameter(String.class); ParameterExpression<Integer> vesselFeaturesIdParam = cb.parameter(Integer.class);
ParameterExpression<String> searchExteriorMarkingParam = builder.parameter(String.class); ParameterExpression<String> searchNameParam = cb.parameter(String.class);
ParameterExpression<String> searchRegistrationCodeParam = builder.parameter(String.class); ParameterExpression<String> searchExteriorMarkingParam = cb.parameter(String.class);
ParameterExpression<Boolean> hasStatusIdsParam = builder.parameter(Boolean.class); ParameterExpression<String> searchRegistrationCodeParam = cb.parameter(String.class);
ParameterExpression<Collection> statusIdsParam = builder.parameter(Collection.class); ParameterExpression<Boolean> hasStatusIdsParam = cb.parameter(Boolean.class);
ParameterExpression<Collection> statusIdsParam = cb.parameter(Collection.class);
query.where(builder.and(
query.where(cb.and(
// Program
cb.or(
cb.isNull(programParam),
cb.equal(programJoin.get(Program.Fields.LABEL), programParam)
),
// Filter: date // Filter: date
builder.or( cb.or(
builder.and( cb.and(
// if no date in filter, will return only active period // if no date in filter, will return only active period
builder.isNull(dateParam.as(String.class)), cb.isNull(dateParam.as(String.class)),
builder.isNull(featuresJoin.get(VesselFeatures.Fields.END_DATE)), cb.isNull(featuresJoin.get(VesselFeatures.Fields.END_DATE)),
builder.isNull(vrpJoin.get(VesselRegistrationPeriod.Fields.END_DATE)) cb.isNull(vrpJoin.get(VesselRegistrationPeriod.Fields.END_DATE))
), ),
builder.and( cb.and(
builder.isNotNull(dateParam.as(String.class)), cb.isNotNull(dateParam.as(String.class)),
builder.and( cb.and(
builder.or( cb.or(
builder.isNull(featuresJoin.get(VesselFeatures.Fields.END_DATE)), cb.isNull(featuresJoin.get(VesselFeatures.Fields.END_DATE)),
builder.greaterThan(featuresJoin.get(VesselFeatures.Fields.END_DATE), dateParam) cb.greaterThan(featuresJoin.get(VesselFeatures.Fields.END_DATE), dateParam)
), ),
builder.lessThan(featuresJoin.get(VesselFeatures.Fields.START_DATE), dateParam) cb.lessThan(featuresJoin.get(VesselFeatures.Fields.START_DATE), dateParam)
), ),
builder.and( cb.and(
builder.or( cb.or(
builder.isNull(vrpJoin.get(VesselRegistrationPeriod.Fields.END_DATE)), cb.isNull(vrpJoin.get(VesselRegistrationPeriod.Fields.END_DATE)),
builder.greaterThan(vrpJoin.get(VesselRegistrationPeriod.Fields.END_DATE), dateParam) cb.greaterThan(vrpJoin.get(VesselRegistrationPeriod.Fields.END_DATE), dateParam)
), ),
builder.lessThan(vrpJoin.get(VesselRegistrationPeriod.Fields.START_DATE), dateParam) cb.lessThan(vrpJoin.get(VesselRegistrationPeriod.Fields.START_DATE), dateParam)
) )
) )
), ),
// Filter: vessel features id // Filter: vessel features id
builder.or( cb.or(
builder.isNull(vesselFeaturesIdParam), cb.isNull(vesselFeaturesIdParam),
builder.equal(featuresJoin.get(VesselFeatures.Fields.ID), vesselFeaturesIdParam) cb.equal(featuresJoin.get(VesselFeatures.Fields.ID), vesselFeaturesIdParam)
), ),
// Filter: vessel id // Filter: vessel id
builder.or( cb.or(
builder.isNull(vesselIdParam), cb.isNull(vesselIdParam),
builder.equal(vesselRoot.get(Vessel.Fields.ID), vesselIdParam)) cb.equal(vesselRoot.get(Vessel.Fields.ID), vesselIdParam))
), ),
// Filter: search text (on exterior marking OR id) // Filter: search text (on exterior marking OR id)
builder.or( cb.or(
builder.isNull(searchNameParam), cb.isNull(searchNameParam),
builder.like(builder.lower(featuresJoin.get(VesselFeatures.Fields.NAME)), searchNameParam), cb.like(cb.lower(featuresJoin.get(VesselFeatures.Fields.NAME)), searchNameParam),
builder.like(builder.lower(featuresJoin.get(VesselFeatures.Fields.EXTERIOR_MARKING)), searchExteriorMarkingParam), cb.like(cb.lower(featuresJoin.get(VesselFeatures.Fields.EXTERIOR_MARKING)), searchExteriorMarkingParam),
builder.like(builder.lower(vrpJoin.get(VesselRegistrationPeriod.Fields.REGISTRATION_CODE)), searchRegistrationCodeParam) cb.like(cb.lower(vrpJoin.get(VesselRegistrationPeriod.Fields.REGISTRATION_CODE)), searchRegistrationCodeParam)
), ),
// Status // Status
builder.or( cb.or(
builder.isFalse(hasStatusIdsParam), cb.isFalse(hasStatusIdsParam),
builder.in(vesselRoot.get(Vessel.Fields.STATUS).get(Status.Fields.ID)).value(statusIdsParam) cb.in(vesselRoot.get(Vessel.Fields.STATUS).get(Status.Fields.ID)).value(statusIdsParam)
) )
); );
...@@ -397,6 +406,7 @@ public class VesselDaoImpl extends HibernateDaoSupport implements VesselDao { ...@@ -397,6 +406,7 @@ public class VesselDaoImpl extends HibernateDaoSupport implements VesselDao {
: filter.getStatusIds(); : filter.getStatusIds();
return getEntityManager().createQuery(query) return getEntityManager().createQuery(query)
.setParameter(programParam, filter.getProgramLabel())
.setParameter(dateParam, filter.getDate()) .setParameter(dateParam, filter.getDate())
.setParameter(vesselFeaturesIdParam, filter.getVesselFeaturesId()) .setParameter(vesselFeaturesIdParam, filter.getVesselFeaturesId())
.setParameter(vesselIdParam, filter.getVesselId()) .setParameter(vesselIdParam, filter.getVesselId())
...@@ -410,9 +420,9 @@ public class VesselDaoImpl extends HibernateDaoSupport implements VesselDao { ...@@ -410,9 +420,9 @@ public class VesselDaoImpl extends HibernateDaoSupport implements VesselDao {
// if no date in filter, will return only active period // if no date in filter, will return only active period
query.where( query.where(
builder.and( cb.and(
builder.isNull(featuresJoin.get(VesselFeatures.Fields.END_DATE)), cb.isNull(featuresJoin.get(VesselFeatures.Fields.END_DATE)),
builder.isNull(vrpJoin.get(VesselRegistrationPeriod.Fields.END_DATE)) cb.isNull(vrpJoin.get(VesselRegistrationPeriod.Fields.END_DATE))
) )
); );
......
...@@ -31,6 +31,7 @@ import net.sumaris.core.dao.referential.location.LocationRepository; ...@@ -31,6 +31,7 @@ import net.sumaris.core.dao.referential.location.LocationRepository;
import net.sumaris.core.dao.technical.Daos; import net.sumaris.core.dao.technical.Daos;
import net.sumaris.core.dao.technical.SortDirection; import net.sumaris.core.dao.technical.SortDirection;
import net.sumaris.core.dao.technical.hibernate.HibernateDaoSupport; import net.sumaris.core.dao.technical.hibernate.HibernateDaoSupport;
import net.sumaris.core.model.administration.programStrategy.Program;
import net.sumaris.core.model.data.Vessel; import net.sumaris.core.model.data.Vessel;
import net.sumaris.core.model.data.VesselFeatures; import net.sumaris.core.model.data.VesselFeatures;
import net.sumaris.core.model.data.VesselRegistrationPeriod; import net.sumaris.core.model.data.VesselRegistrationPeriod;
...@@ -99,6 +100,7 @@ public class VesselSnapshotDaoImpl extends HibernateDaoSupport implements Vessel ...@@ -99,6 +100,7 @@ public class VesselSnapshotDaoImpl extends HibernateDaoSupport implements Vessel
Root<VesselFeatures> root = query.from(VesselFeatures.class); Root<VesselFeatures> root = query.from(VesselFeatures.class);
Join<VesselFeatures, Vessel> vesselJoin = root.join(VesselFeatures.Fields.VESSEL, JoinType.INNER); Join<VesselFeatures, Vessel> vesselJoin = root.join(VesselFeatures.Fields.VESSEL, JoinType.INNER);
Join<Vessel, Program> programJoin = vesselJoin.join(Vessel.Fields.PROGRAM, JoinType.INNER);
Join<Vessel, VesselRegistrationPeriod> vrpJoin = vesselJoin.join(Vessel.Fields.VESSEL_REGISTRATION_PERIODS, JoinType.LEFT); Join<Vessel, VesselRegistrationPeriod> vrpJoin = vesselJoin.join(Vessel.Fields.VESSEL_REGISTRATION_PERIODS, JoinType.LEFT);
query.multiselect(root, vrpJoin); query.multiselect(root, vrpJoin);
...@@ -119,6 +121,7 @@ public class VesselSnapshotDaoImpl extends HibernateDaoSupport implements Vessel ...@@ -119,6 +121,7 @@ public class VesselSnapshotDaoImpl extends HibernateDaoSupport implements Vessel
: filter.getStatusIds(); : filter.getStatusIds();
// Apply vessel Filter // Apply vessel Filter
ParameterExpression<String> programParam = cb.parameter(String.class);
ParameterExpression<Date> dateParam = cb.parameter(Date.class); ParameterExpression<Date> dateParam = cb.parameter(Date.class);
ParameterExpression<Integer> vesselIdParam = cb.parameter(Integer.class); ParameterExpression<Integer> vesselIdParam = cb.parameter(Integer.class);
ParameterExpression<Integer> vesselFeaturesIdParam = cb.parameter(Integer.class); ParameterExpression<Integer> vesselFeaturesIdParam = cb.parameter(Integer.class);
...@@ -129,6 +132,12 @@ public class VesselSnapshotDaoImpl extends HibernateDaoSupport implements Vessel ...@@ -129,6 +132,12 @@ public class VesselSnapshotDaoImpl extends HibernateDaoSupport implements Vessel
ParameterExpression<Collection> statusIdsParam = cb.parameter(Collection.class); ParameterExpression<Collection> statusIdsParam = cb.parameter(Collection.class);
query.where(cb.and( query.where(cb.and(
// Program
cb.or(
cb.isNull(programParam),
cb.equal(programJoin.get(Program.Fields.LABEL), programParam)
),
// Filter: date // Filter: date
cb.or( cb.or(
cb.and( cb.and(
...@@ -189,6 +198,7 @@ public class VesselSnapshotDaoImpl extends HibernateDaoSupport implements Vessel ...@@ -189,6 +198,7 @@ public class VesselSnapshotDaoImpl extends HibernateDaoSupport implements Vessel
String searchTextAnyMatch = StringUtils.isNotBlank(searchTextAsPrefix) ? ("%"+searchTextAsPrefix) : null; String searchTextAnyMatch = StringUtils.isNotBlank(searchTextAsPrefix) ? ("%"+searchTextAsPrefix) : null;
TypedQuery<VesselSnapshotResult> q = getEntityManager().createQuery(query) TypedQuery<VesselSnapshotResult> q = getEntityManager().createQuery(query)
.setParameter(programParam, filter.getProgramLabel())
.setParameter(dateParam, filter.getDate()) .setParameter(dateParam, filter.getDate())
.setParameter(vesselFeaturesIdParam, filter.getVesselFeaturesId()) .setParameter(vesselFeaturesIdParam, filter.getVesselFeaturesId())
.setParameter(vesselIdParam, filter.getVesselId()) .setParameter(vesselIdParam, filter.getVesselId())
......
...@@ -30,7 +30,7 @@ import java.util.List; ...@@ -30,7 +30,7 @@ import java.util.List;
@Data @Data
@FieldNameConstants @FieldNameConstants
public class VesselFilterVO { public class VesselFilterVO implements IRootDataFilter {
public static VesselFilterVO nullToEmpty(VesselFilterVO f) { public static VesselFilterVO nullToEmpty(VesselFilterVO f) {
return f != null ? f : new VesselFilterVO(); return f != null ? f : new VesselFilterVO();
...@@ -38,12 +38,30 @@ public class VesselFilterVO { ...@@ -38,12 +38,30 @@ public class VesselFilterVO {
private String programLabel; private String programLabel;
private Date date;
private Integer vesselId; private Integer vesselId;
private Integer vesselFeaturesId; private Integer vesselFeaturesId;
private String searchText; private String searchText;
private List<Integer> statusIds; private List<Integer> statusIds;
private Integer locationId; // TODO: use it in repository
private Integer recorderDepartmentId; // TODO: use it in repository
private Integer recorderPersonId; // TODO: use it in repository
private Date startDate;
private Date endDate; // TODO: use it in repository
public void setDate(Date date) {
this.startDate = date;
}
/**
* @deprecated use startDate instead
* @return
*/
@Deprecated
public Date getDate() {
return this.startDate;
}
} }
...@@ -32,6 +32,7 @@ import net.sumaris.core.dao.technical.Page; ...@@ -32,6 +32,7 @@ import net.sumaris.core.dao.technical.Page;
import net.sumaris.core.dao.technical.Pageables; import net.sumaris.core.dao.technical.Pageables;
import net.sumaris.core.dao.technical.SortDirection; import net.sumaris.core.dao.technical.SortDirection;
import net.sumaris.core.dao.technical.model.IEntity; import net.sumaris.core.dao.technical.model.IEntity;
import net.sumaris.core.model.administration.programStrategy.ProgramEnum;
import net.sumaris.core.model.data.*; import net.sumaris.core.model.data.*;
import net.sumaris.core.service.data.*; import net.sumaris.core.service.data.*;
import net.sumaris.core.service.referential.pmfm.PmfmService; import net.sumaris.core.service.referential.pmfm.PmfmService;
...@@ -160,6 +161,12 @@ public class DataGraphQLService { ...@@ -160,6 +161,12 @@ public class DataGraphQLService {
@GraphQLArgument(name = "sortBy", defaultValue = VesselSnapshotVO.Fields.EXTERIOR_MARKING) String sort, @GraphQLArgument(name = "sortBy", defaultValue = VesselSnapshotVO.Fields.EXTERIOR_MARKING) String sort,
@GraphQLArgument(name = "sortDirection", defaultValue = "asc") String direction @GraphQLArgument(name = "sortDirection", defaultValue = "asc") String direction
) { ) {
// Filter on SIH program, when not an admin
if (!authService.isAdmin()) {
filter = VesselFilterVO.nullToEmpty(filter);
filter.setProgramLabel(ProgramEnum.SIH.getLabel());
}
return vesselService.findSnapshotByFilter( return vesselService.findSnapshotByFilter(
filter, filter,
offset, size, sort, offset, size, sort,
...@@ -175,6 +182,12 @@ public class DataGraphQLService { ...@@ -175,6 +182,12 @@ public class DataGraphQLService {
@GraphQLArgument(name = "sortBy") String sort, @GraphQLArgument(name = "sortBy") String sort,
@GraphQLArgument(name = "sortDirection", defaultValue = "asc") String direction @GraphQLArgument(name = "sortDirection", defaultValue = "asc") String direction
) { ) {
// Filter on SIH program, when not an admin
if (!authService.isAdmin()) {
filter = VesselFilterVO.nullToEmpty(filter);
filter.setProgramLabel(ProgramEnum.SIH.getLabel());
}
return vesselService.findVesselsByFilter( return vesselService.findVesselsByFilter(
filter, filter,
offset, size, sort, offset, size, sort,
......
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