001    //$$HeadURL: svn+ssh://rbezema@svn.wald.intevation.org/deegree/base/branches/2.2_testing/src/org/deegree/ogcwebservices/wpvs/configuration/LocalWFSDataSource.java $$
002    /*----------------    FILE HEADER  ------------------------------------------
003    
004     This file is part of deegree.
005     Copyright (C) 2001-2008 by:
006     EXSE, Department of Geography, University of Bonn
007     http://www.giub.uni-bonn.de/deegree/
008     lat/lon GmbH
009     http://www.lat-lon.de
010    
011     This library is free software; you can redistribute it and/or
012     modify it under the terms of the GNU Lesser General Public
013     License as published by the Free Software Foundation; either
014     version 2.1 of the License, or (at your option) any later version.
015    
016     This library is distributed in the hope that it will be useful,
017     but WITHOUT ANY WARRANTY; without even the implied warranty of
018     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
019     Lesser General Public License for more details.
020    
021     You should have received a copy of the GNU Lesser General Public
022     License along with this library; if not, write to the Free Software
023     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
024    
025     Contact:
026    
027     Andreas Poth
028     lat/lon GmbH
029     Aennchenstraße 19
030     53177 Bonn
031     Germany
032     E-Mail: poth@lat-lon.de
033    
034     Prof. Dr. Klaus Greve
035     Department of Geography
036     University of Bonn
037     Meckenheimer Allee 166
038     53115 Bonn
039     Germany
040     E-Mail: greve@giub.uni-bonn.de
041     
042     ---------------------------------------------------------------------------*/
043    
044    package org.deegree.ogcwebservices.wpvs.configuration;
045    
046    import java.io.IOException;
047    import java.net.URL;
048    import java.util.Map;
049    import java.util.concurrent.ConcurrentHashMap;
050    
051    import org.deegree.datatypes.QualifiedName;
052    import org.deegree.framework.log.ILogger;
053    import org.deegree.framework.log.LoggerFactory;
054    import org.deegree.framework.xml.InvalidConfigurationException;
055    import org.deegree.i18n.Messages;
056    import org.deegree.model.filterencoding.Filter;
057    import org.deegree.model.spatialschema.Surface;
058    import org.deegree.ogcbase.PropertyPath;
059    import org.deegree.ogcwebservices.OGCWebService;
060    import org.deegree.ogcwebservices.OGCWebServiceException;
061    import org.deegree.ogcwebservices.wfs.WFServiceFactory;
062    import org.deegree.ogcwebservices.wfs.configuration.WFSConfiguration;
063    import org.deegree.ogcwebservices.wfs.configuration.WFSConfigurationDocument;
064    import org.deegree.ogcwebservices.wpvs.capabilities.OWSCapabilities;
065    import org.deegree.owscommon.OWSDomainType;
066    import org.xml.sax.SAXException;
067    
068    /**
069     * This class represents a local WFS dataSource object.
070     * 
071     * @author <a href="mailto:taddei@lat-lon.de">Ugo Taddei</a>
072     * @author last edited by: $Author: apoth $
073     * 
074     * $Revision: 9345 $, $Date: 2007-12-27 17:22:25 +0100 (Do, 27 Dez 2007) $
075     */
076    public class LocalWFSDataSource extends AbstractDataSource {
077    
078        private static final ILogger LOG = LoggerFactory.getLogger( LocalWFSDataSource.class );
079    
080        private final PropertyPath geometryProperty;
081    
082        private int maxFeatures;
083    
084        private static Map<URL, WFSConfiguration> cache = new ConcurrentHashMap<URL, WFSConfiguration>();
085    
086        /**
087         * Creates a new <code>LocalWFSDataSource</code> object from the given parameters.
088         * 
089         * @param name
090         * @param owsCapabilities
091         * @param validArea
092         * @param minScaleDenominator
093         * @param maxScaleDenominator
094         * @param geomProperty
095         * @param filterCondition
096         *            a wfs query //TODO give an example //*
097         * @param maxFeatures to query this datasource for.
098         */
099        public LocalWFSDataSource( QualifiedName name, OWSCapabilities owsCapabilities,
100                                   Surface validArea, double minScaleDenominator,
101                                   double maxScaleDenominator, PropertyPath geomProperty,
102                                   Filter filterCondition/* , FeatureCollectionAdapter adapter */, int maxFeatures) {
103    
104            super( LOCAL_WFS, name, owsCapabilities, validArea, minScaleDenominator,
105                   maxScaleDenominator, filterCondition );
106            this.geometryProperty = geomProperty;
107            this.maxFeatures = maxFeatures;
108            // this.fcAdapter = adapter;
109        }
110    
111        @Override
112        public String toString() {
113            return super.toString() +"\n\t" + geometryProperty.getAsString();
114        }
115    
116        /**
117         * @return the Filter of the filterCondition.
118         */
119        public Filter getFilter() {
120            return (Filter) getFilterCondition();
121        }
122    
123        /**
124         * @return the geometryProperty.
125         */
126        public PropertyPath getGeometryProperty() {
127            return geometryProperty;
128        }
129    
130        @Override
131        public OGCWebService getOGCWebService()
132                                throws OGCWebServiceException {
133            WFSConfiguration wfsConfig = null;
134            synchronized ( this ) {
135    
136                URL url = getOwsCapabilities().getOnlineResource();
137                wfsConfig = cache.get( url );
138                if ( !cache.containsKey( url ) || wfsConfig == null ) {
139                    WFSConfigurationDocument wfsDoc = new WFSConfigurationDocument();
140                    try {
141                        wfsDoc.load( getOwsCapabilities().getOnlineResource() );
142                        wfsConfig = wfsDoc.getConfiguration();
143                        cache.put( url, wfsConfig );
144                    } catch ( IOException e ) {
145                        throw new OGCWebServiceException(
146                                                          Messages.getMessage(
147                                                                               "WPVS_DATASOURCE_CAP_ERROR",
148                                                                               toString() )
149                                                                                  + e.getMessage() );
150                    } catch ( SAXException e ) {
151                        throw new OGCWebServiceException(
152                                                          Messages.getMessage(
153                                                                               "WPVS_DATASOURCE_CAP_ERROR",
154                                                                               toString() )
155                                                                                  + e.getMessage() );
156                    } catch ( InvalidConfigurationException e ) {
157                        throw new OGCWebServiceException(
158                                                          Messages.getMessage(
159                                                                               "WPVS_DATASOURCE_CAP_ERROR",
160                                                                               toString() )
161                                                                                  + e.getMessage() );
162                    }
163    
164                }
165            }
166            return WFServiceFactory.createInstance( wfsConfig );
167        }
168    
169        // /**
170        // * @return the configured FeatureCollectionAdapter.
171        // */
172        // public FeatureCollectionAdapter getFeatureCollectionAdapter() {
173        // return fcAdapter;
174        // }
175    
176        /**
177         * ---DO NOT REMOVE --- NOT FUNCTIONAL YET, BUT might be if the WFS uses the new OWSCommon
178         * Package.
179         * 
180         * Retrieves (if it exists) the first value of the requestedParameterName of the Operation
181         * defined by it's name. For example one wants to get GetFeature#outputFormat
182         * 
183         * @param operationName
184         *            the name of the configured Operation
185         * @param requestedParameterName
186         *            the name of the Parameter.
187         * @return <code>null</code> - in the future: the Value of the (first) parameter if it exists else
188         *         <code>null</code>.
189         */
190        @SuppressWarnings("unused")
191        public String retrieveConfiguredValueForOperationOfNewOWSCommon( String operationName,
192                                                                         String requestedParameterName ) {
193            String result = null;
194            /*
195             * if( operationName == null || requestedParameterName == null )return null; OGCCapabilities
196             * ogcCap = getOGCWebService().getCapabilities(); List<Operation> operations =
197             * ((org.deegree.owscommon_new.OWSCommonCapabilities)ogcCap).getOperationsMetadata().getOperations();
198             * 
199             * for( Operation operation : operations ){ if( operationName.equalsIgnoreCase(
200             * operation.getName().getLocalName() ) ){ QualifiedName outputFormatName = new
201             * QualifiedName( operation.getName().getPrefix(), requestedParameterName,
202             * operation.getName().getNamespace() ); Parameter para = operation.getParameter(
203             * outputFormatName ); if( para != null ){ if( para instanceof DomainType ){ List<TypedLiteral>
204             * values = ((DomainType)para).getValues(); if( values.size() > 0 ){ outputFormat =
205             * values.get(0).getValue(); } else { outputFormat =
206             * ((DomainType)para).getDefaultValue().getValue(); } } } } }
207             */
208            return result;
209    
210        }
211    
212        /**
213         * Retrieves (if it exists) the first value of the requestedParameterName of the Operation
214         * defined by it's name. For example one wants to get GetFeature#outputFormat
215         * 
216         * @param operationName
217         *            the name of the configured Operation
218         * @param requestedParameterName
219         *            the name of the Parameter.
220         * @return the Value of the (first) parameter if it exists else <code>null</code>.
221         */
222        public String retrieveConfiguredValueForOperation( String operationName,
223                                                           String requestedParameterName ) {
224            if ( operationName == null || requestedParameterName == null )
225                return null;
226            WFSConfiguration wfsConfig = null;
227            synchronized ( this ) {
228                URL url = getOwsCapabilities().getOnlineResource();
229                wfsConfig = cache.get( url );
230                if ( !cache.containsKey( url ) || wfsConfig == null ) {
231                    WFSConfigurationDocument wfsDoc = new WFSConfigurationDocument();
232                    try {
233                        wfsDoc.load( getOwsCapabilities().getOnlineResource() );
234                        wfsConfig = wfsDoc.getConfiguration();
235                        cache.put( url, wfsConfig );
236                    } catch ( IOException e ) {
237                        LOG.logError(
238                                                          Messages.getMessage(
239                                                                               "WPVS_DATASOURCE_CAP_ERROR",
240                                                                               toString() )
241                                                                                  + e.getMessage() );
242                        return null;                    
243                    } catch ( SAXException e ) {
244                        LOG.logError(
245                                                          Messages.getMessage(
246                                                                               "WPVS_DATASOURCE_CAP_ERROR",
247                                                                               toString() )
248                                                                                  + e.getMessage() );
249                        return null;
250                    } catch ( InvalidConfigurationException e ) {
251                        LOG.logError(
252                                                          Messages.getMessage(
253                                                                               "WPVS_DATASOURCE_CAP_ERROR",
254                                                                               toString() )
255                                                                                  + e.getMessage() );
256                        return null;
257                    }
258    
259                }
260            }
261    
262            OWSDomainType[] operations = ( (org.deegree.owscommon.OWSCommonCapabilities) wfsConfig ).getOperationsMetadata().getParameter();
263            for ( OWSDomainType operation : operations ) {
264                if ( operationName.equalsIgnoreCase( operation.getName() ) ) {
265                    String[] values = operation.getValues();
266                    if ( values != null && values.length > 0 ) {
267                        return values[0];
268                    }
269                }
270            }
271            return null;
272        }
273    
274        /**
275         * returns the (first) value of the configured constraint (given by it's name) for this
276         * WFSDataSource.
277         * 
278         * @param constraintName
279         *            the name of the constraint.
280         * @return the value of the Constraint or <code>null</code> if no such constraint could be found.
281         */
282        public String retrieveConfiguredConstraintValue( String constraintName ) {
283            if ( constraintName == null )
284                return null;
285            WFSConfiguration wfsConfig = null;
286            synchronized ( this ) {
287                URL url = getOwsCapabilities().getOnlineResource();
288                wfsConfig = cache.get( url );
289                if ( !cache.containsKey( url ) || wfsConfig == null ) {
290                    WFSConfigurationDocument wfsDoc = new WFSConfigurationDocument();
291                    try {
292                        wfsDoc.load( getOwsCapabilities().getOnlineResource() );
293                        wfsConfig = wfsDoc.getConfiguration();
294                        cache.put( url, wfsConfig );
295                    } catch ( IOException e ) {
296                        LOG.logError(
297                                                          Messages.getMessage(
298                                                                               "WPVS_DATASOURCE_CAP_ERROR",
299                                                                               toString() )
300                                                                                  + e.getMessage() );
301                        return null;                    
302                    } catch ( SAXException e ) {
303                        LOG.logError(
304                                                          Messages.getMessage(
305                                                                               "WPVS_DATASOURCE_CAP_ERROR",
306                                                                               toString() )
307                                                                                  + e.getMessage() );
308                        return null;
309                    } catch ( InvalidConfigurationException e ) {
310                        LOG.logError(
311                                                          Messages.getMessage(
312                                                                               "WPVS_DATASOURCE_CAP_ERROR",
313                                                                               toString() )
314                                                                                  + e.getMessage() );
315                        return null;
316                    }
317    
318                }
319            }
320    
321            OWSDomainType[] constraints = ( (org.deegree.owscommon.OWSCommonCapabilities) wfsConfig ).getOperationsMetadata().getConstraints();
322            for ( OWSDomainType constraint : constraints ) {
323                if ( constraintName.equalsIgnoreCase( constraint.getName() ) ) {
324                    String[] values = constraint.getValues();
325                    if ( values != null && values.length > 0 ) {
326                        return values[0];
327                    }
328                }
329            }
330            return null;
331        }
332    
333        /**
334         * @return the configured MaxFeatures.
335         */
336        public final int getMaxFeatures() {
337            return maxFeatures;
338        }
339    }