Search in sources :

Example 1 with RelatedFeatureEntity

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

the class RelatedFeaturesCacheUpdate method execute.

@Override
public void execute() {
    LOGGER.debug("Executing RelatedFeaturesCacheUpdate");
    startStopwatch();
    try {
        for (RelatedFeatureEntity relatedFeature : new RelatedFeatureDao(getSession()).getAllInstances(new DbQuery(IoParameters.createDefaults()))) {
            String identifier = relatedFeature.getFeature().getIdentifier();
            for (OfferingEntity offering : relatedFeature.getOfferings()) {
                getCache().addRelatedFeatureForOffering(offering.getIdentifier(), identifier);
            }
            getCache().addRoleForRelatedFeature(identifier, relatedFeature.getRole());
        }
    } catch (HibernateException | DataAccessException dae) {
        getErrors().add(new NoApplicableCodeException().causedBy(dae).withMessage("Error while updating related feature cache!"));
    }
    LOGGER.debug("Finished executing RelatedFeaturesCacheUpdate ({})", getStopwatchResult());
}
Also used : DbQuery(org.n52.series.db.dao.DbQuery) HibernateException(org.hibernate.HibernateException) NoApplicableCodeException(org.n52.shetland.ogc.ows.exception.NoApplicableCodeException) RelatedFeatureDao(org.n52.series.db.dao.RelatedFeatureDao) OfferingEntity(org.n52.series.db.beans.OfferingEntity) DataAccessException(org.n52.series.db.DataAccessException) RelatedFeatureEntity(org.n52.series.db.beans.RelatedFeatureEntity)

Example 2 with RelatedFeatureEntity

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

the class RelatedFeatureDAO method getOrInsertRelatedFeature.

/**
 * Insert and get related feature objects.
 *
 * @param feature
 *            Related feature
 * @param role
 *            Related feature role objects
 * @param session
 *            Hibernate session
 * @return Related feature objects
 * @throws OwsExceptionReport
 *             If an error occurs
 */
public List<RelatedFeatureEntity> getOrInsertRelatedFeature(final AbstractFeature feature, final String role, final Session session) throws OwsExceptionReport {
    // TODO: create featureOfInterest and link to relatedFeature
    List<RelatedFeatureEntity> relFeats = getRelatedFeatures(feature.getIdentifierCodeWithAuthority().getValue(), session);
    if (relFeats == null) {
        relFeats = new LinkedList<>();
    }
    if (relFeats.isEmpty() && role != null && !role.isEmpty()) {
        final RelatedFeatureEntity relFeat = new RelatedFeatureEntity();
        String identifier = feature.getIdentifierCodeWithAuthority().getValue();
        String url = null;
        if (feature instanceof AbstractSamplingFeature) {
            identifier = daoFactory.getFeatureQueryHandler().insertFeature((AbstractSamplingFeature) feature, session);
            url = ((AbstractSamplingFeature) feature).getUrl();
        }
        relFeat.setFeature(daoFactory.getFeatureOfInterestDAO().getOrInsert(identifier, url, session));
        relFeat.setRole(role);
        session.save(relFeat);
        session.flush();
        relFeats.add(relFeat);
    }
    return relFeats;
}
Also used : AbstractSamplingFeature(org.n52.shetland.ogc.om.features.samplingFeatures.AbstractSamplingFeature) RelatedFeatureEntity(org.n52.series.db.beans.RelatedFeatureEntity)

Example 3 with RelatedFeatureEntity

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

the class OfferingDAO method getAndUpdateOrInsert.

/**
 * Insert or update and get offering
 *
 * @param assignedOffering
 *            SosOffering to insert, update or get
 * @param relatedFeatures
 *            Related feature objects
 * @param observationTypes
 *            Allowed observation type objects
 * @param featureOfInterestTypes
 *            Allowed featureOfInterest type objects
 * @param session
 *            Hibernate session
 * @return Offering object
 */
