001    //$HeadURL: svn+ssh://jwilden@svn.wald.intevation.org/deegree/base/branches/2.5_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 of the comparsion
055     * geometries at the physical datasource. <br>
056     * At th moment just support for GetFeature is implemented TODO support for update and delete requests
057     * 
058     * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a>
059     * @author last edited by: $Author: hrubach $
060     * 
061     * @version $Revision: 23693 $, $Date: 2010-04-20 14:33:55 +0200 (Di, 20 Apr 2010) $
062     */
063    public class RequestCRSTransformationTrigger implements Trigger {
064    
065        private static final ILogger LOG = LoggerFactory.getLogger( RequestCRSTransformationTrigger.class );
066    
067        private String name;
068    
069        /**
070         * @param caller
071         *            calling instance
072         * @param values
073         *            parameter values passed from the caller. Because RequestCRSTransformationTrigger is intented to be
074         *            used as a preTrigger this will be all parameters passed to the calling method
075         */
076        public Object[] doTrigger( Object caller, Object... values ) {
077    
078            Query query = (Query) values[0];
079    
080            Filter filter = query.getFilter();
081    
082            if ( filter instanceof ComplexFilter ) {
083                // transformations are just required if a filter is complex
084                // because FeatureID filters does not contain spatial operations
085                ComplexFilter cFilter = (ComplexFilter) filter;
086                SpatialOperation[] so = FilterTools.extractSpatialFilter( cFilter );
087                QualifiedName[] qns = query.getTypeNames();
088                GeoTransformer gt = null;
089                Datastore ds = (Datastore) caller;
090                for ( int i = 0; i < qns.length; i++ ) {
091                    MappedFeatureType mft = ds.getFeatureType( qns[i] );
092                    CoordinateSystem targetCRS = mft.getGMLSchema().getDefaultCS();
093                    for ( int j = 0; j < so.length; j++ ) {
094                        CoordinateSystem sourceCRS = so[i].getGeometry().getCoordinateSystem();
095                        if ( sourceCRS != null && !targetCRS.equals( sourceCRS )
096                             && !ds.canTransformTo( targetCRS.getIdentifier() ) ) {
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    }