001    //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/branches/2.3_testing/src/org/deegree/portal/portlet/modules/map/actions/portlets/MapWindowPortletAction.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.map.actions.portlets;
037    
038    import java.util.Iterator;
039    import java.util.List;
040    
041    import javax.servlet.http.HttpSession;
042    
043    import org.apache.jetspeed.modules.actions.portlets.JspPortletAction;
044    import org.apache.jetspeed.om.profile.Controller;
045    import org.apache.jetspeed.om.profile.Entry;
046    import org.apache.jetspeed.om.profile.Portlets;
047    import org.apache.jetspeed.portal.Portlet;
048    import org.apache.jetspeed.portal.PortletConfig;
049    import org.apache.turbine.util.RunData;
050    import org.deegree.framework.util.StringTools;
051    import org.deegree.model.spatialschema.Envelope;
052    import org.deegree.portal.portlet.modules.actions.AbstractPortletPerform;
053    import org.deegree.portal.portlet.portlets.WebMapPortlet;
054    
055    /**
056     *
057     *
058     * @version $Revision: 18195 $
059     * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a>
060     * @author last edited by: $Author: mschneider $
061     *
062     * @version 1.0. $Revision: 18195 $, $Date: 2009-06-18 17:55:39 +0200 (Do, 18. Jun 2009) $
063     *
064     * @since 2.0
065     */
066    public class MapWindowPortletAction extends JspPortletAction {
067    
068        protected void buildConfigureContext( Portlet portlet, RunData rundata )
069                                throws Exception {
070            // TODO
071            super.buildConfigureContext( portlet, rundata );
072        }
073    
074        /**
075         * called when portlet is maximized
076         *
077         * @param portlet
078         * @param rundata
079         */
080        protected void buildMaximizedContext( Portlet portlet, RunData rundata )
081                                throws Exception {
082            super.buildMaximizedContext( portlet, rundata );
083    
084            try {
085                MapWindowPortletPerform mwpp = new MapWindowPortletPerform( rundata.getRequest(), portlet,
086                                                                            rundata.getServletContext() );
087    
088                // common to all operations
089                mwpp.setMode();
090    
091                // set map size to 95% of the window size
092                rundata.getRequest().setAttribute( "WIDTH", new Integer( -95 ) );
093            } catch ( Exception e ) {
094                e.printStackTrace();
095            }
096    
097        }
098    
099        protected void buildNormalContext( Portlet portlet, RunData rundata )
100                                throws Exception {
101    
102            try {
103                MapWindowPortletPerform mwpp = new MapWindowPortletPerform( rundata.getRequest(), portlet,
104                                                                            rundata.getServletContext() );
105    
106                mwpp.buildNormalContext();
107                // common to all operations
108                mwpp.setMode();
109    
110                HttpSession ses = rundata.getRequest().getSession();
111                if ( ses.getAttribute( AbstractPortletPerform.SESSION_HOME ) == null ) {
112                    Envelope homeEnv = ( (WebMapPortlet) portlet ).getHome();
113                    ses.setAttribute( AbstractPortletPerform.SESSION_HOME, homeEnv );
114    
115                    // make sure first envelope in history is the home envelope
116                    List<Envelope> envelopes = (List<Envelope>) ses.getAttribute( MapWindowPortletPerform.SESSION_HISTORY );
117                    if ( envelopes != null ) {
118                        envelopes.add( 0, homeEnv );
119                    }
120    
121                }
122    
123                PortletConfig pc = portlet.getPortletConfig();
124                // set map with to current portlet width if responsible init parameter is set
125                if ( pc.getInitParameter( MapWindowPortletPerform.INIT_SCALEMAPTOPORTLETSIZE ) != null
126                     && pc.getInitParameter( MapWindowPortletPerform.INIT_SCALEMAPTOPORTLETSIZE ).equalsIgnoreCase( "true" ) ) {
127                    int width = calcPortletWidth( portlet, rundata );
128                    rundata.getRequest().setAttribute( "WIDTH", Integer.toString( width ) );
129                }
130    
131                // this is set here because it's jetspeed specific stuff
132                String fiTarget = portlet.getPortletConfig().getInitParameter(
133                                                                               MapWindowPortletPerform.INIT_FEATUREINFOTARGETPORTLET );
134    
135                String[] tmp = StringTools.toArray( fiTarget, ";", false );
136                if ( tmp.length > 1 ) {
137                    rundata.getRequest().setAttribute( "FEATUREINFOTARGETPANE", tmp[0] );
138                    rundata.getRequest().setAttribute( "FEATUREINFOTARGETPORTLET", tmp[1] );
139                } else {
140                    rundata.getRequest().setAttribute( "FEATUREINFOTARGETPANE", "" );
141                    rundata.getRequest().setAttribute( "FEATUREINFOTARGETPORTLET", fiTarget );
142                }
143    
144            } catch ( Exception e ) {
145                e.printStackTrace();
146            }
147    
148        }
149    
150        /**
151         * returns the width of a portlet. If the size is given as a relative value the returned value i
152         * &lt; 0 an absolute size will be returned as positive value.
153         *
154         * @param portlet
155         * @param rundata
156         */
157        private int calcPortletWidth( Portlet portlet, RunData rundata ) {
158            int width = 0;
159            try {
160                Iterator iter = portlet.getInstance( rundata ).getDocument().getPortlets().getPortletsIterator();
161                boolean contains = false;
162                Portlets portlets = null;
163                int col = 0;
164                // find psml portlet that contains the portlet in question
165                do {
166                    portlets = (Portlets) iter.next();
167                    Iterator iter2 = portlets.getEntriesIterator();
168                    Entry entry = null;
169                    while ( entry == null && iter2.hasNext() ) {
170                        entry = (Entry) iter2.next();
171                        if ( !entry.getId().equals( portlet.getID() ) ) {
172                            entry = null;
173                        } else {
174                            String tmp = entry.getLayout().getParameterValue( "column" );
175                            col = Integer.parseInt( tmp );
176                        }
177                    }
178                    contains = entry != null;
179                } while ( !contains && iter.hasNext() );
180    
181                Controller controller = portlets.getController();
182                String sizes = controller.getParameterValue( "sizes" );
183                if ( sizes != null ) {
184                    String[] tmp = StringTools.toArray( sizes, ",", false );
185                    if ( tmp[col].endsWith( "%" ) ) {
186                        // parse relative portlet width. a relative portlet width is
187                        // marked by a negative sign
188                        width = -1 * Integer.parseInt( tmp[col].substring( 0, tmp[col].length() - 1 ) );
189                    } else {
190                        // parse absolute portlet width.
191                        width = Integer.parseInt( tmp[col] );
192                    }
193                } else {
194                    // use 50% as default
195                    width = 50;
196                }
197            } catch ( Exception e2 ) {
198                e2.printStackTrace();
199            }
200            return width;
201        }
202    
203    }