Search in sources :

Example 1 with GetFeatureOfInterestDao

use of org.n52.sos.ds.dao.GetFeatureOfInterestDao in project SOS by 52North.

the class InsertObservationHandler method insertObservation.

private void insertObservation(OmObservation sosObservation, InsertObservationCache cache, CompositeOwsException exceptions, Session session) throws OwsExceptionReport, CodedException {
    checkSpatialFilteringProfile(sosObservation);
    OmObservationConstellation sosObsConst = sosObservation.getObservationConstellation();
    cache.addOfferings(sosObsConst.getOfferings());
    AbstractFeatureEntity hFeature = null;
    // if (sosObsConst.getOfferings().size() > 1) {
    // 
    // }
    String offeringID = sosObsConst.getOfferings().iterator().next();
    DatasetEntity hDataset = cache.get(sosObsConst, offeringID);
    if (hDataset == null) {
        if (!cache.isChecked(sosObsConst, offeringID)) {
            try {
                hDataset = getDaoFactory().getSeriesDAO().checkSeries(sosObsConst, offeringID, session, Sos2Constants.InsertObservationParams.observationType.name());
                // add to cache table
                cache.putConstellation(sosObsConst, offeringID, hDataset);
            } catch (OwsExceptionReport owse) {
                exceptions.add(owse);
            }
            // mark as checked
            cache.checkConstellation(sosObsConst, offeringID);
        }
    }
    if (hDataset != null) {
        // getFeature feature from local cache or create if necessary
        hFeature = getFeature(sosObsConst.getFeatureOfInterest(), cache, session);
        // AbstractFeature/offering combo
        if (!cache.isChecked(sosObsConst.getFeatureOfInterest(), offeringID)) {
            getDaoFactory().getFeatureOfInterestDAO().checkOrInsertRelatedFeatureRelation(hFeature, hDataset.getOffering(), session);
            cache.checkFeature(sosObsConst.getFeatureOfInterest(), offeringID);
        }
        AbstractObservationDAO observationDAO = getDaoFactory().getObservationDAO();
        DatasetEntity dataset = null;
        if (sosObservation.getValue() instanceof SingleObservationValue) {
            dataset = observationDAO.insertObservationSingleValue(hDataset, hFeature, sosObservation, cache.getCodespaceCache(), cache.getUnitCache(), cache.getFormatCache(), session);
        } else if (sosObservation.getValue() instanceof MultiObservationValues) {
            dataset = observationDAO.insertObservationMultiValue(hDataset, hFeature, sosObservation, cache.getCodespaceCache(), cache.getUnitCache(), cache.getFormatCache(), session);
        }
        if (dataset != null && !cache.get(sosObsConst, offeringID).equals(dataset)) {
            cache.putConstellation(sosObsConst, offeringID, dataset);
        }
    }
}
Also used : SingleObservationValue(org.n52.shetland.ogc.om.SingleObservationValue) AbstractFeatureEntity(org.n52.series.db.beans.AbstractFeatureEntity) DatasetEntity(org.n52.series.db.beans.DatasetEntity) OmObservationConstellation(org.n52.shetland.ogc.om.OmObservationConstellation) AbstractObservationDAO(org.n52.sos.ds.hibernate.dao.observation.AbstractObservationDAO) OwsExceptionReport(org.n52.shetland.ogc.ows.exception.OwsExceptionReport) MultiObservationValues(org.n52.shetland.ogc.om.MultiObservationValues)

Example 2 with GetFeatureOfInterestDao

use of org.n52.sos.ds.dao.GetFeatureOfInterestDao in project SOS by 52North.

the class InsertResultHandler method getFeature.

/**
 * Get the hibernate AbstractFeatureOfInterest object for an
 * AbstractFeature, returning it from the local cache if already requested
 *
 * @param abstractFeature
 *            the abstract feature
 * @param cache
 *            the feature cache
 * @param session
 *            Hibernate session
 * @return hibernet AbstractFeatureOfInterest
 * @throws OwsExceptionReport
 *             If an error occurs
 */
