Search in sources :

Example 1 with InternalInsertResultTemplateRequest

use of org.n52.sos.request.InternalInsertResultTemplateRequest 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 2 with InternalInsertResultTemplateRequest

use of org.n52.sos.request.InternalInsertResultTemplateRequest in project SOS by 52North.

the class ResultTemplateDAO method updateTemplates.

private void updateTemplates(List<ResultTemplateEntity> resultTemplates, InternalInsertResultTemplateRequest request, Session session) throws EncodingException {
    for (ResultTemplateEntity resultTemplate : resultTemplates) {
        if (!resultTemplate.isSetObservationEncoding() || !resultTemplate.isSetObservationStructure()) {
            setObservationStructureEncoding(resultTemplate, request);
            session.update(resultTemplate);
            session.flush();
            session.refresh(resultTemplate);
        }
    }
}
Also used : ResultTemplateEntity(org.n52.series.db.beans.ResultTemplateEntity)

Example 3 with InternalInsertResultTemplateRequest

use of org.n52.sos.request.InternalInsertResultTemplateRequest in project SOS by 52North.

the class ResultTemplateDAO method createAndSaveResultTemplate.

/**
 * Insert result template
 *
 * @param request
 *            Insert result template request
 * @param observationConstellation
 *            Observation constellation object
 * @param procedure
 *            the procedure
 * @param featureOfInterest
 *            FeatureOfInterest object
 * @param session
 *            Hibernate session
 * @return created or exiting {@link ResultTemplateEntity}
 * @throws OwsExceptionReport
 *             If an error occurs
 */
private ResultTemplateEntity createAndSaveResultTemplate(final InsertResultTemplateRequest request, final DatasetEntity observationConstellation, ProcedureEntity procedure, final AbstractFeatureEntity featureOfInterest, final Session session) throws EncodingException {
    final ResultTemplateEntity resultTemplate = new ResultTemplateEntity();
    resultTemplate.setIdentifier(request.getIdentifier().getValue());
    resultTemplate.setPhenomenon(observationConstellation.getObservableProperty());
    resultTemplate.setOffering(observationConstellation.getOffering());
    if (procedure != null) {
        resultTemplate.setProcedure(procedure);
    }
    if (featureOfInterest != null) {
        resultTemplate.setFeature(featureOfInterest);
    }
    if (request.isSetResultEncoding()) {
        if (request.getResultEncoding().getXml().isPresent()) {
            resultTemplate.setEncoding(request.getResultEncoding().getXml().get());
        } else {
            resultTemplate.setEncoding(encodeObjectToXmlText(SweConstants.NS_SWE_20, request.getResultEncoding().get().get()));
        }
    }
    if (request.isSetResultStructure()) {
        if (request.getResultStructure().getXml().isPresent()) {
            resultTemplate.setStructure(request.getResultStructure().getXml().get());
        } else {
            resultTemplate.setStructure(encodeObjectToXmlText(SweConstants.NS_SWE_20, request.getResultStructure().get().get()));
        }
    }
    if (request instanceof InternalInsertResultTemplateRequest) {
        setObservationStructureEncoding(resultTemplate, (InternalInsertResultTemplateRequest) request);
    }
    session.save(resultTemplate);
    session.flush();
    session.refresh(resultTemplate);
    return resultTemplate;
}
Also used : ResultTemplateEntity(org.n52.series.db.beans.ResultTemplateEntity) InternalInsertResultTemplateRequest(org.n52.sos.request.InternalInsertResultTemplateRequest)

Example 4 with InternalInsertResultTemplateRequest

use of org.n52.sos.request.InternalInsertResultTemplateRequest in project SOS by 52North.

the class ObservationPersister method visit.

@Override
public DataEntity<?> visit(SweDataArrayValue value) throws OwsExceptionReport {
    DataArrayDataEntity dataArray = observationFactory.sweDataEntityArray();
    // if (value.getValue().getElementType() instanceof
    // SweAbstractDataRecord
    // && checkFields((SweAbstractDataRecord)
    // value.getValue().getElementType())) {
    // dataArray = persist(dataArray, new HashSet<DataEntity<?>>());
    // persistChildren(value.getValue(), dataArray.getId());
    // } else {
    dataArray.setStringValue(value.getValue().getValueAsString());
    dataArray = (DataArrayDataEntity) persist((DataEntity) dataArray, new HashSet<DataEntity<?>>());
    // }
    // create result template
    InternalInsertResultTemplateRequest insertResultTemplateRequest = new InternalInsertResultTemplateRequest();
    insertResultTemplateRequest.setObservationEncoding(new SosResultEncoding(value.getValue().getEncoding(), value.getValue().getEncoding().getXml()));
    insertResultTemplateRequest.setObservationStructure(new SosResultStructure(value.getValue().getElementType(), value.getValue().getElementType().getXml()));
    insertResultTemplateRequest.setIdentifier("OBS_" + IdGenerator.generate(value.getValue().getElementType().getXml()));
    ResultTemplateEntity resultTemplate = daoFactory.getResultTemplateDAO().checkOrInsertResultTemplate(insertResultTemplateRequest, dataArray.getDataset(), session);
    dataArray.setResultTemplate(resultTemplate);
    session.update(dataArray);
    return dataArray;
}
Also used : DataArrayDataEntity(org.n52.series.db.beans.DataArrayDataEntity) ResultTemplateEntity(org.n52.series.db.beans.ResultTemplateEntity) DataEntity(org.n52.series.db.beans.DataEntity) DataArrayDataEntity(org.n52.series.db.beans.DataArrayDataEntity) GeometryDataEntity(org.n52.series.db.beans.GeometryDataEntity) ProfileDataEntity(org.n52.series.db.beans.ProfileDataEntity) ComplexDataEntity(org.n52.series.db.beans.ComplexDataEntity) ReferencedDataEntity(org.n52.series.db.beans.ReferencedDataEntity) InternalInsertResultTemplateRequest(org.n52.sos.request.InternalInsertResultTemplateRequest) SosResultEncoding(org.n52.shetland.ogc.sos.SosResultEncoding) SosResultStructure(org.n52.shetland.ogc.sos.SosResultStructure)

Aggregations

ResultTemplateEntity (org.n52.series.db.beans.ResultTemplateEntity)4 InternalInsertResultTemplateRequest (org.n52.sos.request.InternalInsertResultTemplateRequest)3 SosResultEncoding (org.n52.shetland.ogc.sos.SosResultEncoding)2 SosResultStructure (org.n52.shetland.ogc.sos.SosResultStructure)2 ArrayList (java.util.ArrayList)1 ComplexDataEntity (org.n52.series.db.beans.ComplexDataEntity)1 DataArrayDataEntity (org.n52.series.db.beans.DataArrayDataEntity)1 DataEntity (org.n52.series.db.beans.DataEntity)1 GeometryDataEntity (org.n52.series.db.beans.GeometryDataEntity)1 ProfileDataEntity (org.n52.series.db.beans.ProfileDataEntity)1 ReferencedDataEntity (org.n52.series.db.beans.ReferencedDataEntity)1 InvalidParameterValueException (org.n52.shetland.ogc.ows.exception.InvalidParameterValueException)1 NoApplicableCodeException (org.n52.shetland.ogc.ows.exception.NoApplicableCodeException)1 DecodingException (org.n52.svalbard.decode.exception.DecodingException)1 XmlDecodingException (org.n52.svalbard.decode.exception.XmlDecodingException)1 EncodingException (org.n52.svalbard.encode.exception.EncodingException)1