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 {
/* -- 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,
Join<Vessel, VesselFeatures> featuresJoin,
Join<Vessel, VesselRegistrationPeriod> vrpJoin,
VesselFilterVO filter) {
if (filter != null) {
Join<Vessel, Program> programJoin = vesselRoot.join(Vessel.Fields.PROGRAM, JoinType.INNER);
// Apply vessel Filter
ParameterExpression<Date> dateParam = builder.parameter(Date.class);
ParameterExpression<Integer> vesselIdParam = builder.parameter(Integer.class);
ParameterExpression<Integer> vesselFeaturesIdParam = builder.parameter(Integer.class);
ParameterExpression<String> searchNameParam = builder.parameter(String.class);
ParameterExpression<String> searchExteriorMarkingParam = builder.parameter(String.class);
ParameterExpression<String> searchRegistrationCodeParam = builder.parameter(String.class);
ParameterExpression<Boolean> hasStatusIdsParam = builder.parameter(Boolean.class);
ParameterExpression<Collection> statusIdsParam = builder.parameter(Collection.class);
ParameterExpression<String> programParam = cb.parameter(String.class);
ParameterExpression<Date> dateParam = cb.parameter(Date.class);
ParameterExpression<Integer> vesselIdParam = cb.parameter(Integer.class);
ParameterExpression<Integer> vesselFeaturesIdParam = cb.parameter(Integer.class);
ParameterExpression<String> searchNameParam = cb.parameter(String.class);
ParameterExpression<String> searchExteriorMarkingParam = cb.parameter(String.class);
ParameterExpression<String> searchRegistrationCodeParam = cb.parameter(String.class);
ParameterExpression<Boolean> hasStatusIdsParam = cb.parameter(Boolean.class);
ParameterExpression<Collection> statusIdsParam = cb.parameter(Collection.class);
query.where(cb.and(
// Program
cb.or(
cb.isNull(programParam),
cb.equal(programJoin.get(Program.Fields.LABEL), programParam)
),
query.where(builder.and(
// Filter: date
builder.or(
builder.and(
cb.or(
cb.and(
// if no date in filter, will return only active period
builder.isNull(dateParam.as(String.class)),
builder.isNull(featuresJoin.get(VesselFeatures.Fields.END_DATE)),
builder.isNull(vrpJoin.get(VesselRegistrationPeriod.Fields.END_DATE))
cb.isNull(dateParam.as(String.class)),
cb.isNull(featuresJoin.get(VesselFeatures.Fields.END_DATE)),
cb.isNull(vrpJoin.get(VesselRegistrationPeriod.Fields.END_DATE))
),
builder.and(
builder.isNotNull(dateParam.as(String.class)),
builder.and(
builder.or(
builder.isNull(featuresJoin.get(VesselFeatures.Fields.END_DATE)),
builder.greaterThan(featuresJoin.get(VesselFeatures.Fields.END_DATE), dateParam)
cb.and(
cb.isNotNull(dateParam.as(String.class)),
cb.and(
cb.or(
cb.isNull(featuresJoin.get(VesselFeatures.Fields.END_DATE)),
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(
builder.or(
builder.isNull(vrpJoin.get(VesselRegistrationPeriod.Fields.END_DATE)),
builder.greaterThan(vrpJoin.get(VesselRegistrationPeriod.Fields.END_DATE), dateParam)
cb.and(
cb.or(
cb.isNull(vrpJoin.get(VesselRegistrationPeriod.Fields.END_DATE)),
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
builder.or(
builder.isNull(vesselFeaturesIdParam),
builder.equal(featuresJoin.get(VesselFeatures.Fields.ID), vesselFeaturesIdParam)
cb.or(
cb.isNull(vesselFeaturesIdParam),
cb.equal(featuresJoin.get(VesselFeatures.Fields.ID), vesselFeaturesIdParam)
),
// Filter: vessel id
builder.or(
builder.isNull(vesselIdParam),
builder.equal(vesselRoot.get(Vessel.Fields.ID), vesselIdParam))
cb.or(
cb.isNull(vesselIdParam),
cb.equal(vesselRoot.get(Vessel.Fields.ID), vesselIdParam))
),
// Filter: search text (on exterior marking OR id)
builder.or(
builder.isNull(searchNameParam),
builder.like(builder.lower(featuresJoin.get(VesselFeatures.Fields.NAME)), searchNameParam),
builder.like(builder.lower(featuresJoin.get(VesselFeatures.Fields.EXTERIOR_MARKING)), searchExteriorMarkingParam),
builder.like(builder.lower(vrpJoin.get(VesselRegistrationPeriod.Fields.REGISTRATION_CODE)), searchRegistrationCodeParam)
cb.or(
cb.isNull(searchNameParam),
cb.like(cb.lower(featuresJoin.get(VesselFeatures.Fields.NAME)), searchNameParam),
cb.like(cb.lower(featuresJoin.get(VesselFeatures.Fields.EXTERIOR_MARKING)), searchExteriorMarkingParam),
cb.like(cb.lower(vrpJoin.get(VesselRegistrationPeriod.Fields.REGISTRATION_CODE)), searchRegistrationCodeParam)
),
// Status
builder.or(
builder.isFalse(hasStatusIdsParam),
builder.in(vesselRoot.get(Vessel.Fields.STATUS).get(Status.Fields.ID)).value(statusIdsParam)
cb.or(
cb.isFalse(hasStatusIdsParam),
cb.in(vesselRoot.get(Vessel.Fields.STATUS).get(Status.Fields.ID)).value(statusIdsParam)
)
);
......@@ -397,6 +406,7 @@ public class VesselDaoImpl extends HibernateDaoSupport implements VesselDao {
: filter.getStatusIds();
return getEntityManager().createQuery(query)
.setParameter(programParam, filter.getProgramLabel())
.setParameter(dateParam, filter.getDate())
.setParameter(vesselFeaturesIdParam, filter.getVesselFeaturesId())
.setParameter(vesselIdParam, filter.getVesselId())
......@@ -410,9 +420,9 @@ public class VesselDaoImpl extends HibernateDaoSupport implements VesselDao {
// if no date in filter, will return only active period
query.where(
builder.and(
builder.isNull(featuresJoin.get(VesselFeatures.Fields.END_DATE)),
builder.isNull(vrpJoin.get(VesselRegistrationPeriod.Fields.END_DATE))
cb.and(
cb.isNull(featuresJoin.get(VesselFeatures.Fields.END_DATE)),
cb.isNull(vrpJoin.get(VesselRegistrationPeriod.Fields.END_DATE))
)
);
......
......@@ -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.SortDirection;
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.VesselFeatures;
import net.sumaris.core.model.data.VesselRegistrationPeriod;
......@@ -99,6 +100,7 @@ public class VesselSnapshotDaoImpl extends HibernateDaoSupport implements Vessel
Root<VesselFeatures> root = query.from(VesselFeatures.class);
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);
query.multiselect(root, vrpJoin);
......@@ -119,6 +121,7 @@ public class VesselSnapshotDaoImpl extends HibernateDaoSupport implements Vessel
: filter.getStatusIds();
// Apply vessel Filter
ParameterExpression<String> programParam = cb.parameter(String.class);
ParameterExpression<Date> dateParam = cb.parameter(Date.class);
ParameterExpression<Integer> vesselIdParam = cb.parameter(Integer.class);
ParameterExpression<Integer> vesselFeaturesIdParam = cb.parameter(Integer.class);
......@@ -129,6 +132,12 @@ public class VesselSnapshotDaoImpl extends HibernateDaoSupport implements Vessel
ParameterExpression<Collection> statusIdsParam = cb.parameter(Collection.class);
query.where(cb.and(
// Program
cb.or(
cb.isNull(programParam),
cb.equal(programJoin.get(Program.Fields.LABEL), programParam)
),
// Filter: date
cb.or(
cb.and(
......@@ -189,6 +198,7 @@ public class VesselSnapshotDaoImpl extends HibernateDaoSupport implements Vessel
String searchTextAnyMatch = StringUtils.isNotBlank(searchTextAsPrefix) ? ("%"+searchTextAsPrefix) : null;
TypedQuery<VesselSnapshotResult> q = getEntityManager().createQuery(query)
.setParameter(programParam, filter.getProgramLabel())
.setParameter(dateParam, filter.getDate())
.setParameter(vesselFeaturesIdParam, filter.getVesselFeaturesId())
.setParameter(vesselIdParam, filter.getVesselId())
......
......@@ -30,7 +30,7 @@ import java.util.List;
@Data
@FieldNameConstants
public class VesselFilterVO {
public class VesselFilterVO implements IRootDataFilter {
public static VesselFilterVO nullToEmpty(VesselFilterVO f) {
return f != null ? f : new VesselFilterVO();
......@@ -38,12 +38,30 @@ public class VesselFilterVO {
private String programLabel;
private Date date;
private Integer vesselId;
private Integer vesselFeaturesId;
private String searchText;
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;
import net.sumaris.core.dao.technical.Pageables;
import net.sumaris.core.dao.technical.SortDirection;
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.service.data.*;
import net.sumaris.core.service.referential.pmfm.PmfmService;
......@@ -160,6 +161,12 @@ public class DataGraphQLService {
@GraphQLArgument(name = "sortBy", defaultValue = VesselSnapshotVO.Fields.EXTERIOR_MARKING) String sort,
@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(
filter,
offset, size, sort,
......@@ -175,6 +182,12 @@ public class DataGraphQLService {
@GraphQLArgument(name = "sortBy") String sort,
@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(
filter,
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