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);
}
}
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);
}
}
}
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;
}
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;
}
Aggregations