[Solved] [Java] adding .csv datasource

Java, C++, C#, Delphi, ??? - Using the UNO bridges

[Solved] [Java] adding .csv datasource

Postby amongalen » Thu Aug 06, 2015 1:57 pm

Hello
I'm trying to link .csv file as datasource for mailmerge in writer but I can't figure out whats wrong. When I run my code almost all works fine, I can see new datasource in writer, it contains table but there is some rubbish in this table (not data from file). Here's code of function supposed to link it :

Code: Select all   Expand viewCollapse view
public static void createNewDataSource(com.sun.star.lang.XMultiComponentFactory _rMSF,
         com.sun.star.uno.XComponentContext xContext) throws com.sun.star.uno.Exception {

      XSingleServiceFactory xFac = (XSingleServiceFactory) UnoRuntime.queryInterface(XSingleServiceFactory.class,
            _rMSF.createInstanceWithContext("com.sun.star.sdb.DatabaseContext", xContext));

      Object xDs = xFac.createInstance();
      XNamingService xServ = (XNamingService) UnoRuntime.queryInterface(XNamingService.class, xFac);

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

      XModel model = (XModel) UnoRuntime.queryInterface(XModel.class, xDDS.getDatabaseDocument());
      
      store.storeAsURL("file:///C:/file", model.getArgs());

      xServ.revokeObject("NewDataSourceName");
      xServ.registerObject("NewDataSourceName", xDDS);

      XPropertySet xDsProps = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, xDDS);
      xDsProps.setPropertyValue("URL", "sdbc:flat:C:/file.csv");

      store.store();
}

Could someone point out what's wrong in this code? I've tried to figure it out myself for hours now but still nothing.

I'm using LibreOffice but I think there isn't much difference and it's best place to get some help.

Amongalen
Last edited by amongalen on Tue Aug 11, 2015 9:55 am, edited 1 time in total.
libreoffice 4.4 on win 7
amongalen
 
Posts: 3
Joined: Thu Aug 06, 2015 1:41 pm

Re: [java] adding .csv datasource

Postby Villeroy » Thu Aug 06, 2015 2:39 pm

Please do yourself a favour and set up a data source just like an ordinary user would do. Then you would see that a data source of type Text (csv) is NOT bound to one file. It is bound to a whole directory of similar files sharing the same (network-)directory, encoding, delimiters and the same file name suffix. Each file represents one database table of a read-only pseudo-database.
You should not need to generate this programatically. Just set up the configuration file (aka "Base document") and let your program put the right files into the right place without accessing the UNO API.
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04, OpenOffice 4.x & LibreOffice 6.x
User avatar
Villeroy
Volunteer
 
Posts: 26635
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: [java] adding .csv datasource

Postby amongalen » Thu Aug 06, 2015 3:30 pm

The point is I can't/don't want to set it up in writer manually, that's why im trying to do it programatically. Tell me if there is any better way to import data from file for mailmerge than adding it as data source? How to do it?

Then you would see that a data source of type Text (csv) is NOT bound to one file

Not sure what u mean by that. When I try to select datasource in writer of Text/.csv type (in edit-> exchange database) I can only pick one file, not directory, and it works fine. I want to achive the same programatically.
libreoffice 4.4 on win 7
amongalen
 
Posts: 3
Joined: Thu Aug 06, 2015 1:41 pm

Re: [java] adding .csv datasource

Postby Villeroy » Thu Aug 06, 2015 4:00 pm

That Writer "wizard" for serial letters hides everything away from the user which is a really bad thing.

Open some (new) Writer document and hit F4 which opens the data source window with 2 panes
On the left you see named data sources with tables and queries. One is "Bibliography" (dBase directory shipped with the office suite) and the others are produced by that nasty wizard. Using that thing you may end up with many data sources pointing to the same text file.

Now right-click your data source name and choose "Edit Database ...".
This opens a new document window with a so called database document.
Notice the status bar of that window. It indicates that it is a text connection and the directory of that text data source.
menu:Edit>Database shows the connection parameters encoding, delimiters, name suffix and directory.

Here is the easy way to create a serial letter without any wizard:
Create a letter layout with some fixed content, date field, page fields etc.
Hit F4
Browse the right query or table.
Drag column headers into the document.
Save the resulting letter as a letter template or as a spontanious serial letter from scratch.
My users like it very much as long as they have access to the right sources and queries (which is my job).

The idea is that you define a source of data once and the document template with formatting and layout once. Then you open a template, add some additional content then print the serial letter to a file or to some printer using all records filtered records or manually selected records.

The wizard encourages to create a new data source and a whole new document for each serial letter which is highly unprofessional use of an office suite.
1) Create a configuration file ("Base document") as a link to connect the office suite with csv data.
2) Optionally, add simple SQL queries to the configuration. Queries on text, spreadsheets and dBase can select the right columns and rows in the right order and they can add alias names to the columns. Queries may also use named parameters to prompt the user for some filtering arguments (print all letters from region <enter_region>).
3) Create some professional document template for serial letters with changing address lists and letter content.
4) Drag column headers from the right pane of the data source window into your document template.
5) Write a simple program (shell script if you like) which updates the underlying csv file(s).

The user hits Ctrl+Shift+N, picks the right template (or just Ctrl+N if this is the user's default template), types some text to the right places, hits Ctrl+P, confirms printing a serial letter and finally confirms the record set (default=all) and target medium (default printer).
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04, OpenOffice 4.x & LibreOffice 6.x
User avatar
Villeroy
Volunteer
 
Posts: 26635
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: [java] adding .csv datasource

Postby amongalen » Tue Aug 11, 2015 9:55 am

I must admit, I'm stupid.
I'm remaking module for Office. In Word one have to set datasource every time he opens document (not just when creating new document). I've expected it to work the same way in Libre and I was wrong.

Thanks for ur help tho.
libreoffice 4.4 on win 7
amongalen
 
Posts: 3
Joined: Thu Aug 06, 2015 1:41 pm

Re: [solved][java] adding .csv datasource

Postby Villeroy » Tue Aug 11, 2015 10:35 am

MS Office encourages its users to write VBA programs enforcing the vendor lock-in which is a good thing from Microsoft's point of view. Quite often this is not a viable option with OpenOffice where the API is more complex without much sugar coating around the highly abstract Unified Network Objects (UNO). Advanced usage of OpenOffice is more about configuration rather than programming. Users who are unfamiliar with styles and templates don't have much fun with OpenOffice. But how productive is your productivity suite when you don't know how to utilize its core features? Configuration, templates and styles facilitate programming tasks as well.
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04, OpenOffice 4.x & LibreOffice 6.x
User avatar
Villeroy
Volunteer
 
Posts: 26635
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany


Return to External Programs

Who is online

Users browsing this forum: No registered users and 2 guests