use of org.n52.sos.ds.hibernate.dao.observation.AbstractObservationDAO in project SOS by 52North.
the class OfferingDAO method getMaxDate4Offering.
/**
* Get max time from observations for offering
*
* @param offering
* Offering identifier
* @param session
* Hibernate session Hibernate session
* @return max time for offering
* @throws CodedException If an error occurs
*/
public DateTime getMaxDate4Offering(final String offering, final Session session) throws OwsExceptionReport {
Object maxStart;
Object maxEnd;
if (HibernateHelper.isNamedQuerySupported(SQL_QUERY_GET_MAX_DATE_FOR_OFFERING, session)) {
Query namedQuery = session.getNamedQuery(SQL_QUERY_GET_MAX_DATE_FOR_OFFERING);
namedQuery.setParameter(OFFERING, offering);
LOGGER.debug("QUERY getMaxDate4Offering(offering) with NamedQuery: {}", SQL_QUERY_GET_MAX_DATE_FOR_OFFERING);
maxStart = namedQuery.uniqueResult();
maxEnd = maxStart;
} else {
AbstractObservationDAO observationDAO = getDaoFactory().getObservationDAO();
Criteria cstart = observationDAO.getDefaultObservationInfoCriteria(session);
Criteria cend = observationDAO.getDefaultObservationInfoCriteria(session);
addOfferingRestricionForObservation(cstart, offering);
addOfferingRestricionForObservation(cend, offering);
addMinMaxProjection(cstart, MinMax.MAX, DataEntity.PROPERTY_SAMPLING_TIME_START);
addMinMaxProjection(cend, MinMax.MAX, DataEntity.PROPERTY_SAMPLING_TIME_END);
LOGGER.debug("QUERY getMaxDate4Offering(offering) start: {}", HibernateHelper.getSqlString(cstart));
LOGGER.debug("QUERY getMaxDate4Offering(offering) end: {}", HibernateHelper.getSqlString(cend));
if (HibernateHelper.getSqlString(cstart).equals(HibernateHelper.getSqlString(cend))) {
maxStart = cstart.uniqueResult();
maxEnd = maxStart;
LOGGER.debug("Max time start and end query are identically, only one query is executed!");
} else {
maxStart = cstart.uniqueResult();
maxEnd = cend.uniqueResult();
}
}
if (maxStart == null && maxEnd == null) {
return null;
} else {
final DateTime start = new DateTime(maxStart, DateTimeZone.UTC);
if (maxEnd != null) {
final DateTime end = new DateTime(maxEnd, DateTimeZone.UTC);
if (end.isAfter(start)) {
return end;
}
}
return start;
}
}
use of org.n52.sos.ds.hibernate.dao.observation.AbstractObservationDAO in project SOS by 52North.
the class ProcedureDAO method getMaxDate4Procedure.
/**
* Get max time from observations for procedure
*
* @param procedure
* ProcedureEntity identifier
* @param session
* Hibernate session
* @return max time for procedure
* @throws CodedException
* If an error occurs
*/
public DateTime getMaxDate4Procedure(final String procedure, final Session session) throws OwsExceptionReport {
Object maxStart = null;
Object maxEnd = null;
if (HibernateHelper.isNamedQuerySupported(SQL_QUERY_GET_MAX_DATE_FOR_PROCEDURE, session)) {
Query namedQuery = session.getNamedQuery(SQL_QUERY_GET_MAX_DATE_FOR_PROCEDURE);
namedQuery.setParameter(PROCEDURE, procedure);
LOGGER.trace("QUERY getMaxDate4Procedure(procedure) with NamedQuery: {}", SQL_QUERY_GET_MAX_DATE_FOR_PROCEDURE);
maxStart = namedQuery.uniqueResult();
maxEnd = maxStart;
} else {
AbstractObservationDAO observationDAO = getDaoFactory().getObservationDAO();
Criteria cstart = observationDAO.getDefaultObservationInfoCriteria(session);
Criteria cend = observationDAO.getDefaultObservationInfoCriteria(session);
if (observationDAO instanceof SeriesObservationDAO) {
addProcedureRestrictionForSeries(cstart, procedure);
addProcedureRestrictionForSeries(cend, procedure);
} else {
addProcedureRestrictionForObservation(cstart, procedure);
addProcedureRestrictionForObservation(cend, procedure);
}
addMinMaxProjection(cstart, MinMax.MAX, DataEntity.PROPERTY_SAMPLING_TIME_START);
addMinMaxProjection(cend, MinMax.MAX, DataEntity.PROPERTY_SAMPLING_TIME_END);
LOGGER.trace("QUERY getMaxDate4Procedure(procedure) start: {}", HibernateHelper.getSqlString(cstart));
LOGGER.trace("QUERY getMaxDate4Procedure(procedure) end: {}", HibernateHelper.getSqlString(cend));
if (HibernateHelper.getSqlString(cstart).endsWith(HibernateHelper.getSqlString(cend))) {
maxStart = cstart.uniqueResult();
maxEnd = maxStart;
LOGGER.trace("Max time start and end query are identically, only one query is executed!");
} else {
maxStart = cstart.uniqueResult();
maxEnd = cend.uniqueResult();
}
}
if (maxStart == null && maxEnd == null) {
return null;
} else {
final DateTime start = new DateTime(maxStart, DateTimeZone.UTC);
if (maxEnd != null) {
final DateTime end = new DateTime(maxEnd, DateTimeZone.UTC);
if (end.isAfter(start)) {
return end;
}
}
return start;
}
}
use of org.n52.sos.ds.hibernate.dao.observation.AbstractObservationDAO in project SOS by 52North.
the class ProcedureDAO method getProcedureTimeExtrema.
@SuppressWarnings("unchecked")
public Map<String, TimeExtrema> getProcedureTimeExtrema(Session session) throws OwsExceptionReport {
List<ProcedureTimeExtrema> results = null;
if (isAllProcedureTimeExtremaNamedQuerySupported(session)) {
Query namedQuery = session.getNamedQuery(SQL_QUERY_GET_ALL_PROCEDURE_TIME_EXTREMA);
LOGGER.trace("QUERY getProcedureTimeExtrema() with NamedQuery '{}': {}", SQL_QUERY_GET_ALL_PROCEDURE_TIME_EXTREMA, namedQuery.getQueryString());
namedQuery.setResultTransformer(transformer);
results = namedQuery.list();
} else {
AbstractSeriesDAO seriesDAO = getDaoFactory().getSeriesDAO();
if (seriesDAO != null) {
Criteria c = seriesDAO.getDefaultSeriesCriteria(session);
c.createAlias(DatasetEntity.PROPERTY_PROCEDURE, "p");
c.setProjection(Projections.projectionList().add(Projections.groupProperty(P_PREFIX + ProcedureEntity.IDENTIFIER)).add(Projections.min(DatasetEntity.PROPERTY_FIRST_VALUE_AT)).add(Projections.max(DatasetEntity.PROPERTY_LAST_VALUE_AT)));
LOGGER.trace(QUERY_TIME_EXTREMA_LOG_TEMPLATE, HibernateHelper.getSqlString(c));
c.setResultTransformer(transformer);
results = c.list();
}
if (checkHasNoProcedureTimeResult(results)) {
AbstractObservationDAO observationDAO = getDaoFactory().getObservationDAO();
Criteria criteria = observationDAO.getDefaultObservationTimeCriteria(session);
String alias = observationDAO.addProcedureAlias(criteria);
criteria.setProjection(Projections.projectionList().add(Projections.groupProperty(alias + ProcedureEntity.IDENTIFIER)).add(Projections.min(DataEntity.PROPERTY_SAMPLING_TIME_START)).add(Projections.max(DataEntity.PROPERTY_SAMPLING_TIME_START)).add(Projections.max(DataEntity.PROPERTY_SAMPLING_TIME_START)));
LOGGER.trace(QUERY_TIME_EXTREMA_LOG_TEMPLATE, HibernateHelper.getSqlString(criteria));
criteria.setResultTransformer(transformer);
results = criteria.list();
}
}
Map<String, TimeExtrema> procedureTimeExtrema = Maps.newHashMap();
if (results != null && !results.isEmpty()) {
for (ProcedureTimeExtrema pte : results) {
if (pte != null && pte.isSetProcedure()) {
procedureTimeExtrema.put(pte.getProcedure(), pte);
}
}
}
return procedureTimeExtrema;
}
use of org.n52.sos.ds.hibernate.dao.observation.AbstractObservationDAO in project SOS by 52North.
the class InsertObservationHandler method insertObservation.
private void insertObservation(OmObservation sosObservation, InsertObservationCache cache, CompositeOwsException exceptions, Session session) throws OwsExceptionReport, CodedException {
checkSpatialFilteringProfile(sosObservation);
OmObservationConstellation sosObsConst = sosObservation.getObservationConstellation();
cache.addOfferings(sosObsConst.getOfferings());
AbstractFeatureEntity hFeature = null;
// if (sosObsConst.getOfferings().size() > 1) {
//
// }
String offeringID = sosObsConst.getOfferings().iterator().next();
DatasetEntity hDataset = cache.get(sosObsConst, offeringID);
if (hDataset == null) {
if (!cache.isChecked(sosObsConst, offeringID)) {
try {
hDataset = getDaoFactory().getSeriesDAO().checkSeries(sosObsConst, offeringID, session, Sos2Constants.InsertObservationParams.observationType.name());
// add to cache table
cache.putConstellation(sosObsConst, offeringID, hDataset);
} catch (OwsExceptionReport owse) {
exceptions.add(owse);
}
// mark as checked
cache.checkConstellation(sosObsConst, offeringID);
}
}
if (hDataset != null) {
// getFeature feature from local cache or create if necessary
hFeature = getFeature(sosObsConst.getFeatureOfInterest(), cache, session);
// AbstractFeature/offering combo
if (!cache.isChecked(sosObsConst.getFeatureOfInterest(), offeringID)) {
getDaoFactory().getFeatureOfInterestDAO().checkOrInsertRelatedFeatureRelation(hFeature, hDataset.getOffering(), session);
cache.checkFeature(sosObsConst.getFeatureOfInterest(), offeringID);
}
AbstractObservationDAO observationDAO = getDaoFactory().getObservationDAO();
DatasetEntity dataset = null;
if (sosObservation.getValue() instanceof SingleObservationValue) {
dataset = observationDAO.insertObservationSingleValue(hDataset, hFeature, sosObservation, cache.getCodespaceCache(), cache.getUnitCache(), cache.getFormatCache(), session);
} else if (sosObservation.getValue() instanceof MultiObservationValues) {
dataset = observationDAO.insertObservationMultiValue(hDataset, hFeature, sosObservation, cache.getCodespaceCache(), cache.getUnitCache(), cache.getFormatCache(), session);
}
if (dataset != null && !cache.get(sosObsConst, offeringID).equals(dataset)) {
cache.putConstellation(sosObsConst, offeringID, dataset);
}
}
}
use of org.n52.sos.ds.hibernate.dao.observation.AbstractObservationDAO in project SOS by 52North.
the class HibernateObservationBuilder method getSeries.
protected DatasetEntity getSeries(OfferingEntity offering, DataEntity o) throws OwsExceptionReport {
AbstractObservationDAO observationDAO = daoFactory.getObservationDAO();
SeriesObservationFactory observationFactory = (SeriesObservationFactory) observationDAO.getObservationFactory();
Criteria criteria = session.createCriteria(observationFactory.seriesClass()).setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY).add(Restrictions.eq(DatasetEntity.PROPERTY_FEATURE, getFeatureOfInterest())).add(Restrictions.eq(DatasetEntity.PROPERTY_PHENOMENON, getObservableProperty())).add(Restrictions.eq(DatasetEntity.PROPERTY_PROCEDURE, getProcedure())).add(Restrictions.eq(DatasetEntity.PROPERTY_OFFERING, offering));
DatasetEntity series = (DatasetEntity) criteria.uniqueResult();
if (series == null) {
series = (DatasetEntity) daoFactory.getSeriesDAO().getDatasetFactory().visit(o);
series.setObservableProperty(getObservableProperty());
series.setProcedure(getProcedure());
series.setCategory(getCategory());
series.setPlatform(getPlatform());
series.setFeature(getFeatureOfInterest());
series.setOffering(offering);
series.setDeleted(false);
series.setPublished(true);
if (series.hasEreportingProfile()) {
series.getEreportingProfile().setSamplingPoint(getEReportingSamplingPoint());
}
session.save(series);
session.flush();
session.refresh(series);
} else if (series.isDeleted()) {
series.setDeleted(false);
session.update(series);
session.flush();
session.refresh(series);
}
return series;
}
Aggregations