001 //$HeadURL: svn+ssh://rbezema@svn.wald.intevation.org/deegree/base/branches/2.2_testing/src/org/deegree/tools/shape/GenericSQLShapeImporter.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.tools.shape;
044
045 import java.io.IOException;
046 import java.nio.charset.Charset;
047 import java.security.InvalidParameterException;
048 import java.sql.Types;
049 import java.util.Properties;
050
051 import org.deegree.framework.log.ILogger;
052 import org.deegree.framework.log.LoggerFactory;
053 import org.deegree.i18n.Messages;
054 import org.deegree.io.dbaseapi.DBaseException;
055 import org.deegree.io.quadtree.DBQuadtreeManager;
056 import org.deegree.io.quadtree.IndexException;
057 import org.deegree.io.shpapi.HasNoDBaseFileException;
058
059 /**
060 *
061 *
062 *
063 * @version August 13th 2007
064 * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a>
065 * @author last edited by: $Author: apoth $
066 *
067 * @version August 13th 2007
068 *
069 * @since 2.0
070 */
071 public class GenericSQLShapeImporter {
072 private static ILogger LOG = LoggerFactory.getLogger( GenericSQLShapeImporter.class );
073
074 private void validate( Properties map ) {
075 if ( null == map.get( "-driver" ) || "".equals( map.get( "-driver" ) ) ) {
076 throw new InvalidParameterException( "-driver must be set" );
077 }
078
079 if ( null == map.get( "-url" ) || "".equals( map.get( "-url" ) ) ) {
080 throw new InvalidParameterException( "-url must be set" );
081 }
082
083 if ( null == map.get( "-user" ) || "".equals( map.get( "-user" ) ) ) {
084 throw new InvalidParameterException( "-user must be set" );
085 }
086
087 if ( null == map.get( "-password" ) || "".equals( map.get( "-password" ) ) ) {
088 LOG.logInfo( "You supplied no password, is this correct?" );
089 }
090
091 if ( null == map.get( "-indexName" ) || "".equals( map.get( "-indexName" ) ) ) {
092 throw new InvalidParameterException( "-indexName must be set" );
093 }
094
095 if ( null == map.get( "-table" ) || "".equals( map.get( "-table" ) ) ) {
096 throw new InvalidParameterException( "-table must be set" );
097 }
098
099 if ( null == map.get( "-shapeFile" ) || "".equals( map.get( "-shapeFile" ) ) ) {
100 throw new InvalidParameterException( "-shapeFile must be set" );
101 }
102
103 if ( null == map.get( "-maxTreeDepth" ) || "".equals( map.get( "-maxTreeDepth" ) ) ) {
104 map.put( "-maxTreeDepth", new Integer(6) );
105 }
106
107 }
108
109 private void printHelp() {
110 System.out.println( Messages.getMessage( "DATASTORE_GENERICSQLSHAPEIMPORTER.HELP_DRIVER" ) );
111 System.out.println( Messages.getMessage( "DATASTORE_GENERICSQLSHAPEIMPORTER.HELP_URL" ) );
112 System.out.println( Messages.getMessage( "DATASTORE_GENERICSQLSHAPEIMPORTER.HELP_USER" ) );
113 System.out.println( Messages.getMessage( "DATASTORE_GENERICSQLSHAPEIMPORTER.HELP_PASSWORD" ) );
114 System.out.println( Messages.getMessage( "DATASTORE_GENERICSQLSHAPEIMPORTER.HELP_INDEXNAME" ) );
115 System.out.println( Messages.getMessage( "DATASTORE_GENERICSQLSHAPEIMPORTER.HELP_TABLE" ) );
116 System.out.println( Messages.getMessage( "DATASTORE_GENERICSQLSHAPEIMPORTER.HELP_OWNER" ) );
117 System.out.println( Messages.getMessage( "DATASTORE_GENERICSQLSHAPEIMPORTER.HELP_SHAPEFILE" ) );
118 System.out.println( Messages.getMessage( "DATASTORE_GENERICSQLSHAPEIMPORTER.HELP_MAXTREEDEPTH" ) );
119 System.out.println( Messages.getMessage( "DATASTORE_GENERICSQLSHAPEIMPORTER.HELP_IDTYPE" ) );
120 System.out.println( Messages.getMessage( "DATASTORE_GENERICSQLSHAPEIMPORTER.HELP" ) );
121 System.exit( 1 );
122 }
123
124 /**
125 * @param args
126 */
127 public static void main( String[] args ) {
128
129 Properties map = new Properties();
130 for ( int i = 0; i < args.length; ) {
131 String first = args[i++];
132 if( first != null && !"".equals( first.trim() ) && first.startsWith( "-" ) ){
133 //do we have no argument left
134 if( i+1 >= args.length ){
135 if( first != null && !"".equals( first.trim() )){
136 if( first.trim().startsWith( "-" ) ){
137 map.put( first, "" );
138 } else{
139 System.out.println( "The last commandline parameter doesn't start with a '-' sign, I'm confused, please check for previous errors (e.g. unescaped characters like spaces etc.) please quote all commandline arguments with spaces in them." );
140 System.exit(1);
141 }
142 }
143 } else {
144 StringBuilder tmp = new StringBuilder( args[i] );
145 while( i++ < args.length && !args[i].startsWith( "-" ) ){
146 tmp.append( " " );
147 tmp.append( args[i] );
148 }
149 String second = tmp.toString();
150 map.put( first, second );
151 }
152 } else{
153 i++;
154 }
155 }
156
157 LOG.logInfo( "You supplied following command line values: " + map );
158
159 GenericSQLShapeImporter gs = new GenericSQLShapeImporter();
160 if ( map.get( "-?" ) != null || map.get( "-h" ) != null ) {
161 gs.printHelp();
162 }
163
164 try{
165 gs.validate( map );
166 } catch( InvalidParameterException ipe ){
167 LOG.logError( ipe.getMessage()+"\n" );
168 gs.printHelp();
169 }
170
171 int depth = Integer.parseInt( map.getProperty( "-maxTreeDepth" ) );
172 DBQuadtreeManager qtm = null;
173 if ( "INTEGER".equalsIgnoreCase( (String)map.get( "-idType" ) ) ) {
174 LOG.logInfo( "Using Integer as id type" );
175 qtm = new DBQuadtreeManager<Integer>( map.getProperty( "-driver" ),
176 map.getProperty( "-url" ),
177 map.getProperty( "-user" ),
178 map.getProperty( "-password" ), Charset.defaultCharset().name(),
179 map.getProperty( "-indexName" ),
180 map.getProperty( "-table" ), "geometry",
181 map.getProperty( "-owner" ), depth, Types.INTEGER );
182 } else {
183 LOG.logInfo( "Using VARCHAR as id type" );
184 qtm = new DBQuadtreeManager<String>( map.getProperty( "-driver" ),
185 map.getProperty( "-url" ),
186 map.getProperty( "-user" ),
187 map.getProperty( "-password" ), Charset.defaultCharset().name(),
188 map.getProperty( "-indexName" ),
189 map.getProperty( "-table" ), "geometry",
190 map.getProperty( "-owner" ), depth, Types.VARCHAR );
191 }
192
193 try {
194 qtm.importShape( map.getProperty( "-shapeFile" ) );
195 } catch ( IOException e ) {
196 LOG.logError( e.getMessage() );
197 e.printStackTrace();
198 } catch ( IndexException e ) {
199 LOG.logError( e.getMessage() );
200 e.printStackTrace();
201 } catch ( HasNoDBaseFileException e ) {
202 LOG.logError( e.getMessage() );
203 e.printStackTrace();
204 } catch ( DBaseException e ) {
205 LOG.logError( e.getMessage() );
206 e.printStackTrace();
207 }
208 System.exit( 0 );
209
210 }
211
212 }