
dk.eobjects.metamodel.OpenOfficeDataContextStrategy Maven / Gradle / Ivy
The newest version!
/**
* This file is part of MetaModel.
*
* MetaModel is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* MetaModel is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with MetaModel. If not, see .
*/
package dk.eobjects.metamodel;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import dk.eobjects.metamodel.data.DataSet;
import dk.eobjects.metamodel.query.Query;
import dk.eobjects.metamodel.schema.Schema;
import dk.eobjects.metamodel.util.FileHelper;
public class OpenOfficeDataContextStrategy implements IDataContextStrategy {
private static final Log _log = LogFactory
.getLog(OpenOfficeDataContextStrategy.class);
private IDataContextStrategy _strategy;
private Connection _connection;
public OpenOfficeDataContextStrategy(File dbFile) throws MetaModelException {
try {
String databaseName = dbFile.getName();
File tempDir = FileHelper.getTempDir();
ZipFile zipFile = new ZipFile(dbFile);
Enumeration extends ZipEntry> entries = zipFile.entries();
while (entries.hasMoreElements()) {
ZipEntry entry = entries.nextElement();
String entryName = entry.getName();
if (entryName.startsWith("database/")) {
File destFile = new File(tempDir, databaseName + "."
+ entryName.substring(9));
destFile.createNewFile();
if (_log.isDebugEnabled()) {
_log.debug("Processing entry: " + entryName);
_log.debug("Writing temp file: "
+ destFile.getAbsolutePath());
}
byte[] buffer = new byte[1024];
InputStream inputStream = zipFile.getInputStream(entry);
OutputStream outputStream = new BufferedOutputStream(
new FileOutputStream(destFile));
for (int len = inputStream.read(buffer); len >= 0; len = inputStream
.read(buffer)) {
outputStream.write(buffer, 0, len);
}
inputStream.close();
outputStream.close();
destFile.deleteOnExit();
} else {
_log.debug("Ignoring entry: " + entryName);
}
}
zipFile.close();
Class.forName("org.hsqldb.jdbcDriver");
String url = "jdbc:hsqldb:file:" + tempDir.getAbsolutePath() + "/"
+ databaseName;
_log.info("Using database URL: " + url);
_connection = DriverManager.getConnection(url, "SA", "");
_connection.setReadOnly(true);
_strategy = new JdbcDataContextStrategy(_connection,
JdbcDataContextFactory.DEFAULT_TABLE_TYPES, null);
} catch (Exception e) {
throw new MetaModelException(e);
}
}
public DataSet executeQuery(Query query) throws MetaModelException {
return _strategy.executeQuery(query);
}
public String getDefaultSchemaName() throws MetaModelException {
return _strategy.getDefaultSchemaName();
}
public Schema getSchemaByName(String name) throws MetaModelException {
return _strategy.getSchemaByName(name);
}
public String[] getSchemaNames() throws MetaModelException {
return _strategy.getSchemaNames();
}
@Override
protected void finalize() throws Throwable {
super.finalize();
_connection.close();
}
public Connection getConnection() {
return _connection;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy