    001    //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/branches/2.4_testing/src/org/deegree/portal/portlet/modules/map/actions/portlets/ScaleChooserPortletPerform.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.portal.portlet.modules.map.actions.portlets;
    038
    039    import java.awt.Rectangle;
    040    import java.util.List;
    041
    042    import javax.servlet.ServletContext;
    043    import javax.servlet.http.HttpServletRequest;
    044    import javax.servlet.http.HttpSession;
    045
    046    import org.apache.jetspeed.portal.Portlet;
    047    import org.deegree.framework.log.ILogger;
    048    import org.deegree.framework.log.LoggerFactory;
    049    import org.deegree.framework.util.MapUtils;
    050    import org.deegree.model.crs.CoordinateSystem;
    051    import org.deegree.model.spatialschema.Envelope;
    052    import org.deegree.model.spatialschema.GeometryFactory;
    053    import org.deegree.model.spatialschema.Point;
    054    import org.deegree.portal.PortalException;
    055    import org.deegree.portal.context.ViewContext;
    056    import org.deegree.portal.portlet.modules.actions.IGeoPortalPortletPerform;
    057
    058    /**
    059     * This Perform class takes care of changing the WMC's bounding box based on a scale paramter. The
    060     * parameter is passed in the request. The paramter name is defined by the static member
    061     * NEW_SCALE_VALUE
    062     *
    063     * @author <a href="mailto:taddei@lat-lon.de">Ugo Taddei</a>
    064     * @author last edited by: $Author: mschneider $
    065     *
    066     * @version $Revision: 18195 $, $Date: 2009-06-18 17:55:39 +0200 (Do, 18. Jun 2009) $
    067     */
    068
    069    public class ScaleChooserPortletPerform extends IGeoPortalPortletPerform {
    070
    071        private static final ILogger LOG = LoggerFactory.getLogger( ScaleChooserPortletPerform.class );
    072
    073        public static final String REQUESTED_SCALE_VALUE = "REQUESTED_SCALE_VALUE";
    074
    075        public static final String AVAILABLE_SCALES = "AVAILABLE_SCALES";
    076
    077        // TODO make pixel size a property
    078        public static final double DEFAULT_PIXEL_SIZE = 0.00028;
    079
    080        /**
    081         * private constructor
    082         *
    083         * @param request
    084         * @param portlet
    085         * @param sc
    086         */
    087        ScaleChooserPortletPerform( HttpServletRequest request, Portlet portlet, ServletContext sc ) {
    088            super( request, portlet, sc );
    089        }
    090
    091        public void buildNormalContext()
    092                                throws PortalException {
    093            super.buildNormalContext();
    094            readInitParameter();
    095        }
    096
    097        /**
    098         * TODO reads the init parameters of the portlet and build the scale list
    099         *
    100         */
    101        private void readInitParameter() {
    102
    103            HttpSession ses = request.getSession();
    104            if ( ses.getAttribute( AVAILABLE_SCALES ) == null ) {
    105                String list = getInitParam( AVAILABLE_SCALES );
    106                if ( list == null ) {
    107                    list = "10000;25000;50000;100000;500000;1000000";
    108                }
    109                String[] tmp = list.split( ";" );
    110
    111                ses.setAttribute( AVAILABLE_SCALES, tmp );
    112            }
    113
    114        }
    115
    116        /**
    117         * This method changes the scale of the current bounding box
    118         *
    119         * @throws PortalException
    120         *
    121         */
    122        void doChangeScale()
    123                                throws PortalException {
    124
    125            String newScale = parameter.get( REQUESTED_SCALE_VALUE );
    126
    127            if ( newScale == null ) {
    128                // throw new PortalException( "No scale available. Missing " + REQUESTED_SCALE_VALUE
    129                // + " parameter" );
    130                return;
    131            }
    132
    133            request.setAttribute( REQUESTED_SCALE_VALUE, newScale );
    134
    135            ViewContext vc = getCurrentViewContext( getInitParam( INIT_MAPPORTLETID ) );
    136            if ( vc == null ) {
    137                LOG.logDebug( "No VC with PID '" + getInitParam( INIT_MAPPORTLETID ) + "'" );
    138                throw new PortalException( "no valid view context available through users session" );
    139            }
    140
    141            // rad need pars from ViewCOntext
    142            Point p0 = vc.getGeneral().getBoundingBox()[0];
    143            Point p1 = vc.getGeneral().getBoundingBox()[1];
    144            CoordinateSystem cs = p0.getCoordinateSystem();
    145
    146            Rectangle window = vc.getGeneral().getWindow();
    147
    148            Envelope env = GeometryFactory.createEnvelope( p0.getX(), p0.getY(), p1.getX(), p1.getY(), cs );
    149
    150            try {
    151                double reqScale = Double.parseDouble( newScale );
    152
    153                double currentScale = MapUtils.calcScale( window.width, window.height, env, cs, DEFAULT_PIXEL_SIZE );
    154
    155                // calc new envelope
    156                env = MapUtils.scaleEnvelope( env, currentScale, reqScale );
    157
    158                vc.getGeneral().setBoundingBox( env );
    159                if ( parameter.get( "MODE" ) != null ) {
    160                    vc.getGeneral().getExtension().setMode( parameter.get( "MODE" ) );
    161                }
    162
    163                List<Envelope> history = (List<Envelope>) request.getSession().getAttribute( SESSION_HISTORY );
    164                int p = (Integer) request.getSession().getAttribute( SESSION_HISTORYPOSITION );
    165                Envelope current = history.get( p );
    166                if ( current == null || !current.equals( env ) ) {
    167                    p++;
    168                    history.add( p, env );
    169                    request.getSession().setAttribute( IGeoPortalPortletPerform.SESSION_HISTORYPOSITION, p );
    170                }
    171
    172            } catch ( Exception e ) {
    173                LOG.logError( e.getMessage(), e );
    174                throw new PortalException( e.getMessage() );
    175            }
    176        }
    177
    178    }