cast exception while using OpenOffice SDK to create a DataSource

287 views Asked by At

I have a C# application that performs mail merges with MS Office (using Interop API). I am now trying to have it support Open office. I want to use OpenOffice SDK: http://www.openoffice.org/api/docs/common/ref/com/sun/star/text/MailMerge.html#Command

Does not look crystal clear to me right now....

I somehow managed to get the mail merge code to work. The thing is we need to create a "DataSource" before actually performing the MailMerge and I encounter difficulties to do it.

I can get a sample in Java here: https://wiki.openoffice.org/wiki/Documentation/DevGuide/Database/The_DataSource_Service

I would need to convert this into C#.

My difficulty is that Java uses this object to perform its casts:

XStorable store = ( XStorable)UnoRuntime.queryInterface(XStorable.class, xDs);

There is nothing equivalent in C#.

I converted the code this way:

  public static void CreateDataSource(string dataSourceProvidedFilePath, string dataSourceSavedFilePath)
    {
                XComponentContext oStrap = uno.util.Bootstrap.bootstrap();
        XMultiServiceFactory _rMSF = (XMultiServiceFactory)oStrap.getServiceManager();

         // the XSingleServiceFactory of the database context creates new generic 
      // com.sun.star.sdb.DataSources (!)
      // retrieve the database context at the global service manager and get its 
      // XSingleServiceFactory interface
      XSingleServiceFactory xFac = (XSingleServiceFactory) _rMSF.createInstance("com.sun.star.sdb.DatabaseContext");
          //(XSingleServiceFactory)UnoRuntime.queryInterface(XSingleServiceFactory.class, _rMSF.createInstance("com.sun.star.sdb.DatabaseContext"));

      // instantiate an empty data source at the XSingleServiceFactory 
      // interface of the DatabaseContext
      Object xDs = xFac.createInstance();

      // register it with the database context
      XNamingService xServ = (XNamingService)xFac;
          //(XNamingService)UnoRuntime.queryInterface(XNamingService.class, xFac);

      XStorable store = ( XStorable) xDs;
          //( XStorable)UnoRuntime.queryInterface(XStorable.class, xDs);

      XModel model =( XModel) xDs;
          //( XModel)UnoRuntime.queryInterface(XModel.class, xDs);

        //on détermine le fichier ou sera sauvegardée la data source
      string dataSourcePathURL = Path.Combine(Path.GetDirectoryName(dataSourceProvidedFilePath), dataSourceSavedFilePath + ".odb").ConvertToOpenOfficeURL();
      store.storeAsURL(/*"file:///c:/test.odb"*/dataSourcePathURL,model.getArgs());
      xServ.registerObject("NewDataSourceName", xDs);

      // setting the necessary data source properties
      XPropertySet xDsProps = (XPropertySet)xDs;
          //(XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, xDs);
      // Adabas D URL
      xDsProps.setPropertyValue("URL", new uno.Any("sdbc:adabas::MYDB1"));

      // force password dialog
      //xDsProps.setPropertyValue("IsPasswordRequired", new Boolean(true));

      // suggest dsadmin as user name
      xDsProps.setPropertyValue("User", new uno.Any("dsadmin"));
      store.store();
    }

Some casts worked fine:

XNamingService xServ = (XNamingService)xFac;
          //(XNamingService)UnoRuntime.queryInterface(XNamingService.class, xFac);

But some other casts throw an exception:

XStorable store = ( XStorable) xDs; //( XStorable)UnoRuntime.queryInterface(XStorable.class, xDs);

->

Unable to cast transparent proxy to type 'unoidl.com.sun.star.frame.XStorable'.

Is there a way to have this code correctly converted to C#?

Otherwise, do you know any other resource showing how to create an Open Office DataSource in Java?

Thx

1

There are 1 answers

0
Jim K On

First I tried using C# and encountered the same error you described.

Then I tried the example using Java and ended up with a null value for XStorable. So I think your problem is not due to C#, but because for some reason the empty data source is not getting created properly.

In Create a libreoffice text-based datasource and set settings with java, the poster seems to have had success, so I'm not sure what went wrong when I tried it.

This code to print data sources does work for me: https://wiki.openoffice.org/wiki/Documentation/DevGuide/Database/Data_Sources_in_OpenOffice.org_API.