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