Search in sources :

Example 1 with ResultTemplateEntity

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

the class GetResultHandler method getResult.

@Override
public GetResultResponse getResult(final GetResultRequest request) throws OwsExceptionReport {
    Session session = null;
    try {
        session = sessionHolder.getSession();
        final GetResultResponse response = new GetResultResponse();
        response.setService(request.getService());
        response.setVersion(request.getVersion());
        final Set<String> featureIdentifier = QueryHelper.getFeatures(getDaoFactory().getFeatureQueryHandler(), request, session);
        final ResultTemplateEntity resultTemplate = queryResultTemplate(request, featureIdentifier, session);
        SosResultEncoding sosResultEncoding = null;
        SosResultStructure sosResultStructure = null;
        if (resultTemplate != null) {
            sosResultEncoding = createSosResultEncoding(resultTemplate.getEncoding());
            sosResultStructure = createSosResultStructure(resultTemplate.getStructure());
        } else {
            sosResultEncoding = createSosResultEncoding();
            sosResultStructure = generateSosResultStructure(request.getObservedProperty(), request.getOffering(), featureIdentifier, session);
        }
        final List<DataEntity<?>> observations;
        observations = querySeriesObservation(request, featureIdentifier, session);
        response.setResultValues(getResultHandlingHelper().createResultValuesFromObservations(observations, sosResultEncoding, sosResultStructure, getProfileHandler().getActiveProfile().getResponseNoDataPlaceholder(), session));
        return response;
    } catch (final HibernateException he) {
        throw new NoApplicableCodeException().causedBy(he).withMessage("Error while querying result data!").setStatus(HTTPStatus.INTERNAL_SERVER_ERROR);
    } finally {
        sessionHolder.returnSession(session);
    }
}
Also used : HibernateException(org.hibernate.HibernateException) NoApplicableCodeException(org.n52.shetland.ogc.ows.exception.NoApplicableCodeException) GetResultResponse(org.n52.shetland.ogc.sos.response.GetResultResponse) ResultTemplateEntity(org.n52.series.db.beans.ResultTemplateEntity) DataEntity(org.n52.series.db.beans.DataEntity) SosResultEncoding(org.n52.shetland.ogc.sos.SosResultEncoding) SosResultStructure(org.n52.shetland.ogc.sos.SosResultStructure) Session(org.hibernate.Session)

Example 2 with ResultTemplateEntity

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

the class InsertResultHandler method getObservation.

/**
 * Get internal observation
 *
 * @param resultTemplate
 *            Associated ResultTemplate
 * @param blockValues
 *            Block values from result values
 * @param resultStructure
 *            Associated ResultStructure
 * @param encoding
 *            Associated ResultEncoding
 * @param session
 *            Hibernate session
 * @return Internal observation
 * @throws OwsExceptionReport
 *             If processing fails
 */
private OmObservation getObservation(final ResultTemplateEntity resultTemplate, final String[] blockValues, final SweAbstractDataComponent resultStructure, final SweAbstractEncoding encoding, final Session session) throws OwsExceptionReport {
    final int resultTimeIndex = helper.hasResultTime(resultStructure);
    final int phenomenonTimeIndex = helper.hasPhenomenonTime(resultStructure);
    final SweDataRecord record = setRecordFrom(resultStructure);
    final Map<Integer, String> observedProperties = new HashMap<Integer, String>(record.getFields().size() - 1);
    final Map<Integer, String> units = new HashMap<Integer, String>(record.getFields().size() - 1);
    final Map<Integer, String> featureOfInterest = new HashMap<Integer, String>(record.getFields().size() - 1);
    final Map<Integer, String> procedure = new HashMap<Integer, String>(record.getFields().size() - 1);
    int j = 0;
    getIndexFor(record, j, observedProperties, units, featureOfInterest, procedure, Sets.newHashSet(resultTimeIndex, phenomenonTimeIndex), encoding);
    final MultiObservationValues<SweDataArray> sosValues = createObservationValueFrom(blockValues, record, encoding, resultTimeIndex, phenomenonTimeIndex);
    final OmObservation observation = new OmObservation();
    observation.setObservationConstellation(getSosObservationConstellation(resultTemplate, session));
    observation.setResultType(OmConstants.OBS_TYPE_SWE_ARRAY_OBSERVATION);
    observation.setValue(sosValues);
    return observation;
}
Also used : SweDataRecord(org.n52.shetland.ogc.swe.SweDataRecord) HashMap(java.util.HashMap) OmObservation(org.n52.shetland.ogc.om.OmObservation) SweDataArray(org.n52.shetland.ogc.swe.SweDataArray)

