001    //$HeadURL: svn+ssh://rbezema@svn.wald.intevation.org/deegree/base/tags/2.1/src/org/deegree/datatypes/Types.java $
002    /*----------------    FILE HEADER  ------------------------------------------
003    
004     This file is part of deegree.
005     Copyright (C) 2001-2006 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     53115 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.datatypes;
044    
045    import java.lang.reflect.Field;
046    import java.net.URI;
047    import java.util.HashMap;
048    import java.util.Map;
049    
050    import org.deegree.framework.log.ILogger;
051    import org.deegree.framework.log.LoggerFactory;
052    import org.deegree.framework.util.BootLogger;
053    import org.deegree.ogcbase.CommonNamespaces;
054    
055    /**
056     * General data type constants definition. the type values are the same as in
057     * <code>java.sql.Types<code>. Except for several geometry types, 
058     * <code>UNKNOWN</code>, <code>FEATURE</code>, <code>FEATURES</code> and 
059     * <code>FEATURECOLLECTION</code> that are not known by 
060     * <code>java.sql.Types</code>.
061     * <p>
062     * NOTE: Generally, it would be feasible to extend <code>java.sql.Types</code>,
063     * but unfortunately, this is not possible, as it's default constructor is not visible. 
064     * </p> 
065     * 
066     * @author <a href="mailto:poth@lat-lon.de">Andreas Poth </a>
067     * @author last edited by: $Author: apoth $
068     * 
069     * @version $Revision: 6749 $, $Date: 2007-05-03 10:39:12 +0200 (Do, 03 Mai 2007) $
070     * 
071     * @see java.sql.Types
072     */
073    public final class Types {
074    
075        private static ILogger LOG = LoggerFactory.getLogger( Types.class );
076    
077        private static URI GMLNS = CommonNamespaces.GMLNS;
078    
079        // generic sql types
080    
081        public final static int ARRAY = java.sql.Types.ARRAY;
082    
083        public final static int BIGINT = java.sql.Types.BIGINT;
084    
085        public final static int BINARY = java.sql.Types.BINARY;
086    
087        public final static int BIT = java.sql.Types.BIT;
088    
089        public final static int BLOB = java.sql.Types.BLOB;
090    
091        public final static int BOOLEAN = java.sql.Types.BOOLEAN;
092    
093        public final static int CHAR = java.sql.Types.CHAR;
094    
095        public final static int CLOB = java.sql.Types.CLOB;
096    
097        public final static int DATALINK = java.sql.Types.DATALINK;
098    
099        public final static int DATE = java.sql.Types.DATE;
100    
101        public final static int DECIMAL = java.sql.Types.DECIMAL;
102    
103        public final static int DISTINCT = java.sql.Types.DISTINCT;
104    
105        public final static int DOUBLE = java.sql.Types.DOUBLE;
106    
107        public final static int FLOAT = java.sql.Types.FLOAT;
108    
109        public final static int INTEGER = java.sql.Types.INTEGER;
110    
111        public final static int JAVA_OBJECT = java.sql.Types.JAVA_OBJECT;
112    
113        public final static int LONGVARBINARY = java.sql.Types.LONGVARBINARY;
114    
115        public final static int LONGVARCHAR = java.sql.Types.LONGVARCHAR;
116    
117        public final static int NULL = java.sql.Types.NULL;
118    
119        public final static int NUMERIC = java.sql.Types.NUMERIC;
120    
121        public final static int OTHER = java.sql.Types.OTHER;
122    
123        public final static int REAL = java.sql.Types.REAL;
124    
125        public final static int REF = java.sql.Types.REF;
126    
127        public final static int SMALLINT = java.sql.Types.SMALLINT;
128    
129        public final static int STRUCT = java.sql.Types.STRUCT;
130    
131        public final static int TIME = java.sql.Types.TIME;
132    
133        public final static int TIMESTAMP = java.sql.Types.TIMESTAMP;
134    
135        public final static int TINYINT = java.sql.Types.TINYINT;
136    
137        public final static int VARBINARY = java.sql.Types.VARBINARY;
138    
139        public final static int VARCHAR = java.sql.Types.VARCHAR;
140    
141        // geometry + gml types
142    
143        public static final int GEOMETRY = java.sql.Types.VARCHAR + 10000;
144    
145        public static final int MULTIGEOMETRY = java.sql.Types.VARCHAR + 10001;
146    
147        public static final int FEATURE = java.sql.Types.VARCHAR + 10002;
148    
149        public static final int FEATURECOLLECTION = java.sql.Types.VARCHAR + 10004;
150    
151        public static final int POINT = java.sql.Types.VARCHAR + 11000;
152    
153        public static final int CURVE = java.sql.Types.VARCHAR + 11001;
154    
155        public static final int SURFACE = java.sql.Types.VARCHAR + 11002;
156    
157        public static final int MULTIPOINT = java.sql.Types.VARCHAR + 11003;
158    
159        public static final int MULTICURVE = java.sql.Types.VARCHAR + 11004;
160    
161        public static final int MULTISURFACE = java.sql.Types.VARCHAR + 11005;
162    
163        public static final int ENVELOPE = java.sql.Types.VARCHAR + 11006;
164    
165        public static final QualifiedName GEOMETRY_PROPERTY_NAME = new QualifiedName( "GeometryPropertyType", GMLNS );
166    
167        public static final QualifiedName MULTI_GEOMETRY_PROPERTY_NAME = new QualifiedName( "MultiGeometryPropertyType",
168                                                                                            GMLNS );
169    
170        public static final QualifiedName FEATURE_PROPERTY_NAME = new QualifiedName( "FeaturePropertyType", GMLNS );
171    
172        // TODO check if this is really needed
173        public static final QualifiedName FEATURE_ARRAY_PROPERTY_NAME = new QualifiedName( "FeatureArrayPropertyType",
174                                                                                           GMLNS );
175    
176        // key instances: Integer, value instances: String
177        private static Map<Integer, String> typeNameMap = new HashMap<Integer, String>();
178    
179        // key instances: String, value instances: Integer
180        private static Map<String, Integer> typeCodeMap = new HashMap<String, Integer>();
181    
182        static {
183            try {
184                Field[] fields = java.sql.Types.class.getFields();
185                for ( int i = 0; i < fields.length; i++ ) {
186                    String typeName = fields[i].getName();
187                    Integer typeCode = (Integer) fields[i].get( null );
188                    typeNameMap.put( typeCode, typeName );
189                    typeCodeMap.put( typeName, typeCode );
190                }
191            } catch ( Exception e ) {
192                BootLogger.logError( "Error populating sql type code maps: " + e.getMessage(), e );
193            }
194        }
195    
196        /**
197         * Returns the generic sql type code for the given type name.
198         * 
199         * @param typeName
200         * @return the generic sql type code for the given type name.
201         * @throws UnknownTypeException
202         *             if the type name is not an sql type name
203         * @see java.sql.Types
204         */
205        public static int getTypeCodeForSQLType( String typeName )
206                                throws UnknownTypeException {
207            Integer typeCode = typeCodeMap.get( typeName );
208            if ( typeCode == null ) {
209                throw new UnknownTypeException( "Type name '" + typeName + "' does not denote an sql type." );
210            }
211            return typeCode.intValue();
212        }
213    
214        /**
215         * Returns the generic sql type name for the given type code.
216         * 
217         * @param typeCode
218         * @return the generic sql type name for the given type code.
219         * @throws UnknownTypeException
220         *             if the type code is not an sql type code
221         * @see java.sql.Types
222         */
223        public static String getTypeNameForSQLTypeCode( int typeCode )
224                                throws UnknownTypeException {
225            String typeName = typeNameMap.get( new Integer( typeCode ) );
226            if ( typeName == null ) {
227                throw new UnknownTypeException( "Type code '" + typeCode + "' does not denote an sql type." );
228            }
229            return typeName;
230        }
231    
232        /**
233         * mapping between GML-typenames and java-classnames for GML-geometry types
234         * 
235         * @param gmlTypeName
236         *            the name of the GML type name
237         * @return the internal type
238         * @throws UnknownTypeException
239         *             if the given name cannot be mapped to a known type.
240         */
241        public static int getJavaTypeForGMLType( String gmlTypeName )
242                                throws UnknownTypeException {
243            if ( "GeometryPropertyType".equals( gmlTypeName ) )
244                return Types.GEOMETRY;
245    
246            if ( "PointPropertyType".equals( gmlTypeName ) )
247                // return Types.POINT;
248                return Types.GEOMETRY;
249    
250            if ( "MultiPointPropertyType".equals( gmlTypeName ) )
251                // return Types.MULTIPOINT;
252                return Types.GEOMETRY;
253    
254            if ( "PolygonPropertyType".equals( gmlTypeName ) )
255                // return Types.SURFACE;
256                return Types.GEOMETRY;
257    
258            if ( "MultiPolygonPropertyType".equals( gmlTypeName ) )
259                // return Types.MULTISURFACE;
260                return Types.GEOMETRY;
261    
262            if ( "LineStringPropertyType".equals( gmlTypeName ) )
263                // return Types.CURVE;
264                return Types.GEOMETRY;
265    
266            if ( "MultiLineStringPropertyType".equals( gmlTypeName ) )
267                // return Types.MULTICURVE;
268                return Types.GEOMETRY;
269    
270            if ( "CurvePropertyType".equals( gmlTypeName ) )
271                // return Types.POINT;
272                return Types.GEOMETRY;
273    
274            if ( "MultiCurvePropertyType".equals( gmlTypeName ) )
275                // return Types.POINT;
276                return Types.GEOMETRY;
277    
278            if ( "SurfacePropertyType".equals( gmlTypeName ) )
279                // return Types.POINT;
280                return Types.GEOMETRY;
281    
282            if ( "MultiSurfacePropertyType".equals( gmlTypeName ) )
283                // return Types.POINT;
284                return Types.GEOMETRY;
285    
286            throw new UnknownTypeException( "Unsupported Type: '" + gmlTypeName + "'" );
287        }
288    
289        /**
290         * mapping between xml-typenames and java-classnames for XMLSCHEMA-simple types
291         * 
292         * @param schemaTypeName
293         *            of the XML schema type
294         * @return the internal type
295         * @throws UnknownTypeException
296         *             if the given name cannot be mapped to a known type.
297         * @todo TODO map them all over registry
298         */
299        public static int getJavaTypeForXSDType( String schemaTypeName )
300                                throws UnknownTypeException {
301    
302            if ( "integer".equals( schemaTypeName ) || "int".equals( schemaTypeName ) || "long".equals( schemaTypeName ) )
303                return Types.INTEGER;
304    
305            if ( "string".equals( schemaTypeName ) )
306                return Types.VARCHAR;
307    
308            if ( "date".equals( schemaTypeName ) )
309                return Types.DATE;
310    
311            if ( "boolean".equals( schemaTypeName ) )
312                return Types.BOOLEAN;
313    
314            if ( "float".equals( schemaTypeName ) )
315                return Types.FLOAT;
316    
317            if ( "double".equals( schemaTypeName ) )
318                return Types.DOUBLE;
319    
320            if ( "decimal".equals( schemaTypeName ) )
321                return Types.DECIMAL;
322    
323            if ( "dateTime".equals( schemaTypeName ) )
324                return Types.TIMESTAMP;
325    
326            if ( "time".equals( schemaTypeName ) )
327                return Types.TIME;
328    
329            if ( "date".equals( schemaTypeName ) )
330                return Types.DATE;
331    
332            if ( "anyURI".equals( schemaTypeName ) )
333                return Types.VARCHAR;
334    
335            throw new UnknownTypeException( "Unsupported Type:" + schemaTypeName );
336        }
337    
338        /**
339         * 
340         * @param type
341         *            SQL datatype code
342         * @param precision
343         *            precision (just used for type NUMERIC)
344         * @return typename
345         */
346        public static String getXSDTypeForSQLType( int type, int precision ) {
347            String s = null;
348    
349            switch ( type ) {
350            case Types.VARCHAR:
351            case Types.CHAR:
352                s = "string";
353                break;
354            case Types.NUMERIC: {
355                if ( precision <= 1 ) {
356                    s = "integer";
357                    break;
358                }
359                s = "double";
360                break;
361            }
362            case Types.DECIMAL:
363                s = "decimal";
364                break;
365            case Types.DOUBLE:
366            case Types.REAL:
367                s = "double";
368                break;
369            case Types.FLOAT:
370                s = "float";
371                break;
372            case Types.INTEGER:
373            case Types.SMALLINT:
374            case Types.BIGINT:
375                s = "integer";
376                break;
377            case Types.TIMESTAMP:
378            case Types.TIME:
379            case Types.DATE:
380                s = "dateTime";
381                break;
382            case Types.CLOB:
383                s = "string";
384                break;
385            case Types.BIT:
386            case Types.BOOLEAN:
387                s = "boolean";
388                break;
389            case Types.GEOMETRY:
390            case Types.OTHER:
391            case Types.STRUCT:
392                s = "gml:GeometryPropertyType";
393                break;
394            case Types.FEATURE:
395                s = "gml:FeaturePropertyType";
396                break;
397            default:
398                LOG.logWarning( "could not determine XSDType for SQLType; using 'XXX': " + type );
399                s = "code: " + type;
400            }
401            return s;
402        }
403    
404    }