1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 package connectivity.tools;
24 
25 import com.sun.star.container.XNameAccess;
26 import com.sun.star.frame.XModel;
27 import com.sun.star.frame.XStorable;
28 import com.sun.star.io.IOException;
29 import com.sun.star.lang.XMultiServiceFactory;
30 import com.sun.star.sdb.XDocumentDataSource;
31 import com.sun.star.sdb.XOfficeDatabaseDocument;
32 import com.sun.star.sdbc.SQLException;
33 import com.sun.star.sdbc.XCloseable;
34 import com.sun.star.sdbc.XStatement;
35 import com.sun.star.uno.UnoRuntime;
36 import com.sun.star.util.CloseVetoException;
37 import connectivity.tools.sdb.Connection;
38 import java.io.File;
39 
40 /**
41  *
42  * @author oj93728
43  */
44 public abstract class AbstractDatabase implements DatabaseAccess
45 {
AbstractDatabase(final XMultiServiceFactory orb)46     public AbstractDatabase(final XMultiServiceFactory orb) throws Exception
47     {
48         m_orb = orb;
49     }
50 
51     // --------------------------------------------------------------------------------------------------------
AbstractDatabase(final XMultiServiceFactory orb, final String _existingDocumentURL )52     public AbstractDatabase(final XMultiServiceFactory orb, final String _existingDocumentURL ) throws Exception
53     {
54         m_orb = orb;
55         createDBDocument( _existingDocumentURL );
56     }
57 
58     /** returns a connection to the database
59      *
60      * Multiple calls to this method return the same connection. The DbaseDatabase object keeps
61      * the ownership of the connection, so you don't need to (and should not) dispose/close it.
62      */
defaultConnection()63     public Connection defaultConnection() throws SQLException
64     {
65         if ( m_connection == null )
66             m_connection = new Connection( m_databaseDocument.getDataSource().getConnection("", "") );
67 
68         return m_connection;
69     }
70 
71     /** executes the given SQL statement via the defaultConnection
72      */
executeSQL(final String statementString)73     public void executeSQL(final String statementString) throws SQLException
74     {
75         final XStatement statement = defaultConnection().createStatement();
76         statement.execute(statementString);
77     }
78 
79     /** stores the database document
80      */
store()81     public void store() throws IOException
82     {
83         if (m_databaseDocument != null)
84         {
85             final XStorable storeDoc = UnoRuntime.queryInterface(XStorable.class, m_databaseDocument);
86             storeDoc.store();
87         }
88     }
89 
90     /** closes the database document
91      *
92      *  Any CloseVetoExceptions fired by third parties are ignored, and any reference to the
93      *  database document is released.
94      */
close()95     public void close()
96     {
97         // close connection
98         final XCloseable closeConn = UnoRuntime.queryInterface( XCloseable.class,
99             m_connection != null ? m_connection.getXConnection() : null );
100         if (closeConn != null)
101         {
102             try
103             {
104                 closeConn.close();
105             }
106             catch (SQLException e)
107             {
108             }
109         }
110         m_connection = null;
111 
112         // close document
113         final com.sun.star.util.XCloseable closeDoc = UnoRuntime.queryInterface( com.sun.star.util.XCloseable.class, m_databaseDocument );
114         if (closeDoc != null)
115         {
116             try
117             {
118                 closeDoc.close(true);
119             }
120             catch (CloseVetoException e)
121             {
122             }
123         }
124         m_databaseDocument = null;
125     }
126 
127     /** closes the document, and deletes the underlying file
128      */
closeAndDelete()129     public void closeAndDelete()
130     {
131         close();
132 
133         if (m_databaseDocumentFile != null)
134         {
135             try
136             {
137                 final File file = new File(m_databaseDocumentFile);
138                 file.delete();
139             }
140             catch (Exception e)
141             {
142             }
143         }
144     }
145 
146     /** returns the underlying database document
147      */
getDatabaseDocument()148     public XOfficeDatabaseDocument getDatabaseDocument()
149     {
150         return m_databaseDocument;
151     }
152 
153     /** returns the model interface of the underlying database document
154      */
getModel()155     public XModel getModel()
156     {
157         return UnoRuntime.queryInterface( XModel.class, m_databaseDocument );
158     }
159 
getORB()160     public XMultiServiceFactory getORB()
161     {
162         return m_orb;
163     }
164 
165     // --------------------------------------------------------------------------------------------------------
createDBDocument(final String _docURL)166     final protected void createDBDocument(final String _docURL) throws Exception
167     {
168         m_databaseDocumentFile = _docURL;
169 
170         final XNameAccess dbContext = UnoRuntime.queryInterface( XNameAccess.class,
171             m_orb.createInstance( "com.sun.star.sdb.DatabaseContext" ) );
172         final XDocumentDataSource dataSource = UnoRuntime.queryInterface( XDocumentDataSource.class, dbContext.getByName( _docURL ) );
173 
174         m_databaseDocument = dataSource.getDatabaseDocument();
175         m_dataSource = new DataSource(m_orb, m_databaseDocument.getDataSource());
176     }
177 
178     /** returns the URL of the ODB document represented by this instance
179      */
getDocumentURL()180     public String getDocumentURL()
181     {
182         return m_databaseDocumentFile;
183     }
184 
185     /** returns the data source belonging to this database
186      */
getDataSource()187     public DataSource getDataSource()
188     {
189         return m_dataSource;
190     }
191 
192     /** creates a row set operating the database, with a given command/type
193      */
createRowSet(final int _commandType, final String _command)194     public RowSet createRowSet(final int _commandType, final String _command)
195     {
196         return new RowSet(m_orb, getDocumentURL(), _commandType, _command);
197     }
198 
199     @Override
finalize()200     protected void finalize() throws Throwable
201     {
202         closeAndDelete();
203         super.finalize();
204     }
205 
206     // the service factory
207     protected final XMultiServiceFactory m_orb;
208     // the URL of the temporary file used for the database document
209     protected String m_databaseDocumentFile;
210     // the database document
211     protected XOfficeDatabaseDocument m_databaseDocument;
212     // the data source belonging to the database document
213     protected DataSource m_dataSource;
214     // the default connection
215     protected Connection    m_connection;
216 }
217