diff --git a/NOTICE b/NOTICE index f9f6232141..1df298e643 100644 --- a/NOTICE +++ b/NOTICE @@ -302,9 +302,6 @@ This project includes: org.apache.xmlgraphics:batik-transcoder under The Apache Software License, Version 2.0 org.apache.xmlgraphics:batik-util under The Apache Software License, Version 2.0 org.apache.xmlgraphics:batik-xml under The Apache Software License, Version 2.0 - org.eclipse.emf.common under The Eclipse Public License Version 1.0 - org.eclipse.emf.ecore under The Eclipse Public License Version 1.0 - org.eclipse.emf.ecore.xmi under The Eclipse Public License Version 1.0 org.locationtech.jts:jts-core under Eclipse Publish License, Version 1.0 or Eclipse Distribution License - v 1.0 parent-join under The Apache Software License, Version 2.0 Portele Schape Change schema (spec. v3.0) under The Apache Software License, Version 2.0 diff --git a/hibernate/common/src/main/java/org/n52/sos/ds/hibernate/dao/observation/series/AbstractSeriesDAO.java b/hibernate/common/src/main/java/org/n52/sos/ds/hibernate/dao/observation/series/AbstractSeriesDAO.java index 0f5b1ba9da..9698335771 100644 --- a/hibernate/common/src/main/java/org/n52/sos/ds/hibernate/dao/observation/series/AbstractSeriesDAO.java +++ b/hibernate/common/src/main/java/org/n52/sos/ds/hibernate/dao/observation/series/AbstractSeriesDAO.java @@ -1104,10 +1104,10 @@ private Criteria createCriteriaFor(String observedProperty, String offering, Col if (CollectionHelper.isNotEmpty(features)) { addFeatureOfInterestToCriteria(c, features); } - if (Strings.isNullOrEmpty(observedProperty)) { + if (!Strings.isNullOrEmpty(observedProperty)) { addObservablePropertyToCriteria(c, observedProperty); } - if (Strings.isNullOrEmpty(offering)) { + if (!Strings.isNullOrEmpty(offering)) { addOfferingToCriteria(c, offering); } return c; diff --git a/hibernate/common/src/main/java/org/n52/sos/ds/hibernate/util/HibernateUnproxy.java b/hibernate/common/src/main/java/org/n52/sos/ds/hibernate/util/HibernateUnproxy.java new file mode 100644 index 0000000000..43d191a748 --- /dev/null +++ b/hibernate/common/src/main/java/org/n52/sos/ds/hibernate/util/HibernateUnproxy.java @@ -0,0 +1,45 @@ +/* + * Copyright (C) 2012-2020 52°North Initiative for Geospatial Open Source + * Software GmbH + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 as published + * by the Free Software Foundation. + * + * If the program is linked with libraries which are licensed under one of + * the following licenses, the combination of the program with the linked + * library is not considered a "derivative work" of the program: + * + * - Apache License, version 2.0 + * - Apache Software License, version 1.0 + * - GNU Lesser General Public License, version 3 + * - Mozilla Public License, versions 1.0, 1.1 and 2.0 + * - Common Development and Distribution License (CDDL), version 1.0 + * + * Therefore the distribution of the program linked with libraries licensed + * under the aforementioned licenses, is permitted by the copyright holders + * if the distribution is compliant with both the GNU General Public + * License version 2 and the aforementioned licenses. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General + * Public License for more details. + */ +package org.n52.sos.ds.hibernate.util; + +import org.hibernate.Hibernate; +import org.hibernate.Session; +import org.hibernate.proxy.HibernateProxy; +import org.n52.series.db.beans.DataEntity; + +public interface HibernateUnproxy { + + default DataEntity unproxy(DataEntity dataEntity, Session session) { + return !(dataEntity instanceof HibernateProxy) ? dataEntity + : ((HibernateProxy) dataEntity).getHibernateLazyInitializer() + .getSession() == null ? unproxy(session.load(DataEntity.class, dataEntity.getId()), session) + : (DataEntity) Hibernate.unproxy(dataEntity); + } + +} diff --git a/hibernate/common/src/main/java/org/n52/sos/ds/hibernate/util/observation/AbstractObservationValueCreator.java b/hibernate/common/src/main/java/org/n52/sos/ds/hibernate/util/observation/AbstractObservationValueCreator.java new file mode 100644 index 0000000000..32c166a8d1 --- /dev/null +++ b/hibernate/common/src/main/java/org/n52/sos/ds/hibernate/util/observation/AbstractObservationValueCreator.java @@ -0,0 +1,178 @@ +/* + * Copyright (C) 2012-2020 52°North Initiative for Geospatial Open Source + * Software GmbH + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 as published + * by the Free Software Foundation. + * + * If the program is linked with libraries which are licensed under one of + * the following licenses, the combination of the program with the linked + * library is not considered a "derivative work" of the program: + * + * - Apache License, version 2.0 + * - Apache Software License, version 1.0 + * - GNU Lesser General Public License, version 3 + * - Mozilla Public License, versions 1.0, 1.1 and 2.0 + * - Common Development and Distribution License (CDDL), version 1.0 + * + * Therefore the distribution of the program linked with libraries licensed + * under the aforementioned licenses, is permitted by the copyright holders + * if the distribution is compliant with both the GNU General Public + * License version 2 and the aforementioned licenses. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General + * Public License for more details. + */ +package org.n52.sos.ds.hibernate.util.observation; + +import org.n52.series.db.beans.BlobDataEntity; +import org.n52.series.db.beans.BooleanDataEntity; +import org.n52.series.db.beans.CategoryDataEntity; +import org.n52.series.db.beans.ComplexDataEntity; +import org.n52.series.db.beans.CountDataEntity; +import org.n52.series.db.beans.DataArrayDataEntity; +import org.n52.series.db.beans.DataEntity; +import org.n52.series.db.beans.GeometryDataEntity; +import org.n52.series.db.beans.HibernateRelations.HasObservablePropertyGetter; +import org.n52.series.db.beans.ProfileDataEntity; +import org.n52.series.db.beans.QuantityDataEntity; +import org.n52.series.db.beans.ReferencedDataEntity; +import org.n52.series.db.beans.TextDataEntity; +import org.n52.series.db.beans.UnitEntity; +import org.n52.shetland.ogc.UoM; +import org.n52.shetland.ogc.om.values.BooleanValue; +import org.n52.shetland.ogc.om.values.CategoryValue; +import org.n52.shetland.ogc.om.values.GeometryValue; +import org.n52.shetland.ogc.om.values.QuantityValue; +import org.n52.shetland.ogc.om.values.ReferenceValue; +import org.n52.shetland.ogc.om.values.TextValue; +import org.n52.shetland.ogc.om.values.UnknownValue; +import org.n52.shetland.ogc.om.values.Value; +import org.n52.shetland.ogc.ows.exception.OwsExceptionReport; +import org.n52.shetland.ogc.swe.simpleType.SweAbstractSimpleType; +import org.n52.svalbard.decode.DecoderRepository; + +public abstract class AbstractObservationValueCreator extends AbstractValuedObservationCreator> { + + public AbstractObservationValueCreator(DecoderRepository decoderRepository) { + super(decoderRepository); + } + + public AbstractObservationValueCreator(DecoderRepository decoderRepository, boolean noValues) { + super(decoderRepository, noValues); + } + + @SuppressWarnings("rawtypes") + protected void addAdditonalDataEntity(DataEntity o, SweAbstractSimpleType v) { + if (o.hasValueIdentifier()) { + v.setIdentifier(o.getValueIdentifier()); + } + if (o.hasValueName()) { + v.setName(o.getValueName()); + } + if (o.hasValueDescription()) { + v.setDescription(o.getValueDescription()); + } + } + + @SuppressWarnings("rawtypes") + protected void addDefinitionFromObservableProperty(DataEntity o, SweAbstractSimpleType v) { + if (o instanceof HasObservablePropertyGetter) { + if (((HasObservablePropertyGetter) o).getObservableProperty() != null) { + v.setDefinition(((HasObservablePropertyGetter) o).getObservableProperty().getIdentifier()); + } + } + } + + protected UoM getUnit(UnitEntity unit) { + UoM uom = new UoM(unit.getUnit()); + return uom; + } + + @Override + public Value visit(DataEntity o) throws OwsExceptionReport { + if (o instanceof QuantityDataEntity) { + return visit((QuantityDataEntity) o); + } else if (o instanceof BlobDataEntity) { + return visit((BlobDataEntity) o); + } else if (o instanceof BooleanDataEntity) { + return visit((BooleanDataEntity) o); + } else if (o instanceof CategoryDataEntity) { + return visit((CategoryDataEntity) o); + } else if (o instanceof ComplexDataEntity) { + return visit((ComplexDataEntity) o); + } else if (o instanceof CountDataEntity) { + return visit((CountDataEntity) o); + } else if (o instanceof GeometryDataEntity) { + return visit((GeometryDataEntity) o); + } else if (o instanceof TextDataEntity) { + return visit((TextDataEntity) o); + } else if (o instanceof DataArrayDataEntity) { + return visit((DataArrayDataEntity) o); + } else if (o instanceof ProfileDataEntity) { + return visit((ProfileDataEntity) o); + } else if (o instanceof ReferencedDataEntity) { + return visit((ReferencedDataEntity) o); + } + return null; + } + + public QuantityValue visit(QuantityDataEntity o, QuantityValue v) { + if (o.getDataset().hasUnit()) { + v.setUnit(getUnit(o.getDataset().getUnit())); + } + return v; + } + + public UnknownValue visit(BlobDataEntity o, UnknownValue v) { + if (o.getDataset().hasUnit()) { + v.setUnit(getUnit(o.getDataset().getUnit())); + } + return v; + } + + public BooleanValue visit(BooleanDataEntity o, BooleanValue v) { + if (o.getDataset().hasUnit()) { + v.setUnit(getUnit(o.getDataset().getUnit())); + } + return v; + } + + public CategoryValue visit(CategoryDataEntity o, CategoryValue v) { + addAdditonalDataEntity(o, v); + addDefinitionFromObservableProperty(o, v); + if (o.getDataset().hasUnit()) { + v.setUnit(getUnit(o.getDataset().getUnit())); + } + return v; + } + + public GeometryValue visit(GeometryDataEntity o, GeometryValue v) throws OwsExceptionReport { + if (o.getDataset().hasUnit()) { + v.setUnit(getUnit(o.getDataset().getUnit())); + } + return v; + } + + public TextValue visit(TextDataEntity o, TextValue v) { + addAdditonalDataEntity(o, v); + addDefinitionFromObservableProperty(o, v); + if (o.getDataset().hasUnit()) { + v.setUnit(getUnit(o.getDataset().getUnit())); + } + return v; + } + + public ReferenceValue visit(ReferencedDataEntity o, ReferenceValue v) { + if (o.hasValueName()) { + v.getValue().setTitle(o.getValueName()); + } + if (o.getDataset().hasUnit()) { + v.setUnit(getUnit(o.getDataset().getUnit())); + } + return v; + } +} diff --git a/hibernate/common/src/main/java/org/n52/sos/ds/hibernate/util/observation/AbstractValuedObservationCreator.java b/hibernate/common/src/main/java/org/n52/sos/ds/hibernate/util/observation/AbstractValuedObservationCreator.java new file mode 100644 index 0000000000..31f37d5248 --- /dev/null +++ b/hibernate/common/src/main/java/org/n52/sos/ds/hibernate/util/observation/AbstractValuedObservationCreator.java @@ -0,0 +1,128 @@ +/* + * Copyright (C) 2012-2020 52°North Initiative for Geospatial Open Source + * Software GmbH + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 as published + * by the Free Software Foundation. + * + * If the program is linked with libraries which are licensed under one of + * the following licenses, the combination of the program with the linked + * library is not considered a "derivative work" of the program: + * + * - Apache License, version 2.0 + * - Apache Software License, version 1.0 + * - GNU Lesser General Public License, version 3 + * - Mozilla Public License, versions 1.0, 1.1 and 2.0 + * - Common Development and Distribution License (CDDL), version 1.0 + * + * Therefore the distribution of the program linked with libraries licensed + * under the aforementioned licenses, is permitted by the copyright holders + * if the distribution is compliant with both the GNU General Public + * License version 2 and the aforementioned licenses. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General + * Public License for more details. + */ +package org.n52.sos.ds.hibernate.util.observation; + +import java.util.LinkedList; +import java.util.List; + +import org.apache.xmlbeans.XmlObject; +import org.n52.series.db.beans.DataArrayDataEntity; +import org.n52.series.db.beans.DataEntity; +import org.n52.shetland.ogc.ows.exception.CodedException; +import org.n52.shetland.ogc.ows.exception.NoApplicableCodeException; +import org.n52.shetland.ogc.swe.SweAbstractDataComponent; +import org.n52.shetland.ogc.swe.SweAbstractDataRecord; +import org.n52.shetland.ogc.swe.SweDataArray; +import org.n52.shetland.ogc.swe.encoding.SweAbstractEncoding; +import org.n52.shetland.ogc.swe.encoding.SweTextEncoding; +import org.n52.shetland.util.JavaHelper; +import org.n52.svalbard.decode.DecoderRepository; +import org.n52.svalbard.decode.exception.DecodingException; +import org.n52.svalbard.util.CodingHelper; +import org.n52.svalbard.util.XmlHelper; + +public abstract class AbstractValuedObservationCreator implements ValuedObservationVisitor { + + private DecoderRepository decoderRepository; + private boolean noValues; + + public AbstractValuedObservationCreator(DecoderRepository decoderRepository) { + this(decoderRepository, false); + } + + public AbstractValuedObservationCreator(DecoderRepository decoderRepository, boolean noValues) { + this.decoderRepository = decoderRepository; + this.noValues = noValues; + } + + protected DecoderRepository getDecoderRepository() { + return decoderRepository; + } + + protected boolean isNoValues() { + return noValues; + } + + protected Object decode(XmlObject xml) throws DecodingException { + return getDecoderRepository().getDecoder(CodingHelper.getDecoderKey(xml)).decode(xml); + } + + protected SweDataArray createSweDataArray(DataArrayDataEntity o) throws CodedException { + try { + SweDataArray array = new SweDataArray(); + decode(XmlHelper.parseXmlString(o.getResultTemplate().getEncoding())); + array.setEncoding( + (SweAbstractEncoding) decode(XmlHelper.parseXmlString(o.getResultTemplate().getEncoding()))); + array.setElementType( + (SweAbstractDataComponent) decode(XmlHelper.parseXmlString(o.getResultTemplate().getStructure()))); + if (isNoValues()) { + if (o.isSetStringValue()) { + array.setXml(null); + List> values = new LinkedList<>(); + for (String block : o.getStringValue() + .split(((SweTextEncoding) array.getEncoding()).getBlockSeparator())) { + List v = new LinkedList<>(); + for (String value : block.split(((SweTextEncoding) array.getEncoding()).getTokenSeparator())) { + v.add(value); + } + values.add(v); + } + array.setValues(values); + } else if (o.getValue() != null && !o.getValue() + .isEmpty()) { + int i = ((SweAbstractDataRecord) array.getElementType()).getFieldIndexByIdentifier(o.getDataset() + .getPhenomenon() + .getIdentifier()) == 0 ? 1 : 0; + List> values = new LinkedList<>(); + for (DataEntity v : o.getValue()) { + List value = new LinkedList<>(); + if (i == 0) { + value.add(v.getDataset() + .getPhenomenon() + .getName()); + value.add(JavaHelper.asString(v.getValue())); + } else { + value.add(JavaHelper.asString(v.getValue())); + value.add(v.getDataset() + .getPhenomenon() + .getName()); + } + values.add(value); + } + array.setValues(values); + } + } + return array; + } catch (DecodingException e) { + throw new NoApplicableCodeException().causedBy(e) + .withMessage("Error while creating SweDataArray from database entity!"); + } + } + +} diff --git a/hibernate/common/src/main/java/org/n52/sos/ds/hibernate/util/observation/ObservationOmObservationCreator.java b/hibernate/common/src/main/java/org/n52/sos/ds/hibernate/util/observation/ObservationOmObservationCreator.java index db9471870a..4832e6295e 100644 --- a/hibernate/common/src/main/java/org/n52/sos/ds/hibernate/util/observation/ObservationOmObservationCreator.java +++ b/hibernate/common/src/main/java/org/n52/sos/ds/hibernate/util/observation/ObservationOmObservationCreator.java @@ -36,9 +36,7 @@ import java.util.Map; import java.util.Set; -import org.hibernate.Hibernate; import org.hibernate.Session; -import org.hibernate.proxy.HibernateProxy; import org.joda.time.DateTime; import org.joda.time.DateTimeZone; import org.n52.iceland.convert.ConverterException; @@ -68,6 +66,7 @@ import org.n52.shetland.ogc.sos.SosConstants; import org.n52.shetland.ogc.sos.SosProcedureDescription; import org.n52.shetland.ogc.sos.request.AbstractObservationRequest; +import org.n52.sos.ds.hibernate.util.HibernateUnproxy; import org.n52.sos.util.SosHelper; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -77,7 +76,7 @@ import com.google.common.collect.Maps; import com.google.common.collect.Sets; -public class ObservationOmObservationCreator extends AbstractOmObservationCreator { +public class ObservationOmObservationCreator extends AbstractOmObservationCreator implements HibernateUnproxy { private static final Logger LOGGER = LoggerFactory.getLogger(ObservationOmObservationCreator.class); private final Collection> observations; @@ -165,7 +164,7 @@ protected OmObservation createObservation(DataEntity hObservation) String phenomenonId = createPhenomenon(hObservation); Set offerings = createOfferingSet(hObservation, procedureId, phenomenonId); final Value value = new ObservationValueCreator(getCreatorContext().getDecoderRepository()) - .visit(hObservation instanceof HibernateProxy ? unproxy(hObservation) : hObservation); + .visit(unproxy(hObservation, getSession())); OmObservation sosObservation = null; if (value != null) { value.setUnit(queryUnit(hObservation.getDataset())); @@ -194,14 +193,6 @@ protected OmObservation createObservation(DataEntity hObservation) return sosObservation; } - private DataEntity unproxy(DataEntity dataEntity) { - if (dataEntity instanceof HibernateProxy - && ((HibernateProxy) dataEntity).getHibernateLazyInitializer().getSession() == null) { - return unproxy(getSession().load(DataEntity.class, dataEntity.getId())); - } - return (DataEntity) Hibernate.unproxy(dataEntity); - } - private void addRelatedObservations(OmObservation sosObservation, DataEntity hObservation) throws CodedException { new RelatedObservationAdder(sosObservation, hObservation, getCreatorContext().getServiceURL().toString(), diff --git a/hibernate/common/src/main/java/org/n52/sos/ds/hibernate/util/observation/ObservationValueCreator.java b/hibernate/common/src/main/java/org/n52/sos/ds/hibernate/util/observation/ObservationValueCreator.java index 6db3345f87..4ee42e8d48 100644 --- a/hibernate/common/src/main/java/org/n52/sos/ds/hibernate/util/observation/ObservationValueCreator.java +++ b/hibernate/common/src/main/java/org/n52/sos/ds/hibernate/util/observation/ObservationValueCreator.java @@ -28,25 +28,17 @@ */ package org.n52.sos.ds.hibernate.util.observation; -import java.util.LinkedList; -import java.util.List; - -import org.apache.xmlbeans.XmlObject; import org.n52.series.db.beans.BlobDataEntity; import org.n52.series.db.beans.BooleanDataEntity; import org.n52.series.db.beans.CategoryDataEntity; import org.n52.series.db.beans.ComplexDataEntity; import org.n52.series.db.beans.CountDataEntity; import org.n52.series.db.beans.DataArrayDataEntity; -import org.n52.series.db.beans.DataEntity; import org.n52.series.db.beans.GeometryDataEntity; -import org.n52.series.db.beans.HibernateRelations.HasObservablePropertyGetter; import org.n52.series.db.beans.ProfileDataEntity; import org.n52.series.db.beans.QuantityDataEntity; import org.n52.series.db.beans.ReferencedDataEntity; import org.n52.series.db.beans.TextDataEntity; -import org.n52.series.db.beans.UnitEntity; -import org.n52.shetland.ogc.UoM; import org.n52.shetland.ogc.gml.ReferenceType; import org.n52.shetland.ogc.om.values.BooleanValue; import org.n52.shetland.ogc.om.values.CategoryValue; @@ -59,104 +51,44 @@ import org.n52.shetland.ogc.om.values.SweDataArrayValue; import org.n52.shetland.ogc.om.values.TextValue; import org.n52.shetland.ogc.om.values.UnknownValue; -import org.n52.shetland.ogc.om.values.Value; -import org.n52.shetland.ogc.ows.exception.NoApplicableCodeException; import org.n52.shetland.ogc.ows.exception.OwsExceptionReport; -import org.n52.shetland.ogc.swe.SweAbstractDataComponent; -import org.n52.shetland.ogc.swe.SweAbstractDataRecord; -import org.n52.shetland.ogc.swe.SweDataArray; import org.n52.shetland.ogc.swe.SweDataRecord; -import org.n52.shetland.ogc.swe.encoding.SweAbstractEncoding; -import org.n52.shetland.ogc.swe.encoding.SweTextEncoding; -import org.n52.shetland.ogc.swe.simpleType.SweAbstractSimpleType; -import org.n52.shetland.util.JavaHelper; import org.n52.svalbard.decode.DecoderRepository; -import org.n52.svalbard.decode.exception.DecodingException; -import org.n52.svalbard.util.CodingHelper; -import org.n52.svalbard.util.XmlHelper; /** * TODO JavaDoc * * @author Christian Autermann */ -public class ObservationValueCreator implements ValuedObservationVisitor> { - - private DecoderRepository decoderRepository; +public class ObservationValueCreator extends AbstractObservationValueCreator { public ObservationValueCreator(DecoderRepository decoderRepository) { - this.decoderRepository = decoderRepository; - } - - @Override - public Value visit(DataEntity o) throws OwsExceptionReport { - if (o instanceof QuantityDataEntity) { - return visit((QuantityDataEntity) o); - } else if (o instanceof BlobDataEntity) { - return visit((BlobDataEntity) o); - } else if (o instanceof BooleanDataEntity) { - return visit((BooleanDataEntity) o); - } else if (o instanceof CategoryDataEntity) { - return visit((CategoryDataEntity) o); - } else if (o instanceof ComplexDataEntity) { - return visit((ComplexDataEntity) o); - } else if (o instanceof CountDataEntity) { - return visit((CountDataEntity) o); - } else if (o instanceof GeometryDataEntity) { - return visit((GeometryDataEntity) o); - } else if (o instanceof TextDataEntity) { - return visit((TextDataEntity) o); - } else if (o instanceof DataArrayDataEntity) { - return visit((DataArrayDataEntity) o); - } else if (o instanceof ProfileDataEntity) { - return visit((ProfileDataEntity) o); - } else if (o instanceof ReferencedDataEntity) { - return visit((ReferencedDataEntity) o); - } - return null; + super(decoderRepository); } @Override public QuantityValue visit(QuantityDataEntity o) { - QuantityValue v = new QuantityValue(o.hasValue() ? o.getValue().doubleValue() : null); - if (o.getDataset().hasUnit()) { - v.setUnit(getUnit(o.getDataset().getUnit())); - } - return v; + return visit(o, new QuantityValue(o.hasValue() ? o.getValue().doubleValue() : null)); } @Override public UnknownValue visit(BlobDataEntity o) { - UnknownValue v = new UnknownValue(o.getValue()); - if (o.getDataset().hasUnit()) { - v.setUnit(getUnit(o.getDataset().getUnit())); - } - return v; + return visit(o, new UnknownValue(o.getValue())); } @Override public BooleanValue visit(BooleanDataEntity o) { - BooleanValue v = new BooleanValue(o.getValue()); - if (o.getDataset().hasUnit()) { - v.setUnit(getUnit(o.getDataset().getUnit())); - } - return v; + return visit(o, new BooleanValue(o.getValue())); } @Override public CategoryValue visit(CategoryDataEntity o) { - CategoryValue v = new CategoryValue(o.getValue()); - addAdditonalDataEntity(o, v); - addDefinitionFromObservableProperty(o, v); - if (o.getDataset().hasUnit()) { - v.setUnit(getUnit(o.getDataset().getUnit())); - } - return v; + return visit(o, new CategoryValue(o.getValue())); } @Override public ComplexValue visit(ComplexDataEntity o) throws OwsExceptionReport { - SweAbstractDataComponentCreator visitor = new SweAbstractDataComponentCreator(decoderRepository); + SweAbstractDataComponentCreator visitor = new SweAbstractDataComponentCreator(getDecoderRepository()); SweDataRecord record = visitor.visit(o); return new ComplexValue(record); } @@ -168,67 +100,17 @@ public CountValue visit(CountDataEntity o) { @Override public GeometryValue visit(GeometryDataEntity o) throws OwsExceptionReport { - GeometryValue v = new GeometryValue(o.hasValue() ? o.getValue().getGeometry() : null); - if (o.getDataset().hasUnit()) { - v.setUnit(getUnit(o.getDataset().getUnit())); - } - return v; + return visit(o, new GeometryValue(o.hasValue() ? o.getValue().getGeometry() : null)); } @Override public TextValue visit(TextDataEntity o) { - TextValue v = new TextValue(o.getValue()); - addAdditonalDataEntity(o, v); - addDefinitionFromObservableProperty(o, v); - if (o.getDataset().hasUnit()) { - v.setUnit(getUnit(o.getDataset().getUnit())); - } - return v; + return visit(o, new TextValue(o.getValue())); } @Override public SweDataArrayValue visit(DataArrayDataEntity o) throws OwsExceptionReport { - try { - SweDataArray array = new SweDataArray(); - decode(XmlHelper.parseXmlString(o.getResultTemplate().getEncoding())); - array.setEncoding( - (SweAbstractEncoding) decode(XmlHelper.parseXmlString(o.getResultTemplate().getEncoding()))); - array.setElementType( - (SweAbstractDataComponent) decode(XmlHelper.parseXmlString(o.getResultTemplate().getStructure()))); - if (o.isSetStringValue()) { - array.setXml(null); - List> values = new LinkedList<>(); - for (String block : o.getStringValue() - .split(((SweTextEncoding) array.getEncoding()).getBlockSeparator())) { - List v = new LinkedList<>(); - for (String value : block.split(((SweTextEncoding) array.getEncoding()).getTokenSeparator())) { - v.add(value); - } - values.add(v); - } - array.setValues(values); - } else if (o.getValue() != null && !o.getValue().isEmpty()) { - int i = ((SweAbstractDataRecord) array.getElementType()) - .getFieldIndexByIdentifier(o.getDataset().getPhenomenon().getIdentifier()) == 0 ? 1 : 0; - List> values = new LinkedList<>(); - for (DataEntity v : o.getValue()) { - List value = new LinkedList<>(); - if (i == 0) { - value.add(v.getDataset().getPhenomenon().getName()); - value.add(JavaHelper.asString(v.getValue())); - } else { - value.add(JavaHelper.asString(v.getValue())); - value.add(v.getDataset().getPhenomenon().getName()); - } - values.add(value); - } - array.setValues(values); - } - return new SweDataArrayValue(array); - } catch (DecodingException e) { - throw new NoApplicableCodeException().causedBy(e) - .withMessage("Error while creating SweDataArray from database entity!"); - } + return new SweDataArrayValue(createSweDataArray(o)); } @Override @@ -238,45 +120,7 @@ public ProfileValue visit(ProfileDataEntity o) throws OwsExceptionReport { @Override public ReferenceValue visit(ReferencedDataEntity o) { - ReferenceValue v = new ReferenceValue(new ReferenceType(o.getValue())); - if (o.hasValueName()) { - v.getValue().setTitle(o.getValueName()); - } - if (o.getDataset().hasUnit()) { - v.setUnit(getUnit(o.getDataset().getUnit())); - } - return v; - } - - protected Object decode(XmlObject xml) throws DecodingException { - return decoderRepository.getDecoder(CodingHelper.getDecoderKey(xml)).decode(xml); - } - - @SuppressWarnings("rawtypes") - protected void addAdditonalDataEntity(DataEntity o, SweAbstractSimpleType v) { - if (o.hasValueIdentifier()) { - v.setIdentifier(o.getValueIdentifier()); - } - if (o.hasValueName()) { - v.setName(o.getValueName()); - } - if (o.hasValueDescription()) { - v.setDescription(o.getValueDescription()); - } - } - - @SuppressWarnings("rawtypes") - protected void addDefinitionFromObservableProperty(DataEntity o, SweAbstractSimpleType v) { - if (o instanceof HasObservablePropertyGetter) { - if (((HasObservablePropertyGetter) o).getObservableProperty() != null) { - v.setDefinition(((HasObservablePropertyGetter) o).getObservableProperty().getIdentifier()); - } - } - } - - protected UoM getUnit(UnitEntity unit) { - UoM uom = new UoM(unit.getUnit()); - return uom; + return visit(o, new ReferenceValue(new ReferenceType(o.getValue()))); } } diff --git a/hibernate/common/src/main/java/org/n52/sos/ds/hibernate/util/observation/ProfileGeneratorSplitter.java b/hibernate/common/src/main/java/org/n52/sos/ds/hibernate/util/observation/ProfileGeneratorSplitter.java index c87c27a362..8bd7613070 100644 --- a/hibernate/common/src/main/java/org/n52/sos/ds/hibernate/util/observation/ProfileGeneratorSplitter.java +++ b/hibernate/common/src/main/java/org/n52/sos/ds/hibernate/util/observation/ProfileGeneratorSplitter.java @@ -56,9 +56,9 @@ public class ProfileGeneratorSplitter { private static final Logger LOG = LoggerFactory.getLogger(ProfileGeneratorSplitter.class); - private ObservationValueCreator creator; + private AbstractObservationValueCreator creator; - public ProfileGeneratorSplitter(ObservationValueCreator creator) { + public ProfileGeneratorSplitter(AbstractObservationValueCreator creator) { this.creator = creator; } diff --git a/hibernate/common/src/main/java/org/n52/sos/ds/hibernate/util/observation/SweAbstractDataComponentCreator.java b/hibernate/common/src/main/java/org/n52/sos/ds/hibernate/util/observation/SweAbstractDataComponentCreator.java index 77cbd21f52..cff99f95a8 100644 --- a/hibernate/common/src/main/java/org/n52/sos/ds/hibernate/util/observation/SweAbstractDataComponentCreator.java +++ b/hibernate/common/src/main/java/org/n52/sos/ds/hibernate/util/observation/SweAbstractDataComponentCreator.java @@ -30,10 +30,6 @@ import java.net.URI; import java.net.URISyntaxException; -import java.util.LinkedList; -import java.util.List; - -import org.apache.xmlbeans.XmlObject; import org.n52.janmayen.NcName; import org.n52.series.db.beans.BlobDataEntity; import org.n52.series.db.beans.BooleanDataEntity; @@ -53,23 +49,16 @@ import org.n52.shetland.ogc.ows.exception.NoApplicableCodeException; import org.n52.shetland.ogc.ows.exception.OwsExceptionReport; import org.n52.shetland.ogc.swe.SweAbstractDataComponent; -import org.n52.shetland.ogc.swe.SweAbstractDataRecord; import org.n52.shetland.ogc.swe.SweDataArray; import org.n52.shetland.ogc.swe.SweDataRecord; import org.n52.shetland.ogc.swe.SweField; -import org.n52.shetland.ogc.swe.encoding.SweAbstractEncoding; -import org.n52.shetland.ogc.swe.encoding.SweTextEncoding; import org.n52.shetland.ogc.swe.simpleType.SweAbstractUomType; import org.n52.shetland.ogc.swe.simpleType.SweBoolean; import org.n52.shetland.ogc.swe.simpleType.SweCategory; import org.n52.shetland.ogc.swe.simpleType.SweCount; import org.n52.shetland.ogc.swe.simpleType.SweQuantity; import org.n52.shetland.ogc.swe.simpleType.SweText; -import org.n52.shetland.util.JavaHelper; import org.n52.svalbard.decode.DecoderRepository; -import org.n52.svalbard.decode.exception.DecodingException; -import org.n52.svalbard.util.CodingHelper; -import org.n52.svalbard.util.XmlHelper; /** * {@code ValuedObservationVisitor} to create {@link SweAbstractDataComponent} @@ -78,12 +67,14 @@ * @author Christian Autermann */ public class SweAbstractDataComponentCreator - implements ValuedObservationVisitor { - - private DecoderRepository decoderRepository; + extends AbstractValuedObservationCreator { public SweAbstractDataComponentCreator(DecoderRepository decoderRepository) { - this.decoderRepository = decoderRepository; + super(decoderRepository); + } + + public SweAbstractDataComponentCreator(DecoderRepository decoderRepository, boolean noValues) { + super(decoderRepository, noValues); } public SweAbstractDataComponent visit(DataEntity o) throws OwsExceptionReport { @@ -129,21 +120,27 @@ public SweAbstractDataComponent visit(BlobDataEntity o) @Override public SweQuantity visit(QuantityDataEntity o) throws CodedException { SweQuantity component = new SweQuantity(); - component.setValue(o.getValue()); + if (!isNoValues()) { + component.setValue(o.getValue()); + } return setCommonValues(component, o); } @Override public SweBoolean visit(BooleanDataEntity o) throws CodedException { SweBoolean component = new SweBoolean(); - component.setValue(o.getValue()); + if (!isNoValues()) { + component.setValue(o.getValue()); + } return setCommonValues(component, o); } @Override public SweCategory visit(CategoryDataEntity o) throws CodedException { SweCategory component = new SweCategory(); - component.setValue(o.getValue()); + if (!isNoValues()) { + component.setValue(o.getValue()); + } return setCommonValues(component, o); } @@ -161,7 +158,9 @@ public SweDataRecord visit(ComplexDataEntity o) @Override public SweCount visit(CountDataEntity o) throws CodedException { SweCount component = new SweCount(); - component.setValue(o.getValue()); + if (!isNoValues()) { + component.setValue(o.getValue()); + } return setCommonValues(component, o); } @@ -169,74 +168,35 @@ public SweCount visit(CountDataEntity o) throws CodedException { public SweText visit(TextDataEntity o) throws OwsExceptionReport { SweText component = new SweText(); - component.setValue(o.getValue()); + if (!isNoValues()) { + component.setValue(o.getValue()); + } return setCommonValues(component, o); } @Override public SweDataArray visit(DataArrayDataEntity o) throws OwsExceptionReport { - try { - SweDataArray array = new SweDataArray(); - decode(XmlHelper.parseXmlString(o.getResultTemplate().getEncoding())); - array.setEncoding( - (SweAbstractEncoding) decode(XmlHelper.parseXmlString(o.getResultTemplate().getEncoding()))); - array.setElementType( - (SweAbstractDataComponent) decode(XmlHelper.parseXmlString(o.getResultTemplate().getStructure()))); - if (o.isSetStringValue()) { - array.setXml(null); - List> values = new LinkedList<>(); - for (String block : o.getStringValue() - .split(((SweTextEncoding) array.getEncoding()).getBlockSeparator())) { - List v = new LinkedList<>(); - for (String value : block.split(((SweTextEncoding) array.getEncoding()).getTokenSeparator())) { - v.add(value); - } - values.add(v); - } - array.setValues(values); - } else if (o.getValue() != null && !o.getValue().isEmpty()) { - int i = ((SweAbstractDataRecord) array.getElementType()) - .getFieldIndexByIdentifier(o.getDataset().getPhenomenon().getIdentifier()) == 0 ? 1 : 0; - List> values = new LinkedList<>(); - for (DataEntity v : o.getValue()) { - List value = new LinkedList<>(); - if (i == 0) { - value.add(v.getDataset().getPhenomenon().getName()); - value.add(JavaHelper.asString(v.getValue())); - } else { - value.add(JavaHelper.asString(v.getValue())); - value.add(v.getDataset().getPhenomenon().getName()); - } - values.add(value); - } - array.setValues(values); - } - return array; - } catch (DecodingException e) { - throw new NoApplicableCodeException().causedBy(e) - .withMessage("Error while creating SweDataArray from database entity!"); - } + return createSweDataArray(o); } @Override public SweAbstractDataComponent visit(ProfileDataEntity o) throws OwsExceptionReport { - return new ProfileGeneratorSplitter(new ObservationValueCreator(decoderRepository)).createValue(o); + return new ProfileGeneratorSplitter(new ObservationValueCreator(getDecoderRepository())).createValue(o); } @Override public SweCategory visit(ReferencedDataEntity o) throws OwsExceptionReport { SweCategory component = new SweCategory(); - if (o.hasValue()) { - component.setValue(o.getValue()); - } else if (o.hasValueName()) { - component.setValue(o.getValueName()); + if (!isNoValues()) { + if (o.hasValue()) { + component.setValue(o.getValue()); + } else if (o.hasValueName()) { + component.setValue(o.getValueName()); + } } return setCommonValues(component, o); } - protected Object decode(XmlObject xml) throws DecodingException { - return decoderRepository.getDecoder(CodingHelper.getDecoderKey(xml)).decode(xml); - } protected String getFieldName(DataEntity sub) { String name = sub.getDataset().getObservableProperty().getName(); diff --git a/hibernate/handler/src/main/java/org/n52/sos/ds/hibernate/AbstractResultHandler.java b/hibernate/handler/src/main/java/org/n52/sos/ds/hibernate/AbstractResultHandler.java new file mode 100644 index 0000000000..f7a036d520 --- /dev/null +++ b/hibernate/handler/src/main/java/org/n52/sos/ds/hibernate/AbstractResultHandler.java @@ -0,0 +1,91 @@ +/* + * Copyright (C) 2012-2020 52°North Initiative for Geospatial Open Source + * Software GmbH + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 as published + * by the Free Software Foundation. + * + * If the program is linked with libraries which are licensed under one of + * the following licenses, the combination of the program with the linked + * library is not considered a "derivative work" of the program: + * + * - Apache License, version 2.0 + * - Apache Software License, version 1.0 + * - GNU Lesser General Public License, version 3 + * - Mozilla Public License, versions 1.0, 1.1 and 2.0 + * - Common Development and Distribution License (CDDL), version 1.0 + * + * Therefore the distribution of the program linked with libraries licensed + * under the aforementioned licenses, is permitted by the copyright holders + * if the distribution is compliant with both the GNU General Public + * License version 2 and the aforementioned licenses. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General + * Public License for more details. + */ +package org.n52.sos.ds.hibernate; + +import java.util.List; +import java.util.Set; +import java.util.stream.Collectors; + +import org.hibernate.Session; +import org.n52.series.db.beans.DatasetEntity; +import org.n52.shetland.ogc.ows.exception.OwsExceptionReport; +import org.n52.shetland.ogc.sos.SosResultEncoding; +import org.n52.shetland.ogc.sos.SosResultStructure; +import org.n52.shetland.ogc.swe.SweDataRecord; +import org.n52.sos.ds.hibernate.dao.DaoFactory; +import org.n52.sos.ds.hibernate.util.HibernateUnproxy; +import org.n52.sos.ds.hibernate.util.ResultHandlingHelper; +import org.n52.sos.exception.sos.concrete.NoSweCommonEncodingForOfferingObservablePropertyCombination; +import org.n52.svalbard.util.SweHelper; + +public interface AbstractResultHandler extends HibernateUnproxy { + + SweHelper getSweHelper(); + + ResultHandlingHelper getResultHandlingHelper(); + + DaoFactory getDaoFactory(); + + default SosResultEncoding createSosResultEncoding() { + return new SosResultEncoding(getSweHelper().createDefaultTextEncoding()); + } + + default SosResultStructure generateSosResultStructure(String observedProperty, String offering, + Set featureIdentifier, Session session) throws OwsExceptionReport { + List datasets = getDaoFactory().getSeriesDAO() + .getSeries(null, observedProperty, offering, null, session); + if (datasets != null && !datasets.isEmpty()) { + boolean procedure = checkForProcedures(datasets); + boolean feature = checkForFeatures(datasets); + DatasetEntity dataset = datasets.get(0); + if (dataset.getFirstObservation() != null) { + SweDataRecord createRecord = getResultHandlingHelper().createDataRecordForResultTemplate( + unproxy(dataset.getFirstObservation(), session), procedure, feature); + return new SosResultStructure(createRecord); + } + } + throw new NoSweCommonEncodingForOfferingObservablePropertyCombination(offering, observedProperty); + } + + default boolean checkForProcedures(List series) { + return series.stream() + .map(d -> d.getProcedure() + .getId()) + .collect(Collectors.toSet()) + .size() > 1; + } + + default boolean checkForFeatures(List series) { + return series.stream() + .map(d -> d.getFeature() + .getId()) + .collect(Collectors.toSet()) + .size() > 1; + } +} diff --git a/hibernate/handler/src/main/java/org/n52/sos/ds/hibernate/GetResultHandler.java b/hibernate/handler/src/main/java/org/n52/sos/ds/hibernate/GetResultHandler.java index 1bf5471a53..3714b2dc59 100644 --- a/hibernate/handler/src/main/java/org/n52/sos/ds/hibernate/GetResultHandler.java +++ b/hibernate/handler/src/main/java/org/n52/sos/ds/hibernate/GetResultHandler.java @@ -28,7 +28,6 @@ */ package org.n52.sos.ds.hibernate; - import java.util.Collection; import java.util.List; import java.util.Set; @@ -45,6 +44,7 @@ import org.n52.faroe.annotation.Setting; import org.n52.iceland.ds.ConnectionProvider; import org.n52.janmayen.http.HTTPStatus; +import org.n52.janmayen.lifecycle.Constructable; import org.n52.series.db.beans.DataEntity; import org.n52.series.db.beans.DatasetEntity; import org.n52.series.db.beans.ResultTemplateEntity; @@ -59,7 +59,6 @@ import org.n52.shetland.ogc.sos.response.GetResultResponse; import org.n52.shetland.util.CollectionHelper; import org.n52.sos.ds.AbstractGetResultHandler; -import org.n52.sos.ds.FeatureQueryHandler; import org.n52.sos.ds.hibernate.dao.DaoFactory; import org.n52.sos.ds.hibernate.util.HibernateHelper; import org.n52.sos.ds.hibernate.util.QueryHelper; @@ -70,8 +69,8 @@ import org.n52.sos.exception.ows.concrete.UnsupportedTimeException; import org.n52.sos.exception.ows.concrete.UnsupportedValueReferenceException; import org.n52.sos.service.SosSettings; -import org.n52.sos.util.GeometryHandler; import org.n52.svalbard.ConformanceClasses; +import org.n52.svalbard.util.SweHelper; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -84,20 +83,20 @@ * */ @Configurable -public class GetResultHandler extends AbstractGetResultHandler { +public class GetResultHandler extends AbstractGetResultHandler implements AbstractResultHandler, Constructable { private static final Logger LOGGER = LoggerFactory.getLogger(GetResultHandler.class); private HibernateSessionHolder sessionHolder; - private FeatureQueryHandler featureQueryHandler; - private DaoFactory daoFactory; - private GeometryHandler geometryHandler; - private boolean strictSpatialFilteringProfile; + private ResultHandlingHelper resultHandlingHelper; + + private boolean supportsDatabaseEntities; + public GetResultHandler() { super(SosConstants.SOS); } @@ -107,55 +106,19 @@ public void setDaoFactory(DaoFactory daoFactory) { this.daoFactory = daoFactory; } - @Inject - public void setFeatureQueryHandler(FeatureQueryHandler featureQueryHandler) { - this.featureQueryHandler = featureQueryHandler; - } - @Inject public void setConnectionProvider(ConnectionProvider connectionProvider) { this.sessionHolder = new HibernateSessionHolder(connectionProvider); } - @Inject - public void setGeometryHandler(GeometryHandler geometryHandler) { - this.geometryHandler = geometryHandler; - } - @Setting(SosSettings.STRICT_SPATIAL_FILTERING_PROFILE) public void setStrictSpatialFilteringProfile(final boolean strictSpatialFilteringProfile) { this.strictSpatialFilteringProfile = strictSpatialFilteringProfile; } @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 featureIdentifier = QueryHelper.getFeatures(this.featureQueryHandler, request, session); - final List resultTemplates = - queryResultTemplate(request, featureIdentifier, session); - if (CollectionHelper.isNotEmpty(resultTemplates)) { - final SosResultEncoding sosResultEncoding = - createSosResultEncoding(resultTemplates.get(0).getEncoding()); - final SosResultStructure sosResultStructure = - createSosResultStructure(resultTemplates.get(0).getStructure()); - final List> observations; - observations = querySeriesObservation(request, featureIdentifier, session); - response.setResultValues(new ResultHandlingHelper(geometryHandler, daoFactory.getSweHelper()) - .createResultValuesFromObservations(observations, sosResultEncoding, sosResultStructure, - getProfileHandler().getActiveProfile().getResponseNoDataPlaceholder())); - } - 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); - } + public void init() { + this.supportsDatabaseEntities = HibernateHelper.isEntitySupported(ResultTemplateEntity.class); } @Override @@ -176,7 +139,63 @@ public Set getConformanceClasses(String service, String version) { @Override public boolean isSupported() { - return HibernateHelper.isEntitySupported(ResultTemplateEntity.class); + return true; + } + + @Override + public SweHelper getSweHelper() { + return getDaoFactory().getSweHelper(); + } + + @Override + public ResultHandlingHelper getResultHandlingHelper() { + if (resultHandlingHelper == null) { + this.resultHandlingHelper = new ResultHandlingHelper(getDaoFactory().getGeometryHandler(), + getDaoFactory().getSweHelper(), getDaoFactory().getDecoderRepository()); + } + return resultHandlingHelper; + } + + @Override + public DaoFactory getDaoFactory() { + return daoFactory; + } + + @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 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> 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); + } } /** @@ -201,15 +220,18 @@ protected List> querySeriesObservation(GetResultRequest request, addSpatialFilteringProfileRestrictions(c, request, session); addParentChildRestriction(c); - List series = daoFactory.getSeriesDAO().getSeries(request, featureIdentifiers, session); + List series = getDaoFactory().getSeriesDAO() + .getSeries(request, featureIdentifiers, session); if (CollectionHelper.isEmpty(series)) { return null; } else { - c.add(Restrictions.in(DataEntity.PROPERTY_DATASET_ID, - series.stream().map(DatasetEntity::getId).collect(Collectors.toSet()))); + c.add(Restrictions.in(DataEntity.PROPERTY_DATASET_ID, series.stream() + .map(DatasetEntity::getId) + .collect(Collectors.toSet()))); } - if (request.getTemporalFilter() != null && !request.getTemporalFilter().isEmpty()) { + if (request.getTemporalFilter() != null && !request.getTemporalFilter() + .isEmpty()) { addTemporalFilter(c, request.getTemporalFilter()); } @@ -229,11 +251,17 @@ protected List> querySeriesObservation(GetResultRequest request, * Hibernate session * @return Resulting ResultTemplates as list */ - private List queryResultTemplate(final GetResultRequest request, + private ResultTemplateEntity queryResultTemplate(final GetResultRequest request, final Set featureIdentifier, final Session session) { - final List resultTemplates = daoFactory.getResultTemplateDAO().getResultTemplateObject( - request.getOffering(), request.getObservedProperty(), featureIdentifier, session); - return resultTemplates; + if (supportsDatabaseEntities) { + List resultTemplates = getDaoFactory().getResultTemplateDAO() + .getResultTemplateObject(request.getOffering(), request.getObservedProperty(), featureIdentifier, + session); + if (!resultTemplates.isEmpty()) { + return resultTemplates.get(0); + } + } + return null; } /** @@ -268,7 +296,8 @@ private void addTemporalFilter(Criteria c, List temporalFilter) */ @SuppressWarnings("rawtypes") private Criteria createCriteriaFor(Class clazz, Session session) { - return session.createCriteria(clazz).setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY) + return session.createCriteria(clazz) + .setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY) .add(Restrictions.eq(DataEntity.PROPERTY_DELETED, false)) .addOrder(Order.asc(DataEntity.PROPERTY_SAMPLING_TIME_START)); } @@ -291,10 +320,12 @@ private void addParentChildRestriction(Criteria c) { private void addSpatialFilteringProfileRestrictions(Criteria criteria, GetResultRequest request, Session session) throws OwsExceptionReport { if (request.hasSpatialFilteringProfileSpatialFilter()) { - criteria.add(SpatialRestrictions.filter(DataEntity.PROPERTY_GEOMETRY_ENTITY, - request.getSpatialFilter().getOperator(), - geometryHandler.switchCoordinateAxisFromToDatasourceIfNeeded( - request.getSpatialFilter().getGeometry().toGeometry()))); + criteria.add(SpatialRestrictions.filter(DataEntity.PROPERTY_GEOMETRY_ENTITY, request.getSpatialFilter() + .getOperator(), + getDaoFactory().getGeometryHandler() + .switchCoordinateAxisFromToDatasourceIfNeeded(request.getSpatialFilter() + .getGeometry() + .toGeometry()))); } } } diff --git a/hibernate/handler/src/main/java/org/n52/sos/ds/hibernate/GetResultTemplateHandler.java b/hibernate/handler/src/main/java/org/n52/sos/ds/hibernate/GetResultTemplateHandler.java index cff580b610..ce089d87c9 100644 --- a/hibernate/handler/src/main/java/org/n52/sos/ds/hibernate/GetResultTemplateHandler.java +++ b/hibernate/handler/src/main/java/org/n52/sos/ds/hibernate/GetResultTemplateHandler.java @@ -33,6 +33,7 @@ import org.hibernate.HibernateException; import org.hibernate.Session; import org.n52.iceland.ds.ConnectionProvider; +import org.n52.janmayen.lifecycle.Constructable; import org.n52.series.db.beans.ResultTemplateEntity; import org.n52.shetland.ogc.ows.exception.NoApplicableCodeException; import org.n52.shetland.ogc.ows.exception.OwsExceptionReport; @@ -42,7 +43,8 @@ import org.n52.sos.ds.AbstractGetResultTemplateHandler; import org.n52.sos.ds.hibernate.dao.DaoFactory; import org.n52.sos.ds.hibernate.util.HibernateHelper; -import org.n52.sos.exception.sos.concrete.NoSweCommonEncodingForOfferingObservablePropertyCombination; +import org.n52.sos.ds.hibernate.util.ResultHandlingHelper; +import org.n52.svalbard.util.SweHelper; /** * Implementation of the abstract class AbstractGetResultTemplateHandler @@ -50,12 +52,16 @@ * @since 4.0.0 * */ -public class GetResultTemplateHandler - extends AbstractGetResultTemplateHandler { +public class GetResultTemplateHandler extends AbstractGetResultTemplateHandler + implements AbstractResultHandler, Constructable { private HibernateSessionHolder sessionHolder; private DaoFactory daoFactory; + private ResultHandlingHelper resultHandlingHelper; + + private boolean supportsDatabaseEntities; + public GetResultTemplateHandler() { super(SosConstants.SOS); } @@ -70,23 +76,54 @@ public void setConnectionProvider(ConnectionProvider connectionProvider) { this.sessionHolder = new HibernateSessionHolder(connectionProvider); } + @Override + public void init() { + this.supportsDatabaseEntities = HibernateHelper.isEntitySupported(ResultTemplateEntity.class); + } + + @Override + public boolean isSupported() { + return true; + } + + @Override + public SweHelper getSweHelper() { + return getDaoFactory().getSweHelper(); + } + + @Override + public ResultHandlingHelper getResultHandlingHelper() { + if (resultHandlingHelper == null) { + this.resultHandlingHelper = new ResultHandlingHelper(getDaoFactory().getGeometryHandler(), + getDaoFactory().getSweHelper(), getDaoFactory().getDecoderRepository()); + } + return resultHandlingHelper; + } + + @Override + public DaoFactory getDaoFactory() { + return daoFactory; + } + @Override public GetResultTemplateResponse getResultTemplate(GetResultTemplateRequest request) throws OwsExceptionReport { Session session = null; try { session = sessionHolder.getSession(); - ResultTemplateEntity resultTemplate = daoFactory.getResultTemplateDAO() - .getResultTemplateObject(request.getOffering(), request.getObservedProperty(), session); + GetResultTemplateResponse response = new GetResultTemplateResponse(); + response.setService(request.getService()); + response.setVersion(request.getVersion()); + ResultTemplateEntity resultTemplate = supportsDatabaseEntities ? getDaoFactory().getResultTemplateDAO() + .getResultTemplateObject(request.getOffering(), request.getObservedProperty(), session) : null; if (resultTemplate != null) { - GetResultTemplateResponse response = new GetResultTemplateResponse(); - response.setService(request.getService()); - response.setVersion(request.getVersion()); response.setResultEncoding(createSosResultEncoding(resultTemplate.getEncoding())); response.setResultStructure(createSosResultStructure(resultTemplate.getStructure())); - return response; + } else { + response.setResultEncoding(createSosResultEncoding()); + response.setResultStructure(generateSosResultStructure(request.getObservedProperty(), + request.getOffering(), null, session)); } - throw new NoSweCommonEncodingForOfferingObservablePropertyCombination(request.getOffering(), - request.getObservedProperty()); + return response; } catch (HibernateException he) { throw new NoApplicableCodeException().causedBy(he) .withMessage("Error while querying data result template data!"); @@ -95,8 +132,4 @@ public GetResultTemplateResponse getResultTemplate(GetResultTemplateRequest requ } } - @Override - public boolean isSupported() { - return HibernateHelper.isEntitySupported(ResultTemplateEntity.class); - } } diff --git a/hibernate/handler/src/main/java/org/n52/sos/ds/hibernate/InsertResultHandler.java b/hibernate/handler/src/main/java/org/n52/sos/ds/hibernate/InsertResultHandler.java index e17f826ffd..f0b2306207 100644 --- a/hibernate/handler/src/main/java/org/n52/sos/ds/hibernate/InsertResultHandler.java +++ b/hibernate/handler/src/main/java/org/n52/sos/ds/hibernate/InsertResultHandler.java @@ -140,7 +140,8 @@ public InsertResultHandler() { @Override public void init() { this.sessionHolder = new HibernateSessionHolder(connectionProvider); - helper = new ResultHandlingHelper(getDaoFactory().getGeometryHandler(), getDaoFactory().getSweHelper()); + helper = new ResultHandlingHelper(getDaoFactory().getGeometryHandler(), getDaoFactory().getSweHelper(), + getDaoFactory().getDecoderRepository()); } @Override diff --git a/hibernate/handler/src/main/java/org/n52/sos/ds/hibernate/InsertResultTemplateHandler.java b/hibernate/handler/src/main/java/org/n52/sos/ds/hibernate/InsertResultTemplateHandler.java index 2cd10799c6..9bb446f9b5 100644 --- a/hibernate/handler/src/main/java/org/n52/sos/ds/hibernate/InsertResultTemplateHandler.java +++ b/hibernate/handler/src/main/java/org/n52/sos/ds/hibernate/InsertResultTemplateHandler.java @@ -95,7 +95,8 @@ public void setAllowTemplateWithoutProcedureAndFeature(boolean allowTemplateWith @Override public void init() { sessionHolder = new HibernateSessionHolder(connectionProvider); - helper = new ResultHandlingHelper(daoFactory.getGeometryHandler(), daoFactory.getSweHelper()); + helper = new ResultHandlingHelper(getDaoFactory().getGeometryHandler(), getDaoFactory().getSweHelper(), + getDaoFactory().getDecoderRepository()); } @Override @@ -113,7 +114,7 @@ public synchronized InsertResultTemplateResponse insertResultTemplate(InsertResu OmObservationConstellation sosObsConst = request.getObservationTemplate(); DatasetEntity obsConst = null; for (String offeringID : sosObsConst.getOfferings()) { - obsConst = daoFactory.getSeriesDAO().checkSeries(sosObsConst, offeringID, session, + obsConst = getDaoFactory().getSeriesDAO().checkSeries(sosObsConst, offeringID, session, Sos2Constants.InsertResultTemplateParams.proposedTemplate.name()); if (obsConst != null) { // check if result structure elements are supported @@ -122,7 +123,7 @@ public synchronized InsertResultTemplateResponse insertResultTemplate(InsertResu ProcedureEntity procedure = null; AbstractFeatureEntity feature = null; if (sosObsConst.isSetFeatureOfInterest()) { - FeatureOfInterestDAO featureOfInterestDAO = daoFactory.getFeatureOfInterestDAO(); + FeatureOfInterestDAO featureOfInterestDAO = getDaoFactory().getFeatureOfInterestDAO(); feature = featureOfInterestDAO.checkOrInsert(sosObsConst.getFeatureOfInterest(), session); featureOfInterestDAO.checkOrInsertRelatedFeatureRelation(feature, obsConst.getOffering(), session); @@ -160,9 +161,14 @@ public boolean isSupported() { return HibernateHelper.isEntitySupported(ResultTemplateEntity.class); } + public DaoFactory getDaoFactory() { + return daoFactory; + } + private void checkOrInsertResultTemplate(InsertResultTemplateRequest request, DatasetEntity obsConst, ProcedureEntity procedure, AbstractFeatureEntity feature, Session session) throws OwsExceptionReport { - daoFactory.getResultTemplateDAO().checkOrInsertResultTemplate(request, obsConst, procedure, feature, session); + getDaoFactory().getResultTemplateDAO() + .checkOrInsertResultTemplate(request, obsConst, procedure, feature, session); } private void checkResultStructure(SosResultStructure resultStructure, String observedProperty, diff --git a/hibernate/handler/src/main/java/org/n52/sos/ds/hibernate/admin/HibernateRenameDAO.java b/hibernate/handler/src/main/java/org/n52/sos/ds/hibernate/admin/HibernateRenameDAO.java index 5334a27013..21af5ea35a 100644 --- a/hibernate/handler/src/main/java/org/n52/sos/ds/hibernate/admin/HibernateRenameDAO.java +++ b/hibernate/handler/src/main/java/org/n52/sos/ds/hibernate/admin/HibernateRenameDAO.java @@ -46,6 +46,7 @@ * @author Christian Autermann */ public class HibernateRenameDAO implements RenameDAO { + private HibernateSessionHolder sessionHolder; @Inject diff --git a/hibernate/handler/src/main/java/org/n52/sos/ds/hibernate/util/ResultHandlingHelper.java b/hibernate/handler/src/main/java/org/n52/sos/ds/hibernate/util/ResultHandlingHelper.java index aa84672c1b..85aa90957c 100644 --- a/hibernate/handler/src/main/java/org/n52/sos/ds/hibernate/util/ResultHandlingHelper.java +++ b/hibernate/handler/src/main/java/org/n52/sos/ds/hibernate/util/ResultHandlingHelper.java @@ -28,33 +28,45 @@ */ package org.n52.sos.ds.hibernate.util; +import java.math.BigDecimal; import java.util.Collection; import java.util.Date; import java.util.HashMap; +import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.Set; import java.util.TreeMap; +import org.hibernate.Session; import org.joda.time.DateTime; import org.joda.time.DateTimeZone; import org.locationtech.jts.geom.Coordinate; import org.locationtech.jts.geom.Geometry; import org.locationtech.jts.geom.Point; import org.locationtech.jts.io.WKTWriter; +import org.n52.janmayen.NcName; import org.n52.series.db.beans.BlobDataEntity; import org.n52.series.db.beans.BooleanDataEntity; import org.n52.series.db.beans.CategoryDataEntity; import org.n52.series.db.beans.ComplexDataEntity; import org.n52.series.db.beans.CountDataEntity; import org.n52.series.db.beans.DataEntity; +import org.n52.series.db.beans.Describable; import org.n52.series.db.beans.GeometryDataEntity; +import org.n52.series.db.beans.PhenomenonEntity; import org.n52.series.db.beans.ProfileDataEntity; import org.n52.series.db.beans.QuantityDataEntity; import org.n52.series.db.beans.TextDataEntity; +import org.n52.series.db.beans.VerticalMetadataEntity; +import org.n52.series.db.beans.dataset.DatasetType; +import org.n52.series.db.beans.dataset.ObservationType; import org.n52.series.db.beans.parameter.ParameterEntity; import org.n52.shetland.ogc.om.OmConstants; +import org.n52.shetland.ogc.om.values.ProfileLevel; +import org.n52.shetland.ogc.om.values.ProfileValue; +import org.n52.shetland.ogc.om.values.Value; import org.n52.shetland.ogc.ows.exception.CodedException; import org.n52.shetland.ogc.ows.exception.NoApplicableCodeException; import org.n52.shetland.ogc.ows.exception.OwsExceptionReport; @@ -62,6 +74,7 @@ import org.n52.shetland.ogc.sos.SosResultEncoding; import org.n52.shetland.ogc.sos.SosResultStructure; import org.n52.shetland.ogc.swe.SweAbstractDataComponent; +import org.n52.shetland.ogc.swe.SweConstants.SweCoordinateNames; import org.n52.shetland.ogc.swe.SweCoordinate; import org.n52.shetland.ogc.swe.SweDataArray; import org.n52.shetland.ogc.swe.SweDataRecord; @@ -70,11 +83,17 @@ import org.n52.shetland.ogc.swe.encoding.SweAbstractEncoding; import org.n52.shetland.ogc.swe.encoding.SweTextEncoding; import org.n52.shetland.ogc.swe.simpleType.SweAbstractSimpleType; +import org.n52.shetland.ogc.swe.simpleType.SweQuantity; import org.n52.shetland.ogc.swe.simpleType.SweText; +import org.n52.shetland.ogc.swe.simpleType.SweTime; +import org.n52.shetland.ogc.swe.simpleType.SweTimeRange; import org.n52.shetland.util.CollectionHelper; import org.n52.shetland.util.DateTimeHelper; +import org.n52.sos.ds.hibernate.util.observation.ObservationValueCreator; +import org.n52.sos.ds.hibernate.util.observation.SweAbstractDataComponentCreator; import org.n52.sos.util.GeometryHandler; import org.n52.sos.util.IncDecInteger; +import org.n52.svalbard.decode.DecoderRepository; import org.n52.svalbard.util.SweHelper; import com.google.common.base.Strings; @@ -83,17 +102,23 @@ * @since 4.0.0 * */ -public class ResultHandlingHelper { +public class ResultHandlingHelper implements HibernateUnproxy { public static final String OM_PROCEDURE = "om:procedure"; public static final String OM_FEATURE_OF_INTEREST = "om:featureOfInterest"; + public static final String PHENOMENON_TIME = "phenomenonTime"; + private final SweHelper helper; private GeometryHandler geometryHandler; - public ResultHandlingHelper(GeometryHandler geometryHandler, SweHelper sweHelper) { + private DecoderRepository decoderRepository; + + public ResultHandlingHelper(GeometryHandler geometryHandler, SweHelper sweHelper, + DecoderRepository decoderRepository) { + this.decoderRepository = decoderRepository; this.geometryHandler = geometryHandler; this.helper = sweHelper; } @@ -108,6 +133,8 @@ public ResultHandlingHelper(GeometryHandler geometryHandler, SweHelper sweHelper * The ResultEncoding * @param sosResultStructure * The ResultStructure + * @param session + * The Hibernate session * @return Result values String from observation according to ResultEncoding * and ResultStructure * @throws OwsExceptionReport @@ -115,28 +142,35 @@ public ResultHandlingHelper(GeometryHandler geometryHandler, SweHelper sweHelper */ public String createResultValuesFromObservations(final Collection> observations, final SosResultEncoding sosResultEncoding, final SosResultStructure sosResultStructure, - String noDataPlaceholder) throws OwsExceptionReport { - final Map valueOrder = getValueOrderMap(sosResultStructure.get().get()); + String noDataPlaceholder, Session session) throws OwsExceptionReport { + final Map valueOrder = getValueOrderMap(sosResultStructure.get() + .get()); return createResultValuesFromObservations(observations, sosResultEncoding, sosResultStructure, - noDataPlaceholder, valueOrder, true); + noDataPlaceholder, valueOrder, true, null, session); } private String createResultValuesFromObservations(final Collection> observations, final SosResultEncoding sosResultEncoding, final SosResultStructure sosResultStructure, - String noDataPlaceholder, Map valueOrder, boolean addCount) throws OwsExceptionReport { + String noDataPlaceholder, Map valueOrder, boolean addCount, + VerticalMetadataEntity vertical, Session session) throws OwsExceptionReport { final StringBuilder builder = new StringBuilder(); if (CollectionHelper.isNotEmpty(observations)) { - final String tokenSeparator = getTokenSeparator(sosResultEncoding.get().get()); - final String blockSeparator = getBlockSeparator(sosResultEncoding.get().get()); + final String tokenSeparator = getTokenSeparator(sosResultEncoding.get() + .get()); + final String blockSeparator = getBlockSeparator(sosResultEncoding.get() + .get()); if (addCount) { addElementCount(builder, observations.size(), blockSeparator); } - for (final DataEntity observation : observations) { - for (final Entry entry : valueOrder.entrySet()) { - if (observation instanceof ProfileDataEntity) { - builder.append(createResultValuesFromObservations(((ProfileDataEntity) observation).getValue(), - sosResultEncoding, sosResultStructure, noDataPlaceholder, valueOrder, false)); - } else { + for (final DataEntity obs : observations) { + DataEntity observation = unproxy(obs, session); + if (observation instanceof ProfileDataEntity) { + builder.append(createResultValuesFromObservations(((ProfileDataEntity) observation).getValue(), + sosResultEncoding, sosResultStructure, noDataPlaceholder, valueOrder, false, + ((ProfileDataEntity) observation).getDataset().getVerticalMetadata(), session)); + builder.append(blockSeparator); + } else { + for (final Entry entry : valueOrder.entrySet()) { final String definition = entry.getValue(); switch (definition) { case OmConstants.PHENOMENON_TIME: @@ -148,26 +182,32 @@ private String createResultValuesFromObservations(final Collection getTimeStringForResultTime(observation.getResultTime(), noDataPlaceholder)); break; case OmConstants.PARAM_NAME_SAMPLING_GEOMETRY: - builder.append(getSamplingGeometry(observation, tokenSeparator, - sosResultStructure.get().get(), noDataPlaceholder)); + builder.append( + getSamplingGeometry(observation, tokenSeparator, sosResultStructure.get() + .get(), noDataPlaceholder)); break; case OmConstants.OM_PARAMETER: case OmConstants.PARAMETER: - builder.append( - getParameters(observation, tokenSeparator, sosResultStructure.get().get())); + builder.append(getParameters(observation, tokenSeparator, sosResultStructure.get() + .get(), vertical)); break; case OM_PROCEDURE: - if (observation.getDataset().getProcedure() != null - && observation.getDataset().getProcedure().isSetIdentifier()) { + if (observation.getDataset() + .getProcedure() != null && observation.getDataset().getProcedure() + .isSetIdentifier()) { builder.append(observation.getDataset().getProcedure().getIdentifier()); } else { builder.append(""); } break; case OM_FEATURE_OF_INTEREST: - if (observation.getDataset().getFeature() != null - && observation.getDataset().getFeature().isSetIdentifier()) { - builder.append(observation.getDataset().getFeature().getIdentifier()); + if (observation.getDataset() + .getFeature() != null && observation.getDataset() + .getFeature() + .isSetIdentifier()) { + builder.append(observation.getDataset() + .getFeature() + .getIdentifier()); } else { builder.append(""); } @@ -179,8 +219,8 @@ private String createResultValuesFromObservations(final Collection builder.append(tokenSeparator); } builder.delete(builder.lastIndexOf(tokenSeparator), builder.length()); + builder.append(blockSeparator); } - builder.append(blockSeparator); } if (builder.length() > 0) { builder.delete(builder.lastIndexOf(blockSeparator), builder.length()); @@ -271,7 +311,8 @@ public int checkFields(final List fields, final String definition) { int i = 0; for (final SweField f : fields) { final SweAbstractDataComponent element = f.getElement(); - if (element.isSetDefinition() && element.getDefinition().equals(definition)) { + if (element.isSetDefinition() && element.getDefinition() + .equals(definition)) { return i; } ++i; @@ -335,7 +376,8 @@ private void addOrderAndDefinitionToMap(final List fields, final Map valueOrder, fina } private String getValueAsStringForObservedProperty(final DataEntity observation, final String definition) { - final String observedProperty = observation.getDataset().getObservableProperty().getIdentifier(); + final String observedProperty = observation.getDataset() + .getObservableProperty() + .getIdentifier(); if (observation instanceof ComplexDataEntity) { for (DataEntity contentObservation : ((ComplexDataEntity) observation).getValue()) { String value = getValueAsStringForObservedProperty(contentObservation, definition); @@ -392,7 +436,8 @@ private String getValueAsStringForObservedProperty(final DataEntity observati return String.valueOf(((TextDataEntity) observation).getValue()); } else if (observation instanceof GeometryDataEntity) { final WKTWriter writer = new WKTWriter(); - return writer.write(((GeometryDataEntity) observation).getValue().getGeometry()); + return writer.write(((GeometryDataEntity) observation).getValue() + .getGeometry()); } else if (observation instanceof BlobDataEntity) { return String.valueOf(((BlobDataEntity) observation).getValue()); } @@ -410,7 +455,8 @@ private String getSamplingGeometry(DataEntity observation, String tokenSepara Geometry samplingGeometry = null; if (observation.isSetGeometryEntity()) { samplingGeometry = getGeomtryHandler() - .switchCoordinateAxisFromToDatasourceIfNeeded(observation.getGeometryEntity().getGeometry()); + .switchCoordinateAxisFromToDatasourceIfNeeded(observation.getGeometryEntity() + .getGeometry()); } for (final Entry entry : valueOrder.entrySet()) { final String definition = entry.getValue(); @@ -438,7 +484,8 @@ private String getSamplingGeometry(DataEntity observation, String tokenSepara } builder.append(tokenSeparator); } - return builder.delete(builder.lastIndexOf(tokenSeparator), builder.length()).toString(); + return builder.delete(builder.lastIndexOf(tokenSeparator), builder.length()) + .toString(); } return noDataPlaceholder; } @@ -471,7 +518,7 @@ private SweVector getVector(List fields) throws CodedException { } private String getParameters(DataEntity observation, String tokenSeparator, - SweAbstractDataComponent resultStructure) { + SweAbstractDataComponent resultStructure, VerticalMetadataEntity vertical) { SweDataRecord record = null; if (resultStructure instanceof SweDataArray && ((SweDataArray) resultStructure).getElementType() instanceof SweDataRecord) { @@ -489,18 +536,32 @@ record = (SweDataRecord) resultStructure; if (observation.hasParameters() && hasParameter(observation.getParameters(), order.getValue())) { builder.append(getParameterValue(observation.getParameters(), order.getValue())) .append(tokenSeparator); + } else if (vertical != null && (vertical.getVerticalFromName().equals(order.getValue()) + || vertical.getVerticalToName().equals(order.getValue()))) { + if (vertical.areVerticalNamesEqual()) { + builder.append(observation.getVerticalTo().toPlainString()).append(tokenSeparator); + } else { + if (vertical.getVerticalFromName().equals(order.getValue())) { + builder.append(observation.getVerticalFrom().toPlainString()).append(tokenSeparator); + } else if (vertical.getVerticalToName().equals(order.getValue())) { + builder.append(observation.getVerticalTo().toPlainString()).append(tokenSeparator); + } + } } else { - builder.append("").append(tokenSeparator); + builder.append("") + .append(tokenSeparator); } } - return builder.delete(builder.lastIndexOf(tokenSeparator), builder.length()).toString(); + return builder.delete(builder.lastIndexOf(tokenSeparator), builder.length()) + .toString(); } return ""; } private String getParameterValue(Set> set, String value) { for (ParameterEntity parameter : set) { - if (parameter.getName().equals(value)) { + if (parameter.getName() + .equals(value)) { return parameter.getValueAsString(); } } @@ -509,7 +570,8 @@ private String getParameterValue(Set> set, String value) { private boolean hasParameter(Set> set, String value) { for (ParameterEntity parameter : set) { - if (parameter.getName().equals(value)) { + if (parameter.getName() + .equals(value)) { return true; } } @@ -589,8 +651,10 @@ public boolean checkVectorForSamplingGeometry(SweField swefield) throws CodedExc } public boolean checkDefinition(SweField sweField, String definition) { - if (sweField != null && sweField.getElement().isSetDefinition()) { - return definition.equals(sweField.getElement().getDefinition()); + if (sweField != null && sweField.getElement() + .isSetDefinition()) { + return definition.equals(sweField.getElement() + .getDefinition()); } return false; } @@ -619,4 +683,174 @@ private GeometryHandler getGeomtryHandler() { return geometryHandler; } + public SweDataRecord createDataRecordForResultTemplate(DataEntity observation, boolean procedure, + boolean feature) throws OwsExceptionReport { + SweDataRecord record = new SweDataRecord(); + if (observation.isSamplingTimePeriod()) { + record.addField(new SweField(PHENOMENON_TIME, new SweTimeRange().setUom(OmConstants.PHEN_UOM_ISO8601) + .setDefinition(OmConstants.PHENOMENON_TIME))); + } else { + record.addField(new SweField(PHENOMENON_TIME, new SweTime().setUom(OmConstants.PHEN_UOM_ISO8601) + .setDefinition(OmConstants.PHENOMENON_TIME))); + } + record.addField(new SweField("resultTime", new SweTime().setUom(OmConstants.PHEN_UOM_ISO8601) + .setDefinition(OmConstants.RESULT_TIME))); + createObservedPropertyField(observation).stream().forEach(f -> record.addField(f)); + if (procedure) { + record.addField(new SweField("procedure", new SweText().setDefinition(OM_PROCEDURE))); + } + if (feature) { + record.addField(new SweField("featureOfInterest", new SweText().setDefinition(OM_FEATURE_OF_INTEREST))); + } + if (observation.isSetGeometryEntity()) { + record.addField(new SweField("samplingGeometry", createSamplingGeometryVector())); + } + return record; + } + + private List createObservedPropertyField(DataEntity observation) throws OwsExceptionReport { + List fields = new LinkedList<>(); + PhenomenonEntity phenomenon = observation.getDataset() + .getPhenomenon(); + if (observation.getDataset() + .getDatasetType() + .equals(DatasetType.profile) + || observation.getDataset() + .getObservationType() + .equals(ObservationType.profile)) { + ProfileValue profile = (ProfileValue) new ObservationValueCreator(decoderRepository).visit(observation); + ProfileLevel level = profile.getValue().get(0); + fields.add(createVerticalParameter(level)); + if (level.getValue() + .get(0) instanceof SweAbstractDataComponent) { + if (level.getValue().size() > 1) { + SweDataRecord record = new SweDataRecord(); + for (Value v : level.getValue()) { + SweAbstractDataComponent dc = (SweAbstractDataComponent) v; + record.addField(new SweField(NcName.makeValid(dc.getDefinition()), dc)); + } + fields.add(new SweField(getNcNameName(phenomenon), record)); + return fields; + } else { + SweAbstractDataComponent swe = (SweAbstractDataComponent) level.getValue().get(0).setValue(null); + swe.setDefinition(phenomenon.getIdentifier()); + fields.add(new SweField(getNcNameName(phenomenon), swe)); + return fields; + } + } + } else { + SweAbstractDataComponent value = + new SweAbstractDataComponentCreator(decoderRepository, true).visit(observation); + fields.add(new SweField(getNcNameName(phenomenon), (SweAbstractDataComponent) value)); + return fields; + } + throw new NoApplicableCodeException(); + } + + private SweField createVerticalParameter(ProfileLevel level) { + SweDataRecord record = new SweDataRecord(); + record.setDefinition(OmConstants.OM_PARAMETER); + if (level.isSetLevelStart()) { + record.addField(new SweField(level.getLevelStart() + .getName(), + level.getLevelStart() + .setValue((BigDecimal) null))); + } + if (level.isSetLevelEnd()) { + record.addField(new SweField(level.getLevelEnd() + .getName(), + level.getLevelEnd() + .setValue((BigDecimal) null))); + } + return new SweField(OmConstants.OM_PARAMETER, record); + } + + private SweVector createSamplingGeometryVector() { + SweVector vector = new SweVector(); + vector.setDefinition(OmConstants.PARAM_NAME_SAMPLING_GEOMETRY); + List> coordinates = new LinkedList<>(); + coordinates.add(new SweCoordinate<>(SweCoordinateNames.LATITUDE, + createSweQuantityLatLon(SweCoordinateNames.LATITUDE, "lat"))); + coordinates.add(new SweCoordinate<>(SweCoordinateNames.LONGITUDE, + createSweQuantityLatLon(SweCoordinateNames.LONGITUDE, "lon"))); +// coordinates.add(new SweCoordinate<>(SweCoordinateNames.ALTITUDE, +// createSweQuantity(SweCoordinateNames.ALTITUDE, "alt", "m"))); + vector.setCoordinates(coordinates); + return vector; + } + + private SweQuantity createSweQuantityLatLon(String definition, String axis) { + return createSweQuantity(definition, axis, "deg"); + } + + private SweQuantity createSweQuantity(String definition, String axis, String unit) { + return (SweQuantity) new SweQuantity().setAxisID(axis) + .setUom(unit) + .setDefinition(definition); + } + + private String getNcNameName(Describable entity) { + return NcName.makeValid( + entity.isSetName() ? entity.getName() : entity.getIdentifier()); + } + +// @Override +// public QuantityValue visit(QuantityDataEntity o) { +// return visit(o, new QuantityValue((BigDecimal) null)); +// } +// +// @Override +// public UnknownValue visit(BlobDataEntity o) { +// return visit(o, new UnknownValue(null)); +// } +// +// @Override +// public BooleanValue visit(BooleanDataEntity o) { +// return visit(o, new BooleanValue(null)); +// } +// +// @Override +// public CategoryValue visit(CategoryDataEntity o) { +// return visit(o, new CategoryValue(null)); +// } +// +// @Override +// public ComplexValue visit(ComplexDataEntity o) throws OwsExceptionReport { +// SweAbstractDataComponentCreator visitor = new SweAbstractDataComponentCreator(getDecoderRepository(), true); +// SweDataRecord record = visitor.visit(o); +// return new ComplexValue(record); +// } +// +// @Override +// public CountValue visit(CountDataEntity o) { +// return new CountValue(null); +// } +// +// @Override +// public GeometryValue visit(GeometryDataEntity o) throws OwsExceptionReport { +// return visit(o, new GeometryValue((AbstractGeometry) null)); +// } +// +// @Override +// public TextValue visit(TextDataEntity o) { +// return visit(o, new TextValue(null)); +// } +// +// @Override +// public SweDataArrayValue visit(DataArrayDataEntity o) throws OwsExceptionReport { +// SweDataArray array = createSweDataArray(o); +// array.setEncoding(null); +// return new SweDataArrayValue(array); +// } +// +// @Override +// public ProfileValue visit(ProfileDataEntity o) throws OwsExceptionReport { +// return new ProfileGeneratorSplitter(this).create(o); +// } +// +// @Override +// public ReferenceValue visit(ReferencedDataEntity o) { +// return visit(o, new ReferenceValue(null)); +// } + } diff --git a/hibernate/handler/src/test/java/org/n52/sos/ds/hibernate/AbstractInsertDAOTest.java b/hibernate/handler/src/test/java/org/n52/sos/ds/hibernate/AbstractInsertDAOTest.java index 455f34bf65..49994a327c 100644 --- a/hibernate/handler/src/test/java/org/n52/sos/ds/hibernate/AbstractInsertDAOTest.java +++ b/hibernate/handler/src/test/java/org/n52/sos/ds/hibernate/AbstractInsertDAOTest.java @@ -73,7 +73,6 @@ import org.n52.shetland.ogc.gml.ReferenceType; import org.n52.shetland.ogc.gml.time.TimeInstant; import org.n52.shetland.ogc.om.NamedValue; -import org.n52.shetland.ogc.om.ObservationStream; import org.n52.shetland.ogc.om.ObservationValue; import org.n52.shetland.ogc.om.OmConstants; import org.n52.shetland.ogc.om.OmObservableProperty; @@ -133,6 +132,7 @@ import org.n52.svalbard.decode.SensorMLDecoderV20; import org.n52.svalbard.decode.SweCommonDecoderV101; import org.n52.svalbard.decode.SweCommonDecoderV20; +import org.n52.svalbard.encode.Encoder; import org.n52.svalbard.encode.EncoderRepository; import org.n52.svalbard.encode.GmlEncoderv311; import org.n52.svalbard.encode.GmlEncoderv321; @@ -140,6 +140,7 @@ import org.n52.svalbard.encode.SensorMLEncoderv20; import org.n52.svalbard.encode.SweCommonEncoderv101; import org.n52.svalbard.encode.SweCommonEncoderv20; +import org.n52.svalbard.encode.XmlEncoderKey; import org.n52.svalbard.encode.exception.EncodingException; import org.n52.svalbard.util.CodingHelper; import org.n52.svalbard.util.SweHelper; @@ -272,6 +273,10 @@ public abstract class AbstractInsertDAOTest extends HibernateTestCase { protected final GetObservationDaoImpl getObsDAO = new GetObservationDaoImpl(); + protected final GetResultTemplateHandler getResultTemplateHandler = new GetResultTemplateHandler(); + + protected final GetResultHandler getResultHandler = new GetResultHandler(); + protected final SosInsertObservationOperatorV20 insertObservationOperatorv2 = new SosInsertObservationOperatorV20(); protected final I18NDAORepository i18NDAORepository = new I18NDAORepository(); @@ -336,6 +341,7 @@ public void setUp() throws OwsExceptionReport, ConverterException, EncodingExcep daoFactory.setDecoderRepository(decoderRepository); daoFactory.setEncoderRepository(encoderRepository); daoFactory.setI18NDAORepository(i18NDAORepository); + daoFactory.setSweHelper(initSweHelper()); cacheFeeder.setConnectionProvider(holder); cacheFeeder.setI18NDAORepository(i18NDAORepository); @@ -372,7 +378,6 @@ public void setUp() throws OwsExceptionReport, ConverterException, EncodingExcep featureQueryHandler, converterRepository, factoryRepository, geometryHandler, decoderRepository, null, bindingRepository); observationCtx.setDefaultLanguage("eng"); - initDaos(); Session session = null; try { session = getSession(); @@ -380,7 +385,17 @@ public void setUp() throws OwsExceptionReport, ConverterException, EncodingExcep } finally { returnSession(session); } + initDaos(); + } + protected SweHelper initSweHelper() { + SweHelper helper = new SweHelper(); + helper.setDecimalSeparator(DECIMAL_SEPARATOR); + helper.setTokenSeparator(TOKEN_SEPARATOR); + helper.setTupleSeparator(BLOCK_SEPARATOR); + helper.setNorthingNames(SweConstants.SweCoordinateNames.LATITUDE); + helper.setEastingNames(SweConstants.SweCoordinateNames.LONGITUDE); + return helper; } @After @@ -422,6 +437,15 @@ private void initDaos() { getObsDAO.setEncoderRepository(encoderRepository); getObsDAO.setDefaultLanguage("eng"); getObsDAO.setOmObservationCreatorContext(observationCtx); + getResultTemplateHandler.setConnectionProvider(this); + getResultTemplateHandler.setDecoderRepository(decoderRepository); + getResultTemplateHandler.setDaoFactory(daoFactory); + getResultTemplateHandler.init(); + getResultHandler.setConnectionProvider(this); + getResultHandler.setDecoderRepository(decoderRepository); + getResultHandler.setDaoFactory(daoFactory); + getResultHandler.setProfileHandler(new DefaultProfileHandler()); + getResultHandler.init(); } private void initEncoder() { @@ -788,6 +812,13 @@ protected NamedValue createTextParameter(String name, String value) { return namedValue; } + protected void printXml(Object o, String namespace) throws EncodingException { + Encoder encoder = encoderRepository.getEncoder(new XmlEncoderKey(namespace, o.getClass())); + if (encoder != null) { + System.out.println(encoder.encode(o).xmlText(new XmlOptions())); + } + } + private class TestingSosContentCacheControllerImpl extends SosContentCacheControllerImpl { @Override diff --git a/hibernate/handler/src/test/java/org/n52/sos/ds/hibernate/ComplexObservationInsertDAOTest.java b/hibernate/handler/src/test/java/org/n52/sos/ds/hibernate/ComplexObservationInsertDAOTest.java index 8b7dea31f8..494902fc70 100644 --- a/hibernate/handler/src/test/java/org/n52/sos/ds/hibernate/ComplexObservationInsertDAOTest.java +++ b/hibernate/handler/src/test/java/org/n52/sos/ds/hibernate/ComplexObservationInsertDAOTest.java @@ -46,9 +46,14 @@ import org.n52.shetland.ogc.om.values.ComplexValue; import org.n52.shetland.ogc.ows.exception.OwsExceptionReport; import org.n52.shetland.ogc.sos.request.GetObservationRequest; +import org.n52.shetland.ogc.sos.request.GetResultRequest; +import org.n52.shetland.ogc.sos.request.GetResultTemplateRequest; import org.n52.shetland.ogc.sos.request.InsertObservationRequest; import org.n52.shetland.ogc.sos.response.GetObservationResponse; +import org.n52.shetland.ogc.sos.response.GetResultResponse; +import org.n52.shetland.ogc.sos.response.GetResultTemplateResponse; import org.n52.shetland.ogc.sos.response.InsertObservationResponse; +import org.n52.shetland.ogc.swe.SweAbstractDataComponent; import org.n52.shetland.ogc.swe.SweAbstractDataRecord; import org.n52.shetland.ogc.swe.SweDataRecord; import org.n52.shetland.ogc.swe.SweField; @@ -80,6 +85,57 @@ public void setUp() throws OwsExceptionReport, ConverterException, EncodingExcep @Test public void testInsertComplexObservation() throws OwsExceptionReport, InterruptedException, ConverterException { + insertData(); + assertInsertionAftermathBeforeAndAfterCacheReload(); + checkObservation(); + } + + @Test + public void testGeneratedGetResultTemplate() throws OwsExceptionReport, ConverterException { + insertData(); + GetResultTemplateRequest request = new GetResultTemplateRequest(); + request.setObservedProperty(OBSPROP3); + request.setOffering(OFFERING3); + GetResultTemplateResponse response = getResultTemplateHandler.getResultTemplate(request); + assertThat(response, notNullValue()); + assertThat(response.getResultStructure(), notNullValue()); + assertThat(response.getResultStructure().get().isPresent(), is(true)); + SweAbstractDataComponent sweAbstractDataComponent = response.getResultStructure().get().get(); + assertThat(sweAbstractDataComponent, instanceOf(SweDataRecord.class)); + SweDataRecord record = (SweDataRecord) sweAbstractDataComponent; + assertThat(record.getFields().size(), is(3)); + SweField field = record.getFieldByIdentifier(OBSPROP3); + assertThat(field, notNullValue()); + assertThat(field.getElement(), notNullValue()); + assertThat(field.getElement(), instanceOf(SweDataRecord.class)); + SweDataRecord valueRecord = (SweDataRecord) field.getElement(); + assertThat(valueRecord.getFields().size(), is(3)); + assertThat(valueRecord.getFields().get(0).getElement().getDefinition(), is(OBSPROP_VALUE1)); + assertThat(valueRecord.getFields().get(1).getElement().getDefinition(), is(OBSPROP_VALUE2)); + assertThat(valueRecord.getFields().get(2).getElement().getDefinition(), is(OBSPROP_VALUE3)); + } + + @Test + public void testGeneratedGetResult() throws OwsExceptionReport, ConverterException { + insertData(); + GetResultRequest request = new GetResultRequest(); + request.setObservedProperty(OBSPROP3); + request.setOffering(OFFERING3); + GetResultResponse response = getResultHandler.getResult(request); + assertThat(response, notNullValue()); + String resultValues = response.getResultValues(); + String[] blocks = resultValues.split(BLOCK_SEPARATOR); + assertThat(blocks.length, is(2)); + String[] tokens = blocks[1].split(TOKEN_SEPARATOR); + assertThat(tokens.length, is(5)); + assertThat(BigDecimal.valueOf(VAL1).equals(BigDecimal.valueOf(Double.valueOf(tokens[2]))), is(true)); + assertThat(BigDecimal.valueOf(VAL2).equals(BigDecimal.valueOf(Double.valueOf(tokens[3]))), is(true)); + assertThat(BigDecimal.valueOf(VAL3).equals(BigDecimal.valueOf(Double.valueOf(tokens[4]))), is(true)); + assertThat(resultValues, is( + "1#2013-07-18T03:00:00.000Z,2013-07-18T03:00:00.000Z,19.1000000000,19.8000000000,20.4000000000")); + } + + private void insertData() throws OwsExceptionReport, ConverterException { InsertObservationRequest req = new InsertObservationRequest(); req.setAssignedSensorId(PROCEDURE3); req.setOfferings(Lists.newArrayList(OFFERING3)); @@ -110,18 +166,16 @@ public void testInsertComplexObservation() throws OwsExceptionReport, Interrupte req.setObservation(Lists.newArrayList(obs)); InsertObservationResponse resp = insertObservationDAO.insertObservation(req); this.serviceEventBus.submit(new ObservationInsertion(req, resp)); - assertInsertionAftermathBeforeAndAfterCacheReload(); - checkObservation(); } protected void checkObservation() throws OwsExceptionReport { GetObservationRequest getObsReq = createDefaultGetObservationRequest(OFFERING3, PROCEDURE3, OBSPROP3, OBS_TIME, FEATURE3); - GetObservationResponse getObsResponse = + GetObservationResponse response = getObsDAO.queryObservationData(getObsReq, getGetObservationRequest(getObsReq)); - assertThat(getObsResponse, notNullValue()); - assertThat(getObsResponse.getObservationCollection().hasNext(), is(true)); - OmObservation omObservation = getObservation(getObsResponse); + assertThat(response, notNullValue()); + assertThat(response.getObservationCollection().hasNext(), is(true)); + OmObservation omObservation = getObservation(response); assertThat(omObservation.getObservationConstellation(), notNullValue()); OmObservationConstellation obsConst = omObservation.getObservationConstellation(); assertThat(obsConst.getProcedure().getIdentifier(), is(PROCEDURE3)); diff --git a/hibernate/handler/src/test/java/org/n52/sos/ds/hibernate/InsertObservationInsertDAOTest.java b/hibernate/handler/src/test/java/org/n52/sos/ds/hibernate/InsertObservationInsertDAOTest.java index 2f4a3e791b..8893e362c2 100644 --- a/hibernate/handler/src/test/java/org/n52/sos/ds/hibernate/InsertObservationInsertDAOTest.java +++ b/hibernate/handler/src/test/java/org/n52/sos/ds/hibernate/InsertObservationInsertDAOTest.java @@ -108,6 +108,7 @@ public void testInsertObservation() throws OwsExceptionReport, InterruptedExcept TEMP_UNIT); } + @Test public void testInsertObservationWithSamplingGeometry() throws OwsExceptionReport, ConverterException, InterruptedException { @@ -198,5 +199,4 @@ public void testInsertDuplicateObservation() throws OwsExceptionReport, Converte assertInsertionAftermathBeforeAndAfterCacheReload(); } - } diff --git a/hibernate/handler/src/test/java/org/n52/sos/ds/hibernate/InsertSensorInsertDAOTest.java b/hibernate/handler/src/test/java/org/n52/sos/ds/hibernate/InsertSensorInsertDAOTest.java index 6893dc59c1..36937f8698 100644 --- a/hibernate/handler/src/test/java/org/n52/sos/ds/hibernate/InsertSensorInsertDAOTest.java +++ b/hibernate/handler/src/test/java/org/n52/sos/ds/hibernate/InsertSensorInsertDAOTest.java @@ -44,8 +44,6 @@ public void setUp() throws OwsExceptionReport, ConverterException, EncodingExcep super.setUp(); Session session = null; try { - session = getSession(); - HibernateMetadataCache.init(session); session = getSession(); HibernateMetadataCache.init(session); insertSensor(PROCEDURE1, OFFERING1, OBSPROP1, null, OmConstants.OBS_TYPE_MEASUREMENT); diff --git a/hibernate/handler/src/test/java/org/n52/sos/ds/hibernate/ProfileObservationInsertDAOTest.java b/hibernate/handler/src/test/java/org/n52/sos/ds/hibernate/ProfileObservationInsertDAOTest.java index b42d165fad..e34f1f809d 100644 --- a/hibernate/handler/src/test/java/org/n52/sos/ds/hibernate/ProfileObservationInsertDAOTest.java +++ b/hibernate/handler/src/test/java/org/n52/sos/ds/hibernate/ProfileObservationInsertDAOTest.java @@ -51,9 +51,16 @@ import org.n52.shetland.ogc.om.values.QuantityValue; import org.n52.shetland.ogc.ows.exception.OwsExceptionReport; import org.n52.shetland.ogc.sos.request.GetObservationRequest; +import org.n52.shetland.ogc.sos.request.GetResultRequest; +import org.n52.shetland.ogc.sos.request.GetResultTemplateRequest; import org.n52.shetland.ogc.sos.request.InsertObservationRequest; import org.n52.shetland.ogc.sos.response.GetObservationResponse; +import org.n52.shetland.ogc.sos.response.GetResultResponse; +import org.n52.shetland.ogc.sos.response.GetResultTemplateResponse; import org.n52.shetland.ogc.sos.response.InsertObservationResponse; +import org.n52.shetland.ogc.swe.SweAbstractDataComponent; +import org.n52.shetland.ogc.swe.SweDataRecord; +import org.n52.shetland.ogc.swe.SweField; import org.n52.shetland.ogc.swe.simpleType.SweQuantity; import org.n52.sos.ds.hibernate.util.HibernateMetadataCache; import org.n52.sos.event.events.ObservationInsertion; @@ -64,7 +71,6 @@ public class ProfileObservationInsertDAOTest extends AbstractInsertDAOTest { - private static final Double PROFILE_VAL1 = 2.5; private static final Double PROFILE_VAL2 = 7.5; @@ -107,37 +113,120 @@ public void setUp() throws OwsExceptionReport, ConverterException, EncodingExcep @Test public void testInsertProfileIntervalObservation() throws OwsExceptionReport, InterruptedException, ConverterException { + insertProfileIntervalObservationData(); + assertInsertionAftermathBeforeAndAfterCacheReload(); + checkObservation(true); + } + + @Test + public void testGeneratedProfileIntervalGetResultTemplate() throws OwsExceptionReport, ConverterException { + insertProfileIntervalObservationData(); + GetResultTemplateRequest request = new GetResultTemplateRequest(); + request.setObservedProperty(OBSPROP3); + request.setOffering(OFFERING3); + GetResultTemplateResponse response = getResultTemplateHandler.getResultTemplate(request); + assertThat(response, notNullValue()); + assertThat(response.getResultStructure(), notNullValue()); + assertThat(response.getResultStructure().get().isPresent(), is(true)); + SweAbstractDataComponent sweAbstractDataComponent = response.getResultStructure().get().get(); + assertThat(sweAbstractDataComponent, instanceOf(SweDataRecord.class)); + SweDataRecord record = (SweDataRecord) sweAbstractDataComponent; + assertThat(record.getFields().size(), is(4)); + SweField param = record.getFieldByIdentifier("om:parameter"); + assertThat(param, notNullValue()); + assertThat(param.getElement(), notNullValue()); + assertThat(param.getElement(), instanceOf(SweDataRecord.class)); + SweDataRecord paramRecord = (SweDataRecord) param.getElement(); + assertThat(paramRecord.getDefinition(), is(OmConstants.OM_PARAMETER)); + assertThat(paramRecord.getFields().size(), is(2)); + assertThat(paramRecord.getFields().get(0).getElement().getDefinition(), is(FROM)); + assertThat(paramRecord.getFields().get(1).getElement().getDefinition(), is(TO)); + SweField field = record.getFieldByIdentifier(OBSPROP3); + assertThat(field, notNullValue()); + assertThat(field.getElement(), notNullValue()); + assertThat(field.getElement(), instanceOf(SweQuantity.class)); + } + + @Test + public void testGeneratedProfileIntervalGetResult() throws OwsExceptionReport, ConverterException { + insertProfileIntervalObservationData(); + GetResultRequest request = new GetResultRequest(); + request.setObservedProperty(OBSPROP3); + request.setOffering(OFFERING3); + GetResultResponse response = getResultHandler.getResult(request); + assertThat(response, notNullValue()); + String resultValues = response.getResultValues(); + assertThat(resultValues, is( + "1#2013-07-18T03:00:00.000Z,2013-07-18T03:00:00.000Z,0.0000000000,5.0000000000,2.5000000000#" + + "2013-07-18T03:00:00.000Z,2013-07-18T03:00:00.000Z,5.0000000000,10.0000000000,7.5000000000#" + + "2013-07-18T03:00:00.000Z,2013-07-18T03:00:00.000Z,10.0000000000,15.0000000000,12.5000000000#" + + "2013-07-18T03:00:00.000Z,2013-07-18T03:00:00.000Z,15.0000000000,20.0000000000,17.5000000000")); + } + + private void insertProfileIntervalObservationData() throws OwsExceptionReport, ConverterException { InsertObservationRequest req = new InsertObservationRequest(); req.setAssignedSensorId(PROCEDURE3); req.setOfferings(Lists.newArrayList(OFFERING3)); - OmObservation obs = new OmObservation(); - Session session = null; - try { - session = getSession(); - obs.setObservationConstellation(getOmObsConst(PROCEDURE3, OBSPROP3, TEMP_UNIT, OFFERING3, FEATURE3, - OmConstants.OBS_TYPE_PROFILE_OBSERVATION, session)); - } finally { - returnSession(session); - } - obs.setResultTime(new TimeInstant(OBS_TIME)); - SingleObservationValue> obsVal = new SingleObservationValue>(); - ProfileValue profileValue = createProfileValue(true); - obsVal.setValue(profileValue); - obs.setValue(obsVal); - req.setObservation(Lists.newArrayList(obs)); + req.setObservation(Lists.newArrayList(createDefaultObservation(createProfileValue(true)))); InsertObservationResponse resp = insertObservationDAO.insertObservation(req); this.serviceEventBus.submit(new ObservationInsertion(req, resp)); - assertInsertionAftermathBeforeAndAfterCacheReload(); - checkObservation(true); } @Test public void testInsertProfileObservation() throws OwsExceptionReport, InterruptedException, ConverterException { - InsertObservationRequest req = new InsertObservationRequest(); - req.setAssignedSensorId(PROCEDURE3); - req.setOfferings(Lists.newArrayList(OFFERING3)); - OmObservation obs = new OmObservation(); + insertProfileObservationData(); + assertInsertionAftermathBeforeAndAfterCacheReload(); + checkObservation(false); + } + @Test + public void testGeneratedProfileGetResultTemplate() + throws OwsExceptionReport, ConverterException, EncodingException { + insertProfileObservationData(); + GetResultTemplateRequest request = new GetResultTemplateRequest(); + request.setObservedProperty(OBSPROP3); + request.setOffering(OFFERING3); + GetResultTemplateResponse response = getResultTemplateHandler.getResultTemplate(request); + assertThat(response, notNullValue()); + assertThat(response.getResultStructure(), notNullValue()); + assertThat(response.getResultStructure().get().isPresent(), is(true)); + SweAbstractDataComponent sweAbstractDataComponent = response.getResultStructure().get().get(); + assertThat(sweAbstractDataComponent, instanceOf(SweDataRecord.class)); + SweDataRecord record = (SweDataRecord) sweAbstractDataComponent; + assertThat(record.getFields().size(), is(4)); + SweField param = record.getFieldByIdentifier("om:parameter"); + assertThat(param, notNullValue()); + assertThat(param.getElement(), notNullValue()); + assertThat(param.getElement(), instanceOf(SweDataRecord.class)); + SweDataRecord paramRecord = (SweDataRecord) param.getElement(); + assertThat(paramRecord.getDefinition(), is(OmConstants.OM_PARAMETER)); + assertThat(paramRecord.getFields().size(), is(1)); + assertThat(paramRecord.getFields().get(0).getElement().getDefinition(), is(DEPTH)); + SweField field = record.getFieldByIdentifier(OBSPROP3); + assertThat(field, notNullValue()); + assertThat(field.getElement(), notNullValue()); + assertThat(field.getElement(), instanceOf(SweQuantity.class)); + } + + @Test + public void testGeneratedProfileGetResult() throws OwsExceptionReport, ConverterException { + insertProfileObservationData(); + GetResultRequest request = new GetResultRequest(); + request.setObservedProperty(OBSPROP3); + request.setOffering(OFFERING3); + GetResultResponse response = getResultHandler.getResult(request); + assertThat(response, notNullValue()); + String resultValues = response.getResultValues(); + assertThat(resultValues, is( + "1#2013-07-18T03:00:00.000Z,2013-07-18T03:00:00.000Z,5.0000000000,2.5000000000#" + + "2013-07-18T03:00:00.000Z,2013-07-18T03:00:00.000Z,10.0000000000,7.5000000000#" + + "2013-07-18T03:00:00.000Z,2013-07-18T03:00:00.000Z,15.0000000000,12.5000000000#" + + "2013-07-18T03:00:00.000Z,2013-07-18T03:00:00.000Z,20.0000000000,17.5000000000")); + } + + private OmObservation createDefaultObservation(ProfileValue profileValue) + throws OwsExceptionReport, ConverterException { + OmObservation obs = new OmObservation(); Session session = null; try { session = getSession(); @@ -146,18 +235,21 @@ public void testInsertProfileObservation() throws OwsExceptionReport, Interrupte } finally { returnSession(session); } - obs.setResultTime(new TimeInstant(OBS_TIME)); SingleObservationValue> obsVal = new SingleObservationValue>(); obsVal.setPhenomenonTime(new TimeInstant(OBS_TIME)); - ProfileValue profileValue = createProfileValue(false); obsVal.setValue(profileValue); obs.setValue(obsVal); - req.setObservation(Lists.newArrayList(obs)); + return obs; + } + + private void insertProfileObservationData() throws OwsExceptionReport, ConverterException { + InsertObservationRequest req = new InsertObservationRequest(); + req.setAssignedSensorId(PROCEDURE3); + req.setOfferings(Lists.newArrayList(OFFERING3)); + req.setObservation(Lists.newArrayList(createDefaultObservation(createProfileValue(false)))); InsertObservationResponse resp = insertObservationDAO.insertObservation(req); this.serviceEventBus.submit(new ObservationInsertion(req, resp)); - assertInsertionAftermathBeforeAndAfterCacheReload(); - checkObservation(false); } protected void checkObservation(boolean interval) throws OwsExceptionReport { @@ -168,15 +260,19 @@ protected void checkObservation(boolean interval) throws OwsExceptionReport { OmObservation omObservation = getObservation(getObsResponse); assertThat(omObservation.getObservationConstellation(), notNullValue()); OmObservationConstellation obsConst = omObservation.getObservationConstellation(); - assertThat(obsConst.getProcedure().getIdentifier(), is(PROCEDURE3)); - assertThat(obsConst.getObservableProperty().getIdentifier(), is(OBSPROP3)); - assertThat(obsConst.getFeatureOfInterest().getIdentifier(), is(FEATURE3)); + assertThat(obsConst.getProcedure() + .getIdentifier(), is(PROCEDURE3)); + assertThat(obsConst.getObservableProperty() + .getIdentifier(), is(OBSPROP3)); + assertThat(obsConst.getFeatureOfInterest() + .getIdentifier(), is(FEATURE3)); assertThat(omObservation.getValue(), notNullValue()); ObservationValue value = omObservation.getValue(); assertThat(value.getValue(), instanceOf(ProfileValue.class)); assertThat(value.getPhenomenonTime(), instanceOf(TimeInstant.class)); TimeInstant timeInstant = (TimeInstant) value.getPhenomenonTime(); - assertThat(timeInstant.getValue().toDate(), is(OBS_TIME.toDate())); + assertThat(timeInstant.getValue() + .toDate(), is(OBS_TIME.toDate())); assertNotNull(value.getValue()); assertThat(value.getValue() instanceof ProfileValue, is(true)); @@ -193,41 +289,48 @@ private void checkProfileValue(ProfileValue profile, boolean interval) { checkQuantity(profile.getToLevel(), VERTICAL_20); assertNotNull(profile.getValue()); assertTrue(profile.isSetValue()); - assertThat(profile.getValue().size(), is(4)); - checkProfileLevel(profile.getValue().get(0), PROFILE_VAL1, interval, VERTICAL_0, VERTICAL_5); - checkProfileLevel(profile.getValue().get(1), PROFILE_VAL2, interval, VERTICAL_5, VERTICAL_10); - checkProfileLevel(profile.getValue().get(2), PROFILE_VAL3, interval, VERTICAL_10, VERTICAL_15); - checkProfileLevel(profile.getValue().get(3), PROFILE_VAL4, interval, VERTICAL_15, VERTICAL_20); + assertThat(profile.getValue() + .size(), is(4)); + checkProfileLevel(profile.getValue() + .get(0), PROFILE_VAL1, interval, VERTICAL_0, VERTICAL_5); + checkProfileLevel(profile.getValue() + .get(1), PROFILE_VAL2, interval, VERTICAL_5, VERTICAL_10); + checkProfileLevel(profile.getValue() + .get(2), PROFILE_VAL3, interval, VERTICAL_10, VERTICAL_15); + checkProfileLevel(profile.getValue() + .get(3), PROFILE_VAL4, interval, VERTICAL_15, VERTICAL_20); } private void checkProfileLevel(ProfileLevel level, Double value, boolean interval, Double from, Double to) { - if (interval) { - assertNotNull(level.getLevelStart()); - assertTrue(level.isSetLevelStart()); - checkQuantity(level.getLevelStart(), from); - assertNotNull(level.getLevelEnd()); - assertTrue(level.isSetLevelEnd()); - checkQuantity(level.getLevelEnd(), to); - } else { - assertNotNull(level.getLevelEnd()); - assertTrue(level.isSetLevelEnd()); - checkQuantity(level.getLevelEnd(), to); - } - assertTrue(level.isSetValue()); - assertThat(level.getSimpleValue() instanceof QuantityValue, is(true)); - checkQuantityValue((QuantityValue) level.getSimpleValue(), value); + if (interval) { + assertNotNull(level.getLevelStart()); + assertTrue(level.isSetLevelStart()); + checkQuantity(level.getLevelStart(), from); + assertNotNull(level.getLevelEnd()); + assertTrue(level.isSetLevelEnd()); + checkQuantity(level.getLevelEnd(), to); + } else { + assertNotNull(level.getLevelEnd()); + assertTrue(level.isSetLevelEnd()); + checkQuantity(level.getLevelEnd(), to); + } + assertTrue(level.isSetValue()); + assertThat(level.getSimpleValue() instanceof QuantityValue, is(true)); + checkQuantityValue((QuantityValue) level.getSimpleValue(), value); } private void checkQuantityValue(QuantityValue quantity, Double value) { assertNotNull(quantity.getValue()); - assertThat(quantity.getValue().doubleValue(), is(value)); + assertThat(quantity.getValue() + .doubleValue(), is(value)); assertNotNull(quantity.getUom()); assertThat(quantity.getUom(), is(TEMP_UNIT)); } private void checkQuantity(SweQuantity quantity, Double value) { assertNotNull(quantity.getValue()); - assertThat(quantity.getValue().doubleValue(), is(value)); + assertThat(quantity.getValue() + .doubleValue(), is(value)); assertNotNull(quantity.getUom()); assertThat(quantity.getUom(), is(VERTICAL_UNIT)); } @@ -237,11 +340,11 @@ private ProfileValue createProfileValue(boolean interval) { profileValue.setGmlId("pv_1"); UoM uom = new UoM(VERTICAL_UNIT); if (interval) { - profileValue.setFromLevel(createQuantityValue(VERTICAL_0, uom, DEPTH)); - profileValue.setToLevel(createQuantityValue(VERTICAL_20, uom, DEPTH)); - } else { profileValue.setFromLevel(createQuantityValue(VERTICAL_0, uom, FROM)); profileValue.setToLevel(createQuantityValue(VERTICAL_20, uom, TO)); + } else { + profileValue.setFromLevel(createQuantityValue(VERTICAL_0, uom, DEPTH)); + profileValue.setToLevel(createQuantityValue(VERTICAL_20, uom, DEPTH)); } profileValue.setValue(createProfileLevel(interval)); return profileValue; @@ -334,7 +437,6 @@ protected void assertInsertionAftermath(boolean afterCacheUpdate) throws OwsExce // check child procedures assertThat(getCache().getChildProcedures(PROCEDURE3, true, false), empty()); - if (afterCacheUpdate) { // check features of interest for offering assertThat(getCache().getFeaturesOfInterestForOffering(OFFERING3), contains(FEATURE3)); diff --git a/hibernate/handler/src/test/java/org/n52/sos/ds/hibernate/TrajectoryObservationInsertDAOTest.java b/hibernate/handler/src/test/java/org/n52/sos/ds/hibernate/TrajectoryObservationInsertDAOTest.java index 5bf25b9c7f..a9ff05f3bb 100644 --- a/hibernate/handler/src/test/java/org/n52/sos/ds/hibernate/TrajectoryObservationInsertDAOTest.java +++ b/hibernate/handler/src/test/java/org/n52/sos/ds/hibernate/TrajectoryObservationInsertDAOTest.java @@ -48,9 +48,19 @@ import org.n52.shetland.ogc.sensorML.System; import org.n52.shetland.ogc.sensorML.elements.SmlCapabilities; import org.n52.shetland.ogc.sensorML.elements.SmlCapability; +import org.n52.shetland.ogc.sos.request.GetResultRequest; +import org.n52.shetland.ogc.sos.request.GetResultTemplateRequest; import org.n52.shetland.ogc.sos.request.InsertObservationRequest; +import org.n52.shetland.ogc.sos.response.GetResultResponse; +import org.n52.shetland.ogc.swe.SweConstants.SweCoordinateNames; +import org.n52.shetland.ogc.sos.response.GetResultTemplateResponse; import org.n52.shetland.ogc.sos.response.InsertObservationResponse; +import org.n52.shetland.ogc.swe.SweAbstractDataComponent; +import org.n52.shetland.ogc.swe.SweDataRecord; +import org.n52.shetland.ogc.swe.SweField; +import org.n52.shetland.ogc.swe.SweVector; import org.n52.shetland.ogc.swe.simpleType.SweBoolean; +import org.n52.shetland.ogc.swe.simpleType.SweQuantity; import org.n52.sos.ds.hibernate.util.HibernateMetadataCache; import org.n52.sos.event.events.ObservationInsertion; import org.n52.svalbard.encode.exception.EncodingException; @@ -116,9 +126,68 @@ protected void modifySystem(System system) { @Test public void testInsertObservation() throws OwsExceptionReport, InterruptedException, ConverterException { - InsertObservationRequest req = new InsertObservationRequest(); - req.setAssignedSensorId(PROCEDURE3); - req.setOfferings(Lists.newArrayList(OFFERING3)); + inserObservationData(); + assertInsertionAftermathBeforeAndAfterCacheReload(); + checkValue(checkSamplingGeometry(OFFERING3, PROCEDURE3, OBSPROP3, FEATURE3, TRAJ_OBS_TIME_1, TRAJ_GEOMETRY_1), + TRAJ_OBS_TIME_1, TRAJ_OBS_VALUE_1, TEMP_UNIT); + checkValue(checkSamplingGeometry(OFFERING3, PROCEDURE3, OBSPROP3, FEATURE3, TRAJ_OBS_TIME_2, TRAJ_GEOMETRY_2), + TRAJ_OBS_TIME_2, TRAJ_OBS_VALUE_2, TEMP_UNIT); + checkValue(checkSamplingGeometry(OFFERING3, PROCEDURE3, OBSPROP3, FEATURE3, TRAJ_OBS_TIME_3, TRAJ_GEOMETRY_3), + TRAJ_OBS_TIME_3, TRAJ_OBS_VALUE_3, TEMP_UNIT); + checkValue(checkSamplingGeometry(OFFERING3, PROCEDURE3, OBSPROP3, FEATURE3, TRAJ_OBS_TIME_4, TRAJ_GEOMETRY_4), + TRAJ_OBS_TIME_4, TRAJ_OBS_VALUE_4, TEMP_UNIT); + checkValue(checkSamplingGeometry(OFFERING3, PROCEDURE3, OBSPROP3, FEATURE3, TRAJ_OBS_TIME_5, TRAJ_GEOMETRY_5), + TRAJ_OBS_TIME_5, TRAJ_OBS_VALUE_5, TEMP_UNIT); + } + + @Test + public void testGeneratedGetResultTemplate() + throws OwsExceptionReport, ConverterException, EncodingException { + inserObservationData(); + GetResultTemplateRequest request = new GetResultTemplateRequest(); + request.setObservedProperty(OBSPROP3); + request.setOffering(OFFERING3); + GetResultTemplateResponse response = getResultTemplateHandler.getResultTemplate(request); + assertThat(response, notNullValue()); + assertThat(response.getResultStructure(), notNullValue()); + assertThat(response.getResultStructure().get().isPresent(), is(true)); + SweAbstractDataComponent sweAbstractDataComponent = response.getResultStructure().get().get(); + assertThat(sweAbstractDataComponent, instanceOf(SweDataRecord.class)); + SweDataRecord record = (SweDataRecord) sweAbstractDataComponent; + assertThat(record.getFields().size(), is(4)); + SweField samp = record.getFieldByIdentifier("samplingGeometry"); + assertThat(samp, notNullValue()); + assertThat(samp.getElement(), notNullValue()); + assertThat(samp.getElement(), instanceOf(SweVector.class)); + SweVector sampVector = (SweVector) samp.getElement(); + assertThat(sampVector.getDefinition(), is(OmConstants.PARAM_NAME_SAMPLING_GEOMETRY)); + assertThat(sampVector.getCoordinates().size(), is(2)); + assertThat(sampVector.getCoordinates().get(0).getName(), is(SweCoordinateNames.LATITUDE)); + assertThat(sampVector.getCoordinates().get(1).getName(), is(SweCoordinateNames.LONGITUDE)); + SweField field = record.getFieldByIdentifier(OBSPROP3); + assertThat(field, notNullValue()); + assertThat(field.getElement(), notNullValue()); + assertThat(field.getElement(), instanceOf(SweQuantity.class)); + } + + @Test + public void testGeneratedGetResult() throws OwsExceptionReport, ConverterException { + inserObservationData(); + GetResultRequest request = new GetResultRequest(); + request.setObservedProperty(OBSPROP3); + request.setOffering(OFFERING3); + GetResultResponse response = getResultHandler.getResult(request); + assertThat(response, notNullValue()); + String resultValues = response.getResultValues(); + assertThat(resultValues, is( + "5#2013-07-18T03:00:00.000Z,2013-07-18T03:00:00.000Z,1.0000000000,52.7,7.52#" + + "2013-07-18T04:00:00.000Z,2013-07-18T04:00:00.000Z,2.0000000000,52.8,7.53#" + + "2013-07-18T05:00:00.000Z,2013-07-18T05:00:00.000Z,3.0000000000,52.9,7.54#" + + "2013-07-18T06:00:00.000Z,2013-07-18T06:00:00.000Z,4.0000000000,53.0,7.55#" + + "2013-07-18T07:00:00.000Z,2013-07-18T07:00:00.000Z,5.0000000000,53.1,7.56")); + } + + private List createDefaultObservation() throws OwsExceptionReport, ConverterException { OmObservation obs = new OmObservation(); Session session = null; @@ -135,21 +204,16 @@ public void testInsertObservation() throws OwsExceptionReport, InterruptedExcept observations.add(addData(obs.copyTo(new OmObservation()), TRAJ_GEOMETRY_3, TRAJ_OBS_TIME_3, TRAJ_OBS_VALUE_3)); observations.add(addData(obs.copyTo(new OmObservation()), TRAJ_GEOMETRY_4, TRAJ_OBS_TIME_4, TRAJ_OBS_VALUE_4)); observations.add(addData(obs.copyTo(new OmObservation()), TRAJ_GEOMETRY_5, TRAJ_OBS_TIME_5, TRAJ_OBS_VALUE_5)); + return observations; + } - req.setObservation(observations); + private void inserObservationData() throws OwsExceptionReport, ConverterException { + InsertObservationRequest req = new InsertObservationRequest(); + req.setAssignedSensorId(PROCEDURE3); + req.setOfferings(Lists.newArrayList(OFFERING3)); + req.setObservation(createDefaultObservation()); InsertObservationResponse resp = insertObservationDAO.insertObservation(req); this.serviceEventBus.submit(new ObservationInsertion(req, resp)); - assertInsertionAftermathBeforeAndAfterCacheReload(); - checkValue(checkSamplingGeometry(OFFERING3, PROCEDURE3, OBSPROP3, FEATURE3, TRAJ_OBS_TIME_1, TRAJ_GEOMETRY_1), - TRAJ_OBS_TIME_1, TRAJ_OBS_VALUE_1, TEMP_UNIT); - checkValue(checkSamplingGeometry(OFFERING3, PROCEDURE3, OBSPROP3, FEATURE3, TRAJ_OBS_TIME_2, TRAJ_GEOMETRY_2), - TRAJ_OBS_TIME_2, TRAJ_OBS_VALUE_2, TEMP_UNIT); - checkValue(checkSamplingGeometry(OFFERING3, PROCEDURE3, OBSPROP3, FEATURE3, TRAJ_OBS_TIME_3, TRAJ_GEOMETRY_3), - TRAJ_OBS_TIME_3, TRAJ_OBS_VALUE_3, TEMP_UNIT); - checkValue(checkSamplingGeometry(OFFERING3, PROCEDURE3, OBSPROP3, FEATURE3, TRAJ_OBS_TIME_4, TRAJ_GEOMETRY_4), - TRAJ_OBS_TIME_4, TRAJ_OBS_VALUE_4, TEMP_UNIT); - checkValue(checkSamplingGeometry(OFFERING3, PROCEDURE3, OBSPROP3, FEATURE3, TRAJ_OBS_TIME_5, TRAJ_GEOMETRY_5), - TRAJ_OBS_TIME_5, TRAJ_OBS_VALUE_5, TEMP_UNIT); } private OmObservation addData(OmObservation obs, Geometry geometry, DateTime time, Double value) { diff --git a/pom.xml b/pom.xml index 5060a4c79e..dafa5e38d9 100644 --- a/pom.xml +++ b/pom.xml @@ -91,7 +91,7 @@ 8.0.20 1.4.200 0.9 - 5.4.15.Final + 5.4.16.Final 0.9.5.5 3.4.5 23.0 @@ -100,7 +100,7 @@ 1.7.30 5.3.2 - 7.4.1 + 7.5.0-SNAPSHOT 2.7.0 3.1.0 3.1.0 @@ -1610,6 +1610,18 @@ javax.media jai_core + + org.eclipse.emf + org.eclipse.emf.ecore + + + org.eclipse.emf + org.eclipse.emf.ecore.xmi + + + org.eclipse.emf + org.eclipse.emf.common + @@ -1625,6 +1637,18 @@ org.apache.commons commons-lang3 + + org.eclipse.emf + org.eclipse.emf.ecore + + + org.eclipse.emf + org.eclipse.emf.ecore.xmi + + + org.eclipse.emf + org.eclipse.emf.common + @@ -1640,6 +1664,18 @@ javax.media jai_core + + org.eclipse.emf + org.eclipse.emf.ecore + + + org.eclipse.emf + org.eclipse.emf.ecore.xmi + + + org.eclipse.emf + org.eclipse.emf.common + @@ -1655,6 +1691,18 @@ org.apache.commons commons-lang3 + + org.eclipse.emf + org.eclipse.emf.ecore + + + org.eclipse.emf + org.eclipse.emf.ecore.xmi + + + org.eclipse.emf + org.eclipse.emf.common + @@ -1666,6 +1714,18 @@ javax.media jai_core + + org.eclipse.emf + org.eclipse.emf.ecore + + + org.eclipse.emf + org.eclipse.emf.ecore.xmi + + + org.eclipse.emf + org.eclipse.emf.common +