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