Search in sources :

Example 1 with SeriesTimeExtrema

use of org.n52.sos.ds.hibernate.dao.observation.series.SeriesTimeExtrema in project SOS by 52North.

the class DeleteObservationHelper method checkSeriesForFirstLatest.

/**
 * Check if {@link Dataset} should be updated
 *
 * @param serieses
 *            Deleted observation
 * @param session
 *            Hibernate session
 * @throws OwsExceptionReport
 *             If an error occurs
 */
default void checkSeriesForFirstLatest(Collection<Long> serieses, Session session) throws OwsExceptionReport {
    if (!serieses.isEmpty()) {
        AbstractSeriesObservationDAO observationDAO = getDaoFactory().getObservationDAO();
        Map<Long, SeriesTimeExtrema> minMaxTimes = observationDAO.getMinMaxSeriesTimesById(serieses instanceof Set ? (Set<Long>) serieses : new LinkedHashSet<>(serieses), session);
        for (Long id : serieses) {
            DatasetEntity series = session.get(DatasetEntity.class, id);
            boolean update = false;
            if (minMaxTimes.containsKey(series.getId())) {
                SeriesTimeExtrema extrema = minMaxTimes.get(series.getId());
                if (!series.isSetFirstValueAt() || series.isSetFirstValueAt() && !DateTimeHelper.makeDateTime(series.getFirstValueAt()).equals(extrema.getMinPhenomenonTime())) {
                    series.setFirstValueAt(extrema.getMinPhenomenonTime().toDate());
                    DataEntity<?> o = unproxy(observationDAO.getMinObservation(series, extrema.getMinPhenomenonTime(), session), session);
                    series.setFirstObservation(o);
                    if (series.getValueType().equals(ValueType.quantity)) {
                        series.setFirstQuantityValue(((QuantityDataEntity) o).getValue());
                    }
                    update = true;
                }
                if (!series.isSetLastValueAt() || series.isSetLastValueAt() && !DateTimeHelper.makeDateTime(series.getLastValueAt()).equals(extrema.getMaxPhenomenonTime())) {
                    series.setLastValueAt(extrema.getMaxPhenomenonTime().toDate());
                    DataEntity<?> o = unproxy(observationDAO.getMaxObservation(series, extrema.getMaxPhenomenonTime(), session), session);
                    series.setLastObservation(o);
                    if (series.getValueType().equals(ValueType.quantity)) {
                        series.setLastQuantityValue(((QuantityDataEntity) o).getValue());
                    }
                    update = true;
                }
            } else {
                series.setFirstValueAt(null);
                series.setFirstQuantityValue(null);
                series.setFirstObservation(null);
                series.setLastValueAt(null);
                series.setLastQuantityValue(null);
                series.setLastObservation(null);
                update = true;
            }
            if (update) {
                session.saveOrUpdate(series);
                session.flush();
            }
        }
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) Set(java.util.Set) DatasetEntity(org.n52.series.db.beans.DatasetEntity) AbstractSeriesObservationDAO(org.n52.sos.ds.hibernate.dao.observation.series.AbstractSeriesObservationDAO) SeriesTimeExtrema(org.n52.sos.ds.hibernate.dao.observation.series.SeriesTimeExtrema)

Aggregations

HashSet (java.util.HashSet)1 LinkedHashSet (java.util.LinkedHashSet)1 Set (java.util.Set)1 DatasetEntity (org.n52.series.db.beans.DatasetEntity)1 AbstractSeriesObservationDAO (org.n52.sos.ds.hibernate.dao.observation.series.AbstractSeriesObservationDAO)1 SeriesTimeExtrema (org.n52.sos.ds.hibernate.dao.observation.series.SeriesTimeExtrema)1