use of org.n52.series.db.beans.UnitEntity in project SOS by 52North.
the class HibernateObservationBuilder method getUnit.
protected UnitEntity getUnit() {
UnitEntity unit = (UnitEntity) session.createCriteria(UnitEntity.class).add(Restrictions.eq(UnitEntity.IDENTIFIER, UNIT)).uniqueResult();
if (unit == null) {
unit = new UnitEntity();
unit.setUnit(UNIT);
session.save(unit);
session.flush();
}
return unit;
}
use of org.n52.series.db.beans.UnitEntity 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);
}
}
use of org.n52.series.db.beans.UnitEntity 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;
}
use of org.n52.series.db.beans.UnitEntity in project SOS by 52North.
the class InsertResultHandler method insertResult.
@Override
public synchronized InsertResultResponse insertResult(final InsertResultRequest request) throws OwsExceptionReport {
final InsertResultResponse response = new InsertResultResponse();
response.setService(request.getService());
response.setVersion(request.getVersion());
Session session = null;
Transaction transaction = null;
Map<String, CodespaceEntity> codespaceCache = Maps.newHashMap();
Map<UoM, UnitEntity> unitCache = Maps.newHashMap();
Map<String, FormatEntity> formatCache = Maps.newHashMap();
try {
session = getHibernateSessionHolder().getSession();
final ResultTemplateEntity resultTemplate = getDaoFactory().getResultTemplateDAO().getResultTemplateObject(request.getTemplateIdentifier(), session);
transaction = session.beginTransaction();
final OmObservation o = getSingleObservationFromResultValues(response.getVersion(), resultTemplate, request.getResultValues(), session);
final List<OmObservation> observations = getSingleObservationsFromObservation(o);
if (o.getObservationConstellation().isSetFeatureOfInterest() && o.getObservationConstellation().isSetProcedure()) {
response.setObservation(o);
} else {
response.setObservations(observations);
}
final AbstractSeriesDAO obsConstDao = getDaoFactory().getSeriesDAO();
final FormatDAO obsTypeDao = getDaoFactory().getObservationTypeDAO();
Map<OmObservationConstellation, DatasetEntity> obsConsts = new HashMap<>();
int insertion = 0;
final int size = observations.size();
final AbstractObservationDAO observationDAO = getDaoFactory().getObservationDAO();
LOGGER.debug("Start saving {} observations.", size);
Map<String, AbstractFeatureEntity> featureEntityMap = new HashMap<>();
for (final OmObservation observation : observations) {
OmObservationConstellation omObsConst = observation.getObservationConstellation();
if (!obsConsts.containsKey(omObsConst)) {
DatasetEntity oc = obsConstDao.getSeries(omObsConst, session);
if (oc != null) {
obsConsts.put(omObsConst, oc);
} else if (isConvertComplexProfileToSingleProfiles() && observation.isSetValue() && observation.getValue().isSetValue() && observation.getValue().getValue() instanceof ProfileValue) {
obsConsts.put(omObsConst, insertObservationConstellationForProfiles(obsConstDao, obsTypeDao, observation, session));
}
}
DatasetEntity obsConst = obsConsts.get(observation.getObservationConstellation());
AbstractFeatureEntity feature = null;
if (resultTemplate.isSetFeature()) {
feature = resultTemplate.getFeature();
} else {
feature = getFeature(omObsConst.getFeatureOfInterest(), featureEntityMap, session);
}
try {
if (observation.getValue() instanceof SingleObservationValue) {
observationDAO.insertObservationSingleValue(obsConst, feature, observation, codespaceCache, unitCache, formatCache, session);
} else if (observation.getValue() instanceof MultiObservationValues) {
observationDAO.insertObservationMultiValue(obsConst, feature, observation, codespaceCache, unitCache, formatCache, session);
}
if (!abortInsertResultForExistingObservations()) {
transaction.commit();
transaction = session.beginTransaction();
}
} catch (PersistenceException pe) {
if (abortInsertResultForExistingObservations()) {
throw pe;
} else {
transaction.rollback();
session.clear();
transaction = session.beginTransaction();
LOGGER.debug("Already existing observation would be ignored!", pe);
}
}
if ((++insertion % FLUSH_THRESHOLD) == 0) {
session.flush();
session.clear();
LOGGER.debug("Saved {}/{} observations.", insertion, size);
}
}
LOGGER.debug("Saved {} observations.", size);
transaction.commit();
} catch (final HibernateException he) {
if (transaction != null) {
transaction.rollback();
}
// XXX exception text
throw new NoApplicableCodeException().causedBy(he);
} finally {
getHibernateSessionHolder().returnSession(session);
}
return response;
}
use of org.n52.series.db.beans.UnitEntity in project SOS by 52North.
the class AbstractHibernateStreamingValue method createParameter.
private NamedValue<BigDecimal> createParameter(String name, BigDecimal value, UnitEntity unit) {
final NamedValue<BigDecimal> namedValue = new NamedValue<>();
final ReferenceType referenceType = new ReferenceType(name);
namedValue.setName(referenceType);
namedValue.setValue(new QuantityValue(value, unit != null ? unit.getUnit() : "m"));
return namedValue;
}
Aggregations