Example 3 with ResultTemplateEntity

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

the class HibernateObservationUtilitiesTest method createSubObservationOfSweArrayObservationViaGetObservationById.

@Test
public // present
void createSubObservationOfSweArrayObservationViaGetObservationById() throws OwsExceptionReport, ConnectionProviderException, ConverterException {
    // PREPARE
    Session session = getSession();
    try {
        Transaction transaction = session.beginTransaction();
        GetObservationByIdRequest request = new GetObservationByIdRequest();
        request.setVersion(Sos2Constants.SERVICEVERSION);
        FormatEntity hProcedureDescriptionFormat = new FormatEntity();
        FormatEntity hFeatureOfInterestType = new FormatEntity();
        FeatureEntity hFeatureOfInterest = new FeatureEntity();
        PhenomenonEntity hObservableProperty = new PhenomenonEntity();
        CategoryEntity hCategory = new CategoryEntity();
        PlatformEntity hPlatform = new PlatformEntity();
        FormatEntity hObservationType = new FormatEntity();
        OfferingEntity hOffering = new OfferingEntity();
        DatasetEntity hObservationConstellation = new DatasetEntity();
        CodespaceEntity hCodespace = new CodespaceEntity();
        ProcedureEntity hProcedure = new ProcedureEntity();
        QuantityDataEntity hObservation = new QuantityDataEntity();
        ResultTemplateEntity hResultTemplateEntity = new ResultTemplateEntity();
        hProcedureDescriptionFormat.setFormat(PROCEDURE_DESCRIPTION_FORMAT);
        hCodespace.setName(CODESPACE);
        hProcedure.setIdentifier(PROCEDURE);
        hProcedure.setFormat(hProcedureDescriptionFormat);
        hFeatureOfInterestType.setFormat(FEATURE_OF_INTEREST_TYPE);
        hFeatureOfInterest.setIdentifier(FEATURE);
        hFeatureOfInterest.setFeatureType(hFeatureOfInterestType);
        hFeatureOfInterest.setIdentifierCodespace(hCodespace);
        hPlatform.setIdentifier(FEATURE);
        hObservableProperty.setIdentifier(OBSERVABLE_PROPERTY);
        hCategory.setIdentifier(OBSERVABLE_PROPERTY);
        hObservationType.setFormat(OmConstants.OBS_TYPE_SWE_ARRAY_OBSERVATION);
        hOffering.setIdentifier(OFFERING);
        hObservationConstellation.setProcedure(hProcedure);
        hObservationConstellation.setOffering(hOffering);
        hObservationConstellation.setObservableProperty(hObservableProperty);
        hObservationConstellation.setOmObservationType(hObservationType);
        hObservationConstellation.setFeature(hFeatureOfInterest);
        hObservationConstellation.setDeleted(false);
        hObservationConstellation.setHidden(false);
        hObservationConstellation.setCategory(hCategory);
        hObservationConstellation.setPlatform(hPlatform);
        hResultTemplateEntity.setFeature(hFeatureOfInterest);
        hResultTemplateEntity.setOffering(hOffering);
        hResultTemplateEntity.setPhenomenon(hObservableProperty);
        hResultTemplateEntity.setProcedure(hProcedure);
        hResultTemplateEntity.setIdentifier("test-rt-1");
        hResultTemplateEntity.setEncoding("<swe:TextEncoding xmlns:swe=\"http://www.opengis.net/swe/2.0\" " + "xmlns:sams=\"http://www.opengis.net/samplingSpatial/2.0\" " + "xmlns:sml=\"http://www.opengis.net/sensorML/1.0.1\" " + "xmlns:sf=\"http://www.opengis.net/sampling/2.0\" " + "xmlns:swes=\"http://www.opengis.net/swes/2.0\" " + "xmlns:sos=\"http://www.opengis.net/sos/2.0\" " + "xmlns:gml=\"http://www.opengis.net/gml/3.2\" " + "xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" " + "xmlns:om=\"http://www.opengis.net/om/2.0\" " + "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" " + "xmlns:xlink=\"http://www.w3.org/1999/xlink\" " + "tokenSeparator=\"#\" blockSeparator=\"@\"/>");
        hResultTemplateEntity.setStructure("\"<swe:DataRecord xmlns:swe=\"http://www.opengis.net/swe/2.0\" " + "xmlns:sams=\"http://www.opengis.net/samplingSpatial/2.0\" " + "xmlns:sml=\"http://www.opengis.net/sensorML/1.0.1\" " + "xmlns:sf=\"http://www.opengis.net/sampling/2.0\" " + "xmlns:swes=\"http://www.opengis.net/swes/2.0\" " + "xmlns:sos=\"http://www.opengis.net/sos/2.0\" " + "xmlns:gml=\"http://www.opengis.net/gml/3.2\" " + "xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" " + "xmlns:om=\"http://www.opengis.net/om/2.0\" " + "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" " + "xmlns:xlink=\"http://www.w3.org/1999/xlink\">\r\n" + "  <swe:field name=\"phenomenonTime\">\r\n" + "    <swe:Time definition=\"http://www.opengis.net/def/property/OGC/0/PhenomenonTime\">\r\n" + "      <swe:uom xlink:href=\"http://www.opengis.net/def/uom/ISO-8601/0/Gregorian\"/>\r\n" + "    </swe:Time>\r\n" + "  </swe:field>\r\n" + "  <swe:field name=\"Discharge\">\r\n" + "    <swe:Quantity definition=\"http://sweet.jpl.nasa.gov/2.0/hydroSurface.owl#Discharge\">\r\n" + "      <swe:uom code=\"m3\"/>\r\n" + "    </swe:Quantity>\r\n" + "  </swe:field>\r\n" + "</swe:DataRecord>\"");
        session.save(hProcedureDescriptionFormat);
        session.save(hProcedure);
        session.save(hCodespace);
        session.save(hOffering);
        session.save(hFeatureOfInterestType);
        session.save(hFeatureOfInterest);
        session.save(hPlatform);
        session.save(hObservableProperty);
        session.save(hCategory);
        session.save(hObservationType);
        session.save(hObservationConstellation);
        session.save(hResultTemplateEntity);
        session.flush();
        hObservation.setValue(BigDecimal.valueOf(1.0));
        hObservation.setSamplingTimeStart(DateTime.now().toDate());
        hObservation.setSamplingTimeEnd(hObservation.getSamplingTimeStart());
        hObservation.setResultTime(hObservation.getSamplingTimeStart());
        hObservation.setDataset(hObservationConstellation);
        hObservation.setStaIdentifier("123");
        hObservation.setDeleted(false);
        session.save(hObservation);
        transaction.commit();
        ArrayList<DataEntity<?>> observationsFromDataBase = new ArrayList<>();
        observationsFromDataBase.add(hObservation);
        SosHelper sosHelper = new SosHelper();
        sosHelper.setServiceURL(URI.create("http://test.org/"));
        DaoFactory daoFactory = new DaoFactory();
        daoFactory.setSosHelper(sosHelper);
        // CALL
        OmObservationCreatorContext ctx = new OmObservationCreatorContext(null, null, daoFactory, new ProfileHanlderMock(), Mockito.mock(AdditionalObservationCreatorRepository.class), null, new FeatureQueryHandlerMock(), null, null, null, null, null, Mockito.mock(BindingRepository.class));
        ObservationStream resultList = HibernateObservationUtilities.createSosObservationsFromObservations(observationsFromDataBase, request, Locale.ENGLISH, null, ctx, session);
        // TEST RESULTS
        assertThat(resultList, is(notNullValue()));
        assertThat(resultList.hasNext(), is(true));
        Object value = resultList.next().getValue().getValue();
        assertThat(resultList.hasNext(), is(false));
        assertThat(value, is(instanceOf(QuantityValue.class)));
        Double val = ((QuantityValue) value).getValue().doubleValue();
        // assertThat(value, is(instanceOf(SweDataArray.class)));
        // Double val = Double.parseDouble(((SweDataArray) value).getValues().get(0).get(1));
        assertThat(val, is(closeTo(1.0, 0.00001)));
    } finally {
        returnSession(session);
    }
}
Also used : CodespaceEntity(org.n52.series.db.beans.CodespaceEntity) ArrayList(java.util.ArrayList) PhenomenonEntity(org.n52.series.db.beans.PhenomenonEntity) DaoFactory(org.n52.sos.ds.hibernate.dao.DaoFactory) AdditionalObservationCreatorRepository(org.n52.sos.ds.hibernate.util.observation.AdditionalObservationCreatorRepository) ProcedureEntity(org.n52.series.db.beans.ProcedureEntity) PlatformEntity(org.n52.series.db.beans.PlatformEntity) QuantityDataEntity(org.n52.series.db.beans.QuantityDataEntity) SosHelper(org.n52.sos.util.SosHelper) GetObservationByIdRequest(org.n52.shetland.ogc.sos.request.GetObservationByIdRequest) CategoryEntity(org.n52.series.db.beans.CategoryEntity) FeatureEntity(org.n52.series.db.beans.FeatureEntity) OmObservationCreatorContext(org.n52.sos.ds.hibernate.util.observation.OmObservationCreatorContext) FormatEntity(org.n52.series.db.beans.FormatEntity) DatasetEntity(org.n52.series.db.beans.DatasetEntity) Transaction(org.hibernate.Transaction) ObservationStream(org.n52.shetland.ogc.om.ObservationStream) BindingRepository(org.n52.iceland.binding.BindingRepository) ResultTemplateEntity(org.n52.series.db.beans.ResultTemplateEntity) DataEntity(org.n52.series.db.beans.DataEntity) QuantityDataEntity(org.n52.series.db.beans.QuantityDataEntity) OfferingEntity(org.n52.series.db.beans.OfferingEntity) Session(org.hibernate.Session) Test(org.junit.Test)

