Search in sources :

Example 21 with DatasetEntity

use of org.n52.series.db.beans.DatasetEntity in project SOS by 52North.

the class ResultTemplateDAO method checkOrInsertResultTemplate.

/**
 * Check or insert result template
 *
 * @param request
 *            Insert result template request
 * @param dataset
 *            Observation constellation object
 * @param procedure
 *            the procedure
 * @param featureOfInterest
 *            FeatureOfInterest object
 * @param session
 *            Hibernate session
 * @return created or exiting {@link ResultTemplateEntity}
 * @throws OwsExceptionReport
 *             If the requested structure/encoding is invalid
 */
public ResultTemplateEntity checkOrInsertResultTemplate(InsertResultTemplateRequest request, DatasetEntity dataset, ProcedureEntity procedure, AbstractFeatureEntity featureOfInterest, Session session) throws OwsExceptionReport {
    try {
        String offering = dataset.getOffering().getIdentifier();
        String observableProperty = dataset.getObservableProperty().getIdentifier();
        List<ResultTemplateEntity> resultTemplates = getResultTemplateObject(offering, observableProperty, null, session);
        if (CollectionHelper.isEmpty(resultTemplates)) {
            return createAndSaveResultTemplate(request, dataset, procedure, featureOfInterest, session);
        } else {
            List<String> storedIdentifiers = new ArrayList<>(0);
            for (ResultTemplateEntity storedResultTemplate : resultTemplates) {
                storedIdentifiers.add(storedResultTemplate.getIdentifier());
                SosResultStructure storedStructure = createSosResultStructure(storedResultTemplate.getStructure());
                SosResultEncoding storedEncoding = createSosResultEncoding(storedResultTemplate.getEncoding());
                SosResultStructure storedObservationStructure = createSosResultStructure(storedResultTemplate.getObservationStructure());
                SosResultEncoding storedObservationEncoding = createSosResultEncoding(storedResultTemplate.getObservationEncoding());
                if (request instanceof InternalInsertResultTemplateRequest) {
                    ((InternalInsertResultTemplateRequest) request).getObservationStructure().clearXml();
                    ((InternalInsertResultTemplateRequest) request).getObservationEncoding().clearXml();
                    if (storedObservationStructure != null && !storedObservationStructure.equals(((InternalInsertResultTemplateRequest) request).getObservationStructure())) {
                        throw new InvalidParameterValueException().at(Sos2Constants.InsertResultTemplateParams.proposedTemplate).withMessage(LOG_TEMPLATE, procedure.getIdentifier(), observableProperty, offering);
                    }
                    if (storedObservationEncoding != null && !storedObservationEncoding.equals(((InternalInsertResultTemplateRequest) request).getObservationEncoding())) {
                        throw new InvalidParameterValueException().at(Sos2Constants.InsertResultTemplateParams.proposedTemplate).withMessage(LOG_TEMPLATE, procedure.getIdentifier(), observableProperty, offering);
                    }
                } else {
                    if (storedStructure != null && !storedStructure.equals(request.getResultStructure())) {
                        throw new InvalidParameterValueException().at(Sos2Constants.InsertResultTemplateParams.proposedTemplate).withMessage(LOG_TEMPLATE, procedure.getIdentifier(), observableProperty, offering);
                    }
                    if (storedEncoding != null && !storedEncoding.equals(request.getResultEncoding())) {
                        throw new InvalidParameterValueException().at(Sos2Constants.InsertResultTemplateParams.proposedTemplate).withMessage(LOG_TEMPLATE, procedure.getIdentifier(), observableProperty, offering);
                    }
                }
            }
            if (request.getIdentifier() != null && request.getIdentifier().isSetValue()) {
                if (!resultTemplates.isEmpty() && request instanceof InternalInsertResultTemplateRequest && request.getIdentifier().getValue().startsWith("OBS_")) {
                    updateTemplates(resultTemplates, (InternalInsertResultTemplateRequest) request, session);
                    return resultTemplates.iterator().next();
                }
                if (!storedIdentifiers.contains(request.getIdentifier().getValue())) {
                    /* save it only if the identifier is different */
                    return createAndSaveResultTemplate(request, dataset, procedure, featureOfInterest, session);
                }
            }
            return resultTemplates.iterator().next();
        }
    } catch (EncodingException | DecodingException ex) {
        throw new NoApplicableCodeException().causedBy(ex);
    }
}
Also used : EncodingException(org.n52.svalbard.encode.exception.EncodingException) NoApplicableCodeException(org.n52.shetland.ogc.ows.exception.NoApplicableCodeException) ArrayList(java.util.ArrayList) XmlDecodingException(org.n52.svalbard.decode.exception.XmlDecodingException) DecodingException(org.n52.svalbard.decode.exception.DecodingException) InternalInsertResultTemplateRequest(org.n52.sos.request.InternalInsertResultTemplateRequest) SosResultEncoding(org.n52.shetland.ogc.sos.SosResultEncoding) InvalidParameterValueException(org.n52.shetland.ogc.ows.exception.InvalidParameterValueException) ResultTemplateEntity(org.n52.series.db.beans.ResultTemplateEntity) SosResultStructure(org.n52.shetland.ogc.sos.SosResultStructure)

