    001    //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/branches/2.4_testing/src/org/deegree/portal/PortalUtils.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    package org.deegree.portal;
    037
    038    import java.net.URL;
    039    import java.net.URLEncoder;
    040    import java.util.ArrayList;
    041    import java.util.Iterator;
    042    import java.util.List;
    043
    044    import org.deegree.framework.log.ILogger;
    045    import org.deegree.framework.log.LoggerFactory;
    046    import org.deegree.framework.util.CharsetUtils;
    047    import org.deegree.model.spatialschema.Point;
    048    import org.deegree.ogcwebservices.OWSUtils;
    049    import org.deegree.ogcwebservices.wms.operation.GetMap;
    050    import org.deegree.portal.context.Format;
    051    import org.deegree.portal.context.GeneralExtension;
    052    import org.deegree.portal.context.Layer;
    053    import org.deegree.portal.context.LayerExtension;
    054    import org.deegree.portal.context.LayerList;
    055    import org.deegree.portal.context.Node;
    056    import org.deegree.portal.context.ViewContext;
    057
    058    /**
    059     *
    060     *
    061     *
    062     * @version $Revision: 18195 $
    063     * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a>
    064     * @author last edited by: $Author: mschneider $
    065     *
    066     * @version 1.0. $Revision: 18195 $, $Date: 2009-06-18 17:55:39 +0200 (Do, 18. Jun 2009) $
    067     *
    068     * @since 2.0
    069     */
    070    public class PortalUtils {
    071
    072        private static final ILogger LOG = LoggerFactory.getLogger( PortalUtils.class );
    073
    074        /**
    075         * returns a comma seperated list of layers marked as sensible for feature info requestes
    076         *
    077         * @param vc
    078         * @return a comma seperated list of layers marked as sensible for feature info requestes
    079         */
    080        public static List<String> getFeatureInfoLayers( ViewContext vc ) {
    081            List<String> list = new ArrayList<String>();
    082            LayerList layerList = vc.getLayerList();
    083            Layer[] layers = layerList.getLayers();
    084            for ( int i = 0; i < layers.length; i++ ) {
    085                if ( layers[i].getExtension() != null && layers[i].getExtension().isSelectedForQuery() ) {
    086                    list.add( layers[i].getName() );
    087                }
    088            }
    089            return list;
    090        }
    091
    092        /**
    093         * returns a comma separated list of visible layers
    094         *
    095         * @param vc
    096         * @return a comma separated list of visible layers
    097         */
    098        public static List<String[]> getVisibleLayers( ViewContext vc ) {
    099            List<String[]> list = new ArrayList<String[]>();
    100            LayerList layerList = vc.getLayerList();
    101            Layer[] layers = layerList.getLayers();
    102            for ( int i = 0; i < layers.length; i++ ) {
    103                if ( !layers[i].isHidden() ) {
    104                    String[] s = new String[2];
    105                    s[0] = layers[i].getName();
    106                    s[1] = layers[i].getServer().getOnlineResource().toExternalForm();
    107                    list.add( s );
    108                }
    109            }
    110            return list;
    111        }
    112
    113        /**
    114         * returns true if at least one layer of the passed server is visible
    115         *
    116         * @param vc
    117         * @param serverTitle
    118         * @return <code>true</code> if at least one layer of the passed server is visible
    119         */
    120        public static boolean hasServerVisibleLayers( ViewContext vc, String serverTitle ) {
    121            LayerList layerList = vc.getLayerList();
    122            Layer[] layers = layerList.getLayers();
    123            for ( int i = 0; i < layers.length; i++ ) {
    124                if ( layers[i].getServer().getTitle().equals( serverTitle ) && !layers[i].isHidden() ) {
    125                    return true;
    126                }
    127            }
    128            return false;
    129        }
    130
    131        /**
    132         * returns true if at least one layer within the node is visible
    133         *
    134         * @param vc
    135         * @param nodeID
    136         * @return <code>true</code> if at least one layer of the passed server is visible
    137         */
    138        public static boolean hasNodeVisibleLayers( ViewContext vc, int nodeID ) {
    139            Layer[] layers = vc.getLayerList().getLayersByNodeId( nodeID );
    140            for ( Layer layer : layers ) {
    141                if ( !layer.isHidden() ) {
    142                    return true;
    143                }
    144            }
    145            Node[] nodes = vc.getGeneral().getExtension().getLayerTreeRoot().getNode( nodeID ).getNodes();
    146            for ( Node node : nodes ) {
    147                if ( hasNodeVisibleLayers( vc, node.getId() ) ) {
    148                    return true;
    149                }
    150            }
    151            return false;
    152        }
    153
    154        /**
    155         * set visibility for a node and all child nodes/layers
    156         *
    157         * @param vc
    158         * @param nodeID
    159         * @param hidden
    160         */
    161        public static void setVisibility( ViewContext vc, int nodeID, boolean hidden ) {
    162            Layer[] layers = vc.getLayerList().getLayersByNodeId( nodeID );
    163            for ( Layer layer : layers ) {
    164                layer.setHidden( hidden );
    165            }
    166            Node[] nodes = vc.getGeneral().getExtension().getLayerTreeRoot().getNode( nodeID ).getNodes();
    167            for ( Node node : nodes ) {
    168                setVisibility( vc, node.getId(), hidden );
    169            }
    170        }
    171
    172        /**
    173         * creates the GetMap basic requests required by the JSP page assigned to the MapViewPortlet.
    174         *
    175         * @param vc
    176         * @param user
    177         * @param password
    178         * @param sessionID
    179         * @return list of GetMap requests
    180         */
    181        public static String[] createBaseRequests( ViewContext vc, String user, String password, String sessionID ) {
    182
    183            Layer[] layers = vc.getLayerList().getLayers();
    184            List<String> list = new ArrayList<String>( layers.length );
    185            int i = layers.length - 1;
    186            try {
    187                while ( i >= 0 ) {
    188                    GeneralExtension gExt = vc.getGeneral().getExtension();
    189                    Point[] bbox = vc.getGeneral().getBoundingBox();
    190                    StringBuffer sb = new StringBuffer( 1000 );
    191                    URL url = OWSUtils.getHTTPGetOperationURL( layers[i].getServer().getCapabilities(), GetMap.class );
    192                    if ( url != null ) {
    193                        String href = url.toExternalForm();
    194
    195                        sb.append( OWSUtils.validateHTTPGetBaseURL( href ) );
    196                        sb.append( "SRS=" ).append( bbox[0].getCoordinateSystem().getIdentifier() );
    197                        if ( "1.0.0".equals( layers[i].getServer().getVersion() ) ) {
    198                            sb.append( "&REQUEST=map" );
    199                            sb.append( "&exceptions=INIMAGE" );
    200                            sb.append( "&WMTVER=" ).append( layers[i].getServer().getVersion() );
    201                        } else {
    202                            sb.append( "&REQUEST=GetMap" );
    203                            sb.append( "&exceptions=application/vnd.ogc.se_inimage" );
    204                            sb.append( "&VERSION=" ).append( layers[i].getServer().getVersion() );
    205                        }
    206                        sb.append( "&transparent=" ).append( true );
    207                        if ( gExt == null ) {
    208                            sb.append( "&BGCOLOR=0xFFFFFF" );
    209                        } else {
    210                            sb.append( "&BGCOLOR=" ).append( gExt.getBgColor() );
    211                        }
    212
    213                        Format format = layers[i].getFormatList().getCurrentFormat();
    214                        sb.append( "&FORMAT=" ).append( format.getName() );
    215                        StringBuffer styles = new StringBuffer( 1000 );
    216                        styles.append( "&STYLES=" );
    217                        StringBuffer lyrs = new StringBuffer( 1000 );
    218                        lyrs.append( "&LAYERS=" );
    219                        String title = layers[i].getServer().getTitle();
    220                        int authentication = -1;
    221                        while ( i >= 0 && title.equals( layers[i].getServer().getTitle() ) ) {
    222                            if ( !layers[i].isHidden() ) {
    223                                if ( authentication < 0 ) {
    224                                    // just evaluate if not has already been loaded
    225                                    authentication = layers[i].getExtension().getAuthentication();
    226                                }
    227                                lyrs.append( URLEncoder.encode( layers[i].getName(), "UTF-8" ) ).append( ',' );
    228                                String style = layers[i].getStyleList().getCurrentStyle().getName();
    229                                if ( style.equalsIgnoreCase( "DEFAULT" ) ) {
    230                                    style = "";
    231                                }
    232                                styles.append( style ).append( ',' );
    233                            }
    234                            LayerExtension le = layers[i].getExtension();
    235                            Iterator<String> iterator = le.getVendorspecificParameterNames();
    236                            while ( iterator.hasNext() ) {
    237                                sb.append( '&' );
    238                                String name = iterator.next();
    239                                String value = le.getVendorspecificParameter( name );
    240                                sb.append( name ).append( '=' ).append( value );
    241                            }
    242                            i--;
    243                        }
    244                        if ( authentication == LayerExtension.USERPASSWORD ) {
    245                            if ( user != null ) {
    246                                sb.append( "&user=" ).append( URLEncoder.encode( user, CharsetUtils.getSystemCharset() ) );
    247                            }
    248                            if ( password != null ) {
    249                                sb.append( "&password=" );
    250                                sb.append( URLEncoder.encode( password, CharsetUtils.getSystemCharset() ) );
    251                            }
    252                        } else if ( authentication == LayerExtension.SESSIONID ) {
    253                            if ( sessionID != null ) {
    254                                sb.append( "&sessionID=" );
    255                                sb.append( URLEncoder.encode( sessionID, CharsetUtils.getSystemCharset() ) );
    256                            }
    257                        }
    258                        String s1 = lyrs.substring( 0, lyrs.length() - 1 );
    259                        String s2 = styles.substring( 0, styles.length() - 1 );
    260                        sb.append( s1 ).append( s2 );
    261
    262                        if ( s1.length() > 7 ) {
    263                            // ensure that a request will just be created if
    264                            // at least one layer of a group is visible
    265                            list.add( sb.toString() );
    266                        }
    267                    } else {
    268                        LOG.logWarning( "no service available for layer: " + layers[i--].getName() );
    269                    }
    270
    271                }
    272            } catch ( Exception shouldneverhappen ) {
    273                shouldneverhappen.printStackTrace();
    274            }
    275
    276            return list.toArray( new String[list.size()] );
    277
    278        }
    279
    280        /**
    281         * creates the GetMap basic requests required by the JSP page assigned to the MapViewPortlet.
    282         *
    283         * @param vc
    284         * @return the GetMap basic requests
    285         */
    286        public static String[] createBaseRequests( ViewContext vc ) {
    287
    288            return createBaseRequests( vc, null, null, null );
    289
    290        }
    291
    292    }