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