001    //$HeadURL: svn+ssh://rbezema@svn.wald.intevation.org/deegree/base/branches/2.2_testing/src/org/deegree/io/sdeapi/SDEConnection.java $
002    /*----------------    FILE HEADER  ------------------------------------------
003    
004     This file is part of deegree.
005     Copyright (C) 2006 by: M.O.S.S. Computer Grafik Systeme GmbH
006     Hohenbrunner Weg 13
007     D-82024 Taufkirchen
008     http://www.moss.de/
009    
010     This library is free software; you can redistribute it and/or
011     modify it under the terms of the GNU Lesser General Public
012     License as published by the Free Software Foundation; either
013     version 2.1 of the License, or (at your option) any later version.
014    
015     This library is distributed in the hope that it will be useful,
016     but WITHOUT ANY WARRANTY; without even the implied warranty of
017     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
018     Lesser General Public License for more details.
019    
020     You should have received a copy of the GNU Lesser General Public
021     License along with this library; if not, write to the Free Software
022     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
023    
024     ---------------------------------------------------------------------------*/
025    package org.deegree.io.sdeapi;
026    
027    import com.esri.sde.sdk.client.SeConnection;
028    import com.esri.sde.sdk.client.SeException;
029    import com.esri.sde.sdk.client.SeState;
030    import com.esri.sde.sdk.client.SeVersion;
031    
032    public class SDEConnection {
033    
034        private SeConnection connection;
035    
036        private SeVersion version;
037    
038        private SeState state;
039    
040        public SDEConnection( final String sdeServer, final int sdeInstance, final String sdeDatabase,
041                             final String sdeVersion, final String sdeUser, final String sdePassword ) {
042    
043            try {
044                connection = new SeConnection( sdeServer, sdeInstance, sdeDatabase, sdeUser,
045                                               sdePassword );
046                if ( null == sdeVersion || 0 == sdeVersion.length() ) {
047                    version = new SeVersion( connection );
048                } else {
049                    try {
050                        version = new SeVersion( connection, sdeVersion );
051                    } catch ( SeException dne ) {
052                        version = new SeVersion( connection,
053                                                 SeVersion.SE_QUALIFIED_DEFAULT_VERSION_NAME );
054                        version.setDescription( sdeVersion );
055                        version.setName( sdeVersion );
056                        version.setParentName( SeVersion.SE_QUALIFIED_DEFAULT_VERSION_NAME );
057                        version.create( false, version );
058                    }
059                }
060                reserve();
061            } catch ( Exception e ) {
062                e.printStackTrace();
063            }
064        }
065    
066        public SeConnection getConnection() {
067            return connection;
068        }
069    
070        public SeVersion getVersion() {
071            return version;
072        }
073    
074        public SeState getState() {
075            return state;
076        }
077    
078        public boolean isClosed() {
079            if ( null == connection ) {
080                return true;
081            }
082            return connection.isClosed();
083        }
084    
085        public void close() {
086            if ( !isClosed() ) {
087                release();
088                try {
089                    connection.close();
090                } catch ( Exception e ) {
091                }
092                connection = null;
093            }
094        }
095    
096        private void reserve() {
097            try {
098                SeState verState = new SeState( connection, version.getStateId() );
099                state = new SeState( connection );
100                if ( verState.isOpen() ) {
101                    verState.close();
102                }
103                state.create( verState.getId() );
104            } catch ( Exception e ) {
105                e.printStackTrace();
106            }
107            ;
108        }
109    
110        private void release() {
111            try {
112                version.changeState( state.getId() );
113                state.close();
114            } catch ( Exception e ) {
115                e.printStackTrace();
116            }
117            ;
118        }
119    }