001    //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/branches/2.3_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    /**
033     * <code>SDEConnection</code>
034     * 
035     * @author <a href="mailto:schmitz@lat-lon.de">Andreas Schmitz</a>
036     * @author last edited by: $Author: aschmitz $
037     * 
038     * @version $Revision: 12146 $, $Date: 2008-06-04 12:18:44 +0200 (Mi, 04. Jun 2008) $
039     */
040    public class SDEConnection {
041    
042        private SeConnection connection;
043    
044        private SeVersion version;
045    
046        private SeState state;
047    
048        /**
049         * @param sdeServer
050         * @param sdeInstance
051         * @param sdeDatabase
052         * @param sdeVersion
053         * @param sdeUser
054         * @param sdePassword
055         */
056        public SDEConnection( final String sdeServer, final int sdeInstance, final String sdeDatabase,
057                              final String sdeVersion, final String sdeUser, final String sdePassword ) {
058    
059            try {
060                connection = new SeConnection( sdeServer, sdeInstance, sdeDatabase, sdeUser, sdePassword );
061                if ( null == sdeVersion || 0 == sdeVersion.length() ) {
062                    version = new SeVersion( connection );
063                } else {
064                    try {
065                        version = new SeVersion( connection, sdeVersion );
066                    } catch ( SeException dne ) {
067                        version = new SeVersion( connection, SeVersion.SE_QUALIFIED_DEFAULT_VERSION_NAME );
068                        version.setDescription( sdeVersion );
069                        version.setName( sdeVersion );
070                        version.setParentName( SeVersion.SE_QUALIFIED_DEFAULT_VERSION_NAME );
071                        version.create( false, version );
072                    }
073                }
074                reserve();
075            } catch ( Exception e ) {
076                e.printStackTrace();
077            }
078        }
079    
080        /**
081         * @return the connection
082         */
083        public SeConnection getConnection() {
084            return connection;
085        }
086    
087        /**
088         * @return the version
089         */
090        public SeVersion getVersion() {
091            return version;
092        }
093    
094        /**
095         * @return the state
096         */
097        public SeState getState() {
098            return state;
099        }
100    
101        /**
102         * @return if closed
103         */
104        public boolean isClosed() {
105            if ( null == connection ) {
106                return true;
107            }
108            return connection.isClosed();
109        }
110    
111        /**
112         * 
113         */
114        public void close() {
115            if ( !isClosed() ) {
116                release();
117                try {
118                    connection.close();
119                } catch ( Exception e ) {
120                    // just eat'em, it's good for the health
121                }
122                connection = null;
123            }
124        }
125    
126        private void reserve() {
127            try {
128                SeState verState = new SeState( connection, version.getStateId() );
129                state = new SeState( connection );
130                if ( verState.isOpen() ) {
131                    verState.close();
132                }
133                state.create( verState.getId() );
134            } catch ( Exception e ) {
135                e.printStackTrace();
136            }
137        }
138    
139        private void release() {
140            try {
141                version.changeState( state.getId() );
142                state.close();
143            } catch ( Exception e ) {
144                e.printStackTrace();
145            }
146        }
147    }