001    //$HeadURL: svn+ssh://mschneider@svn.wald.intevation.org/deegree/base/trunk/src/org/deegree/ogcwebservices/wfs/operation/GetFeature.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.ogcwebservices.wfs.operation;
037    
038    import java.util.ArrayList;
039    import java.util.HashMap;
040    import java.util.Map;
041    
042    import org.deegree.datatypes.QualifiedName;
043    import org.deegree.framework.log.ILogger;
044    import org.deegree.framework.log.LoggerFactory;
045    import org.deegree.i18n.Messages;
046    import org.deegree.io.datastore.schema.MappedFeatureType;
047    import org.deegree.model.filterencoding.FeatureFilter;
048    import org.deegree.model.filterencoding.FeatureId;
049    import org.deegree.model.filterencoding.Filter;
050    import org.deegree.ogcbase.PropertyPath;
051    import org.deegree.ogcbase.PropertyPathFactory;
052    import org.deegree.ogcwebservices.InconsistentRequestException;
053    import org.deegree.ogcwebservices.InvalidParameterValueException;
054    import org.deegree.ogcwebservices.wfs.XMLFactory;
055    import org.deegree.ogcwebservices.wfs.configuration.WFSConfiguration;
056    
057    /**
058     * "Proxy" GetFeature request object, only created by {@link GetFeature#create(Map)} if the FEATUREID parameter is used
059     * without TYPENAME parameter.
060     * <p>
061     * It is augmented with information from the {@link WFSConfiguration} to retrieve the names of the associated feature
062     * types.
063     *
064     * @author <a href="mailto:schneider@lat-lon.de">Markus Schneider</a>
065     * @author last edited by: $Author: apoth $
066     *
067     * @version $Revision: 9345 $, $Date: 2007-12-27 16:22:25 +0000 (Do, 27 Dez 2007) $
068     */
069    public class AugmentableGetFeature extends GetFeature {
070    
071        private static final long serialVersionUID = -3001702206522611997L;
072    
073        private static final ILogger LOG = LoggerFactory.getLogger( AugmentableGetFeature.class );
074    
075        private Map<String, String> kvp;
076    
077        /**
078         * Creates a new <code>AugmentableGetFeature</code> instance.
079         *
080         * @param version
081         *            request version
082         * @param id
083         *            id of the request
084         * @param handle
085         * @param resultType
086         *            desired result type (results | hits)
087         * @param outputFormat
088         *            requested result format
089         * @param maxFeatures
090         * @param startPosition
091         *            deegree specific parameter defining where to start considering features
092         * @param traverseXLinkDepth
093         * @param traverseXLinkExpiry
094         * @param queries
095         * @param vendorSpecificParam
096         */
097        AugmentableGetFeature( String version, String id, String handle, RESULT_TYPE resultType, String outputFormat,
098                               int maxFeatures, int startPosition, int traverseXLinkDepth, int traverseXLinkExpiry,
099                               Query[] queries, Map<String, String> vendorSpecificParam ) {
100            super( version, id, handle, resultType, outputFormat, maxFeatures, startPosition, traverseXLinkDepth,
101                   traverseXLinkExpiry, queries, vendorSpecificParam );
102            this.kvp = vendorSpecificParam;
103        }
104    
105        /**
106         * Augments the KVP request with the needed information from the {@link WFSConfiguration}.
107         *
108         * @param config
109         * @throws InconsistentRequestException
110         * @throws InvalidParameterValueException
111         */
112        public void augment( WFSConfiguration config )
113                                throws InconsistentRequestException, InvalidParameterValueException {
114    
115            // SRSNAME
116            String srsName = kvp.get( "SRSNAME" );
117    
118            // FEATUREVERSION
119            String featureVersion = kvp.get( "FEATUREVERSION" );
120    
121            // TYPENAME
122            QualifiedName[] typeNames = extractTypeNames( kvp );
123            if ( typeNames.length == 0 ) {
124                // FEATUREID must be given
125                String featureId = kvp.get( "FEATUREID" );
126                if ( featureId == null ) {
127                    String msg = Messages.getMessage( "WFS_TYPENAME+FID_PARAMS_MISSING" );
128                    throw new InvalidParameterValueException( msg );
129                }
130    
131                // ensure that the WFS has unique feature prefixes
132                if ( !config.hasUniquePrefixMapping() ) {
133                    String msg = Messages.get( "WFS_CONF_FT_PREFICES_NOT_UNIQUE" );
134                    throw new InconsistentRequestException( msg );
135                }
136    
137                // maps feature type to ids that must be fetched for it
138                Map<MappedFeatureType, ArrayList<FeatureId>> ftToFids = new HashMap<MappedFeatureType, ArrayList<FeatureId>>();
139                String[] featureIds = featureId.split( "," );
140                for ( String fid : featureIds ) {
141                    MappedFeatureType ft = config.getFeatureType( fid );
142                    if ( ft == null ) {
143                        String msg = Messages.get( "WFS_CONF_FT_PREFIX_UNKNOWN", fid );
144                        throw new InconsistentRequestException( msg );
145                    }
146                    ArrayList<FeatureId> fidList = ftToFids.get( ft );
147                    if ( fidList == null ) {
148                        fidList = new ArrayList<FeatureId>();
149                    }
150                    fidList.add( new FeatureId( fid ) );
151                    ftToFids.put( ft, fidList );
152                }
153    
154                // build a Query instance for each requested feature type
155                queries = new ArrayList<Query>( ftToFids.size() );
156                for ( MappedFeatureType ft : ftToFids.keySet() ) {
157                    QualifiedName ftName = ft.getName();
158                    ArrayList<FeatureId> fids = ftToFids.get( ft );
159                    Filter filter = new FeatureFilter( fids );
160                    QualifiedName[] ftNames = new QualifiedName[] { ftName };
161                    PropertyPath path = PropertyPathFactory.createPropertyPath( ftName );
162                    PropertyPath[] properties = new PropertyPath[] { path };
163                    queries.add( new Query( properties, null, null, null, featureVersion, ftNames, null, srsName, filter,
164                                            resultType, maxFeatures, startPosition ) );
165                }
166            }
167    
168            if ( LOG.isDebug() ) {
169                try {
170                    GetFeatureDocument doc = XMLFactory.export( this );
171                    doc.prettyPrint( System.out );
172                } catch ( Exception e ) {
173                    e.printStackTrace();
174                }
175            }
176        }
177    }