001    //$HeadURL: svn+ssh://rbezema@svn.wald.intevation.org/deegree/base/tags/2.1/src/org/deegree/portal/portlet/enterprise/WMCUpdateServlet.java $
002    /*----------------    FILE HEADER  ------------------------------------------
003    
004     This file is part of deegree.
005     Copyright (C) 2001-2007 by:
006     EXSE, Department of Geography, University of Bonn
007     http://www.giub.uni-bonn.de/deegree/
008     lat/lon GmbH
009     http://www.lat-lon.de
010    
011     This library is free software; you can redistribute it and/or
012     modify it under the terms of the GNU Lesser General Public
013     License as published by the Free Software Foundation; either
014     version 2.1 of the License, or (at your option) any later version.
015    
016     This library is distributed in the hope that it will be useful,
017     but WITHOUT ANY WARRANTY; without even the implied warranty of
018     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
019     Lesser General Public License for more details.
020    
021     You should have received a copy of the GNU Lesser General Public
022     License along with this library; if not, write to the Free Software
023     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
024    
025     Contact:
026    
027     Andreas Poth
028     lat/lon GmbH
029     Aennchenstr. 19
030     53177 Bonn
031     Germany
032     E-Mail: poth@lat-lon.de
033    
034     Prof. Dr. Klaus Greve
035     Department of Geography
036     University of Bonn
037     Meckenheimer Allee 166
038     53115 Bonn
039     Germany
040     E-Mail: greve@giub.uni-bonn.de
041    
042     ---------------------------------------------------------------------------*/
043    package org.deegree.portal.portlet.enterprise;
044    
045    import java.io.IOException;
046    import java.util.List;
047    import java.util.Map;
048    
049    import javax.servlet.ServletException;
050    import javax.servlet.http.HttpServlet;
051    import javax.servlet.http.HttpServletRequest;
052    import javax.servlet.http.HttpServletResponse;
053    
054    import org.deegree.framework.log.ILogger;
055    import org.deegree.framework.log.LoggerFactory;
056    import org.deegree.framework.util.KVP2Map;
057    import org.deegree.framework.util.StringTools;
058    import org.deegree.model.crs.CoordinateSystem;
059    import org.deegree.model.spatialschema.Envelope;
060    import org.deegree.model.spatialschema.GeometryFactory;
061    import org.deegree.model.spatialschema.Point;
062    import org.deegree.portal.context.ContextException;
063    import org.deegree.portal.context.Layer;
064    import org.deegree.portal.context.ViewContext;
065    import org.deegree.portal.portlet.modules.actions.IGeoPortalPortletPerform;
066    
067    /**
068     * The servlet will be used to update a map model (Web Map Context) in the background (using an
069     * invisible iframe) from an iGeoPortal Portlet Edition
070     * 
071     * 
072     * @version $Revision: 7717 $
073     * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a>
074     * @author last edited by: $Author: apoth $
075     * 
076     * @version 1.0. $Revision: 7717 $, $Date: 2007-07-04 10:35:31 +0200 (Mi, 04 Jul 2007) $
077     * 
078     * @since 2.0
079     */
080    public class WMCUpdateServlet extends HttpServlet {
081    
082        private static final long serialVersionUID = 2927537039728672671L;
083    
084        private static final ILogger LOG = LoggerFactory.getLogger( WMCUpdateServlet.class );
085    
086        @Override
087        public void init()
088                                throws ServletException {
089            super.init();
090        }
091    
092        @Override
093        protected void doGet( HttpServletRequest request, HttpServletResponse response )
094                                throws ServletException, IOException {
095    
096            doPost( request, response );
097        }
098    
099        @Override
100        protected void doPost( HttpServletRequest request, HttpServletResponse response )
101                                throws ServletException, IOException {
102    
103            Map parameter = KVP2Map.toMap( request );
104    
105            String mm = (String) parameter.get( "MAPPORTLET" );
106            LOG.logDebug( "parameter: " + parameter );
107            IGeoPortalPortletPerform igeo = new IGeoPortalPortletPerform( request, null, getServletContext() );
108            try {
109                if ( mm != null ) {
110                    // update bbox
111                    ViewContext vc = igeo.getCurrentViewContext( mm );
112    
113                    String bbox = (String) parameter.get( IGeoPortalPortletPerform.PARAM_BBOX );
114                    if ( bbox != null && vc != null ) {
115                        double[] coords = StringTools.toArrayDouble( bbox, "," );
116                        CoordinateSystem crs = vc.getGeneral().getBoundingBox()[0].getCoordinateSystem();
117                        Point[] pt = new Point[2];
118                        pt[0] = GeometryFactory.createPoint( coords[0], coords[1], crs );
119                        pt[1] = GeometryFactory.createPoint( coords[2], coords[3], crs );
120                        try {
121                            vc.getGeneral().setBoundingBox( pt );
122                        } catch ( ContextException should_never_happen ) {
123                        }
124                        List<Envelope> history = (List<Envelope>) request.getSession().getAttribute(
125                                                                                                     IGeoPortalPortletPerform.SESSION_HISTORY );
126                        int p = (Integer) request.getSession().getAttribute(
127                                                                             IGeoPortalPortletPerform.SESSION_HISTORYPOSITION );
128                        Envelope current = history.get( p );
129                        Envelope env = GeometryFactory.createEnvelope( coords[0], coords[1], coords[2], coords[3], null );
130                        if ( current == null || !current.equals( env ) ) {
131                            p++;
132                            history.add( p, env );
133                            request.getSession().setAttribute( IGeoPortalPortletPerform.SESSION_HISTORYPOSITION, p );
134                        }
135                    }
136    
137                    if ( bbox != null && parameter.get( "LAYERS" ) != null ) {
138                        // just change layerlist if the request contains a BBOX parameter
139                        // and at least one layer because other wise it will be the initial call;
140                        Layer[] layers = vc.getLayerList().getLayers();
141                        String ly = (String) parameter.get( "LAYERS" );
142                        StringBuffer sb = new StringBuffer( 100 );
143                        for ( int i = 0; i < layers.length; i++ ) {
144                            sb.append( layers[i].getName() ).append( '|' );
145                            sb.append( layers[i].getServer().getOnlineResource() );
146                            if ( ly.indexOf( sb.toString() ) > -1 ) {
147                                layers[i].setHidden( false );
148                            } else {
149                                layers[i].setHidden( true );
150                            }
151                            sb.delete( 0, sb.length() );
152                        }
153                        igeo.setCurrentMapContext( vc, mm );
154                    }
155                } else {
156                    System.out.println( "no mapmodel defined in request; ensure that parameter 'MAPPORTLET' is defined!" );
157                }
158            } catch ( Exception e ) {
159                e.printStackTrace();
160                LOG.logError( "could not update WMC: " + e.getMessage(), e );
161            }
162            request.setAttribute( "ACTION", getInitParameter( "ACTION" ) );
163    
164            request.getRequestDispatcher( "igeoportal/enterprise.jsp" ).forward( request, response );
165        }
166    
167    }