    001    //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/branches/2.4_testing/src/org/deegree/portal/portlet/modules/wfs/actions/portlets/AnnotationPortletPerform.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.wfs.actions.portlets;
    037
    038    import java.net.URLDecoder;
    039    import java.sql.Connection;
    040    import java.sql.Date;
    041    import java.sql.Driver;
    042    import java.sql.DriverManager;
    043    import java.sql.PreparedStatement;
    044    import java.sql.SQLException;
    045
    046    import javax.servlet.ServletContext;
    047    import javax.servlet.http.HttpServletRequest;
    048
    049    import org.apache.jetspeed.portal.Portlet;
    050    import org.deegree.datatypes.Types;
    051    import org.deegree.enterprise.WebUtils;
    052    import org.deegree.framework.log.ILogger;
    053    import org.deegree.framework.log.LoggerFactory;
    054    import org.deegree.framework.util.StringTools;
    055    import org.deegree.portal.PortalException;
    056
    057    /**
    058     *
    059     *
    060     *
    061     * @version $Revision: 18195 $
    062     * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a>
    063     * @author last edited by: $Author: mschneider $
    064     *
    065     * @version 1.0. $Revision: 18195 $, $Date: 2009-06-18 17:55:39 +0200 (Do, 18. Jun 2009) $
    066     *
    067     * @since 2.0
    068     */
    069    public class AnnotationPortletPerform extends WFSClientPortletPerform {
    070
    071        private static final ILogger LOG = LoggerFactory.getLogger( AnnotationPortletPerform.class );
    072
    073        /**
    074         *
    075         * @param request
    076         * @param portlet
    077         * @param servletContext
    078         */
    079        public AnnotationPortletPerform( HttpServletRequest request, Portlet portlet, ServletContext servletContext ) {
    080            super( request, portlet, servletContext );
    081        }
    082
    083        /**
    084         * performs a transaction against a WFS-T or a database. The backend type to be used by a
    085         * transaction depends on a portlets initParameters.
    086         *
    087         * @param user
    088         * @throws PortalException
    089         */
    090        public void doTransaction( String user )
    091                                throws PortalException {
    092            if ( getInitParam( "driver" ) != null ) {
    093                doDatabaseTransaction( user );
    094            } else {
    095                // handle transaction against a WFS
    096                super.doTransaction();
    097            }
    098        }
    099
    100        /**
    101         * performs a direct transaction into a database
    102         *
    103         * @param data
    104         */
    105        private void doDatabaseTransaction( String user )
    106                                throws PortalException {
    107
    108            Connection con = null;
    109            LOG.logDebug( "connecting database for insert ... " );
    110            try {
    111                Driver drv = (Driver) Class.forName( getInitParam( "driver" ) ).newInstance();
    112                DriverManager.registerDriver( drv );
    113
    114                con = DriverManager.getConnection( getInitParam( "url" ), getInitParam( "user" ), getInitParam( "password" ) );
    115            } catch ( SQLException e ) {
    116                LOG.logError( "could not establish database connection: " + getInitParam( "url" ), e );
    117                throw new PortalException( "could not establish database connection: " + getInitParam( "url" ), e );
    118            } catch ( Exception e ) {
    119                LOG.logError( "could not initialize driver class: " + getInitParam( "driver" ), e );
    120                throw new PortalException( "could not initialize driver class: " + getInitParam( "driver" ), e );
    121            }
    122
    123            PreparedStatement stmt = null;
    124            try {
    125                if ( "INSERT".equals( parameter.get( "OPERATION" ) ) ) {
    126                    String sql = createGeometryFragment( getInitParam( "SQLInsertTemplate" ) );
    127                    stmt = con.prepareStatement( sql );
    128                    LOG.logDebug( "perform insert " + sql );
    129                } else {
    130                    throw new PortalException( "not a supported Operation: " + parameter.get( "OPERATION" ) );
    131                }
    132                stmt.setDate( 1, new Date( System.currentTimeMillis() ) );
    133
    134                String charset = WebUtils.readCharsetFromContentType( request );
    135
    136                String title = (String) parameter.get( "TITLE" );
    137                if ( title == null ) {
    138                    stmt.setNull( 2, Types.VARCHAR );
    139                } else {
    140                    stmt.setString( 2, URLDecoder.decode( title, charset ) );
    141                }
    142                String annotation = (String) parameter.get( "ANNOTATION" );
    143                if ( annotation == null ) {
    144                    stmt.setNull( 3, Types.VARCHAR );
    145                } else {
    146                    stmt.setString( 3, URLDecoder.decode( annotation, charset ) );
    147                }
    148                String category = (String) parameter.get( "CATEGORY" );
    149                if ( category == null ) {
    150                    stmt.setNull( 4, Types.NUMERIC );
    151                } else {
    152                    stmt.setInt( 4, -1 );
    153                }
    154
    155                stmt.setString( 5, user );
    156                // annotation location
    157                stmt.execute();
    158            } catch ( SQLException e ) {
    159                try {
    160                    stmt.close();
    161                } catch ( Exception e1 ) {
    162                }
    163                try {
    164                    con.close();
    165                } catch ( Exception e1 ) {
    166                }
    167                LOG.logError( "could not perform insert operation ", e );
    168                throw new PortalException( "could not perform insert operation ", e );
    169            } catch ( Exception e ) {
    170                e.printStackTrace();
    171                LOG.logError( "could not tranform geometry", e );
    172                throw new PortalException( "could not tranform geometry", e );
    173            }
    174
    175        }
    176
    177        /**
    178         *
    179         * @return
    180         */
    181        private String createGeometryFragment( String sql ) {
    182            sql = StringTools.replace( sql, "$SRID", getInitParam( "srid" ), false );
    183            sql = StringTools.replace( sql, "$X", (String) parameter.get( "X" ), false );
    184            sql = StringTools.replace( sql, "$Y", (String) parameter.get( "Y" ), false );
    185
    186            return sql;
    187        }
    188    }