Example 22 with DatasetEntity

use of org.n52.series.db.beans.DatasetEntity in project SOS by 52North.

the class AbstractSeriesDAO method getSeriesCriteria.

@SuppressWarnings("unchecked")
protected Set<DatasetEntity> getSeriesCriteria(GetDataAvailabilityRequest request, Session session) throws OwsExceptionReport {
    Set<DatasetEntity> set = new LinkedHashSet<>();
    if (request.hasResultFilter()) {
        for (SubQueryIdentifier identifier : ResultFilterRestrictions.getSubQueryIdentifier(getResultFilterClasses())) {
            Criteria c = getSeriesCriteria(request.getProcedures(), request.getObservedProperties(), request.getFeaturesOfInterest(), request.getOfferings(), session);
            checkAndAddResultFilterCriterion(c, request, identifier, session);
            checkAndAddSpatialFilterCriterion(c, request, session);
            LOGGER.trace("QUERY getSeriesCriteria(request) and result filter sub query '{}': {}", identifier.name(), HibernateHelper.getSqlString(c));
            set.addAll(c.list());
        }
    } else {
        Criteria c = getSeriesCriteria(request.getProcedures(), request.getObservedProperties(), request.getFeaturesOfInterest(), request.getOfferings(), session);
        checkAndAddSpatialFilterCriterion(c, request, session);
        LOGGER.trace(QUERY_SERIES_CRITERIA, HibernateHelper.getSqlString(c));
        set.addAll(c.list());
    }
    return set;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) DatasetEntity(org.n52.series.db.beans.DatasetEntity) Criteria(org.hibernate.Criteria) DetachedCriteria(org.hibernate.criterion.DetachedCriteria) SubQueryIdentifier(org.n52.sos.ds.hibernate.util.ResultFilterRestrictions.SubQueryIdentifier)

Example 23 with DatasetEntity

use of org.n52.series.db.beans.DatasetEntity in project SOS by 52North.

the class AbstractSeriesDAO method delete.

public List<DatasetEntity> delete(ProcedureEntity procedure, Session session) {
    Criteria c = getDefaultAllSeriesCriteria(session);
    addProcedureToCriteria(c, procedure);
    List<DatasetEntity> series = c.list();
    if (series != null && !series.isEmpty()) {
        StringBuilder builder = new StringBuilder();
        builder.append("delete ");
        builder.append(DatasetEntity.class.getSimpleName());
        builder.append(" where ").append(DatasetEntity.PROPERTY_PROCEDURE).append(" = :").append(DatasetEntity.PROPERTY_PROCEDURE);
        Query<?> q = session.createQuery(builder.toString());
        q.setParameter(DatasetEntity.PROPERTY_PROCEDURE, procedure);
        int executeUpdate = q.executeUpdate();
        LOGGER.debug("{} datasets were physically deleted!", executeUpdate);
        session.flush();
    }
    return series;
}
Also used : DatasetEntity(org.n52.series.db.beans.DatasetEntity) Criteria(org.hibernate.Criteria) DetachedCriteria(org.hibernate.criterion.DetachedCriteria)

