001    //$$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/branches/2.3_testing/src/org/deegree/ogcwebservices/wpvs/configuration/LocalWFSDataSource.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.wpvs.configuration;
038    
039    import java.io.IOException;
040    import java.net.URL;
041    import java.util.Map;
042    import java.util.concurrent.ConcurrentHashMap;
043    
044    import org.deegree.datatypes.QualifiedName;
045    import org.deegree.framework.log.ILogger;
046    import org.deegree.framework.log.LoggerFactory;
047    import org.deegree.framework.xml.InvalidConfigurationException;
048    import org.deegree.i18n.Messages;
049    import org.deegree.model.filterencoding.Filter;
050    import org.deegree.model.spatialschema.Surface;
051    import org.deegree.ogcbase.PropertyPath;
052    import org.deegree.ogcwebservices.OGCWebService;
053    import org.deegree.ogcwebservices.OGCWebServiceException;
054    import org.deegree.ogcwebservices.wfs.WFServiceFactory;
055    import org.deegree.ogcwebservices.wfs.configuration.WFSConfiguration;
056    import org.deegree.ogcwebservices.wfs.configuration.WFSConfigurationDocument;
057    import org.deegree.ogcwebservices.wpvs.capabilities.OWSCapabilities;
058    import org.deegree.owscommon.OWSDomainType;
059    import org.xml.sax.SAXException;
060    
061    /**
062     * This class represents a local WFS dataSource object.
063     *
064     * @author <a href="mailto:taddei@lat-lon.de">Ugo Taddei</a>
065     * @author last edited by: $Author: mschneider $
066     *
067     * $Revision: 18195 $, $Date: 2009-06-18 17:55:39 +0200 (Do, 18. Jun 2009) $
068     */
069    public class LocalWFSDataSource extends AbstractDataSource {
070    
071        private static final ILogger LOG = LoggerFactory.getLogger( LocalWFSDataSource.class );
072    
073        private final PropertyPath geometryProperty;
074    
075        private int maxFeatures;
076    
077        private static Map<URL, WFSConfiguration> cache = new ConcurrentHashMap<URL, WFSConfiguration>();
078    
079        /**
080         * Creates a new <code>LocalWFSDataSource</code> object from the given parameters.
081         *
082         * @param name
083         * @param owsCapabilities
084         * @param validArea
085         * @param minScaleDenominator
086         * @param maxScaleDenominator
087         * @param geomProperty
088         * @param filterCondition
089         *            a wfs query //TODO give an example //*
090         * @param maxFeatures to query this datasource for.
091         */
092        public LocalWFSDataSource( QualifiedName name, OWSCapabilities owsCapabilities,
093                                   Surface validArea, double minScaleDenominator,
094                                   double maxScaleDenominator, PropertyPath geomProperty,
095                                   Filter filterCondition/* , FeatureCollectionAdapter adapter */, int maxFeatures) {
096    
097            super( LOCAL_WFS, name, owsCapabilities, validArea, minScaleDenominator,
098                   maxScaleDenominator, filterCondition );
099            this.geometryProperty = geomProperty;
100            this.maxFeatures = maxFeatures;
101            // this.fcAdapter = adapter;
102        }
103    
104        @Override
105        public String toString() {
106            return super.toString() +"\n\t" + geometryProperty.getAsString();
107        }
108    
109        /**
110         * @return the Filter of the filterCondition.
111         */
112        public Filter getFilter() {
113            return (Filter) getFilterCondition();
114        }
115    
116        /**
117         * @return the geometryProperty.
118         */
119        public PropertyPath getGeometryProperty() {
120            return geometryProperty;
121        }
122    
123        @Override
124        public OGCWebService getOGCWebService()
125                                throws OGCWebServiceException {
126            WFSConfiguration wfsConfig = null;
127            synchronized ( this ) {
128    
129                URL url = getOwsCapabilities().getOnlineResource();
130                wfsConfig = cache.get( url );
131                if ( !cache.containsKey( url ) || wfsConfig == null ) {
132                    WFSConfigurationDocument wfsDoc = new WFSConfigurationDocument();
133                    try {
134                        wfsDoc.load( getOwsCapabilities().getOnlineResource() );
135                        wfsConfig = wfsDoc.getConfiguration();
136                        cache.put( url, wfsConfig );
137                    } catch ( IOException e ) {
138                        throw new OGCWebServiceException(
139                                                          Messages.getMessage(
140                                                                               "WPVS_DATASOURCE_CAP_ERROR",
141                                                                               toString() )
142                                                                                  + e.getMessage() );
143                    } catch ( SAXException e ) {
144                        throw new OGCWebServiceException(
145                                                          Messages.getMessage(
146                                                                               "WPVS_DATASOURCE_CAP_ERROR",
147                                                                               toString() )
148                                                                                  + e.getMessage() );
149                    } catch ( InvalidConfigurationException e ) {
150                        throw new OGCWebServiceException(
151                                                          Messages.getMessage(
152                                                                               "WPVS_DATASOURCE_CAP_ERROR",
153                                                                               toString() )
154                                                                                  + e.getMessage() );
155                    }
156    
157                }
158            }
159            return WFServiceFactory.createInstance( wfsConfig );
160        }
161    
162        // /**
163        // * @return the configured FeatureCollectionAdapter.
164        // */
165        // public FeatureCollectionAdapter getFeatureCollectionAdapter() {
166        // return fcAdapter;
167        // }
168    
169        /**
170         * ---DO NOT REMOVE --- NOT FUNCTIONAL YET, BUT might be if the WFS uses the new OWSCommon
171         * Package.
172         *
173         * Retrieves (if it exists) the first value of the requestedParameterName of the Operation
174         * defined by it's name. For example one wants to get GetFeature#outputFormat
175         *
176         * @param operationName
177         *            the name of the configured Operation
178         * @param requestedParameterName
179         *            the name of the Parameter.
180         * @return <code>null</code> - in the future: the Value of the (first) parameter if it exists else
181         *         <code>null</code>.
182         */
183        @SuppressWarnings("unused")
184        public String retrieveConfiguredValueForOperationOfNewOWSCommon( String operationName,
185                                                                         String requestedParameterName ) {
186            String result = null;
187            /*
188             * if( operationName == null || requestedParameterName == null )return null; OGCCapabilities
189             * ogcCap = getOGCWebService().getCapabilities(); List<Operation> operations =
190             * ((org.deegree.owscommon_new.OWSCommonCapabilities)ogcCap).getOperationsMetadata().getOperations();
191             *
192             * for( Operation operation : operations ){ if( operationName.equalsIgnoreCase(
193             * operation.getName().getLocalName() ) ){ QualifiedName outputFormatName = new
194             * QualifiedName( operation.getName().getPrefix(), requestedParameterName,
195             * operation.getName().getNamespace() ); Parameter para = operation.getParameter(
196             * outputFormatName ); if( para != null ){ if( para instanceof DomainType ){ List<TypedLiteral>
197             * values = ((DomainType)para).getValues(); if( values.size() > 0 ){ outputFormat =
198             * values.get(0).getValue(); } else { outputFormat =
199             * ((DomainType)para).getDefaultValue().getValue(); } } } } }
200             */
201            return result;
202    
203        }
204    
205        /**
206         * Retrieves (if it exists) the first value of the requestedParameterName of the Operation
207         * defined by it's name. For example one wants to get GetFeature#outputFormat
208         *
209         * @param operationName
210         *            the name of the configured Operation
211         * @param requestedParameterName
212         *            the name of the Parameter.
213         * @return the Value of the (first) parameter if it exists else <code>null</code>.
214         */
215        public String retrieveConfiguredValueForOperation( String operationName,
216                                                           String requestedParameterName ) {
217            if ( operationName == null || requestedParameterName == null )
218                return null;
219            WFSConfiguration wfsConfig = null;
220            synchronized ( this ) {
221                URL url = getOwsCapabilities().getOnlineResource();
222                wfsConfig = cache.get( url );
223                if ( !cache.containsKey( url ) || wfsConfig == null ) {
224                    WFSConfigurationDocument wfsDoc = new WFSConfigurationDocument();
225                    try {
226                        wfsDoc.load( getOwsCapabilities().getOnlineResource() );
227                        wfsConfig = wfsDoc.getConfiguration();
228                        cache.put( url, wfsConfig );
229                    } catch ( IOException e ) {
230                        LOG.logError(
231                                                          Messages.getMessage(
232                                                                               "WPVS_DATASOURCE_CAP_ERROR",
233                                                                               toString() )
234                                                                                  + e.getMessage() );
235                        return null;
236                    } catch ( SAXException e ) {
237                        LOG.logError(
238                                                          Messages.getMessage(
239                                                                               "WPVS_DATASOURCE_CAP_ERROR",
240                                                                               toString() )
241                                                                                  + e.getMessage() );
242                        return null;
243                    } catch ( InvalidConfigurationException e ) {
244                        LOG.logError(
245                                                          Messages.getMessage(
246                                                                               "WPVS_DATASOURCE_CAP_ERROR",
247                                                                               toString() )
248                                                                                  + e.getMessage() );
249                        return null;
250                    }
251    
252                }
253            }
254    
255            OWSDomainType[] operations = ( (org.deegree.owscommon.OWSCommonCapabilities) wfsConfig ).getOperationsMetadata().getParameter();
256            for ( OWSDomainType operation : operations ) {
257                if ( operationName.equalsIgnoreCase( operation.getName() ) ) {
258                    String[] values = operation.getValues();
259                    if ( values != null && values.length > 0 ) {
260                        return values[0];
261                    }
262                }
263            }
264            return null;
265        }
266    
267        /**
268         * returns the (first) value of the configured constraint (given by it's name) for this
269         * WFSDataSource.
270         *
271         * @param constraintName
272         *            the name of the constraint.
273         * @return the value of the Constraint or <code>null</code> if no such constraint could be found.
274         */
275        public String retrieveConfiguredConstraintValue( String constraintName ) {
276            if ( constraintName == null )
277                return null;
278            WFSConfiguration wfsConfig = null;
279            synchronized ( this ) {
280                URL url = getOwsCapabilities().getOnlineResource();
281                wfsConfig = cache.get( url );
282                if ( !cache.containsKey( url ) || wfsConfig == null ) {
283                    WFSConfigurationDocument wfsDoc = new WFSConfigurationDocument();
284                    try {
285                        wfsDoc.load( getOwsCapabilities().getOnlineResource() );
286                        wfsConfig = wfsDoc.getConfiguration();
287                        cache.put( url, wfsConfig );
288                    } catch ( IOException e ) {
289                        LOG.logError(
290                                                          Messages.getMessage(
291                                                                               "WPVS_DATASOURCE_CAP_ERROR",
292                                                                               toString() )
293                                                                                  + e.getMessage() );
294                        return null;
295                    } catch ( SAXException e ) {
296                        LOG.logError(
297                                                          Messages.getMessage(
298                                                                               "WPVS_DATASOURCE_CAP_ERROR",
299                                                                               toString() )
300                                                                                  + e.getMessage() );
301                        return null;
302                    } catch ( InvalidConfigurationException e ) {
303                        LOG.logError(
304                                                          Messages.getMessage(
305                                                                               "WPVS_DATASOURCE_CAP_ERROR",
306                                                                               toString() )
307                                                                                  + e.getMessage() );
308                        return null;
309                    }
310    
311                }
312            }
313    
314            OWSDomainType[] constraints = ( (org.deegree.owscommon.OWSCommonCapabilities) wfsConfig ).getOperationsMetadata().getConstraints();
315            for ( OWSDomainType constraint : constraints ) {
316                if ( constraintName.equalsIgnoreCase( constraint.getName() ) ) {
317                    String[] values = constraint.getValues();
318                    if ( values != null && values.length > 0 ) {
319                        return values[0];
320                    }
321                }
322            }
323            return null;
324        }
325    
326        /**
327         * @return the configured MaxFeatures.
328         */
329        public final int getMaxFeatures() {
330            return maxFeatures;
331        }
332    }