private AbstractFeatureEntity getFeature(AbstractFeature abstractFeature, Map<String, AbstractFeatureEntity> cache, Session session) throws OwsExceptionReport {
    AbstractFeatureEntity hFeature = cache.get(abstractFeature.getIdentifier());
    if (hFeature == null) {
        hFeature = getDaoFactory().getFeatureOfInterestDAO().checkOrInsert(abstractFeature, session);
        cache.put(abstractFeature.getIdentifier(), hFeature);
    }
    return hFeature;
}
Also used : AbstractFeatureEntity(org.n52.series.db.beans.AbstractFeatureEntity)

Example 3 with GetFeatureOfInterestDao

use of org.n52.sos.ds.dao.GetFeatureOfInterestDao in project SOS by 52North.

the class InsertResultTemplateHandler method insertResultTemplate.

@Override
public synchronized InsertResultTemplateResponse insertResultTemplate(InsertResultTemplateRequest request) throws OwsExceptionReport {
    InsertResultTemplateResponse response = new InsertResultTemplateResponse();
    response.setService(request.getService());
    response.setVersion(request.getVersion());
    response.setAcceptedTemplate(request.getIdentifier().getValue());
    Session session = null;
    Transaction transaction = null;
    try {
        session = sessionHolder.getSession();
        transaction = session.beginTransaction();
        OmObservationConstellation sosObsConst = request.getObservationTemplate();
        DatasetEntity obsConst = null;
        for (String offeringID : sosObsConst.getOfferings()) {
            obsConst = getDaoFactory().getSeriesDAO().checkSeries(sosObsConst, offeringID, session, Sos2Constants.InsertResultTemplateParams.proposedTemplate.name());
            if (obsConst != null) {
                // check if result structure elements are supported
                checkResultStructure(request.getResultStructure(), obsConst.getObservableProperty().getIdentifier(), sosObsConst);
                ProcedureEntity procedure = null;
                AbstractFeatureEntity<?> feature = null;
                if (sosObsConst.isSetFeatureOfInterest()) {
                    FeatureOfInterestDAO featureOfInterestDAO = getDaoFactory().getFeatureOfInterestDAO();
                    feature = featureOfInterestDAO.checkOrInsert(sosObsConst.getFeatureOfInterest(), session);
                    featureOfInterestDAO.checkOrInsertRelatedFeatureRelation(feature, obsConst.getOffering(), session);
                }
                if (sosObsConst.isSetProcedure()) {
                    procedure = obsConst.getProcedure();
                }
                checkOrInsertResultTemplate(request, obsConst, procedure, feature, session);
            } else {
                // TODO make better exception.
                throw new InvalidObservationTypeException(request.getObservationTemplate().getObservationType());
            }
        }
        session.flush();
        transaction.commit();
    } catch (HibernateException he) {
        if (transaction != null) {
            transaction.rollback();
        }
        throw new NoApplicableCodeException().causedBy(he).withMessage("Insert result template into database failed!");
    } catch (OwsExceptionReport owse) {
        if (transaction != null) {
            transaction.rollback();
        }
        throw owse;
    } finally {
        sessionHolder.returnSession(session);
    }
    return response;
}
Also used : InsertResultTemplateResponse(org.n52.shetland.ogc.sos.response.InsertResultTemplateResponse) FeatureOfInterestDAO(org.n52.sos.ds.hibernate.dao.FeatureOfInterestDAO) HibernateException(org.hibernate.HibernateException) NoApplicableCodeException(org.n52.shetland.ogc.ows.exception.NoApplicableCodeException) DatasetEntity(org.n52.series.db.beans.DatasetEntity) OmObservationConstellation(org.n52.shetland.ogc.om.OmObservationConstellation) InvalidObservationTypeException(org.n52.sos.exception.ows.concrete.InvalidObservationTypeException) ProcedureEntity(org.n52.series.db.beans.ProcedureEntity) Transaction(org.hibernate.Transaction) OwsExceptionReport(org.n52.shetland.ogc.ows.exception.OwsExceptionReport) Session(org.hibernate.Session)