Example 4 with ResultTemplateEntity

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

the class ResultTemplateCacheUpdate method execute.

@Override
public void execute() {
    LOGGER.debug("Executing ResultTemplateCacheUpdate");
    startStopwatch();
    if (HibernateHelper.isEntitySupported(ResultTemplateEntity.class)) {
        List<ResultTemplateEntity> resultTemplates = getResultTemplateObjects(getSession());
        for (ResultTemplateEntity resultTemplate : resultTemplates) {
            String id = resultTemplate.getIdentifier();
            getCache().addResultTemplate(id);
            getCache().addResultTemplateForOffering(resultTemplate.getOffering().getIdentifier(), id);
            getCache().addObservablePropertyForResultTemplate(id, resultTemplate.getPhenomenon().getIdentifier());
            if (resultTemplate.getFeature() != null) {
                getCache().addFeatureOfInterestForResultTemplate(id, resultTemplate.getFeature().getIdentifier());
            }
        }
    }
    LOGGER.debug("Finished executing ResultTemplateCacheUpdate ({})", getStopwatchResult());
}
Also used : ResultTemplateEntity(org.n52.series.db.beans.ResultTemplateEntity)

Example 5 with ResultTemplateEntity

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

Aggregations

ResultTemplateEntity (org.n52.series.db.beans.ResultTemplateEntity)13 Session (org.hibernate.Session)4 NoApplicableCodeException (org.n52.shetland.ogc.ows.exception.NoApplicableCodeException)4 SosResultEncoding (org.n52.shetland.ogc.sos.SosResultEncoding)4 SosResultStructure (org.n52.shetland.ogc.sos.SosResultStructure)4 HibernateException (org.hibernate.HibernateException)3 DataEntity (org.n52.series.db.beans.DataEntity)3 DatasetEntity (org.n52.series.db.beans.DatasetEntity)3 OmObservation (org.n52.shetland.ogc.om.OmObservation)3 InternalInsertResultTemplateRequest (org.n52.sos.request.InternalInsertResultTemplateRequest)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 Criteria (org.hibernate.Criteria)2 Transaction (org.hibernate.Transaction)2 CodespaceEntity (org.n52.series.db.beans.CodespaceEntity)2 FormatEntity (org.n52.series.db.beans.FormatEntity)2 OmObservationConstellation (org.n52.shetland.ogc.om.OmObservationConstellation)2 InvalidParameterValueException (org.n52.shetland.ogc.ows.exception.InvalidParameterValueException)2 DeleteResultTemplateInvalidParameterValueException (org.n52.sos.exception.sos.concrete.DeleteResultTemplateInvalidParameterValueException)2 AbstractMap (java.util.AbstractMap)1