001    //$HeadURL: svn+ssh://jwilden@svn.wald.intevation.org/deegree/base/branches/2.5_testing/src/org/deegree/ogcwebservices/wfs/GetGmlObjectHandler.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    
037    package org.deegree.ogcwebservices.wfs;
038    
039    import static java.lang.Integer.parseInt;
040    import static java.util.Collections.singleton;
041    import static org.deegree.framework.log.LoggerFactory.getLogger;
042    import static org.deegree.i18n.Messages.get;
043    
044    import java.util.ArrayList;
045    import java.util.Set;
046    
047    import org.deegree.datatypes.QualifiedName;
048    import org.deegree.framework.log.ILogger;
049    import org.deegree.i18n.Messages;
050    import org.deegree.io.datastore.Datastore;
051    import org.deegree.io.datastore.DatastoreException;
052    import org.deegree.io.datastore.schema.MappedFeatureType;
053    import org.deegree.model.crs.UnknownCRSException;
054    import org.deegree.model.feature.Feature;
055    import org.deegree.model.feature.FeatureCollection;
056    import org.deegree.model.filterencoding.FeatureFilter;
057    import org.deegree.model.filterencoding.FeatureId;
058    import org.deegree.model.filterencoding.Filter;
059    import org.deegree.ogcwebservices.InconsistentRequestException;
060    import org.deegree.ogcwebservices.InvalidParameterValueException;
061    import org.deegree.ogcwebservices.OGCWebServiceException;
062    import org.deegree.ogcwebservices.wfs.capabilities.FormatType;
063    import org.deegree.ogcwebservices.wfs.configuration.WFSConfiguration;
064    import org.deegree.ogcwebservices.wfs.operation.GetGmlObject;
065    import org.deegree.ogcwebservices.wfs.operation.GmlResult;
066    import org.deegree.ogcwebservices.wfs.operation.Query;
067    
068    /**
069     * <code>GetGmlObjectHandler</code>
070     *
071     * @author <a href="mailto:schmitz@lat-lon.de">Andreas Schmitz</a>
072     * @author last edited by: $Author: mschneider $
073     *
074     * @version $Revision: 18195 $, $Date: 2009-06-18 17:55:39 +0200 (Do, 18 Jun 2009) $
075     */
076    class GetGmlObjectHandler {
077    
078        private static final ILogger LOG = getLogger( GetGmlObjectHandler.class );
079    
080        private WFSConfiguration config;
081    
082        GetGmlObjectHandler( WFSConfiguration config ) {
083            this.config = config;
084        }
085    
086        GmlResult handleRequest( GetGmlObject request ) {
087            if ( !config.hasUniquePrefixMapping() ) {
088                return new GmlResult( request, new InconsistentRequestException( get( "WFS_CONF_FT_PREFICES_NOT_UNIQUE2" ) ) );
089            }
090            String objectId = request.getObjectId();
091            MappedFeatureType type = config.getFeatureType( objectId );
092    
093            if ( type == null ) {
094                LOG.logDebug( "The GML type could not be determined for the incoming GMLObjectID" );
095                return new GmlResult( request, new InvalidParameterValueException( "getgmlobject",
096                                                                                   get( "WFS_NO_SUCH_FEATURE", objectId ) ) );
097            }
098    
099            if ( type.isAbstract() ) {
100                String msg = Messages.getMessage( "WFS_FEATURE_TYPE_ABSTRACT", type.getName() );
101                return new GmlResult( request, new OGCWebServiceException( "getgmlobject", msg ) );
102            }
103            if ( !type.isVisible() ) {
104                String msg = Messages.getMessage( "WFS_FEATURE_TYPE_INVISIBLE", type.getName() );
105                return new GmlResult( request, new OGCWebServiceException( "getgmlobject", msg ) );
106            }
107    
108            int idx = objectId.indexOf( "_GEOM_" );
109            int geom = -1;
110            if ( idx > 0 ) {
111                try {
112                    geom = parseInt( objectId.substring( idx + 6 ) );
113                } catch ( NumberFormatException e ) {
114                    LOG.logDebug( "Stack trace: ", e );
115                    return new GmlResult( request, new InvalidParameterValueException( "getgmlobject",
116                                                                                       get( "WFS_NO_SUCH_FEATURE",
117                                                                                            request.getObjectId() ) ) );
118                }
119                objectId = objectId.substring( 0, idx );
120                LOG.logDebug( "Trying to find geometry object number " + geom );
121            }
122    
123            LOG.logDebug( "Feature ID: ", objectId );
124    
125            Set<FeatureId> set = singleton( new FeatureId( objectId ) );
126            Filter filter = new FeatureFilter( new ArrayList<FeatureId>( set ) );
127    
128            Datastore ds = type.getGMLSchema().getDatastore();
129    
130            Query q = new Query( null, null, null, null, null, new QualifiedName[] { type.getName() }, null, null, filter,
131                                 null, -1, -1 );
132            try {
133                FeatureCollection col = ds.performQuery( q, new MappedFeatureType[] { type } );
134                if ( col.size() == 0 ) {
135                    return new GmlResult( request, new InvalidParameterValueException( "getgmlobject",
136                                                                                       get( "WFS_NO_SUCH_FEATURE",
137                                                                                            request.getObjectId() ) ) );
138                }
139                Feature feature = col.getFeature( 0 );
140    
141                // different formats are not supported anyway, so just use the first one
142                FormatType format = config.getFeatureTypeList().getFeatureType( type.getName() ).getOutputFormats()[0];
143    
144                if ( geom > -1 ) {
145                    try {
146                        return new GmlResult( request, feature.getGeometryPropertyValues()[geom], format );
147                    } catch ( ArrayIndexOutOfBoundsException e ) {
148                        return new GmlResult( request, new InvalidParameterValueException( "getgmlobject",
149                                                                                           get( "WFS_NO_SUCH_FEATURE",
150                                                                                                request.getObjectId() ) ) );
151                    }
152                }
153    
154                return new GmlResult( request, feature, format );
155            } catch ( DatastoreException e ) {
156                LOG.logDebug( "Stack trace: ", e );
157                return new GmlResult( request, new OGCWebServiceException( "getgmlobject", e.getMessage() ) );
158            } catch ( UnknownCRSException e ) {
159                LOG.logDebug( "Stack trace: ", e );
160                return new GmlResult( request, new OGCWebServiceException( "getgmlobject", e.getMessage() ) );
161            }
162        }
163    
164    }