    001    //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/branches/2.4_testing/src/org/deegree/portal/standard/wmps/PrintWMPSListener.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.standard.wmps;
    037
    038    import java.io.IOException;
    039    import java.io.InputStream;
    040    import java.net.URL;
    041    import java.util.ArrayList;
    042    import java.util.Collections;
    043    import java.util.Iterator;
    044    import java.util.List;
    045    import java.util.Map;
    046
    047    import org.deegree.enterprise.control.ajax.AbstractListener;
    048    import org.deegree.enterprise.control.ajax.ResponseHandler;
    049    import org.deegree.enterprise.control.ajax.WebEvent;
    050    import org.deegree.framework.log.ILogger;
    051    import org.deegree.framework.log.LoggerFactory;
    052    import org.deegree.framework.util.HttpUtils;
    053    import org.deegree.framework.xml.NamespaceContext;
    054    import org.deegree.framework.xml.XMLFragment;
    055    import org.deegree.framework.xml.XMLTools;
    056    import org.deegree.i18n.Messages;
    057    import org.deegree.model.spatialschema.Envelope;
    058    import org.deegree.model.spatialschema.GeometryFactory;
    059    import org.deegree.model.spatialschema.Point;
    060    import org.deegree.ogcbase.CommonNamespaces;
    061    import org.deegree.portal.Constants;
    062    import org.deegree.portal.context.Layer;
    063    import org.deegree.portal.context.ViewContext;
    064    import org.w3c.dom.Element;
    065    import org.xml.sax.SAXException;
    066
    067    /**
    068     * TODO add class documentation here
    069     *
    070     * @author <a href="mailto:name@deegree.org">Andreas Poth</a>
    071     * @author last edited by: $Author: apoth $
    072     *
    073     * @version $Revision: 24932 $, $Date: 2010-06-17 20:16:34 +0200 (Do, 17. Jun 2010) $
    074     */
    075    public class PrintWMPSListener extends AbstractListener {
    076
    077        private static final ILogger LOG = LoggerFactory.getLogger( PrintWMPSListener.class );
    078
    079        private static NamespaceContext nsc = CommonNamespaces.getNamespaceContext();
    080
    081        /*
    082         * (non-Javadoc)
    083         *
    084         * @see
    085         * org.deegree.enterprise.control.ajax.AbstractListener#actionPerformed(org.deegree.enterprise.control.ajax.WebEvent
    086         * , org.deegree.enterprise.control.ajax.ResponseHandler)
    087         */
    088        @SuppressWarnings("unchecked")
    089        public void actionPerformed( WebEvent event, ResponseHandler responseHandler )
    090                                throws IOException {
    091            String wmpsAddr = getInitParameter( "WMPS" );
    092            List<Layer> visibleLayers = getVisibleLayers( event );
    093            // read parameters from request
    094            Map<String, Object> parameters = event.getParameter();
    095            int dpi = Integer.parseInt( (String) parameters.get( "dpi" ) );
    096            boolean legend = new Boolean( (String) parameters.get( "legend" ) );
    097            boolean scaleBar = new Boolean( (String) parameters.get( "scale" ) );
    098            String template = (String) parameters.get( "template" );
    099            String email = (String) parameters.get( "email" );
    100            int scaleValue = Integer.parseInt( (String) parameters.get( "scaleValue" ) );
    101            String tmp = (String) parameters.get( "bbox" );
    102            Envelope bbox = GeometryFactory.createEnvelope( tmp, null );
    103            String srs = (String) parameters.get( "srs" );
    104            List<Map<String, String>> fields = (List<Map<String, String>>) parameters.get( "fields" );
    105            Map<String, String> fieldMap = (Map<String, String>) fields.get( 0 );
    106
    107            // create PrintMap request using printmap_template.xml as template
    108            XMLFragment printMap = createRequest( visibleLayers, dpi, legend, scaleBar, template, email, scaleValue, bbox,
    109                                                  srs, fieldMap );
    110
    111            LOG.logDebug( printMap.getAsPrettyString() );
    112
    113            // reading initial response from WMPS
    114            InputStream is = HttpUtils.performHttpPost( wmpsAddr, printMap.getAsString(), timeout, null, null, "text/xml",
    115                                                        null, null ).getResponseBodyAsStream();
    116            XMLFragment xml = new XMLFragment();
    117            try {
    118                xml.load( is, wmpsAddr );
    119            } catch ( Exception e ) {
    120                LOG.logError( e );
    121            }
    122            if ( CommonNamespaces.DEEGREEWMPS.toASCIIString().equals( xml.getRootElement().getNamespaceURI() ) ) {
    123                responseHandler.writeAndClose( Messages.getMessage( "IGEO_STD_WMPS_REQ_RECEIVED" ) );
    124            } else {
    125                LOG.logError( xml.getAsPrettyString() );
    126                responseHandler.writeAndClose( Messages.getMessage( "IGEO_STD_WMPS_REQ_ERROR" ) );
    127            }
    128        }
    129
    130        /**
    131         * @param visibleLayers
    132         * @param dpi
    133         * @param legend
    134         * @param scaleBar
    135         * @param template
    136         * @param email
    137         * @param scaleValue
    138         * @param bbox
    139         * @param srs
    140         * @param fieldMap
    141         * @return
    142         * @throws IOException
    143         */
    144        private XMLFragment createRequest( List<Layer> visibleLayers, int dpi, boolean legend, boolean scaleBar,
    145                                           String template, String email, int scaleValue, Envelope bbox, String srs,
    146                                           Map<String, String> fieldMap )
    147                                throws IOException {
    148            URL url = PrintWMPSListener.class.getResource( "printmap_template.xml" );
    149            XMLFragment printMap = null;
    150            try {
    151                printMap = new XMLFragment( url );
    152            } catch ( SAXException e ) {
    153                LOG.logError( e );
    154                throw new IOException( "can not parse PrintMap template" );
    155            }
    156
    157            try {
    158                // set bounding box and SRS
    159                Element element = (Element) XMLTools.getRequiredNode( printMap.getRootElement(),
    160                                                                      "deegreewmps:Center/gml:pos", nsc );
    161                Point center = bbox.getCentroid();
    162                XMLTools.setNodeValue( element, center.getX() + " " + center.getY() );
    163                element = (Element) XMLTools.getRequiredNode( printMap.getRootElement(), "deegreewmps:Center", nsc );
    164                element.setAttribute( "srsName", srs );
    165
    166                element = (Element) XMLTools.getRequiredNode( printMap.getRootElement(), "deegreewmps:ScaleDenominator",
    167                                                              nsc );
    168                XMLTools.setNodeValue( element, Integer.toString( scaleValue ) );
    169
    170                element = (Element) XMLTools.getRequiredNode( printMap.getRootElement(), "deegreewmps:Legend", nsc );
    171                XMLTools.setNodeValue( element, Boolean.toString( legend ) );
    172
    173                element = (Element) XMLTools.getRequiredNode( printMap.getRootElement(), "deegreewmps:ScaleBar", nsc );
    174                XMLTools.setNodeValue( element, Boolean.toString( scaleBar ) );
    175
    176                element = (Element) XMLTools.getRequiredNode( printMap.getRootElement(), "deegreewmps:Template", nsc );
    177                XMLTools.setNodeValue( element, template );
    178
    179                element = (Element) XMLTools.getRequiredNode( printMap.getRootElement(), "deegreewmps:EMailAddress", nsc );
    180                XMLTools.setNodeValue( element, email );
    181
    182                element = (Element) XMLTools.getRequiredNode( printMap.getRootElement(), "deegreewmps:DPI", nsc );
    183                XMLTools.setNodeValue( element, Integer.toString( dpi ) );
    184
    185                // set layers to be printed
    186                element = (Element) XMLTools.getRequiredNode( printMap.getRootElement(), "deegreewmps:Layers", nsc );
    187                for ( Layer layer : visibleLayers ) {
    188                    Element nl = XMLTools.appendElement( element, CommonNamespaces.SLDNS, "NamedLayer" );
    189                    Element l = XMLTools.appendElement( nl, CommonNamespaces.SLDNS, "Name" );
    190                    XMLTools.setNodeValue( l, layer.getName() );
    191                    Element nst = XMLTools.appendElement( nl, CommonNamespaces.SLDNS, "NamedStyle" );
    192                    Element st = XMLTools.appendElement( nst, CommonNamespaces.SLDNS, "Name" );
    193                    XMLTools.setNodeValue( st, layer.getStyleList().getCurrentStyle().getName() );
    194                }
    195
    196                // set free defined text areas
    197                element = (Element) XMLTools.getRequiredNode( printMap.getRootElement(), "deegreewmps:TextAreas", nsc );
    198                Iterator<String> iter = fieldMap.keySet().iterator();
    199                while ( iter.hasNext() ) {
    200                    String key = (String) iter.next();
    201                    String value = fieldMap.get( key );
    202                    if ( value != null && value.trim().length() > 0 ) {
    203                        Element ta = XMLTools.appendElement( element, CommonNamespaces.DEEGREEWMPS, "TextArea" );
    204                        Element name = XMLTools.appendElement( ta, CommonNamespaces.DEEGREEWMPS, "Name" );
    205                        XMLTools.setNodeValue( name, key );
    206                        Element text = XMLTools.appendElement( ta, CommonNamespaces.DEEGREEWMPS, "Text" );
    207                        XMLTools.setNodeValue( text, value );
    208                    }
    209                }
    210            } catch ( Exception e ) {
    211                LOG.logError( e );
    212                throw new IOException( "can not set request values" );
    213            }
    214
    215            return printMap;
    216        }
    217
    218        private List<Layer> getVisibleLayers( WebEvent event ) {
    219            ViewContext vc = (ViewContext) event.getSession().getAttribute( Constants.CURRENTMAPCONTEXT );
    220            Layer[] layers = vc.getLayerList().getLayers();
    221            List<Layer> visibleLayers = new ArrayList<Layer>();
    222            for ( Layer layer : layers ) {
    223                if ( !layer.isHidden() ) {
    224                    visibleLayers.add( layer );
    225                }
    226            }
    227            Collections.reverse( visibleLayers );
    228            return visibleLayers;
    229        }
    230
    231    }