001    //$HeadURL: svn+ssh://jwilden@svn.wald.intevation.org/deegree/base/branches/2.5_testing/src/org/deegree/io/datastore/sql/VirtualContentProvider.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.sql;
037    
038    import java.sql.Connection;
039    import java.util.List;
040    
041    import org.deegree.datatypes.Types;
042    import org.deegree.i18n.Messages;
043    import org.deegree.io.datastore.schema.content.ConstantContent;
044    import org.deegree.io.datastore.schema.content.FieldContent;
045    import org.deegree.io.datastore.schema.content.FunctionParam;
046    import org.deegree.io.datastore.schema.content.SQLFunctionCall;
047    import org.deegree.io.datastore.schema.content.SpecialContent;
048    import org.deegree.io.datastore.schema.content.SpecialContent.VARIABLE;
049    import org.deegree.model.filterencoding.ComplexFilter;
050    import org.deegree.model.filterencoding.Filter;
051    import org.deegree.model.filterencoding.FilterTools;
052    import org.deegree.model.spatialschema.Envelope;
053    import org.deegree.model.spatialschema.Geometry;
054    import org.deegree.model.spatialschema.GeometryFactory;
055    
056    /**
057     * Responsible for determining the value of properties that are mapped to {@link SQLFunctionCall}s.
058     * <p>
059     * This involves the lookup of the values of variables ({@link SpecialContent} instances).
060     *
061     * <table border="1">
062     * <tr>
063     * <th>Variable name</th>
064     * <th>Description</th>
065     * </tr>
066     * <tr>
067     * <td>$QUERY.BBOX</td>
068     * <td>Bounding box of the query (null if it is not present).</td>
069     * </tr>
070     * </table>
071     * </p>
072     *
073     * @see SQLFunctionCall
074     * @see SpecialContent
075     *
076     * @author <a href="mailto:schneider@lat-lon.de">Markus Schneider</a>
077     * @author last edited by: $Author: mschneider $
078     *
079     * @version $Revision: 18195 $, $Date: 2009-06-18 17:55:39 +0200 (Do, 18 Jun 2009) $
080     */
081    public class VirtualContentProvider {
082    
083        // used in case that no BBOX is available
084        private static final Envelope DEFAULT_BBOX = GeometryFactory.createEnvelope( 0.0, 0.0, 0.0, 0.0, null );
085    
086        private Filter filter;
087    
088        private AbstractSQLDatastore ds;
089    
090        private Connection conn;
091    
092        /**
093         * Creates a new instance of {@link VirtualContentProvider}.
094         *
095         * @param filter
096         * @param ds
097         * @param conn
098         */
099        VirtualContentProvider( Filter filter, AbstractSQLDatastore ds, Connection conn ) {
100            this.filter = filter;
101            this.ds = ds;
102            this.conn = conn;
103        }
104    
105        /**
106         * Appends a {@link SQLFunctionCall} to the given {@link StatementBuffer}.
107         * <p>
108         * This includes the correct qualification of all columns that are used as {@link FunctionParam}s.
109         *
110         * @param query
111         * @param tableAlias
112         * @param call
113         */
114        public void appendSQLFunctionCall( StatementBuffer query, String tableAlias, SQLFunctionCall call ) {
115    
116            String callString = call.getCall();
117            int[] usedVars = call.getUsedVars();
118            List<FunctionParam> params = call.getParams();
119            for ( int j = 0; j < usedVars.length; j++ ) {
120                int varNo = usedVars[j];
121                String varString = "\\$" + varNo;
122                FunctionParam param = params.get( varNo - 1 );
123                if ( param instanceof FieldContent ) {
124                    String replace = tableAlias + "." + ( (FieldContent) param ).getField().getField();
125                    callString = callString.replaceAll( varString, replace );
126                } else if ( param instanceof ConstantContent ) {
127                    String replace = ( (ConstantContent) param ).getValue();
128                    callString = callString.replaceAll( varString, replace );
129                } else if ( param instanceof SpecialContent ) {
130                    appendSpecialContentValue( query, (SpecialContent) param );
131                    callString = callString.replaceFirst( varString, "?" );
132                } else {
133                    assert false;
134                }
135            }
136            query.append( callString );
137        }
138    
139        /**
140         * Appends the variable from a {@link SpecialContent} property to the given
141         * {@link StatementBuffer}.
142         *
143         * @param query
144         * @param param
145         */
146        public void appendSpecialContentValue( StatementBuffer query, SpecialContent param ) {
147    
148            VARIABLE var = param.getVariable();
149    
150            switch ( var ) {
151            case QUERY_BBOX: {
152    
153                Object bboxDBGeometry = null;
154    
155                try {
156                    Envelope requestBBOX = DEFAULT_BBOX;
157    
158                    if ( this.filter instanceof ComplexFilter ) {
159                        Object[] objects = FilterTools.extractFirstBBOX( (ComplexFilter) filter );
160                        if ( objects[0] != null ) {
161                            requestBBOX = (Envelope) objects[0];
162                        }
163                    }
164                    Geometry geometry = GeometryFactory.createSurface( requestBBOX, null );
165                    bboxDBGeometry = this.ds.convertDeegreeToDBGeometry( geometry, param.getSRS(), this.conn );
166                } catch ( Exception e ) {
167                    String msg = Messages.getMessage( "DATASTORE_EXTRACTBBOX" );
168                    throw new RuntimeException( msg, e );
169                }
170    
171                // TODO: remove quick hack to make this work on Oracle
172                if (this.ds.getClass().getName().equals( "org.deegree.io.datastore.sql.oracle.OracleDatastore" )) {
173                    query.addArgument( bboxDBGeometry, Types.STRUCT );
174                } else {
175                    query.addArgument( bboxDBGeometry, Types.OTHER );
176                }
177    
178    
179                break;
180            }
181            default: {
182                assert false;
183            }
184            }
185        }
186    }