Example 24 with DatasetEntity

use of org.n52.series.db.beans.DatasetEntity in project SOS by 52North.

the class AbstractSeriesObservationDAO method updateObservationSetAsDeletedForSeries.

/**
 * Update series observation by setting deleted flag
 *
 * @param series
 *            Series for which the observations should be updated
 * @param deleteFlag
 *            New deleted flag value
 * @param session
 *            Hibernate Session
 */
public void updateObservationSetAsDeletedForSeries(List<DatasetEntity> series, boolean deleteFlag, Session session) {
    if (CollectionHelper.isNotEmpty(series)) {
        Criteria criteria = getDefaultObservationCriteria(session);
        criteria.add(Restrictions.in(DataEntity.PROPERTY_DATASET_ID, series.stream().map(DatasetEntity::getId).collect(Collectors.toSet())));
        ScrollableIterable<DataEntity<?>> scroll = ScrollableIterable.fromCriteria(criteria);
        updateObservation(scroll, deleteFlag, session);
    }
}
Also used : DataEntity(org.n52.series.db.beans.DataEntity) Criteria(org.hibernate.Criteria)

Example 25 with DatasetEntity

use of org.n52.series.db.beans.DatasetEntity in project SOS by 52North.

the class AbstractSeriesObservationDAO method getFirstObservationFor.

/**
 * Get the first not deleted observation for the {@link DatasetEntity}
 *
 * @param series
 *            Series to get observation for
 * @param session
 *            Hibernate session
 * @return First not deleted observation
 */
public DataEntity<?> getFirstObservationFor(DatasetEntity series, Session session) {
    Criteria c = getDefaultObservationCriteria(session);
    c.add(Restrictions.eq(DataEntity.PROPERTY_DATASET_ID, series.getId()));
    c.addOrder(Order.asc(DataEntity.PROPERTY_SAMPLING_TIME_START));
    c.setMaxResults(1);
    LOGGER.trace("QUERY getFirstObservationFor(series): {}", HibernateHelper.getSqlString(c));
    return (DataEntity) c.uniqueResult();
}
Also used : DataEntity(org.n52.series.db.beans.DataEntity) Criteria(org.hibernate.Criteria)

Aggregations

DatasetEntity (org.n52.series.db.beans.DatasetEntity)49 Criteria (org.hibernate.Criteria)21 DataEntity (org.n52.series.db.beans.DataEntity)13 NoApplicableCodeException (org.n52.shetland.ogc.ows.exception.NoApplicableCodeException)13 ProcedureEntity (org.n52.series.db.beans.ProcedureEntity)11 Session (org.hibernate.Session)10 DetachedCriteria (org.hibernate.criterion.DetachedCriteria)10 OfferingEntity (org.n52.series.db.beans.OfferingEntity)9 HashSet (java.util.HashSet)8 OmObservationConstellation (org.n52.shetland.ogc.om.OmObservationConstellation)8 LinkedHashSet (java.util.LinkedHashSet)7 Set (java.util.Set)7 HibernateException (org.hibernate.HibernateException)7 FormatEntity (org.n52.series.db.beans.FormatEntity)7 DatastreamEntity (org.n52.series.db.beans.sta.DatastreamEntity)7 OwsExceptionReport (org.n52.shetland.ogc.ows.exception.OwsExceptionReport)7 Collection (java.util.Collection)6 LinkedList (java.util.LinkedList)6 AbstractFeatureEntity (org.n52.series.db.beans.AbstractFeatureEntity)6 PhenomenonEntity (org.n52.series.db.beans.PhenomenonEntity)6