001    //$HeadURL: svn+ssh://rbezema@svn.wald.intevation.org/deegree/base/branches/2.2_testing/src/org/deegree/ogcwebservices/wpvs/configuration/LocalWCSDataSource.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.awt.Color;
047    import java.io.IOException;
048    import java.net.URL;
049    import java.util.Map;
050    import java.util.concurrent.ConcurrentHashMap;
051    
052    import org.deegree.datatypes.Code;
053    import org.deegree.datatypes.CodeList;
054    import org.deegree.datatypes.QualifiedName;
055    import org.deegree.framework.log.ILogger;
056    import org.deegree.framework.log.LoggerFactory;
057    import org.deegree.i18n.Messages;
058    import org.deegree.model.spatialschema.Surface;
059    import org.deegree.ogcwebservices.OGCWebService;
060    import org.deegree.ogcwebservices.OGCWebServiceException;
061    import org.deegree.ogcwebservices.SupportedFormats;
062    import org.deegree.ogcwebservices.getcapabilities.InvalidCapabilitiesException;
063    import org.deegree.ogcwebservices.wcs.CoverageOfferingBrief;
064    import org.deegree.ogcwebservices.wcs.WCService;
065    import org.deegree.ogcwebservices.wcs.configuration.InvalidConfigurationException;
066    import org.deegree.ogcwebservices.wcs.configuration.WCSConfiguration;
067    import org.deegree.ogcwebservices.wcs.describecoverage.CoverageDescription;
068    import org.deegree.ogcwebservices.wcs.describecoverage.CoverageOffering;
069    import org.deegree.ogcwebservices.wcs.getcapabilities.ContentMetadata;
070    import org.deegree.ogcwebservices.wcs.getcapabilities.WCSCapabilities;
071    import org.deegree.ogcwebservices.wcs.getcoverage.GetCoverage;
072    import org.deegree.ogcwebservices.wpvs.capabilities.OWSCapabilities;
073    import org.xml.sax.SAXException;
074    
075    /**
076     * This class represents a local WCS dataSource object.
077     * 
078     * @author <a href="mailto:taddei@lat-lon.de">Ugo Taddei</a>
079     * @author last edited by: $Author: apoth $
080     * 
081     * $Revision: 9345 $, $Date: 2007-12-27 17:22:25 +0100 (Do, 27 Dez 2007) $
082     * 
083     */
084    public class LocalWCSDataSource extends AbstractDataSource {
085    
086        private static final ILogger LOG = LoggerFactory.getLogger( LocalWCSDataSource.class );
087    
088        private Color[] transparentColors;
089    
090        private double configuredMinimalDGMResolution;
091    
092        private static Map<URL, WCSConfiguration> cache = new ConcurrentHashMap<URL, WCSConfiguration>();
093    
094        private String defaultFormat;
095    
096        // private static WCSConfiguration wcsConfig = null;
097    
098        /**
099         * Creates a new <code>LocalWCSDataSource</code> object from the given parameters.
100         * 
101         * @param name
102         * @param owsCapabilities
103         * @param validArea
104         * @param minScaleDenominator
105         * @param maxScaleDenominator
106         * @param filterCondition
107         *            a base request //TODO give an example
108         * @param transparentColors
109         */
110        public LocalWCSDataSource( QualifiedName name, OWSCapabilities owsCapabilities, Surface validArea,
111                                   double minScaleDenominator, double maxScaleDenominator, GetCoverage filterCondition,
112                                   Color[] transparentColors ) {
113    
114            super( LOCAL_WCS, name, owsCapabilities, validArea, minScaleDenominator, maxScaleDenominator, filterCondition );
115            this.transparentColors = transparentColors;
116            configuredMinimalDGMResolution = 0;
117            StringBuilder sb = new StringBuilder( "Couldn't determine default csw format because: " );
118            if ( filterCondition != null ) {
119                defaultFormat = filterCondition.getOutput().getFormat().getCode();
120            }
121            if ( defaultFormat == null || "".equals( defaultFormat.trim() ) ) {
122                try {
123                    WCService service = (WCService) getOGCWebService();
124                    if ( service != null ) {
125                        WCSCapabilities caps = (WCSCapabilities) service.getCapabilities();
126                        if ( caps != null ) {
127                            ContentMetadata md = caps.getContentMetadata();
128                            if ( md != null ) {
129                                CoverageOfferingBrief[] cobs = md.getCoverageOfferingBrief();
130                                if ( cobs != null ) {
131                                    for ( int i = 0; i < cobs.length && defaultFormat == null; ++i ) {
132                                        CoverageOfferingBrief cob = cobs[i];
133                                        String cobName = cob.getName();
134                                        if ( cobName != null && !"".equals( cobName.trim() ) ) {
135                                            if ( name.getLocalName().equals( cobName.trim() ) ) {
136                                                LOG.logDebug( "Found a BriefCoverage Offering with the name of this datasource: " + name.getLocalName() );
137                                                URL url = cob.getConfiguration();
138                                                CoverageDescription cd = CoverageDescription.createCoverageDescription( url );
139                                                if ( cd != null ) {
140                                                    CoverageOffering[] cos = cd.getCoverageOfferings();
141                                                    if ( cos != null ) {
142                                                        for ( int coCount = 0; coCount < cos.length && defaultFormat == null; coCount++ ) {
143                                                            CoverageOffering co = cos[coCount];
144                                                            if ( co != null ) {
145                                                                SupportedFormats sfs = co.getSupportedFormats();
146                                                                if ( sfs != null ) {
147                                                                    Code nativeFormat = sfs.getNativeFormat();
148                                                                    String nf = null;
149                                                                    if ( nativeFormat != null ) {
150                                                                        nf = nativeFormat.getCode();
151                                                                        if ( nf != null ) {
152                                                                            nf = nf.trim();
153                                                                        }
154                                                                    }
155                                                                    CodeList[] cls = sfs.getFormats();
156                                                                    if ( cls != null ) {
157                                                                        for ( int clCount = 0; clCount < cls.length && defaultFormat == null; clCount++ ) {
158                                                                            CodeList cl = cls[clCount];
159                                                                            if ( cl != null ) {
160                                                                                String[] codes = cl.getCodes();
161                                                                                if ( codes != null ) {
162                                                                                    for ( int codeCount = 0; codeCount < codes.length && defaultFormat == null; ++codeCount ) {
163                                                                                        String code = codes[codeCount];
164                                                                                        if ( code != null && !"".equals( code.trim() ) ) {
165                                                                                            if ( code.toLowerCase().contains( "tif" ) ) {
166                                                                                                defaultFormat = code;
167                                                                                            }
168                                                                                        }
169                                                                                    }
170                                                                                }
171                                                                            }
172                                                                        }
173                                                                    }// codelists != null
174                                                                }// supportedformats !=null
175                                                            } // co !=null
176                                                        }// for coverageofferings.
177                                                    }// coverage offerings != null
178                                                }// no CoverageDescriptor
179                                            }// name fits
180                                        }// name not empty
181                                    }// for brief coverages
182                                    if ( defaultFormat == null ) {
183                                        sb.append( "No brief coverage Offering found with datasource name: " )
184                                          .append( name.getLocalName() );
185                                    }
186                                } else {
187                                    sb.append( "no Brief Coverage Offerings found." );
188                                }
189                            } else {
190                                sb.append( "no ContentMetadata found." );
191                            }
192                        } else {
193                            sb.append( "no WCSCapabilities found." );
194                        }
195                    } else {
196                        sb.append( "no WCService found." );
197                    }
198                } catch ( OGCWebServiceException e ) {
199                    if ( LOG.getLevel() == ILogger.LOG_DEBUG ) {
200                        e.printStackTrace();
201                    }
202                    sb.append( e.getMessage() );
203                } catch ( IOException e ) {
204                    if ( LOG.getLevel() == ILogger.LOG_DEBUG ) {
205                        e.printStackTrace();
206                    }
207                    sb.append( e.getMessage() );
208                } catch ( SAXException e ) {
209                    if ( LOG.getLevel() == ILogger.LOG_DEBUG ) {
210                        e.printStackTrace();
211                    }
212                    sb.append( e.getMessage() );
213                }
214            }
215            if ( defaultFormat == null ) {
216                sb.append( "\nsetting defaultFormat to tiff." );
217                LOG.logWarning( sb.toString() );
218                defaultFormat = "tiff";
219            }
220            if ( LOG.getLevel() == ILogger.LOG_DEBUG ) {
221                LOG.logDebug( "Default GetCoverage request format is: " + defaultFormat );
222            }
223        }
224    
225        /**
226         * The <code>filterCondition</code> is a GetCoverage object which extends the WCSRequestBase.
227         * 
228         * @return Returns the filterCondition as a GetCoverage object.
229         */
230        public GetCoverage getCoverageFilterCondition() {
231            return (GetCoverage) getFilterCondition();
232        }
233    
234        /**
235         * @return Returns the transparentColors.
236         */
237        public Color[] getTransparentColors() {
238            return transparentColors;
239        }
240    
241        @Override
242        public String toString() {
243    
244            StringBuffer sb = new StringBuffer( super.toString() );
245    
246            Color[] colors = getTransparentColors();
247            for ( int i = 0; i < colors.length; i++ ) {
248                sb.append( "\n color : " ).append( colors[i] );
249            }
250    
251            GetCoverage filter = getCoverageFilterCondition();
252            try {
253                sb.append( "\n\t filter : " );
254                sb.append( "\n\t  -version : " );
255                sb.append( filter.getVersion() );
256                sb.append( "\n\t  -id : " );
257                sb.append( filter.getId() );
258                sb.append( "\n\t  -serviceName : " );
259                sb.append( filter.getServiceName() );
260                sb.append( "\n\t  -sourceCoverage: " );
261                sb.append( filter.getSourceCoverage() );
262            } catch ( Exception e ) {
263                e.printStackTrace();
264            }
265    
266            return sb.toString();
267        }
268    
269        /**
270         * @throws OGCWebServiceException
271         * @see org.deegree.ogcwebservices.wpvs.configuration.AbstractDataSource#getOGCWebService()
272         */
273        @Override
274        public OGCWebService getOGCWebService()
275                                               throws OGCWebServiceException {
276            WCSConfiguration wcsConfig = null;
277            synchronized ( this ) {
278                URL url = getOwsCapabilities().getOnlineResource();
279                wcsConfig = cache.get( url );
280                if ( !cache.containsKey( url ) || wcsConfig == null ) {
281    
282                    URL caps = getOwsCapabilities().getOnlineResource();
283                    try {
284                        wcsConfig = WCSConfiguration.create( caps );
285                        cache.put( url, wcsConfig );
286                    } catch ( InvalidCapabilitiesException e ) {
287                        throw new OGCWebServiceException( Messages.getMessage( "WPVS_DATASOURCE_CAP_ERROR", toString() ) + e.getMessage() );
288                    } catch ( InvalidConfigurationException e ) {
289                        throw new OGCWebServiceException( Messages.getMessage( "WPVS_DATASOURCE_CAP_ERROR", toString() ) + e.getMessage() );
290                    } catch ( IOException e ) {
291                        throw new OGCWebServiceException( Messages.getMessage( "WPVS_DATASOURCE_CAP_ERROR", toString() ) + e.getMessage() );
292                    } catch ( SAXException e ) {
293                        throw new OGCWebServiceException( Messages.getMessage( "WPVS_DATASOURCE_CAP_ERROR", toString() ) + e.getMessage() );
294                    }
295                }
296                this.notifyAll();
297            }
298            return new WCService( wcsConfig );
299        }
300    
301        /**
302         * @return the configuredMinimalDGMResolution.
303         */
304        public double getConfiguredMinimalDGMResolution() {
305            return configuredMinimalDGMResolution;
306        }
307    
308        /**
309         * @param configuredMinimalDGMResolution
310         *            An other configuredMinimalDGMResolution value.
311         */
312        public void setConfiguredMinimalDGMResolution( double configuredMinimalDGMResolution ) {
313            this.configuredMinimalDGMResolution = configuredMinimalDGMResolution;
314        }
315    
316        /**
317         * @return the defaultFormat to be used for GetCoverage requests
318         */
319        public final String getDefaultFormat() {
320            return defaultFormat;
321        }
322    }