001    //$$HeadURL: svn+ssh://rbezema@svn.wald.intevation.org/deegree/base/tags/2.1/src/org/deegree/ogcwebservices/wpvs/configuration/LocalWFSDataSource.java $$
002    /*----------------    FILE HEADER  ------------------------------------------
003    
004     This file is part of deegree.
005     Copyright (C) 2001-2006 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: bezema $
073     * 
074     * $Revision: 6259 $, $Date: 2007-03-20 10:15:15 +0100 (Di, 20 Mär 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 static Map<URL, WFSConfiguration> cache = new ConcurrentHashMap<URL, WFSConfiguration>();
083    
084        /**
085         * Creates a new <code>LocalWFSDataSource</code> object from the given parameters.
086         * 
087         * @param name
088         * @param owsCapabilities
089         * @param validArea
090         * @param minScaleDenominator
091         * @param maxScaleDenominator
092         * @param geomProperty
093         * @param filterCondition
094         *            a wfs query //TODO give an example //*
095         */
096        public LocalWFSDataSource( QualifiedName name, OWSCapabilities owsCapabilities,
097                                   Surface validArea, double minScaleDenominator,
098                                   double maxScaleDenominator, PropertyPath geomProperty,
099                                   Filter filterCondition/* , FeatureCollectionAdapter adapter */) {
100    
101            super( LOCAL_WFS, name, owsCapabilities, validArea, minScaleDenominator,
102                   maxScaleDenominator, filterCondition );
103            this.geometryProperty = geomProperty;
104            // this.fcAdapter = adapter;
105        }
106    
107        @Override
108        public String toString() {
109            return super.toString() + '\n' + ' ' + geometryProperty.getAsString();
110        }
111    
112        /**
113         * @return the Filter of the filterCondition.
114         */
115        public Filter getFilter() {
116            return (Filter) getFilterCondition();
117        }
118    
119        /**
120         * @return the geometryProperty.
121         */
122        public PropertyPath getGeometryProperty() {
123            return geometryProperty;
124        }
125    
126        @Override
127        public OGCWebService getOGCWebService()
128                                throws OGCWebServiceException {
129            WFSConfiguration wfsConfig = null;
130            synchronized ( this ) {
131    
132                URL url = getOwsCapabilities().getOnlineResource();
133                wfsConfig = cache.get( url );
134                if ( !cache.containsKey( url ) || wfsConfig == null ) {
135                    WFSConfigurationDocument wfsDoc = new WFSConfigurationDocument();
136                    try {
137                        wfsDoc.load( getOwsCapabilities().getOnlineResource() );
138                        wfsConfig = wfsDoc.getConfiguration();
139                        cache.put( url, wfsConfig );
140                    } catch ( IOException e ) {
141                        throw new OGCWebServiceException(
142                                                          Messages.getMessage(
143                                                                               "WPVS_DATASOURCE_CAP_ERROR",
144                                                                               toString() )
145                                                                                  + e.getMessage() );
146                    } catch ( SAXException e ) {
147                        throw new OGCWebServiceException(
148                                                          Messages.getMessage(
149                                                                               "WPVS_DATASOURCE_CAP_ERROR",
150                                                                               toString() )
151                                                                                  + e.getMessage() );
152                    } catch ( InvalidConfigurationException e ) {
153                        throw new OGCWebServiceException(
154                                                          Messages.getMessage(
155                                                                               "WPVS_DATASOURCE_CAP_ERROR",
156                                                                               toString() )
157                                                                                  + e.getMessage() );
158                    }
159    
160                }
161            }
162            return WFServiceFactory.createInstance( wfsConfig );
163        }
164    
165        // /**
166        // * @return the configured FeatureCollectionAdapter.
167        // */
168        // public FeatureCollectionAdapter getFeatureCollectionAdapter() {
169        // return fcAdapter;
170        // }
171    
172        /**
173         * ---DO NOT REMOVE --- NOT FUNCTIONAL YET, BUT might be if the WFS uses the new OWSCommon
174         * Package.
175         * 
176         * Retrieves (if it exists) the first value of the requestedParameterName of the Operation
177         * defined by it's name. For example one wants to get GetFeature#outputFormat
178         * 
179         * @param operationName
180         *            the name of the configured Operation
181         * @param requestedParameterName
182         *            the name of the Parameter.
183         * @return <code>null</code> - in the future: the Value of the (first) parameter if it exists else
184         *         <code>null</code>.
185         */
186        @SuppressWarnings("unused")
187        public String retrieveConfiguredValueForOperationOfNewOWSCommon( String operationName,
188                                                                         String requestedParameterName ) {
189            String result = null;
190            /*
191             * if( operationName == null || requestedParameterName == null )return null; OGCCapabilities
192             * ogcCap = getOGCWebService().getCapabilities(); List<Operation> operations =
193             * ((org.deegree.owscommon_new.OWSCommonCapabilities)ogcCap).getOperationsMetadata().getOperations();
194             * 
195             * for( Operation operation : operations ){ if( operationName.equalsIgnoreCase(
196             * operation.getName().getLocalName() ) ){ QualifiedName outputFormatName = new
197             * QualifiedName( operation.getName().getPrefix(), requestedParameterName,
198             * operation.getName().getNamespace() ); Parameter para = operation.getParameter(
199             * outputFormatName ); if( para != null ){ if( para instanceof DomainType ){ List<TypedLiteral>
200             * values = ((DomainType)para).getValues(); if( values.size() > 0 ){ outputFormat =
201             * values.get(0).getValue(); } else { outputFormat =
202             * ((DomainType)para).getDefaultValue().getValue(); } } } } }
203             */
204            return result;
205    
206        }
207    
208        /**
209         * Retrieves (if it exists) the first value of the requestedParameterName of the Operation
210         * defined by it's name. For example one wants to get GetFeature#outputFormat
211         * 
212         * @param operationName
213         *            the name of the configured Operation
214         * @param requestedParameterName
215         *            the name of the Parameter.
216         * @return the Value of the (first) parameter if it exists else <code>null</code>.
217         */
218        public String retrieveConfiguredValueForOperation( String operationName,
219                                                           String requestedParameterName ) {
220            if ( operationName == null || requestedParameterName == null )
221                return null;
222            WFSConfiguration wfsConfig = null;
223            synchronized ( this ) {
224                URL url = getOwsCapabilities().getOnlineResource();
225                wfsConfig = cache.get( url );
226                if ( !cache.containsKey( url ) || wfsConfig == null ) {
227                    WFSConfigurationDocument wfsDoc = new WFSConfigurationDocument();
228                    try {
229                        wfsDoc.load( getOwsCapabilities().getOnlineResource() );
230                        wfsConfig = wfsDoc.getConfiguration();
231                        cache.put( url, wfsConfig );
232                    } catch ( IOException e ) {
233                        LOG.logError(
234                                                          Messages.getMessage(
235                                                                               "WPVS_DATASOURCE_CAP_ERROR",
236                                                                               toString() )
237                                                                                  + e.getMessage() );
238                        return null;                    
239                    } catch ( SAXException e ) {
240                        LOG.logError(
241                                                          Messages.getMessage(
242                                                                               "WPVS_DATASOURCE_CAP_ERROR",
243                                                                               toString() )
244                                                                                  + e.getMessage() );
245                        return null;
246                    } catch ( InvalidConfigurationException e ) {
247                        LOG.logError(
248                                                          Messages.getMessage(
249                                                                               "WPVS_DATASOURCE_CAP_ERROR",
250                                                                               toString() )
251                                                                                  + e.getMessage() );
252                        return null;
253                    }
254    
255                }
256            }
257    
258            OWSDomainType[] operations = ( (org.deegree.owscommon.OWSCommonCapabilities) wfsConfig ).getOperationsMetadata().getParameter();
259            for ( OWSDomainType operation : operations ) {
260                if ( operationName.equalsIgnoreCase( operation.getName() ) ) {
261                    String[] values = operation.getValues();
262                    if ( values != null && values.length > 0 ) {
263                        return values[0];
264                    }
265                }
266            }
267            return null;
268        }
269    
270        /**
271         * returns the (first) value of the configured constraint (given by it's name) for this
272         * WFSDataSource.
273         * 
274         * @param constraintName
275         *            the name of the constraint.
276         * @return the value of the Constraint or <code>null</code> if no such constraint could be found.
277         */
278        public String retrieveConfiguredConstraintValue( String constraintName ) {
279            if ( constraintName == null )
280                return null;
281            WFSConfiguration wfsConfig = null;
282            synchronized ( this ) {
283                URL url = getOwsCapabilities().getOnlineResource();
284                wfsConfig = cache.get( url );
285                if ( !cache.containsKey( url ) || wfsConfig == null ) {
286                    WFSConfigurationDocument wfsDoc = new WFSConfigurationDocument();
287                    try {
288                        wfsDoc.load( getOwsCapabilities().getOnlineResource() );
289                        wfsConfig = wfsDoc.getConfiguration();
290                        cache.put( url, wfsConfig );
291                    } catch ( IOException e ) {
292                        LOG.logError(
293                                                          Messages.getMessage(
294                                                                               "WPVS_DATASOURCE_CAP_ERROR",
295                                                                               toString() )
296                                                                                  + e.getMessage() );
297                        return null;                    
298                    } catch ( SAXException e ) {
299                        LOG.logError(
300                                                          Messages.getMessage(
301                                                                               "WPVS_DATASOURCE_CAP_ERROR",
302                                                                               toString() )
303                                                                                  + e.getMessage() );
304                        return null;
305                    } catch ( InvalidConfigurationException e ) {
306                        LOG.logError(
307                                                          Messages.getMessage(
308                                                                               "WPVS_DATASOURCE_CAP_ERROR",
309                                                                               toString() )
310                                                                                  + e.getMessage() );
311                        return null;
312                    }
313    
314                }
315            }
316    
317            OWSDomainType[] constraints = ( (org.deegree.owscommon.OWSCommonCapabilities) wfsConfig ).getOperationsMetadata().getConstraints();
318            for ( OWSDomainType constraint : constraints ) {
319                if ( constraintName.equalsIgnoreCase( constraint.getName() ) ) {
320                    String[] values = constraint.getValues();
321                    if ( values != null && values.length > 0 ) {
322                        return values[0];
323                    }
324                }
325            }
326            return null;
327        }
328    }