public OfferingEntity getAndUpdateOrInsert(SosOffering assignedOffering, Collection<RelatedFeatureEntity> relatedFeatures, Collection<FormatEntity> observationTypes, Collection<FormatEntity> featureOfInterestTypes, Session session) {
    OfferingEntity offering = getTOfferingForIdentifier(assignedOffering.getIdentifier(), session);
    if (offering == null) {
        offering = new OfferingEntity();
        offering.setIdentifier(assignedOffering.getIdentifier(), getDaoFactory().isStaSupportsUrls());
        if (assignedOffering.isSetName()) {
            offering.setName(assignedOffering.getFirstName().getValue());
        } else {
            offering.setName("Offering for the procedure " + assignedOffering.getIdentifier());
        }
        if (assignedOffering.isSetDescription()) {
            offering.setDescription(assignedOffering.getDescription());
        }
    }
    if (!relatedFeatures.isEmpty()) {
        offering.setRelatedFeatures(new HashSet<>(relatedFeatures));
    } else {
        offering.setRelatedFeatures(new HashSet<RelatedFeatureEntity>(0));
    }
    if (!observationTypes.isEmpty()) {
        offering.setObservationTypes(new HashSet<>(observationTypes));
    } else {
        offering.setObservationTypes(new HashSet<FormatEntity>(0));
    }
    if (!featureOfInterestTypes.isEmpty()) {
        offering.setFeatureTypes(new HashSet<>(featureOfInterestTypes));
    } else {
        offering.setFeatureTypes(new HashSet<FormatEntity>(0));
    }
    session.saveOrUpdate(offering);
    session.flush();
    session.refresh(offering);
    return offering;
}
Also used : FormatEntity(org.n52.series.db.beans.FormatEntity) OfferingEntity(org.n52.series.db.beans.OfferingEntity) RelatedFeatureEntity(org.n52.series.db.beans.RelatedFeatureEntity)

Example 4 with RelatedFeatureEntity

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

the class InsertSensorHandler method addStaticReferenceValues.

private void addStaticReferenceValues(InsertSensorRequest request, Session session, FormatEntity procedureDescriptionFormat, ProcedureEntity hProcedure, List<FormatEntity> observationTypes, List<FormatEntity> featureOfInterestTypes, List<RelatedFeatureEntity> hRelatedFeatures, OfferingEntity hOffering, PhenomenonEntity hObservableProperty, AbstractSeriesDAO seriesDAO) throws OwsExceptionReport {
    AbstractFeature sosFeatureOfInterest = request.getProcedureDescription().getFeaturesOfInterestMap().entrySet().iterator().next().getValue();
    AbstractFeatureEntity<?> hFeature = getDaoFactory().getFeatureDAO().insertFeature(sosFeatureOfInterest, session);
    for (SmlCapability referenceValue : ((AbstractSensorML) request.getProcedureDescription().getProcedureDescription()).findCapabilities(REFERENCE_VALUES_PREDICATE).get().getCapabilities()) {
        if (!(referenceValue.getAbstractDataComponent() instanceof SweQuantity)) {
            throw new NoApplicableCodeException().withMessage("ReferenceValue of Type '%s' is not supported -> Aborting InsertSensor Operation!", referenceValue.getAbstractDataComponent().getDataComponentType());
        }
        SweQuantity referenceValueValue = (SweQuantity) referenceValue.getAbstractDataComponent();
        String identifier = hProcedure.getIdentifier() + REFERENCE_VALUE;
        SosProcedureDescription<?> procedureReferenceSeries = new SosProcedureDescriptionUnknownType(identifier, procedureDescriptionFormat.getFormat(), "");
        procedureReferenceSeries.setReference(true);
        procedureReferenceSeries.setName(new CodeType(referenceValue.getName()));
        ProcedureEntity hProcedureReferenceSeries = getDaoFactory().getProcedureDAO().getOrInsertProcedure(identifier, procedureDescriptionFormat, procedureReferenceSeries, false, session);
        OfferingEntity hOfferingReferenceSeries = getDaoFactory().getOfferingDAO().getAndUpdateOrInsert(new SosOffering(hOffering.getIdentifier() + REFERENCE_VALUE, hOffering.getName() + REFERENCE_VALUE), hRelatedFeatures, observationTypes, featureOfInterestTypes, session);
        TimeInstant time = new TimeInstant(new DateTime(0));
        SingleObservationValue<BigDecimal> sosValue = new SingleObservationValue<>(new QuantityValue(referenceValueValue.getValue(), referenceValueValue.getUom()));
        OmObservation sosObservation = new OmObservation();
        sosValue.setPhenomenonTime(time);
        sosObservation.setResultTime(time);
        sosObservation.setValue(sosValue);
        sosObservation.setObservationConstellation(new OmObservationConstellation(procedureReferenceSeries, new OmObservableProperty(hObservableProperty.getIdentifier()), sosFeatureOfInterest));
        DatasetEntity hObservationConstellationReferenceSeries = new DatasetEntity();
        hObservationConstellationReferenceSeries.setObservableProperty(hObservableProperty);
        hObservationConstellationReferenceSeries.setOffering(hOfferingReferenceSeries);
        hObservationConstellationReferenceSeries.setProcedure(hProcedureReferenceSeries);
        Map<String, CodespaceEntity> codespaceCache = CollectionHelper.synchronizedMap();
        Map<UoM, UnitEntity> unitCache = CollectionHelper.synchronizedMap();
        Map<String, FormatEntity> formatCache = CollectionHelper.synchronizedMap();
        ObservationPersister persister = new ObservationPersister(getDaoFactory(), getDaoFactory().getObservationDAO(), sosObservation, hObservationConstellationReferenceSeries, hFeature, codespaceCache, unitCache, formatCache, Collections.singleton(hOfferingReferenceSeries), session);
        DataEntity<?> observation = sosValue.getValue().accept(persister);
        DatasetEntity hReferenceSeries = seriesDAO.getSeries(hProcedureReferenceSeries.getIdentifier(), hObservableProperty.getIdentifier(), hOfferingReferenceSeries.getIdentifier(), Collections.singleton(hFeature.getIdentifier()), session).get(0);
        hReferenceSeries.setPublished(false);
        session.update(hReferenceSeries);
        ObservationContext ctxReferenced = new ObservationContext();
        ctxReferenced.setPhenomenon(hObservableProperty);
        ctxReferenced.setFeatureOfInterest(hFeature);
        ctxReferenced.setProcedure(hProcedure);
        ctxReferenced.setOffering(hOffering);
        ctxReferenced.setPublish(false);
        DatasetEntity hSeries = seriesDAO.getOrInsertSeries(ctxReferenced, observation, session);
        if (hSeries.getValueType().equals(ValueType.quantity)) {
            hSeries.setReferenceValues(Lists.newArrayList(hReferenceSeries));
        }
        session.update(hSeries);
    }
}
Also used : SweQuantity(org.n52.shetland.ogc.swe.simpleType.SweQuantity) NoApplicableCodeException(org.n52.shetland.ogc.ows.exception.NoApplicableCodeException) OmObservation(org.n52.shetland.ogc.om.OmObservation) CodespaceEntity(org.n52.series.db.beans.CodespaceEntity) DateTime(org.joda.time.DateTime) ProcedureEntity(org.n52.series.db.beans.ProcedureEntity) UoM(org.n52.shetland.ogc.UoM) ObservationPersister(org.n52.sos.ds.hibernate.dao.observation.ObservationPersister) SingleObservationValue(org.n52.shetland.ogc.om.SingleObservationValue) SosOffering(org.n52.shetland.ogc.sos.SosOffering) UnitEntity(org.n52.series.db.beans.UnitEntity) OmObservableProperty(org.n52.shetland.ogc.om.OmObservableProperty) SmlCapability(org.n52.shetland.ogc.sensorML.elements.SmlCapability) FormatEntity(org.n52.series.db.beans.FormatEntity) DatasetEntity(org.n52.series.db.beans.DatasetEntity) AbstractFeature(org.n52.shetland.ogc.gml.AbstractFeature) OmObservationConstellation(org.n52.shetland.ogc.om.OmObservationConstellation) BigDecimal(java.math.BigDecimal) AbstractSensorML(org.n52.shetland.ogc.sensorML.AbstractSensorML) QuantityValue(org.n52.shetland.ogc.om.values.QuantityValue) SosProcedureDescriptionUnknownType(org.n52.shetland.ogc.sos.SosProcedureDescriptionUnknownType) CodeType(org.n52.shetland.ogc.gml.CodeType) ObservationContext(org.n52.sos.ds.hibernate.dao.observation.ObservationContext) OfferingEntity(org.n52.series.db.beans.OfferingEntity) TimeInstant(org.n52.shetland.ogc.gml.time.TimeInstant)

