Object oDBContext = null;
Object oDBSource = null;
Object newQuery = null;
Object toolkit = null;
XStorable store = null;
try
{
oDBContext = ((XMultiServiceFactory) Param.getMSF ())
.createInstance ("com.sun.star.sdb.DatabaseContext");
oDBSource = ((XMultiServiceFactory) Param.getMSF ())
.createInstance ("com.sun.star.sdb.DataSource");
newQuery = ((XMultiServiceFactory) Param.getMSF ())
.createInstance ("com.sun.star.sdb.QueryDefinition");
toolkit = ((XMultiServiceFactory) Param.getMSF ())
.createInstance ("com.sun.star.awt.Toolkit");
} catch (com.sun.star.uno.Exception e)
{
e.printStackTrace (log);
throw new StatusException (Status.failed ("Couldn't create instance"));
}
String mysqlURL = (String) Param.get ("mysql.url");
if (mysqlURL == null)
{
throw new StatusException (Status.failed (
"Couldn't get 'mysql.url' from ini-file"));
}
user = (String) Param.get ("jdbc.user");
password = (String) Param.get ("jdbc.password");
if ((user == null) || (password == null))
{
throw new StatusException (Status.failed (
"Couldn't get 'jdbc.user' or 'jdbc.password' from ini-file"));
}
PropertyValue[] info = new PropertyValue[2];
info[0] = new PropertyValue ();
info[0].Name = "user";
info[0].Value = user;
info[1] = new PropertyValue ();
info[1].Name = "password";
info[1].Value = password;
XPropertySet propSetDBSource = (XPropertySet) UnoRuntime.queryInterface (
XPropertySet.class, oDBSource);
try
{
propSetDBSource.setPropertyValue ("URL", mysqlURL);
propSetDBSource.setPropertyValue ("Info", info);
} catch (com.sun.star.lang.WrappedTargetException e)
{
e.printStackTrace (log);
throw new StatusException (Status.failed (
"Couldn't set property value"));
} catch (com.sun.star.lang.IllegalArgumentException e)
{
e.printStackTrace (log);
throw new StatusException (Status.failed (
"Couldn't set property value"));
} catch (com.sun.star.beans.PropertyVetoException e)
{
e.printStackTrace (log);
throw new StatusException (Status.failed (
"Couldn't set property value"));
} catch (com.sun.star.beans.UnknownPropertyException e)
{
e.printStackTrace (log);
throw new StatusException (Status.failed (
"Couldn't set property value"));
}
try
{
log.println ("writing database file ...");
XDocumentDataSource xDDS = (XDocumentDataSource)
UnoRuntime.queryInterface(XDocumentDataSource.class, oDBSource);
store = (XStorable) UnoRuntime.queryInterface(XStorable.class,
xDDS.getDatabaseDocument());
aFile = utils.getOfficeTemp ((XMultiServiceFactory) Param.getMSF ())+"JoinView.odb";
log.println ("... filename will be "+aFile);
store.storeAsURL (aFile,new PropertyValue[]
{});
log.println ("... done");
} catch (com.sun.star.uno.Exception e)
{
e.printStackTrace (log);
throw new StatusException (Status.failed ("Couldn't register object"));
}
isolConnection = (XIsolatedConnection) UnoRuntime.queryInterface (
XIsolatedConnection.class,
oDBSource);
XConnection connection = null;
XStatement statement = null;
final String tbl_name1 = "tst_table1";
final String tbl_name2 = "tst_table2";
final String col_name1 = "id1";
final String col_name2 = "id2";
try
{
connection = isolConnection.getIsolatedConnection (user, password);
statement = connection.createStatement ();
statement.executeUpdate ("drop table if exists " + tbl_name1);
statement.executeUpdate ("drop table if exists " + tbl_name2);
statement.executeUpdate ("create table " + tbl_name1 + " (" +
col_name1 + " int)");
statement.executeUpdate ("create table " + tbl_name2 + " (" +
col_name2 + " int)");
} catch (com.sun.star.sdbc.SQLException e)
{
try
{
shortWait ();
connection = isolConnection.getIsolatedConnection (user,
password);
statement = connection.createStatement ();
statement.executeUpdate ("drop table if exists " + tbl_name1);
statement.executeUpdate ("drop table if exists " + tbl_name2);
statement.executeUpdate ("create table " + tbl_name1 + " (" +
col_name1 + " int)");
statement.executeUpdate ("create table " + tbl_name2 + " (" +
col_name2 + " int)");
} catch (com.sun.star.sdbc.SQLException e2)
{
e2.printStackTrace (log);
throw new StatusException (Status.failed ("SQLException"));
}
}
XQueryDefinitionsSupplier querySuppl = (XQueryDefinitionsSupplier) UnoRuntime.queryInterface (
XQueryDefinitionsSupplier.class,
oDBSource);
XNameAccess defContainer = querySuppl.getQueryDefinitions ();
XPropertySet queryProp = (XPropertySet) UnoRuntime.queryInterface (
XPropertySet.class, newQuery);
try
{
final String query = "select * from " + tbl_name1 + ", " +
tbl_name2 + " where " + tbl_name1 + "." +
col_name1 + "=" + tbl_name2 + "." +
col_name2;
queryProp.setPropertyValue ("Command", query);
} catch (com.sun.star.lang.WrappedTargetException e)
{
e.printStackTrace (log);
throw new StatusException (Status.failed (
"Couldn't set property value"));
} catch (com.sun.star.lang.IllegalArgumentException e)
{
e.printStackTrace (log);
throw new StatusException (Status.failed (
"Couldn't set property value"));
} catch (com.sun.star.beans.PropertyVetoException e)
{
e.printStackTrace (log);
throw new StatusException (Status.failed (
"Couldn't set property value"));
} catch (com.sun.star.beans.UnknownPropertyException e)
{
e.printStackTrace (log);
throw new StatusException (Status.failed (
"Couldn't set property value"));
}
XNameContainer queryContainer = (XNameContainer) UnoRuntime.queryInterface (
XNameContainer.class,
defContainer);
try
{
queryContainer.insertByName ("Query1", newQuery);
store.store ();
connection.close ();
} catch (com.sun.star.lang.WrappedTargetException e)
{
e.printStackTrace (log);
throw new StatusException (Status.failed ("Couldn't insert query"));