Example 4 with GetFeatureOfInterestDao

use of org.n52.sos.ds.dao.GetFeatureOfInterestDao in project SOS by 52North.

the class InsertSensorHandler method insertSensor.

@Override
public synchronized InsertSensorResponse insertSensor(final InsertSensorRequest request) throws OwsExceptionReport {
    final InsertSensorResponse response = new InsertSensorResponse();
    response.setService(request.getService());
    response.setVersion(request.getVersion());
    final String assignedProcedureID = request.getAssignedProcedureIdentifier();
    // we use only the first offering for the response because swes 2.0
    // specifies only one single element
    final SosOffering firstAssignedOffering = request.getFirstAssignedOffering();
    Session session = null;
    Transaction transaction = null;
    try {
        session = getHibernateSessionHolder().getSession();
        transaction = session.beginTransaction();
        FormatDAO formatDAO = new FormatDAO();
        final FormatEntity procedureDescriptionFormat = formatDAO.getOrInsertFormatEntity(request.getProcedureDescriptionFormat(), session);
        if (procedureDescriptionFormat != null) {
            final ProcedureEntity hProcedure = new ProcedureDAO(getDaoFactory()).getOrInsertProcedure(assignedProcedureID, procedureDescriptionFormat, request.getProcedureDescription(), request.isType(), session);
            // TODO: set correct validTime,
            new ProcedureHistoryDAO(getDaoFactory()).insert(hProcedure, procedureDescriptionFormat, getSensorDescriptionFromProcedureDescription(request.getProcedureDescription()), new DateTime(DateTimeZone.UTC), session);
            if (!request.isType()) {
                final List<FormatEntity> observationTypes = formatDAO.getOrInsertFormatEntitys(request.getMetadata().getObservationTypes(), session);
                final List<FormatEntity> featureOfInterestTypes = formatDAO.getOrInsertFormatEntitys(request.getMetadata().getFeatureOfInterestTypes(), session);
                if (observationTypes != null && featureOfInterestTypes != null) {
                    final List<PhenomenonEntity> hObservableProperties = getOrInsertNewObservableProperties(request.getObservableProperty(), request.getProcedureDescription(), session);
                    Map<String, UnitEntity> hUnits = getOrInsertNewUnits(hObservableProperties, request.getProcedureDescription(), session);
                    final AbstractSeriesDAO seriesDAO = getDaoFactory().getSeriesDAO();
                    final OfferingDAO offeringDAO = getDaoFactory().getOfferingDAO();
                    Set<String> allParentOfferings = getAllParentOfferings(hProcedure);
                    Set<String> parentOfferings = getParentOfferings(hProcedure);
                    for (final SosOffering assignedOffering : request.getAssignedOfferings()) {
                        final List<RelatedFeatureEntity> hRelatedFeatures = new LinkedList<RelatedFeatureEntity>();
                        if (request.getRelatedFeatures() != null && !request.getRelatedFeatures().isEmpty()) {
                            final RelatedFeatureDAO relatedFeatureDAO = getDaoFactory().getRelatedFeatureDAO();
                            for (final SwesFeatureRelationship relatedFeature : request.getRelatedFeatures()) {
                                hRelatedFeatures.addAll(relatedFeatureDAO.getOrInsertRelatedFeature(relatedFeature.getFeature(), relatedFeature.getRole(), session));
                            }
                        }
                        final OfferingEntity hOffering = offeringDAO.getAndUpdateOrInsert(new SosOffering(assignedOffering.getIdentifier(), assignedOffering.getOfferingName()), hRelatedFeatures, observationTypes, featureOfInterestTypes, session);
                        // is a child/component
                        if (!parentOfferings.isEmpty() && !allParentOfferings.isEmpty() && hProcedure.hasParents() && !allParentOfferings.contains(assignedOffering.getIdentifier()) && !parentOfferings.contains(assignedOffering.getIdentifier())) {
                            offeringDAO.updateParentOfferings(parentOfferings, hOffering, session);
                        }
                        for (final PhenomenonEntity hObservableProperty : hObservableProperties) {
                            CategoryEntity hCategory = getCategory(hObservableProperty, request, session);
                            ObservationContext ctx = new ObservationContext().setCategory(hCategory).setOffering(hOffering).setPhenomenon(hObservableProperty).setProcedure(hProcedure).setPublish(false).setHiddenChild(!assignedOffering.isParentOffering());
                            checkForMobileInsituFlags(ctx, request.getProcedureDescription().getProcedureDescription());
                            if (hUnits.containsKey(hObservableProperty.getIdentifier())) {
                                ctx.setUnit(hUnits.get(hObservableProperty.getIdentifier()));
                            }
                            if (request.getProcedureDescription().isSetFeaturesOfInterestMap()) {
                                boolean inserted = false;
                                for (AbstractFeature feature : request.getProcedureDescription().getFeaturesOfInterestMap().values()) {
                                    if (feature instanceof FeatureWithGeometry && ((FeatureWithGeometry) feature).isSetGeometry()) {
                                        ctx.setFeatureOfInterest(getDaoFactory().getFeatureOfInterestDAO().checkOrInsert(feature, session));
                                        inserted = true;
                                        seriesDAO.getOrInsert(ctx, session);
                                    }
                                }
                                if (!inserted) {
                                    seriesDAO.getOrInsert(ctx, session);
                                }
                            } else {
                                seriesDAO.getOrInsert(ctx, session);
                            }
                            if (checkPreconditionsOfStaticReferenceValues(request)) {
                                addStaticReferenceValues(request, session, procedureDescriptionFormat, hProcedure, observationTypes, featureOfInterestTypes, hRelatedFeatures, hOffering, hObservableProperty, seriesDAO);
                            }
                        }
                    }
                // TODO: parent and child procedures
                } else {
                    throw new NoApplicableCodeException().withMessage("Error while inserting InsertSensor into database!");
                }
            }
            response.setAssignedProcedure(assignedProcedureID);
            response.setAssignedOffering(firstAssignedOffering.getIdentifier());
        } else {
            throw new InvalidParameterValueException(Sos2Constants.InsertSensorParams.procedureDescriptionFormat, request.getProcedureDescriptionFormat());
        }
        session.flush();
        transaction.commit();
    } catch (final HibernateException he) {
        if (transaction != null) {
            transaction.rollback();
        }
        throw new NoApplicableCodeException().causedBy(he).withMessage("Error while inserting sensor data into database!");
    } finally {
        getHibernateSessionHolder().returnSession(session);
    }
    return response;
}
Also used : NoApplicableCodeException(org.n52.shetland.ogc.ows.exception.NoApplicableCodeException) PhenomenonEntity(org.n52.series.db.beans.PhenomenonEntity) InsertSensorResponse(org.n52.shetland.ogc.sos.response.InsertSensorResponse) DateTime(org.joda.time.DateTime) ProcedureEntity(org.n52.series.db.beans.ProcedureEntity) ProcedureDAO(org.n52.sos.ds.hibernate.dao.ProcedureDAO) InvalidParameterValueException(org.n52.shetland.ogc.ows.exception.InvalidParameterValueException) FormatDAO(org.n52.sos.ds.hibernate.dao.FormatDAO) RelatedFeatureDAO(org.n52.sos.ds.hibernate.dao.RelatedFeatureDAO) SosOffering(org.n52.shetland.ogc.sos.SosOffering) UnitEntity(org.n52.series.db.beans.UnitEntity) AbstractSeriesDAO(org.n52.sos.ds.hibernate.dao.observation.series.AbstractSeriesDAO) CategoryEntity(org.n52.series.db.beans.CategoryEntity) HibernateException(org.hibernate.HibernateException) FormatEntity(org.n52.series.db.beans.FormatEntity) AbstractFeature(org.n52.shetland.ogc.gml.AbstractFeature) SwesFeatureRelationship(org.n52.shetland.ogc.swes.SwesFeatureRelationship) OfferingDAO(org.n52.sos.ds.hibernate.dao.OfferingDAO) LinkedList(java.util.LinkedList) RelatedFeatureEntity(org.n52.series.db.beans.RelatedFeatureEntity) Transaction(org.hibernate.Transaction) FeatureWithGeometry(org.n52.shetland.ogc.gml.FeatureWith.FeatureWithGeometry) ObservationContext(org.n52.sos.ds.hibernate.dao.observation.ObservationContext) ProcedureHistoryDAO(org.n52.sos.ds.hibernate.dao.ProcedureHistoryDAO) OfferingEntity(org.n52.series.db.beans.OfferingEntity) Session(org.hibernate.Session)