Example 5 with RelatedFeatureEntity

use of org.n52.series.db.beans.RelatedFeatureEntity 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)

Aggregations

OfferingEntity (org.n52.series.db.beans.OfferingEntity)4 RelatedFeatureEntity (org.n52.series.db.beans.RelatedFeatureEntity)4 FormatEntity (org.n52.series.db.beans.FormatEntity)3 NoApplicableCodeException (org.n52.shetland.ogc.ows.exception.NoApplicableCodeException)3 HibernateException (org.hibernate.HibernateException)2 DateTime (org.joda.time.DateTime)2 ProcedureEntity (org.n52.series.db.beans.ProcedureEntity)2 UnitEntity (org.n52.series.db.beans.UnitEntity)2 AbstractFeature (org.n52.shetland.ogc.gml.AbstractFeature)2 SosOffering (org.n52.shetland.ogc.sos.SosOffering)2 ObservationContext (org.n52.sos.ds.hibernate.dao.observation.ObservationContext)2 BigDecimal (java.math.BigDecimal)1 LinkedList (java.util.LinkedList)1 Session (org.hibernate.Session)1 Transaction (org.hibernate.Transaction)1 DataAccessException (org.n52.series.db.DataAccessException)1 CategoryEntity (org.n52.series.db.beans.CategoryEntity)1 CodespaceEntity (org.n52.series.db.beans.CodespaceEntity)1 DatasetEntity (org.n52.series.db.beans.DatasetEntity)1 PhenomenonEntity (org.n52.series.db.beans.PhenomenonEntity)1