001    //$HeadURL: svn+ssh://jwilden@svn.wald.intevation.org/deegree/base/branches/2.5_testing/src/org/deegree/portal/standard/gazetteer/LoadBBOXCommand.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.gazetteer;
037    
038    import java.util.ArrayList;
039    import java.util.List;
040    import java.util.Map;
041    import java.util.UUID;
042    
043    import org.deegree.datatypes.QualifiedName;
044    import org.deegree.framework.util.Pair;
045    import org.deegree.model.feature.Feature;
046    import org.deegree.model.feature.FeatureCollection;
047    import org.deegree.model.filterencoding.ComplexFilter;
048    import org.deegree.model.filterencoding.Literal;
049    import org.deegree.model.filterencoding.Operation;
050    import org.deegree.model.filterencoding.PropertyIsLikeOperation;
051    import org.deegree.model.filterencoding.PropertyName;
052    import org.deegree.model.spatialschema.Geometry;
053    import org.deegree.ogcbase.ElementStep;
054    import org.deegree.ogcbase.PropertyPath;
055    import org.deegree.ogcbase.PropertyPathStep;
056    import org.deegree.ogcwebservices.wfs.capabilities.WFSCapabilities;
057    import org.deegree.ogcwebservices.wfs.operation.GetFeature;
058    import org.deegree.ogcwebservices.wfs.operation.Query;
059    import org.deegree.ogcwebservices.wfs.operation.GetFeature.RESULT_TYPE;
060    
061    /**
062     * TODO add class documentation here
063     * 
064     * @author <a href="mailto:name@deegree.org">Andreas Poth</a>
065     * @author last edited by: $Author: apoth $
066     * 
067     * @version $Revision: 25857 $, $Date: 2010-08-12 08:29:07 +0200 (Do, 12 Aug 2010) $
068     */
069    public class LoadBBOXCommand extends AbstractGazetteerCommand {
070    
071        private String geogrId;
072    
073        /**
074         * 
075         * @param gazetteerAddress
076         * @param featureType
077         * @param properties
078         * @param geogrId
079         */
080        LoadBBOXCommand( String gazetteerAddress, QualifiedName featureType, Map<String, String> properties, String geogrId ) {
081            this.gazetteerAddress = gazetteerAddress;
082            this.featureType = featureType;
083            this.geogrId = geogrId;
084            this.properties = properties;
085        }
086    
087        /**
088         * 
089         * @return first contains highlight geometry; second geographic extent
090         * @throws Exception
091         */
092        Pair<Geometry, Geometry> execute()
093                                throws Exception {
094    
095            if ( !capabilitiesMap.containsKey( gazetteerAddress ) ) {
096                loadCapabilities();
097            }
098            WFSCapabilities capabilities = capabilitiesMap.get( gazetteerAddress );
099    
100            // create query filter
101            String tmp = properties.get( "GeographicIdentifier" );
102            PropertyName propertyName;
103            if ( tmp.startsWith( "{" ) ) {
104                propertyName = new PropertyName( new QualifiedName( tmp ) );
105            } else {
106                propertyName = new PropertyName( new QualifiedName( tmp, featureType.getNamespace() ) );
107            }
108            Literal literal = new Literal( geogrId );
109            Operation operation = new PropertyIsLikeOperation( propertyName, literal, '*', '?', '/' );
110            ComplexFilter filter = new ComplexFilter( operation );
111    
112            // select just properties needed to fill result list
113            PropertyPath[] propertyNames = getResultProperties( properties );
114    
115            // create Query and GetFeature request
116            Query query = Query.create( propertyNames, null, null, null, null, new QualifiedName[] { featureType }, null,
117                                        null, filter, 500, 0, RESULT_TYPE.RESULTS );
118            GetFeature getFeature = GetFeature.create( capabilities.getVersion(), UUID.randomUUID().toString(),
119                                                       RESULT_TYPE.RESULTS, GetFeature.FORMAT_GML3, null, 500, 0, -1, -1,
120                                                       new Query[] { query } );
121    
122            // perform GetFeature request and create resulting GazetteerItems list
123            FeatureCollection fc = performGetFeature( capabilities, getFeature );
124            if ( fc.size() > 0 ) {
125                Feature feat = fc.getFeature( 0 );
126                // if no highlight geometry has been configured property name for geographic extent will be returned
127                tmp = properties.get( "HighlightGeometry" );
128                QualifiedName pn = null;
129                if ( tmp.startsWith( "{" ) ) {
130                    pn = new QualifiedName( tmp );
131                } else {
132                    pn = new QualifiedName( tmp, featureType.getNamespace() );
133                }
134    
135                Geometry hlGeom = (Geometry) feat.getProperties( pn )[0].getValue();
136    
137                // GeographicExtent is mandatory
138                tmp = properties.get( "GeographicExtent" );
139                if ( tmp.startsWith( "{" ) ) {
140                    pn = new QualifiedName( tmp );
141                } else {
142                    pn = new QualifiedName( tmp, featureType.getNamespace() );
143                }
144    
145                Geometry geGeom = (Geometry) feat.getProperties( pn )[0].getValue();
146                return new Pair<Geometry, Geometry>( hlGeom, geGeom );
147            } else {
148                throw new Exception( "no feature with geographic identifier: " + geogrId + " found" );
149            }
150        }
151    
152        /**
153         * @param properties
154         * @return
155         */
156        protected PropertyPath[] getResultProperties( Map<String, String> properties ) {
157            List<PropertyPath> pathes = new ArrayList<PropertyPath>();
158    
159            String tmp = properties.get( "GeographicExtent" );
160            QualifiedName sortQn = null;
161            if ( tmp.startsWith( "{" ) ) {
162                sortQn = new QualifiedName( tmp );
163            } else {
164                sortQn = new QualifiedName( tmp, featureType.getNamespace() );
165            }
166            List<PropertyPathStep> steps = new ArrayList<PropertyPathStep>();
167            steps.add( new ElementStep( sortQn ) );
168            pathes.add( new PropertyPath( steps ) );
169    
170            tmp = properties.get( "HighlightGeometry" );
171            if ( tmp != null && !tmp.equals( properties.get( "GeographicExtent" ) ) ) {
172                if ( tmp.startsWith( "{" ) ) {
173                    sortQn = new QualifiedName( tmp );
174                } else {
175                    sortQn = new QualifiedName( tmp, featureType.getNamespace() );
176                }
177                steps = new ArrayList<PropertyPathStep>();
178                steps.add( new ElementStep( sortQn ) );
179                pathes.add( new PropertyPath( steps ) );
180            }
181    
182            return pathes.toArray( new PropertyPath[pathes.size()] );
183        }
184    }