001    //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/branches/2.3_testing/src/org/deegree/portal/portlet/modules/actions/IGeoPortalPortletPerform.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.portlet.modules.actions;
037    
038    import java.net.URL;
039    import java.util.HashMap;
040    import java.util.List;
041    import java.util.Map;
042    
043    import javax.servlet.ServletContext;
044    import javax.servlet.http.HttpServletRequest;
045    import javax.servlet.http.HttpSession;
046    
047    import org.apache.jetspeed.portal.Portlet;
048    import org.apache.turbine.util.RunData;
049    import org.deegree.framework.log.ILogger;
050    import org.deegree.framework.log.LoggerFactory;
051    import org.deegree.framework.util.StringTools;
052    import org.deegree.model.crs.CoordinateSystem;
053    import org.deegree.model.spatialschema.Envelope;
054    import org.deegree.model.spatialschema.GeometryFactory;
055    import org.deegree.model.spatialschema.Point;
056    import org.deegree.portal.context.ContextException;
057    import org.deegree.portal.context.Layer;
058    import org.deegree.portal.context.ViewContext;
059    import org.deegree.portal.context.WebMapContextFactory;
060    import org.deegree.security.drm.model.User;
061    
062    /**
063     *
064     *
065     * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a>
066     * @author last edited by: $Author: mschneider $
067     *
068     * @version $Revision: 18195 $, $Date: 2009-06-18 17:55:39 +0200 (Do, 18. Jun 2009) $
069     */
070    public class IGeoPortalPortletPerform extends AbstractPortletPerform {
071    
072        private static final ILogger LOG = LoggerFactory.getLogger( IGeoPortalPortletPerform.class );
073    
074        /**
075         * A String denoting BBOX
076         */
077        public static final String PARAM_BBOX = "BBOX";
078    
079        /**
080         * A String denoting LAYER
081         */
082        public static final String PARAM_LAYER = "LAYER";
083    
084        /**
085         * A String denoting STYLE
086         */
087        public static final String PARAM_STYLE = "STYLE";
088    
089        /**
090         * A String denoting MAPMODE
091         */
092        public static final String PARAM_MAPMODE = "MAPMODE";
093    
094        /**
095         * A String denoting SESSIONID
096         */
097        public static final String PARAM_SESSIONID = "SESSIONID";
098    
099        /**
100         * A String denoting HISTORY
101         */
102        public static final String SESSION_HISTORY = "HISTORY";
103    
104        /**
105         * A String denoting HISTORYPOSITION
106         */
107        public static final String SESSION_HISTORYPOSITION = "HISTORYPOSITION";
108    
109        protected ServletContext sc = null;
110    
111        protected static Map<String, ViewContext> vcMap = new HashMap<String, ViewContext>( 100 );
112    
113    
114        /**
115         * @param request
116         * @param portlet
117         * @param sc context
118         */
119        public IGeoPortalPortletPerform( HttpServletRequest request, Portlet portlet, ServletContext sc ) {
120            super( request, portlet );
121            this.sc = sc;
122        }
123    
124        /**
125         * updates the view context with the current bounding box, the list of visible layers and the
126         * current map mode
127         *
128         */
129        public void updateContext() {
130    
131            // update the view contexts bounding box with the current one.
132            setBoundingBoxFromBBOXParam();
133    
134            // update the view contexts list of visible layers
135            setLayers();
136    
137            // update the map mode
138            setMode();
139    
140        }
141    
142        /**
143         * sets layers of the view context as visible or invisble depending on the incoming request
144         *
145         */
146        public void setLayers() {
147    
148            if ( portlet != null && parameter.get( PARAM_BBOX ) != null && parameter.get( "LAYERS" ) != null ) {
149                // just change layerlist if the request contains a BBOX parameter
150                // and at least one layer because other wise it will be the initial call;
151                ViewContext vc = getCurrentViewContext( portlet.getID() );
152                Layer[] layers = vc.getLayerList().getLayers();
153                String ly = parameter.get( "LAYERS" );
154                StringBuffer sb = new StringBuffer( 100 );
155                for ( int i = 0; i < layers.length; i++ ) {
156                    sb.append( layers[i].getName() ).append( '|' );
157                    sb.append( layers[i].getServer().getOnlineResource() );
158                    if ( ly.indexOf( sb.toString() ) > -1 ) {
159                        layers[i].setHidden( false );
160                    } else {
161                        layers[i].setHidden( true );
162                    }
163                    sb.delete( 0, sb.length() );
164                }
165    
166                setCurrentMapContext( vc, portlet.getID() );
167            }
168    
169        }
170    
171        /**
172         * writes the current map mode (if set) into the users WMC.
173         *
174         */
175        public void setMode() {
176            String mm = parameter.get( PARAM_MAPMODE );
177            if ( mm != null ) {
178                ViewContext vc = getCurrentViewContext( portlet.getID() );
179                vc.getGeneral().getExtension().setMode( mm );
180                setCurrentMapContext( vc, portlet.getID() );
181            }
182        }
183    
184        /**
185         * sets a new bounding box of the map read from the the request object passed when initialized
186         * an instance of <code>MapWindowPortletPerfom</code>
187         *
188         */
189        public void setBoundingBoxFromBBOXParam() {
190            String bbox = parameter.get( PARAM_BBOX );
191            setBoundingBox( bbox );
192        }
193    
194        /**
195         * sets a new bounding box of the map read from the the request object passed when initialized
196         * an instance of <code>MapWindowPortletPerfom</code>
197         *
198         * @param env
199         *            new bounding box
200         */
201        public void setBoundingBox( Envelope env ) {
202    
203            if ( portlet != null ) {
204                ViewContext vc = getCurrentViewContext( portlet.getID() );
205                if ( vc != null ) {
206                    CoordinateSystem crs = vc.getGeneral().getBoundingBox()[0].getCoordinateSystem();
207                    Point[] pt = new Point[2];
208                    pt[0] = GeometryFactory.createPoint( env.getMin().getX(), env.getMin().getY(), crs );
209                    pt[1] = GeometryFactory.createPoint( env.getMax().getX(), env.getMax().getY(), crs );
210                    try {
211                        vc.getGeneral().setBoundingBox( pt );
212                    } catch ( ContextException should_never_happen ) {
213                        //nottin
214                    }
215    
216    
217                    List<Envelope> history = (List<Envelope>) request.getSession().getAttribute( SESSION_HISTORY );
218                    int p = (Integer) request.getSession().getAttribute( SESSION_HISTORYPOSITION );
219                    Envelope current = history.get( p );
220                    if ( current == null || !current.equals( env ) ) {
221                        p++;
222                        history.add( p, env );
223                        request.getSession().setAttribute( IGeoPortalPortletPerform.SESSION_HISTORYPOSITION, p );
224                    }
225    
226                }
227                setCurrentMapContext( vc, portlet.getID() );
228            }
229        }
230    
231        /**
232         * the method expects a string with four comma seperated coordinate values. The created box will
233         * be written
234         *
235         * @param bbox
236         */
237        public void setBoundingBox( String bbox ) {
238    
239            if ( bbox != null ) {
240                double[] coords = StringTools.toArrayDouble( bbox, "," );
241                Envelope env = GeometryFactory.createEnvelope( coords[0], coords[1], coords[2], coords[3], null );
242                setBoundingBox( env );
243            }
244        }
245    
246        /**
247         * returns the current <@link ViewContext> read from the portlet session.
248         *
249         * @param session
250         * @param pid
251         *            MapWindowPortlet id
252         *
253         * @return the current ViewContext read from the portlet session.
254         */
255        public static ViewContext getCurrentViewContext( HttpSession session, String pid ) {
256            return (ViewContext) session.getAttribute( pid + '_' + CURRENT_WMC );
257        }
258    
259        /**
260         * returns the current <@link ViewContext> read from the portlet session.
261         *
262         * @param pid
263         *            MapWindowPortlet id
264         *
265         * @return the current ViewContext read from the portlet session.
266         */
267        public ViewContext getCurrentViewContext( String pid ) {
268            return getCurrentViewContext( request.getSession(), pid );
269        }
270    
271        /**
272         * sets the current MapContext to the users session
273         *
274         * @param session
275         * @param vc
276         * @param pid
277         */
278        public static void setCurrentMapContext( HttpSession session, ViewContext vc, String pid ) {
279            session.setAttribute( pid + '_' + CURRENT_WMC, vc );
280        }
281    
282        /**
283         * sets the current MapContext to the users session
284         *
285         * @param vc
286         * @param pid
287         */
288        public void setCurrentMapContext( ViewContext vc, String pid ) {
289            setCurrentMapContext( request.getSession(), vc, pid );
290        }
291    
292        /**
293         * writes the name of the current WMC into the users session
294         *
295         * @param session
296         * @param pid
297         * @param name
298         */
299        public static void setCurrentMapContextName( HttpSession session, String pid, String name ) {
300            session.setAttribute( pid + '_' + CURRENT_WMC_NAME, name );
301        }
302    
303        /**
304         * writes the name of the current WMC into the users session
305         *
306         * @param pid
307         * @param name
308         */
309        public void setCurrentMapContextName( String pid, String name ) {
310            setCurrentMapContextName( request.getSession(), pid, name );
311        }
312    
313        /**
314         * returns the name of the current WMC into the users session
315         *
316         * @param session
317         * @param pid
318         * @return the name of the current WMC into the users session
319         */
320        public static String getCurrentMapContextName( HttpSession session, String pid ) {
321            return (String) session.getAttribute( pid + '_' + CURRENT_WMC_NAME );
322        }
323    
324        /**
325         * returns the name of the current WMC into the users session
326         *
327         * @param pid
328         * @return the name of the current WMC into the users session
329         */
330        public String getCurrentMapContextName( String pid ) {
331            return getCurrentMapContextName( request.getSession(), pid );
332        }
333    
334        /**
335         * returns an instance of <@link ViewContext> read from the portlet session. If no instance is
336         * available <code>null</code> will be returned.
337         *
338         * @param session
339         * @param name
340         *            map context name/id
341         *
342         * @return an instance of ViewContext read from the portlet session. If no instance is available
343         *         <code>null</code> will be returned.
344         */
345        public ViewContext getNamedViewContext( HttpSession session, String name ) {
346            URL url = (URL) session.getAttribute( SESSION_VIEWCONTEXT + name );
347            ViewContext vc = null;
348            try {
349                if ( url != null ) {
350    
351                    RunData rundata = (RunData) request.getAttribute( "data" );
352                    if ( rundata != null ) {
353                        org.apache.turbine.om.security.User tu = rundata.getUser();
354    
355                        String username = tu.getUserName();
356                        String password = tu.getPassword();
357                        String firstname = tu.getFirstName();
358                        String lastname = tu.getLastName();
359                        String email = tu.getEmail();
360    
361                        User user = new User( 0, username, password, firstname, lastname, email, null );
362                        // User(int id, String name, String password, String
363                        // firstName,
364                        // String lastName, String emailAddress, SecurityRegistry
365                        // registry)
366                        String key = url.toExternalForm() + '|' + session.getId();
367                        if ( vcMap.get( key ) != null ) {
368                            vc = vcMap.get( key );
369                        } else {
370                            vc = WebMapContextFactory.createViewContext( url, user, null );
371                            vcMap.put( key, vc );
372                        }
373                    } else {
374                        String key = url.toExternalForm();
375                        if ( vcMap.get( key ) != null ) {
376                            vc = vcMap.get( key );
377                        } else {
378                            vc = WebMapContextFactory.createViewContext( url, null, null );
379                            vcMap.put( key, vc );
380                        }
381                    }
382                }
383            } catch ( Exception e ) {
384                // should never happen
385                LOG.logError( e.getMessage(), e );
386            }
387    
388            return vc;
389        }
390    
391        /**
392         * returns an instance of <@link ViewContext> read from the portlet session. If no instance is
393         * available <code>null</code> will be returned.
394         *
395         * @param name
396         *            map context name/id
397         *
398         * @return instance of ViewContext read from the portlet session. If no instance is available
399         *         <code>null</code> will be returned.
400         */
401        public ViewContext getNamedViewContext( String name ) {
402            return getNamedViewContext( request.getSession(), name );
403        }
404    
405        /**
406         * writes the URL to a WMC with a assigend name into a users session
407         *
408         * @param session
409         * @param name
410         * @param url
411         */
412        public static void setNameContext( HttpSession session, String name, URL url ) {
413            session.setAttribute( SESSION_VIEWCONTEXT + name, url );
414        }
415    
416        /**
417         * writes the URL to a WMC with a assigend name into a users session
418         *
419         * @param name
420         * @param url
421         */
422        public void setNameContext( String name, URL url ) {
423            setNameContext( request.getSession(), name, url );
424        }
425    
426    }