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