Page 1 of 1

Difficulty to adapt SDK Java sample into C# (cast exception)

PostPosted: Thu Dec 29, 2016 6:47 pm
by Burzum

I have built 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:

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

I somehow managed to get the mail merge code to work.
But I have difficulties to create the "DataSource", which is necesary to generate prior we can actually perform the MailMerge.

I read this Java sample and tried to adapt it into C#: ... ce_Service

My difficulty is that Java uses this object to perform its casts:
Code: Select all   Expand viewCollapse view
XStorable store = ( XStorable)UnoRuntime.queryInterface(XStorable.class, xDs);

There is nothing equivalent in C#.

I converted the Java code this way:
Code: Select all   Expand viewCollapse view
  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
      // (!)
      // retrieve the database context at the global service manager and get its
      // XSingleServiceFactory interface
      XSingleServiceFactory xFac = (XSingleServiceFactory) _rMSF.createInstance("");
          //(XSingleServiceFactory)UnoRuntime.queryInterface(XSingleServiceFactory.class, _rMSF.createInstance(""));

      // 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();
      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"));;

Some casts worked fine:
Code: Select all   Expand viewCollapse view
XNamingService xServ = (XNamingService)xFac;
          //(XNamingService)UnoRuntime.queryInterface(XNamingService.class, xFac);

But some other casts throw an exception:

Code: Select all   Expand viewCollapse view
XStorable store = ( XStorable) xDs; //( XStorable)UnoRuntime.queryInterface(XStorable.class, xDs);


Code: Select all   Expand viewCollapse view
Unable to cast transparent proxy to type ''.

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?