    001    //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/branches/2.4_testing/src/org/deegree/portal/standard/sos/control/GetObservationListener.java $
    002    /*----------------------------------------------------------------------------
    003     This file is part of deegree, http://deegree.org/
    004     Copyright (C) 2001-2009 by:
    005       Department of Geography, University of Bonn
    006     and
    007       lat/lon GmbH
    008
    009     This library is free software; you can redistribute it and/or modify it under
    010     the terms of the GNU Lesser General Public License as published by the Free
    011     Software Foundation; either version 2.1 of the License, or (at your option)
    012     any later version.
    013     This library is distributed in the hope that it will be useful, but WITHOUT
    014     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    015     FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
    016     details.
    017     You should have received a copy of the GNU Lesser General Public License
    018     along with this library; if not, write to the Free Software Foundation, Inc.,
    019     59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
    020
    021     Contact information:
    022
    023     lat/lon GmbH
    024     Aennchenstr. 19, 53177 Bonn
    025     Germany
    026     http://lat-lon.de/
    027
    028     Department of Geography, University of Bonn
    029     Prof. Dr. Klaus Greve
    030     Postfach 1147, 53001 Bonn
    031     Germany
    032     http://www.geographie.uni-bonn.de/deegree/
    033
    034     e-mail: info@deegree.org
    035    ----------------------------------------------------------------------------*/
    036    package org.deegree.portal.standard.sos.control;
    037
    038    import java.util.ArrayList;
    039    import java.util.HashMap;
    040    import java.util.Iterator;
    041    import java.util.List;
    042
    043    import org.deegree.enterprise.control.RPCMethodCall;
    044    import org.deegree.enterprise.control.RPCParameter;
    045    import org.deegree.enterprise.control.RPCStruct;
    046    import org.deegree.framework.log.ILogger;
    047    import org.deegree.framework.log.LoggerFactory;
    048    import org.deegree.framework.xml.NamespaceContext;
    049    import org.deegree.framework.xml.XMLTools;
    050    import org.deegree.ogcbase.CommonNamespaces;
    051    import org.deegree.ogcwebservices.sos.om.Observation;
    052    import org.deegree.ogcwebservices.sos.om.ObservationArray;
    053    import org.deegree.portal.standard.sos.Constants;
    054    import org.deegree.portal.standard.sos.SOSClientException;
    055    import org.w3c.dom.Document;
    056    import org.w3c.dom.Element;
    057    import org.w3c.dom.Node;
    058
    059    /**
    060     * ...
    061     *
    062     * @author <a href="mailto:taddei@lat-lon.de">Ugo Taddei</a>
    063     *
    064     */
    065    public class GetObservationListener extends AbstractSOSListener {
    066
    067        private static final NamespaceContext nsContext = CommonNamespaces.getNamespaceContext();
    068
    069        private static final ILogger LOG = LoggerFactory.getLogger( GetObservationListener.class );
    070
    071        /*
    072         * (non-Javadoc)
    073         *
    074         * @see org.deegree.portal.sos.control.AbstractSOSListener#validateRequest(org.deegree.enterprise.control.RPCMethodCall)
    075         */
    076        @Override
    077        protected void validateRequest( RPCMethodCall mc )
    078                                throws SOSClientException {
    079            // TODO something
    080            // Possible parameters:
    081            // BoundingBox (mandatory)
    082            // time (optional)
    083            // platformID (optional)
    084            // sensorID (optional)
    085            // sos:Query
    086
    087        }
    088
    089        /*
    090         * (non-Javadoc)
    091         *
    092         * @see org.deegree.portal.sos.control.AbstractSOSListener#createRequest(org.deegree.enterprise.control.RPCMethodCall)
    093         */
    094        @Override
    095        protected String createRequest( RPCMethodCall mc )
    096                                throws SOSClientException {
    097            RPCParameter[] params = mc.getParameters();
    098            RPCStruct struct = (RPCStruct) params[0].getValue();
    099
    100            StringBuffer sb = new StringBuffer( 1000 );
    101            sb.append( "<sos:GetObservation " );
    102            sb.append( "xmlns:sos='http://www.opengis.net/sos' " );
    103            sb.append( "xmlns:gml='http://www.opengis.net/gml' xmlns:ogc='http://www.opengis.net/ogc' " );
    104            sb.append( "outputFormat='SWEObservation' " );
    105            sb.append( "service='SOS' " );
    106            sb.append( "version='0.8.0'>" );
    107            sb.append( "<sos:platformID>" );
    108            sb.append( struct.getMember( "platformID" ).getValue() );
    109            sb.append( "</sos:platformID>" );
    110            sb.append( "<sos:sensorID>" );
    111            sb.append( struct.getMember( "sensorID" ).getValue() );
    112            sb.append( "</sos:sensorID>" );
    113
    114            // TODO create bbox from request
    115            // attention, there is code in deegree to extract bbox from RPC
    116            sb.append( "<sos:BoundingBox srsName='EPSG:4326'>" );
    117            sb.append( "<gml:coord>" );
    118            sb.append( "<gml:X>0</gml:X>" );
    119            sb.append( "<gml:Y>0</gml:Y>" );
    120            sb.append( "</gml:coord>" );
    121            sb.append( "<gml:coord>" );
    122            sb.append( "<gml:X>50000000</gml:X>" );
    123            sb.append( "<gml:Y>50000000</gml:Y>" );
    124            sb.append( "</gml:coord>" );
    125            sb.append( "</sos:BoundingBox>" );
    126            sb.append( "</sos:GetObservation >" );
    127
    128            return sb.toString();
    129        }
    130
    131        /*
    132         * (non-Javadoc)
    133         *
    134         * @see org.deegree.portal.sos.control.AbstractSOSListener#createData(org.deegree.enterprise.control.RPCMethodCall,
    135         *      java.util.HashMap)
    136         */
    137        @Override
    138        protected Object createData( RPCMethodCall mc, HashMap<String,Document> map )
    139                                throws SOSClientException {
    140
    141            ObservationArray obsArray = null;
    142
    143            List<Observation> obsList = new ArrayList<Observation>( 100 );
    144
    145            try {
    146                Iterator<String> iterator = map.keySet().iterator();
    147                while ( iterator.hasNext() ) {
    148                    String key = iterator.next();
    149                    Element e = map.get( key ).getDocumentElement();
    150
    151                    List<Node> obsNodes = XMLTools.getNodes( e,
    152                                                       "/om:ObservationCollection/om:observationMembers/om:ObservationArray"
    153                                                                               + "/om:observationMembers/gml:Observation",
    154                                                       nsContext );
    155
    156                    for ( int i = 0; i < obsNodes.size(); i++ ) {
    157
    158                        String timeStamp = XMLTools.getNodeAsString( obsNodes.get( i ),
    159                                                                     "gml:timeStamp/gml:TimeInstant/gml:timePosition",
    160                                                                     nsContext, null );
    161
    162                        String value = XMLTools.getNodeAsString( obsNodes.get( i ), "gml:resultOf/gml:QuantityList",
    163                                                                 nsContext, null );
    164
    165                        obsList.add( new Observation( timeStamp, value ) );
    166
    167                    }
    168
    169                    Observation[] observations = obsList.toArray( new Observation[obsList.size()] );
    170
    171                    obsArray = new ObservationArray( observations, null, null );
    172
    173                }
    174            } catch ( Exception e ) {
    175                LOG.logError( "Error creating observation array: " + e.getMessage() );
    176                throw new SOSClientException( "Couldn't create sensor descriptions", e );
    177            }
    178
    179            return obsArray;
    180        }
    181
    182        /*
    183         * (non-Javadoc)
    184         *
    185         * @see org.deegree.portal.sos.control.AbstractSOSListener#setNextPageData(java.lang.Object)
    186         */
    187        @Override
    188        protected void setNextPageData( Object data ) {
    189            this.getRequest().setAttribute( Constants.OBSERVATION_DATA, data );
    190        }
    191
    192    }