Re: MailMerge settings

Ariel Constenla-Haile <[email protected]> Wed, 11 May 2011 19:32:03 -0300
Newsgroups gmane.comp.openoffice.devel.api
Message-ID <[email protected]>
Hello Paolo,

On Wednesday 11 May 2011, 13:33, Paolo Mantovani wrote:
> Hallo,
> 
> I need to change (via API) the database bound to a given document.
> 
> In practice I need to perform via API the same operation that you
> normally do via the menu Modify->Change Database.

In Writer I see Edit - Exchange Database

> 
> I tried with:
> 
> oSet = oDoc.createInstance("com.sun.star.text.DocumentSettings")
> oSet.CurrentDatabaseDataSource = "newsource"
> oSet.CurrentDatabaseCommandType = com.sun.star.sdb.CommandType.TABLE
> oSet.CurrentDatabaseCommand = "newtable"
> 
In fact, this *does* exchange the database, just see the dialog in the Edit - 
Exchange Database menu, at the bottom you'll see that settings are changed and 
stored; for example, I had Database1.Contacts and after executing your code I 
see in this dialog 

"Database applied to document: newsource.newtable"

Changing the document settings does not even test if there is actually a DB 
and a table with the given names.
It is the client code responsibility to check this and register the new data 
source in case the test fails. 

> But this does not affect the database fieldmasters in the document, that
> are still pointing to the old datasource.

Updating/refreshing the fields as Fernand suggested does not work; as far as I 
could understand reading the implementation, you have to instantiate new field 
masters for your new data source (because Writer does not seem to create them 
until they are "in use", that is, until the user really inserts a field), and 
then update all the fields in the documents iterating on the text field 
collection (not via XRefreshable.refresh()).

I haven't test any of these, but it seems to be what happens internally when 
".uno:ChangeDatabaseField" is dispatched:

* the slot id FN_CHANGE_DBFIELD is executed by the SwBaseShell, void 
SwBaseShell::ExecField
http://svn.services.openoffice.org/opengrok/xref/DEV300_m106/sw/source/ui/shells/basesh.cxx#2914

* the dialog impl. is SwChangeDBDlg
http://svn.services.openoffice.org/opengrok/xref/DEV300_m106/sw/source/ui/inc/changedb.hxx
http://svn.services.openoffice.org/opengrok/xref/DEV300_m106/sw/source/ui/fldui/changedb.cxx

You can see how two different things happen there:
a) changing the data source, will lead to 
void SwDoc::ChgDBData(const SwDBData& rNewData)
http://svn.services.openoffice.org/opengrok/xref/DEV300_m106/sw/source/core/doc/doc.cxx#682

b) updating the document's field will lead to 
void SwDoc::ChangeDBFields( const SvStringsDtor& rOldNames,                                                  
const String& rNewName )
http://svn.services.openoffice.org/opengrok/xref/DEV300_m106/sw/source/core/doc/docfld.cxx#1908

Using the API to change the com.sun.star.text.DocumentSettings does the same 
as (a), it calls SwDoc::ChgDBData
http://svn.services.openoffice.org/opengrok/xref/DEV300_m106/sw/source/ui/uno/SwXDocumentSettings.cxx#465

>It seems that I'm missing something.

the above shows you are missing (b), it is not enough to change the 
com.sun.star.text.DocumentSettings, you have to instantiate the field masters, 
and modify the existing fields.
Note that SwDoc::ChangeDBFields does a few things more: 
it updates the text section's condition (a text section may bi hidden 
according to a db related condition like Datasource.Table.Field EQ Something 
so you have to change Dataource.Table to newdatasource.newtable in your 
example)
http://svn.services.openoffice.org/opengrok/xref/DEV300_m106/sw/source/core/doc/docfld.cxx#1918
it updates *not only* database fields, but also other text fields, see the 
switch in 
http://svn.services.openoffice.org/opengrok/xref/DEV300_m106/sw/source/core/doc/docfld.cxx#1947

Hope this helps.
Regards
-- 
Ariel Constenla-Haile
La Plata, Argentina
signature.asc (application/pgp-signature, 836 B)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.16 (GNU/Linux)

iQIcBAABCAAGBQJNyw5oAAoJEMjP1bm45QNWFaUP/RSO3LNwY1tbsv+Lah1mgIgs
GVgadRcK9kNOcunkyW8rmGBG19cP/bE3UdsU+6vafVXEXd3d9LS0db7h6M7yz81C
4uBgZO2GK6gVJPSZKwQ3ZevBrJyqUI2461NYZWYqPfeUMM3Wed+DxhtxhNx7fLrw
tvHvTayG4hBBGnc/tRhNfir147m7krjOUf1FpZwluO7kdneNsHVikI3rIQuI9ATP
xnoMvyx+Pxt0GYwQHRnxFOfMG7Qo4PSYUk578oSoOejYzMLgA10fHlOQ1rDfukfQ
doAk2mzPP2by9cMYmSuVt9E9QeNQS7nnHRHeZhGJ6s2kbhVw7ufkgwV0/GSzBjL0
ursihQNmGEbF+47NG2KjalSuQ601ZW0RqLOTCRKRJ8mAh5qVFCO2Wh7Q3XH28QUp
20l4cHn/AWvWvQlpvMZzQI7XTzEWb8tJ3bNQRogWEqweAQj+SVV4NhgLn7ad31Zn
8by7R41nfgcfugY/1GQj4Mu1OLXoDAxGBBEEOlzKCACXGJiHLrGpj9Ms5MwSNN5e
gVKFpvzv6kuzSfH/x92BPWo/dkIKfB/7yfaj+WsLyPooz8xG9F1L3zezZKMhVNHG
fDcgyCoPMsJW1z+ywxsiaJbT0KLr2Kz44K0WkTv3Tm5Q/TwtIMGzfREBPG8JdQDu
OnZ1GHiI+X2hThUHEENF
=cxxu
-----END PGP SIGNATURE-----