001    //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/branches/2.3_testing/src/org/deegree/io/datastore/RequestCRSTransformationTrigger.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.io.datastore;
037    
038    import org.deegree.datatypes.QualifiedName;
039    import org.deegree.framework.log.ILogger;
040    import org.deegree.framework.log.LoggerFactory;
041    import org.deegree.framework.trigger.Trigger;
042    import org.deegree.framework.trigger.TriggerException;
043    import org.deegree.io.datastore.schema.MappedFeatureType;
044    import org.deegree.model.crs.CRSTransformationException;
045    import org.deegree.model.crs.CoordinateSystem;
046    import org.deegree.model.crs.GeoTransformer;
047    import org.deegree.model.filterencoding.ComplexFilter;
048    import org.deegree.model.filterencoding.Filter;
049    import org.deegree.model.filterencoding.FilterTools;
050    import org.deegree.model.filterencoding.SpatialOperation;
051    import org.deegree.ogcwebservices.wfs.operation.Query;
052    
053    /**
054     * Trigger implementation for transformation of geometries being part of a WFS request into the CRS
055     * of the comparsion geometries at the physical datasource. <br>
056     * At th moment just support for GetFeature is implemented TODO support for update and delete
057     * requests
058     *
059     * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a>
060     * @author last edited by: $Author: mschneider $
061     *
062     * @version $Revision: 18195 $, $Date: 2009-06-18 17:55:39 +0200 (Do, 18. Jun 2009) $
063     */
064    public class RequestCRSTransformationTrigger implements Trigger {
065    
066        private static final ILogger LOG = LoggerFactory.getLogger( RequestCRSTransformationTrigger.class );
067    
068        private String name;
069    
070        /**
071         * @param caller
072         *            calling instance
073         * @param values
074         *            parameter values passed from the caller. Because RequestCRSTransformationTrigger
075         *            is intented to be used as a preTrigger this will be all parameters passed to the
076         *            calling method
077         */
078        public Object[] doTrigger( Object caller, Object... values ) {
079    
080            Query query = (Query) values[0];
081    
082            Filter filter = query.getFilter();
083    
084            if ( filter instanceof ComplexFilter ) {
085                // transformations are just required if a filter is complex
086                // because FeatureID filters does not contain spatial operations
087                ComplexFilter cFilter = (ComplexFilter) filter;
088                SpatialOperation[] so = FilterTools.extractSpatialFilter( cFilter );
089                QualifiedName[] qns = query.getTypeNames();
090                GeoTransformer gt = null;
091                for ( int i = 0; i < qns.length; i++ ) {
092                    MappedFeatureType mft = ( (Datastore) caller ).getFeatureType( qns[i] );
093                    CoordinateSystem targetCRS = mft.getGMLSchema().getDefaultCS();
094                    for ( int j = 0; j < so.length; j++ ) {
095                        CoordinateSystem sourceCRS = so[i].getGeometry().getCoordinateSystem();
096                        if ( sourceCRS != null && !targetCRS.equals( sourceCRS ) ) {
097                            try {
098                                if ( gt == null ) {
099                                    gt = new GeoTransformer( targetCRS );
100                                }
101                                so[j].setGeometry( gt.transform( so[i].getGeometry() ) );
102                            } catch ( CRSTransformationException e ) {
103                                LOG.logError( e.getMessage(), e );
104                                throw new TriggerException( e );
105                            }
106                        }
107                    }
108                }
109    
110            }
111    
112            return values;
113        }
114    
115        public String getName() {
116            return name;
117        }
118    
119        public void setName( String name ) {
120            this.name = name;
121        }
122    
123        /**
124         * Returns a string representation of the object.
125         *
126         * @return a string representation of the object
127         */
128        @Override
129        public String toString() {
130            return "Trigger name: " + name;
131        }
132    }