<?xml version="1.0" encoding="UTF-8"?>
<script:module xmlns:script="http://openoffice.org/2000/script" script:name="sw_SwXMailMerge" script:language="StarBasic">

'*************************************************************************
'
'  Licensed to the Apache Software Foundation (ASF) under one
'  or more contributor license agreements.  See the NOTICE file
'  distributed with this work for additional information
'  regarding copyright ownership.  The ASF licenses this file
'  to you under the Apache License, Version 2.0 (the
'  "License"); you may not use this file except in compliance
'  with the License.  You may obtain a copy of the License at
'  
'    http://www.apache.org/licenses/LICENSE-2.0
'  
'  Unless required by applicable law or agreed to in writing,
'  software distributed under the License is distributed on an
'  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
'  KIND, either express or implied.  See the License for the
'  specific language governing permissions and limitations
'  under the License.
'
'*************************************************************************



' REQUIRED VARIABLES for interface/service tests:

' com.sun.star.task.XJob:
Global vXJobArgs(0 to 3) as Variant

' com.sun.star.text.MailMerge
Global cMailMerge_DocumentURL as String

Dim cDataSourceName  as String
Dim cDataCommand as String
Dim oXResultSet as Variant

' Be sure that all variables are dimensioned:
option explicit

Sub CreateObj()

'*************************************************************************
' COMPONENT: 
' com.sun.star.sw.SwXMailMerge
'*************************************************************************
On Error Goto ErrHndl

	Dim vXJobArg0(0 to 7) as new com.sun.star.beans.NamedValue
	Dim vXJobArg1(0 to 6) as new com.sun.star.beans.NamedValue
	Dim vXJobArg2(0 to 9) as new com.sun.star.beans.NamedValue
	Dim vXJobArg3(0) as new com.sun.star.beans.NamedValue
	Dim cTestDoc as String
	Dim cOutputURL as String
    Dim oXConnection as Variant
    Dim myBookMarks as Variant
	
    oObj = createUnoService("com.sun.star.text.MailMerge")

	cTestDoc = utils.Path2URL(cTestDocsDir + "/" + "MailMerge.sxw")
    cMailMerge_DocumentURL = cTestDoc
	cOutputURL = ConvertToURL(utils.getUserPath())
    cDataSourceName  = "Bibliography"
    cDataCommand = "biblio"
    oXResultSet = getXResultSet()
    oXConnection = oXResultSet.ActiveConnection
    
	' create arguments for XJob:
    myBookMarks = getMyBookMarks()
	
    oObj.DataSourceName = cDataSourceName
    oObj.Command = cDataCommand
    oObj.CommandType = com.sun.star.sdb.CommandType.TABLE
    oObj.OutputType = com.sun.star.text.MailMergeType.FILE
    oObj.DocumentURL = cTestDoc
    oObj.OutputURL = cOutputURL
    oObj.FileNamePrefix = "Author"
    oObj.FileNameFromColumn = TRUE
    oObj.Selection = DimArray()
    
	' use a simple DataBase with simple Table
	vXJobArg0(0).Name = "DataSourceName" : vXJobArg0(0).Value = cDataSourceName
	vXJobArg0(1).Name = "Command" : vXJobArg0(1).Value = cDataCommand
	vXJobArg0(2).Name = "CommandType" : vXJobArg0(2).Value = com.sun.star.sdb.CommandType.TABLE
	vXJobArg0(3).Name = "OutputType" : vXJobArg0(3).Value = com.sun.star.text.MailMergeType.FILE
	vXJobArg0(4).Name = "DocumentURL" : vXJobArg0(4).Value = cTestDoc
	vXJobArg0(5).Name = "OutputURL" : vXJobArg0(5).Value = cOutputURL
	vXJobArg0(6).Name = "FileNamePrefix" : vXJobArg0(6).Value = "Identifier"
	vXJobArg0(7).Name = "FileNameFromColumn" : vXJobArg0(7).Value = true
	
	' use a simple DataBase with simple Table and write to printer
	vXJobArg1(0).Name = "DataSourceName" : vXJobArg1(0).Value = cDataSourceName
	vXJobArg1(1).Name = "Command" : vXJobArg1(1).Value = cDataCommand
	vXJobArg1(2).Name = "CommandType" : vXJobArg1(2).Value = com.sun.star.sdb.CommandType.TABLE
	vXJobArg1(3).Name = "OutputType" : vXJobArg1(3).Value = com.sun.star.text.MailMergeType.PRINTER
	vXJobArg1(4).Name = "FileNamePrefix" : vXJobArg1(4).Value = "Author"
	vXJobArg1(5).Name = "FileNameFromColumn" : vXJobArg1(5).Value = true
	vXJobArg1(6).Name = "DocumentURL" : vXJobArg1(6).Value = cTestDoc
	
	' use a XConnection
	vXJobArg2(0).Name = "ActiveConnection" : vXJobArg2(0).Value = oXConnection
	vXJobArg2(1).Name = "DataSourceName" : vXJobArg2(1).Value = cDataSourceName
	vXJobArg2(2).Name = "Command" : vXJobArg2(2).Value = cDataCommand
	vXJobArg2(3).Name = "CommandType" : vXJobArg2(3).Value = com.sun.star.sdb.CommandType.TABLE
	vXJobArg2(4).Name = "OutputType" : vXJobArg2(4).Value = com.sun.star.text.MailMergeType.FILE
	vXJobArg2(5).Name = "ResultSet" : vXJobArg2(5).Value = oXResultSet
	vXJobArg2(6).Name = "OutputURL" : vXJobArg2(6).Value = cOutputURL
	vXJobArg2(7).Name = "FileNamePrefix" : vXJobArg2(7).Value = "Identifier"
	vXJobArg2(8).Name = "FileNameFromColumn" : vXJobArg2(8).Value = true
	vXJobArg2(9).Name = "Selection" : vXJobArg2(9).Value = myBookMarks()
	
	vXJobArgs(0) = vXJobArg0()
	vXJobArgs(1) = vXJobArg1()
	vXJobArgs(2) = vXJobArg2()
	vXJobArgs(3) = DimArray()
    
	out.log("com.sun.star.text.MailMerge created.")
    
Exit Sub
ErrHndl:
    Test.Exception()
End Sub

Sub XMailMergeEvent()
    oObj.execute(vXJobArgs(2))
end sub

Function getXResultSet() as Variant
    Dim oRowSet as Object
    oRowSet = createUnoService("com.sun.star.sdb.RowSet")
    oRowSet.DataSourceName = cDataSourceName
    oRowSet.Command = cDataCommand
    oRowSet.CommandType = com.sun.star.sdb.CommandType.TABLE
    oRowSet.execute()
    wait(200)
'    oRowSet.getFirst()
    getXResultSet() = oRowSet 
end Function

Function getMyBookMarks() as Variant
    Dim BookMarks(1) as Variant
    oXResultSet.first()
    BookMarks(0) = oXResultSet.getBookmark()
    oXResultSet.next()
    BookMarks(1) = oXResultSet.getBookmark()
    getMyBookMarks() = BookMarks()
end Function

</script:module>