Example 5 with GetFeatureOfInterestDao

use of org.n52.sos.ds.dao.GetFeatureOfInterestDao in project SOS by 52North.

the class InsertObservationHandler method getFeature.

/**
 * Get the hibernate AbstractFeatureOfInterest object for an
 * AbstractFeature, returning it from the local cache if already requested
 *
 * @param abstractFeature
 *            the abstract features
 * @param cache
 *            THe insertion cache
 * @param session
 *            Hiberante session
 * @return hibernet AbstractFeatureOfInterest
 * @throws OwsExceptionReport
 *             If an error occurs
 */
private AbstractFeatureEntity getFeature(AbstractFeature abstractFeature, InsertObservationCache cache, Session session) throws OwsExceptionReport {
    AbstractFeatureEntity hFeature = cache.getFeature(abstractFeature);
    if (hFeature == null) {
        hFeature = getDaoFactory().getFeatureOfInterestDAO().checkOrInsert(abstractFeature, session);
        cache.putFeature(abstractFeature, hFeature);
    }
    return hFeature;
}
Also used : AbstractFeatureEntity(org.n52.series.db.beans.AbstractFeatureEntity)

Aggregations

AbstractFeatureEntity (org.n52.series.db.beans.AbstractFeatureEntity)3 HibernateException (org.hibernate.HibernateException)2 Session (org.hibernate.Session)2 Transaction (org.hibernate.Transaction)2 DatasetEntity (org.n52.series.db.beans.DatasetEntity)2 ProcedureEntity (org.n52.series.db.beans.ProcedureEntity)2 OmObservationConstellation (org.n52.shetland.ogc.om.OmObservationConstellation)2 NoApplicableCodeException (org.n52.shetland.ogc.ows.exception.NoApplicableCodeException)2 OwsExceptionReport (org.n52.shetland.ogc.ows.exception.OwsExceptionReport)2 LinkedList (java.util.LinkedList)1 DateTime (org.joda.time.DateTime)1 CategoryEntity (org.n52.series.db.beans.CategoryEntity)1 FormatEntity (org.n52.series.db.beans.FormatEntity)1 OfferingEntity (org.n52.series.db.beans.OfferingEntity)1 PhenomenonEntity (org.n52.series.db.beans.PhenomenonEntity)1 RelatedFeatureEntity (org.n52.series.db.beans.RelatedFeatureEntity)1 UnitEntity (org.n52.series.db.beans.UnitEntity)1 AbstractFeature (org.n52.shetland.ogc.gml.AbstractFeature)1 FeatureWithGeometry (org.n52.shetland.ogc.gml.FeatureWith.FeatureWithGeometry)1 MultiObservationValues (org.n52.shetland.ogc.om.MultiObservationValues)1