Skip to content
Snippets Groups Projects
Commit d98fb466 authored by Lene Wasskog's avatar Lene Wasskog
Browse files

feat: Remove unnecessary geoinfo stuff from ObservationTimeSeries

parent 37625f58
Branches
Tags
1 merge request!173ObservationDataSchema for eplevikler and ObservationTimeSeries
......@@ -45,49 +45,14 @@ public class ObservationTimeSeriesBean {
List<ObservationTimeSeries> resultList = em.createNamedQuery("ObservationTimeSeries.findByUserId", ObservationTimeSeries.class)
.setParameter("userId", user.getUserId())
.getResultList();
this.enrichObservationTimeSeriesListWithGeoInfo(resultList);
this.enrichObservationTimeSeriesListWithPointOfInterest(resultList);
this.enrichObservationTimeSeriesListWithObservers(resultList);
return resultList;
}
private void enrichObservationTimeSeriesListWithGeoInfo(List<ObservationTimeSeries> otsList) {
if (otsList.isEmpty()) {
return;
}
// Indexing observations
Map<Integer, ObservationTimeSeries> obsBucket = new HashMap<>();
otsList.forEach((obs) -> {
obsBucket.put(obs.getObservationTimeSeriesId(), obs);
});
// Getting many-to-many relations
Query q = em.createNativeQuery("SELECT gis_id, observation_time_series_id FROM public.gis_observation_time_series WHERE observation_time_series_id IN :observationTimeSeriesIds");
q.setParameter("observationTimeSeriesIds", obsBucket.keySet());
List<Object[]> gisObservationTimeSeriesIds = q.getResultList();
// Collecting and indexing geoInfo
Query q2 = em.createNativeQuery("SELECT * FROM public.gis WHERE gis_id IN (SELECT gis_id FROM public.gis_observation_time_series WHERE observation_time_series_id IN :observationTimeSeriesIds)", Gis.class);
List<Gis> geoInfos = q2.setParameter("observationTimeSeriesIds", obsBucket.keySet()).getResultList();
Map<Integer, Gis> gisBucket = new HashMap<>();
geoInfos.forEach((geoInfo) -> {
gisBucket.put(geoInfo.getGisId(), geoInfo);
});
// Iterating the many-to-many relations,
// adding geoinfo to the correct observations
gisObservationTimeSeriesIds.forEach((gisOtsIds) -> {
Integer gisId = (Integer) gisOtsIds[0];
Integer observationId = (Integer) gisOtsIds[1];
obsBucket.get(observationId).addGeoInfo(gisBucket.get(gisId));
});
}
public ObservationTimeSeries getObservationTimeSeries(Integer id) {
ObservationTimeSeries ots = em.find(ObservationTimeSeries.class, id);
if (ots != null) {
ots.setGeoInfoList(this.getGeoInfoForObservationTimeSeries(ots));
ots.setUser(em.find(VipsLogicUser.class, ots.getUserId()));
if (ots.getLastModifiedBy() != null) {
ots.setLastModifiedByUser(em.find(VipsLogicUser.class, ots.getLastModifiedBy()));
......@@ -96,48 +61,12 @@ public class ObservationTimeSeriesBean {
return ots;
}
public List<Gis> getGeoInfoForObservationTimeSeries(ObservationTimeSeries ots) {
List<Integer> gisIds = em.createNativeQuery("SELECT gis_id FROM public.gis_observation_time_series WHERE observation_time_series_id = :observationTimeSeriesId")
.setParameter("observationTimeSeriesId", ots.getObservationTimeSeriesId())
.getResultList();
if (gisIds != null && !gisIds.isEmpty()) {
return em.createNamedQuery("Gis.findByGisIds", Gis.class)
.setParameter("gisIds", gisIds)
.getResultList();
} else {
return new ArrayList<>();
}
}
/**
* @param ots the observation time series
* @return The merged object
*/
public ObservationTimeSeries storeObservationTimeSeries(ObservationTimeSeries ots) {
ObservationTimeSeries mergedOts = em.merge(ots);
em.createNativeQuery("DELETE FROM public.gis_observation_time_series WHERE observation_time_series_id=:observationTimeSeriesId")
.setParameter("observationTimeSeriesId", mergedOts.getObservationTimeSeriesId())
.executeUpdate();
em.createNativeQuery("DELETE FROM public.gis where gis_id IN (SELECT gis_id FROM public.gis_observation_time_series WHERE observation_time_series_id=:observationTimeSeriesId)")
.setParameter("observationTimeSeriesId", mergedOts.getObservationTimeSeriesId())
.executeUpdate();
// Then persist the new ones
if (ots.getGeoInfoList() != null && !ots.getGeoInfoList().isEmpty()) {
ots.getGeoInfoList().forEach((gis) -> {
em.persist(gis);
});
// Prepare query for inserting gis_observation_time_series
Query q = em.createNativeQuery("INSERT INTO public.gis_observation_time_series(gis_id,observation_time_series_id) VALUES(:gisId,:observationTimeSeriesId)")
.setParameter("observationTimeSeriesId", mergedOts.getObservationTimeSeriesId());
// Add gis_id and execute query
ots.getGeoInfoList().forEach((gis) -> {
q.setParameter("gisId", gis.getGisId()).executeUpdate();
});
}
// The GisObservations are not included in the merged object, so we should add them
mergedOts.setGeoInfoList(this.getGeoInfoForObservationTimeSeries(mergedOts));
return mergedOts;
return em.merge(ots);
}
public void deleteObservationTimeSeries(Integer id) {
......
......@@ -57,20 +57,10 @@ public class ObservationTimeSeries implements Serializable, no.nibio.vips.observ
private VipsLogicUser lastModifiedByUser; // Transient
private Integer locationPointOfInterestId;
private PointOfInterest locationPointOfInterest;
private List<Gis> geoInfoList;
private Boolean locationIsPrivate;
private PolygonService polygonService;
private GISEntityUtil GISEntityUtil;
private GISUtil GISUtil;
public ObservationTimeSeries() {
this.GISEntityUtil = new GISEntityUtil();
this.GISUtil = new GISUtil();
}
public ObservationTimeSeries(Integer observationTimeSeriesId) {
this.observationTimeSeriesId = observationTimeSeriesId;
}
/**
......@@ -307,35 +297,6 @@ public class ObservationTimeSeries implements Serializable, no.nibio.vips.observ
this.locationPointOfInterest = locationPointOfInterest;
}
public void addGeoInfo(Gis geoInfo) {
if (this.geoInfoList == null) {
this.geoInfoList = new ArrayList<>();
}
this.geoInfoList.add(geoInfo);
}
@JsonIgnore
@Transient
public List<Gis> getGeoInfoList() {
return this.geoInfoList;
}
public void setGeoInfoList(List<Gis> geoInfoList) {
this.geoInfoList = geoInfoList;
}
public void setGeoInfoList(String json) {
this.setGeoInfoList(this.GISEntityUtil.getGisFromGeoJSON(json));
}
@Transient
@Override
public String getGeoInfo() {
Map<String, Object> properties = new HashMap<>();
properties.put("observationTimeSeriesId", this.getObservationTimeSeriesId());
return this.GISEntityUtil.getGeoJSONFromGis(this.geoInfoList, properties);
}
/**
* @return the locationIsPrivate
*/
......@@ -399,11 +360,8 @@ public class ObservationTimeSeries implements Serializable, no.nibio.vips.observ
", lastModifiedByUser=" + lastModifiedByUser +
", locationPointOfInterestId=" + locationPointOfInterestId +
", locationPointOfInterest=" + locationPointOfInterest +
", geoInfoList=" + geoInfoList +
", locationIsPrivate=" + locationIsPrivate +
", polygonService=" + polygonService +
", GISEntityUtil=" + GISEntityUtil +
", GISUtil=" + GISUtil +
'}';
}
......
......@@ -13,7 +13,6 @@ import no.nibio.vips.logic.entity.VipsLogicUser;
import no.nibio.vips.logic.entity.rest.PointMappingResponse;
import no.nibio.vips.logic.entity.rest.ReferencedPoint;
import no.nibio.vips.logic.util.GISEntityUtil;
import no.nibio.vips.logic.util.SystemTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.wololo.geojson.Feature;
......@@ -36,7 +35,6 @@ import java.util.stream.Collectors;
public class ObservationTimeSeriesService {
public static final String APPLICATION_JSON = "application/json;charset=UTF-8";
public static final String GEO_INFO = "geoInfo";
private final static Logger LOGGER = LoggerFactory.getLogger(ObservationTimeSeriesService.class);
private static final String DELETED = "deleted";
private static final String OBSERVATION_TIME_SERIES_ID = "observationTimeSeriesId";
......@@ -101,13 +99,10 @@ public class ObservationTimeSeriesService {
@Produces(APPLICATION_JSON)
@TypeHint(ObservationTimeSeries.class)
public Response getObservationTimeSeries(@PathParam("observationTimeSeriesId") Integer observationTimeSeriesId, @QueryParam("userUUID") String userUUID) {
// Observation needs to be masked here as well, or does it create trouble for VIPSLogic observation admin?
ObservationTimeSeries ots = observationTimeSeriesBean.getObservationTimeSeries(observationTimeSeriesId);
if (ots == null) {
return Response.status(Response.Status.NOT_FOUND).build();
}
VipsLogicUser creator = userBean.getVipsLogicUser(ots.getUserId());
VipsLogicUser requester = (VipsLogicUser) httpServletRequest.getSession().getAttribute("user");
if (requester == null && userUUID != null) {
requester = userBean.findVipsLogicUser(UUID.fromString(userUUID));
......@@ -118,12 +113,8 @@ public class ObservationTimeSeriesService {
boolean requesterNotCreator = requester != null && !ots.getUserId().equals(requester.getUserId());
if (requesterNotValidUser || requesterRegularUser) {
// Hide completely for all users except creator, super and orgadmin
if (ots.getLocationIsPrivate() && (requesterNotValidUser || requesterNotCreator)) {
ots.setGeoInfoList(new ArrayList<>());
}
// Mask for all users except creator, super and orgadmin
else if (ots.getPolygonService() != null) {
if (!(ots.getLocationIsPrivate() && (requesterNotValidUser || requesterNotCreator)) && ots.getPolygonService() != null) {
observationTimeSeriesBean.enrichObservationTimeSeriesWithPointOfInterest(ots);
this.maskLocation(ots.getPolygonService(), ots);
}
......@@ -132,8 +123,7 @@ public class ObservationTimeSeriesService {
}
/**
* The location (point of interest) of the given observation time series is masked using
* the provided polygon service.
* The location (point of interest) of the given observation time series is masked using the provided polygon service.
*
* @param polygonService The polygon service that should be used for masking
* @param observationTimeSeries The observation time series to mask location for
......@@ -162,7 +152,6 @@ public class ObservationTimeSeriesService {
Gis polygon = gisEntityUtil.getGisFromFeature(indexedPolygons.get(borderId));
List<Gis> gis = new ArrayList<>();
gis.add(polygon);
observationTimeSeries.setGeoInfoList(gis);
observationTimeSeries.setLocationPointOfInterest(null);
observationTimeSeries.setLocationPointOfInterestId(null);
}
......@@ -183,13 +172,15 @@ public class ObservationTimeSeriesService {
@TypeHint(ObservationTimeSeries.class)
public Response syncObservationTimeSeriesFromApp(String jsonOts) {
LOGGER.info("In syncObservationTimeSeriesFromApp");
LOGGER.info(jsonOts);
VipsLogicUser user = userBean.getUserFromUUID(httpServletRequest);
if (user == null) {
return Response.status(Response.Status.UNAUTHORIZED).build();
}
ObjectMapper oM = new ObjectMapper();
try {
Map<Object, Object> mapFromApp = oM.readValue(jsonOts, new TypeReference<HashMap<Object, Object>>() {});
Map<Object, Object> mapFromApp = oM.readValue(jsonOts, new TypeReference<HashMap<Object, Object>>() {
});
Integer otsId = (Integer) mapFromApp.get(OBSERVATION_TIME_SERIES_ID);
......@@ -222,7 +213,6 @@ public class ObservationTimeSeriesService {
}
otsToSave.setCropOrganism(organismBean.getOrganism(getValueFromMap(mapFromApp, CROP_ORGANISM_ID, Integer.class)));
otsToSave.setOrganism(organismBean.getOrganism(getValueFromMap(mapFromApp, ORGANISM_ID, Integer.class)));
otsToSave.setGeoInfoList(getValueFromMap(mapFromApp, GEO_INFO, String.class));
otsToSave.setLocationPointOfInterestId(getValueFromMap(mapFromApp, LOCATION_POINT_OF_INTEREST_ID, Integer.class));
otsToSave.setYear(getValueFromMap(mapFromApp, YEAR, Integer.class));
otsToSave.setName(getValueFromMap(mapFromApp, NAME, String.class));
......@@ -239,10 +229,11 @@ public class ObservationTimeSeriesService {
otsToSave.setLastModifiedBy(user.getUserId());
// Input check before storing, location must be set
if ((otsToSave.getGeoInfo() == null || otsToSave.getGeoInfo().trim().isEmpty()) && otsToSave.getLocationPointOfInterestId() == null) {
if (otsToSave.getLocationPointOfInterestId() == null) {
LOGGER.error("The observation time series is missing location data");
return Response.status(Response.Status.BAD_REQUEST).entity("The observation time series is missing location data").build();
}
LOGGER.info("otsToSave before storing: " + otsToSave);
otsToSave = observationTimeSeriesBean.storeObservationTimeSeries(otsToSave);
return Response.ok().entity(otsToSave).build();
} catch (IOException e) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment