1 'encoding UTF-8 Do not remove or change this line!
2 '**************************************************************************
3 '* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 '* Copyright 2008 by Sun Microsystems, Inc.
7 '* OpenOffice.org - a multi-platform office productivity suite
9 '* $RCSfile: tabletools.inc,v $
13 '* last change: $Author: jsi $ $Date: 2008-06-16 07:43:46 $
15 '* This file is part of OpenOffice.org.
17 '* OpenOffice.org is free software: you can redistribute it and/or modify
18 '* it under the terms of the GNU Lesser General Public License version 3
19 '* only, as published by the Free Software Foundation.
21 '* OpenOffice.org is distributed in the hope that it will be useful,
22 '* but WITHOUT ANY WARRANTY; without even the implied warranty of
23 '* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 '* GNU Lesser General Public License version 3 for more details
25 '* (a copy is included in the LICENSE file that accompanied this code).
27 '* You should have received a copy of the GNU Lesser General Public License
28 '* version 3 along with OpenOffice.org. If not, see
29 '* <http://www.openoffice.org/license.html>
30 '* for a copy of the LGPLv3 License.
32 '/************************************************************************
34 '* owner : marc.neumann@sun.com
36 '* short description : Tools for Tables
38 '************************************************************************
40 ' #1 fCloseRelationDesign
41 ' #1 fCloseTableDesign
43 ' #1 fCompareMatrixValues
49 ' #1 fOpenRelationDesign
50 ' #1 fOpenNew TableDesign
54 '\***********************************************************************
55 '------------------------------------------------------------------------------
56 function tCompareMatrixValues(sDSName as string, sRefFileUrl as string, cTable_or_Query as string, sTableName as string, iRowPos as integer, iColPos as integer, sWordSeperator as string) as integer
57 'sDSName = data source name f.e. Bibliography
58 'sRefFileUrl = URL to your reference file (os indipendent - extension .ttr) f.e. base\optional\input\reference_tables\test.ttr
59 'cTable_or_Query = differ between comparing reference file with a table (t) or a query-table (q)
60 'sTableName = name of table or query-table to compare with f.e. Table1
61 'iRowPos,iColPos = row and column amount of reference and comparing table (must have the same size ; begin with 1 ; 0 is leftout, but has to be added)
64 dim j as integer 'counter'
65 dim aMatrix(iRowPos, iColPos) as string
68 if cTable_or_Query = "t" then printlog "--- Compare Table with Reference Table ---"
69 if cTable_or_Query = "q" then printlog "--- Compare Query with Reference Table ---"
71 call fReadFile(sRefFileUrl, aMatrix(), sWordSeperator)
75 '/// open Beamer and open datasource
78 printlog "open database beamer"
80 '/// switch between table and querie search
81 dim bfind_ok as boolean 'placeholder
82 if cTable_or_Query = "t" then
83 bfind_ok = ffindTableInBeamer(sDSName,sTableName)
84 printlog "found proper Table: " & sTableName
86 if cTable_or_Query = "q" then
87 bfind_ok = ffindQuery(sDSName, sTableName)
88 printlog "found proper Query: " & sTableName
92 '/// Choosing for the right table
94 Kontext "DatabaseSelection"
95 DatabaseSelection.TypeKeys "<MOD1 SHIFT E>",TRUE ' # type CTRL + SHIFT + E to go from the DatabaseSelection to the TableView'
97 printlog "found and opened proper table"
98 '/// comparing data in table with reference table
99 printlog "start comparing data in table with reference table"
101 dim ivalue_ok as integer
103 TableView.TypeKeys "<TAB>",TRUE
104 TableView.TypeKeys "<HOME>",TRUE
105 TableView.TypeKeys "<HOME>",TRUE
106 for i = 1 to iRowPos -1
107 for j = 1 to iColPos -1
109 'here the values are read out and put in clipboard for comparing
110 TableView.TypeKeys "<MOD1 C>",TRUE
111 TableView.TypeKeys "<TAB>",TRUE
114 'printlog "getClipboard: " & getclipboard()
115 'printlog "matrix: " & aMatrix(i,j)
117 'here the values are compared
118 if getclipboard() <> aMatrix(i,j) then
119 ivalue_ok = ivalue_ok + 1
120 warnlog ivalue_ok & ". fault in pos(row/col): " & i & "/" & j & " - should be: " & aMatrix(i,j) & " but is: " & getclipboard()
122 'printlog ivalue_ok & ". No !!! foult but TEST - should be: " & aMatrix(i,j) & " but is: " & getclipboard()
127 if ivalue_ok = 0 then
128 printlog "** Comparing procedure without faults"
129 tCompareMatrixValues = ivalue_ok
131 warnlog "Comparing procedure went wrong: " + ivalue_ok + " differences found!"
132 tCompareMatrixValues = 1
135 if cTable_or_Query = "t" then warnlog "Table: " + sTableName + " not found - Test aborted"
136 if cTable_or_Query = "q" then warnlog "Table: " + sQueryName + " not found - Test aborted"
138 printlog "*** End Testcase"
144 '------------------------------------------------------------------------------
145 function fCreateRefFile(sDSName as string, cTable_or_Query as string, sTableName as string, sWordSeperator as string) as string
146 dim bfind_ok as boolean 'check if right table found
147 dim iNumber as Integer 'needed for open reference file
148 dim iColAmount as integer
149 dim iRowAmount as integer 'counter
150 dim sReadContCol as string
151 dim sContColMem as string 'read out content string and its memo for getting table column ammount
152 dim sCellValue as string
153 dim sCellValueMem as string 'read out content string and its memo for writing in reference table
155 dim sRefTableName as string
156 sRefTableName = ConvertPath("user\work\" & sTableName & ".ttr")
157 dim sRefFileUrl as string
158 sRefFileUrl = gOfficePath & sRefTableName
159 printlog "path for reference table: " & sRefFileUrl
164 '/// open Beamer and open datasource
167 printlog "open database beamer"
169 '/// switch between table and querie search
170 if cTable_or_Query = "t" then
171 printlog "search for proper Table: " & sTableName
172 bfind_ok = ffindTableInBeamer(sDSName,sTableName)
174 if cTable_or_Query = "q" then
175 printlog "search for proper Query: " & sTableName
176 bfind_ok = ffindQuery(sDSName, sTableName)
180 '/// jumping to the right table
182 Kontext "DatabaseSelection"
183 DatabaseSelection.TypeKeys "<MOD1 SHIFT E>",TRUE ' # type CTRL + SHIFT + E to go from the DatabaseSelection to the TableView'
186 '/// check the row and column amount
187 printlog "check the row and column amount"
189 sReadContCol = 1 'init
190 sContColMem = 1 'init
192 iRowAmount = AllRecords.caption()
193 while sReadContCol = sContColMem
194 sContColMem = sReadContCol
195 sReadContCol = CurrentRecord.getText()
196 TableView.TypeKeys "<TAB>",TRUE
197 TableView.TypeKeys "<TAB>",TRUE
198 iColAmount = iColAmount +1
200 iColAmount = iColAmount -1
201 'printlog "sReadContCol: " & sReadContCol
202 printlog "size of table (columns/rows): " & iColAmount & "/" & iRowAmount
203 fCreateRefFile = iColAmount & "," & iRowAmount
204 TableView.TypeKeys "<UP>",TRUE
205 TableView.TypeKeys "<HOME>",TRUE
206 TableView.TypeKeys "<HOME>",TRUE
209 '/// reading out values from table, create a reference table and input values in
210 printlog "start reading out values from table and writing in ref table"
212 dim i,j as integer 'counter
213 Open sRefFileUrl for Output as iNumber
214 for i = 1 to iRowAmount
215 for j = 1 to iColAmount
217 TableView.TypeKeys "<MOD1 C>"
218 sCellValue = getclipboard()
219 TableView.TypeKeys "<TAB>"
221 sCellValueMem = sCellValueMem & sWordSeperator
223 sCellValueMem = sCellValueMem & sCellValue
224 'printlog i & j & " inhalt: " & sCellValue
227 print #iNumber, sCellValueMem
233 '/// warning if table not found - without proper table the test make no sense
235 warnlog "Table: " + sTableName + " not found."
244 '------------------------------------------------------------------------------
245 function fReadFile(sRefFileUrl as string, aMatrix() as string, sWordSep as string)
246 'connecting and opening file
247 dim iNumber as Integer
249 'position and content variables
250 dim sColumn As String
251 dim iRowPos as integer
252 dim iColPos as integer
254 dim iPos1 as integer 'pointer for searching position
256 'init position in matrix
260 '/// open reference file and store it into an array (matrix)
261 printlog "search for the reference file, open it and store content in an array"
262 sRefFileUrl = gOfficePath + ConvertPath(sRefFileUrl)
263 Open sRefFileUrl for Input as iNumber
264 while not eof(iNumber)
265 Line Input #iNumber, sColumn
266 iPos1 = 1 'startposition
267 iPos = 1 'startposition
268 while iPos <> 0 'if iPos = 0 then no more sWordSeperator found -> end of column reached
269 iPos = Instr(iPos +1,sColumn,sWordSep) 'check column for sWordSeperator
270 'printlog "iPos: " & iPos
271 'printlog "iPos1: " & iPos1
272 if iPos <> 1 then '<> 1 -> no content found
273 aMatrix(iRowPos,iColPos) = Mid(sColumn,iPos1,iPos - iPos1) 'cut content and put into array
274 'printlog "iRow: " & iRowPos
275 'printlog "iCol: " & iColPos
276 'printlog "aMatrix: " & aMatrix(iRowPos,iColPos)
279 else warnlog "reference file is empty"
285 printlog "**end of reading out reference table"
287 '--------------------------------------------------------------------
288 function fCreateTable(aFieldTypeContent(),sTableName,optional sCatalog,optional sSchema)
289 '/// create a table with the given FieldTypes
290 '/// sSchema and sCatalog are optional.
291 '/// If the optional parameter is not given then it's set to ""
292 '/// <u>parameter:</u>
293 '/// <b>aFieldTypeContent:</b> an arry with the table field data
294 '/// the array have to look like the following
295 '/// array(1,1) = first_field_name
296 '/// array(1,2) = first_field_type
297 '/// array(2,1) = second_field_name
298 '/// array(2,2) = second_field_type
299 '/// ...
300 '/// <b>sTableName:</b> the name of the table
301 '/// <b><i>optional</i> sCatalog:</b> the name of the table catalog
302 '/// <b><i>optional</i> sSchema:</b> the name of the table schema
304 Dim i as integer 'counter
305 Dim iFieldNumber as integer 'counter
306 dim iNoDS as integer 'number of data source in listbox
307 Dim iNumberOfFieldTypes as integer 'field type memory
308 Dim iFieldTypesCounter as integer 'counter
309 Dim iIndex as integer 'counter
310 Dim sFieldType as string 'help var for fieldtypes
311 Dim sTypeName as String 'help var for fieldtypes
313 if ( IsMissing(sCatalog) ) then
317 if ( IsMissing(sSchema) ) then
321 call fDeleteTable(sTableName) ' delete the table if exists
323 call fOpenNewTableDesign
325 Kontext "TableDesignTable"
327 for iFieldNumber = 1 to ubound(aFieldTypeContent) - 1
329 printlog "create field : " + aFieldTypeContent(iFieldNumber,1)
330 Fieldname.TypeKeys aFieldTypeContent(iFieldNumber,1) , TRUE
331 Fieldname.TypeKeys "<RETURN>" , TRUE
333 sTypeName = "[ " + aFieldTypeContent(iFieldNumber,2) + " ]"
334 iNumberOfFieldTypes = FieldType.getitemCount()
335 for iFieldTypesCounter = 1 to iNumberOfFieldTypes
336 sFieldType = FieldType.getitemText(iFieldTypesCounter)
337 iIndex = Instr(sFieldType,sTypeName)
339 printlog "FieldType : " + sFieldType
340 iFieldTypesCounter = iNumberOfFieldTypes ' stops the for loop if proper fieldtype found'
343 '/// choose proper field type
344 FieldType.Select(sFieldType) 'listbox entry nr'
346 FieldType.TypeKeys "<RETURN>" , TRUE
347 Description.TypeKeys "<RETURN>" , TRUE
348 printlog "-------------------------------"
351 Kontext "TableDesignTable"
352 TableDesignTable.usemenu
353 MenuSelect MenuGetItemId (1)
355 menuselect MenuGetItemId (7)
357 Kontext "DatabaseTableSaveAs"
358 printlog "save table as "+ sTableName
359 TableName.setText sTableName
361 '/// catalog handling
362 if sCatalog <> "" then
363 if Catalog.IsVisible then
364 Catalog.SetText sCatalog
365 printlog "inserting catalog name: " + sCatalog
367 warnlog "The Catalog-Name could not be inserted"
371 if sSchema <> "" then
372 if Schema.IsVisible then
373 Schema.SetText sSchema
374 printlog "inserting schema name: " + sSchema
376 warnlog "The Schema-Name could not be inserted"
379 DatabaseTableSaveAs.OK
382 if Messagebox.Exists(1) then
384 printlog "create a primary key "
388 ' a sql exception appear. Try to get the error text
390 if Messagebox.Exists(1) then
391 MessageBox.Click 5 ' click the more button
392 Kontext "SQLException"
393 if SQLException.exists() then
394 warnlog Errortext.getText()
401 call fCloseTableDesign
404 '--------------------------------------------------------------------
405 function fDeleteTable(sTableName as string)
406 '/// delete the table with the given name
407 '/// <u>parameter:</u>
408 '/// <b>sTableName:</b> the table which shall be deleted
409 if ( fFindTable(sTableName) = true ) then
410 printlog "Table found press delete"
411 Kontext "ContainerView"
412 'TableTree.TypeKeys "<DELETE>" , true
413 Delete ' uno-Slot .uno:DB/Delete
423 '--------------------------------------------------------------------
424 function fOpenTable(sTableName as string)
425 '/// open the table with the given name
426 '/// <u>parameter:</u>
427 '/// <b>sTableName:</b> the table which shall be opened
428 if ( fFindTable(sTableName) = true ) then
429 printlog "Table found -> open"
430 Kontext "ContainerView"
431 OpenTable ' uno-Slot .uno:DB/Open
439 '--------------------------------------------------------------------
440 function fFindTable(sTableName as string)
441 '/// select the table with the given name in the table container
442 '/// <u>parameter:</u>
443 '/// <b>sTableName:</b> the table which shall be selected
445 Dim iNumbersOfTables as integer
448 Kontext "ContainerView"
454 if ( Not TableTree.exists(1) ) then
455 qaerrorlog "The table tree doesn't exists"
456 ' May be a messagebox appear click OK to close it
458 if MessageBox.exists(1) then
459 qaerrorlog MessageBox.getText()
460 while MessageBox.exists() ' sometimes there are more then 1 message boxe
467 iNumbersOfTables = TableTree.getItemCount()
469 ' this select the first entry
470 TableTree.TypeKeys "<HOME>"
471 TableTree.TypeKeys "<UP>"
474 for i = 1 to iNumbersOfTables
476 TableTree.TypeKeys "<ADD>"
478 'printlog "TableName.getItemCount = " + TableTree.getItemCount
479 if TableTree.getItemCount > iNumbersOfTables then
480 iNumbersOfTables = TableTree.getItemCount()
482 'printlog "TableName.getSeltext = " + TableTree.getSeltext
483 if TableTree.getSeltext = sTableName then
487 TableTree.TypeKeys "<DOWN>"
492 '--------------------------------------------------------------------
493 function fInsertIntoTable( aFieldContent(), sTableName)
494 '/// insert data into a table
495 '/// <u>parameter:</u>
496 '/// <b>aFieldContent:</b> an arry with the table data
497 '/// the array have to look like the following
498 '/// array(1,1) = first_value_for_first_record
499 '/// array(1,2) = second_value_for_first_record
500 '/// array(2,1) = first_value_for_second_record
501 '/// array(2,2) = second_value_for_second_record
502 '/// ...
503 '/// <b>sTableName:</b> the name of the table
505 Dim iCounterOfRecords as integer
506 Dim iCounterOfFields as integer
508 Dim iNumberOfRecords as integer
509 Dim iNumberOfFields as integer
511 iNumberOfRecords = ubound(aFieldContent)
512 iNumberOfFields = ubound(aFieldContent,2)
514 call fOpenTable(sTableName)
517 for iCounterOfRecords = 1 to iNumberOfRecords
518 for iCounterOfFields = 1 to iNumberOfFields
519 TableView.TypeKeys aFieldContent(iCounterOfRecords,iCounterOfFields),TRUE
520 TableView.TypeKeys "<TAB>",TRUE
530 '-------------------------------------------------------------------------
531 function fCloseTableDesign(optional bSave)
532 '/// close an open table design
533 '/// <u>parameter:</u>
534 '/// <b><i>optional</i> bSave:</b> if true then the table design is saved
538 Kontext "TableDesignTable"
539 TableDesignTable.UseMenu
540 ' bug file / close close the whole database
541 hMenuSelectNr(1) ' the file menu
542 hMenuSelectNr(4) ' the Close Window
544 'hMenuSelectNr(6) ' the window menu
545 'hMenuSelectNr(1) ' the Close Window
548 if Messagebox.Exists(3) then
552 fCloseTableDesign = true
554 '-------------------------------------------------------------------------
555 function fCloseTableView()
556 '/// close an open table view
557 '/// <u>parameter:</u>
563 ' bug file / close close the whole database
564 'hMenuSelectNr(1) ' the file menu
565 'hMenuSelectNr(4) ' the Close Window
567 hMenuSelectNr(6) ' the window menu
568 hMenuSelectNr(1) ' the Close Window
569 ' if messages box appear because of unsaved record click no in the dialog
571 if Messagebox.Exists(3) then
575 fCloseTableView = true
577 '-------------------------------------------------------------------------
578 function fOpenRelationDesign()
579 '/// open a new relation design
580 '/// <u>parameter:</u>
583 if ( Database.NotExists(3) ) then
584 fOpenRelationDesign = false
588 Database.MouseDown(50,50)
589 Database.MouseUp(50,50)
597 fOpenRelationDesign = true
599 '-------------------------------------------------------------------------
600 function fCloseRelationDesign(optional bSave)
601 '/// close an open relation design
602 '/// <u>parameter:</u>
603 '/// <b><i>optional</i> bSave:</b> if true then the relation design is saved
606 Kontext "RelationDesign"
607 RelationDesign.UseMenu
608 ' bug file / close close the whole database
609 hMenuSelectNr(1) ' the file menu
610 hMenuSelectNr(4) ' the Close Window
612 'hMenuSelectNr(6) ' the window menu
613 'hMenuSelectNr(1) ' the Close Window
616 if Messagebox.Exists(3) then
617 if ( IsMissing( bSave ) ) then
628 fCloseRelationDesign = true
630 '-------------------------------------------------------------------------
631 function fOpenNewTableDesign
632 printlog "fOpenNewTableDesign called"
634 printlog "check if Database exists"
637 Database.MouseDown(50,50)
638 Database.MouseUp(50,50)
640 if ( Database.NotExists(3) ) then
641 fOpenNewTableDesign = false
642 warnlog "The Database windows doesn't exists"
648 printlog "open new table design"
654 fOpenNewTableDesign = true
657 '-------------------------------------------------------------------------
658 function fOpenTableInDesign(sTableName as String)
659 printlog "fOpenTableInDesign called"
661 printlog "check if Database exists"
664 Database.MouseDown(50,50)
665 Database.MouseUp(50,50)
667 if ( Database.NotExists(3) ) then
668 fOpenTableInDesign = false
669 warnlog "The Database windows doesn't exists"
675 printlog "open new table design"
677 call fFindTable(sTableName)
681 Kontext "TableDesignTable"
682 if ( not TableDesignTable.exists(3) ) then
683 warnlog "The Table design doesn't open"
684 fOpenTableInDesign = false
688 fOpenTableInDesign = true
691 '-------------------------------------------------------------------------
692 function fFindTableInBeamer(sDSName1,sTableName1)
693 '/// select a table with the given name in the beamer
694 '/// <u>parameter:</u>
695 '/// <b>sDSName1:</b> the name of the datasource
696 '/// <b>sTableName1:</b> the name of the table
699 dim bfindTable as boolean
701 dim iNoTable as integer
704 Kontext "DatabaseBeamer"
705 Kontext "DatabaseSelection"
707 iNoDS = DatabaseSelection.getItemCount
709 DatabaseSelection.Select i
710 if DatabaseSelection.getText = sDSName1 then
716 if (bfindTable = false) then
717 warnlog "Datasource " + sDSName + " not found!"
720 printlog "Datasource " + sDSName + " found!"
721 ' I set the bfindTable flag again back to false
722 ' for the next test of the table
727 DatabaseSelection.TypeKeys "<ADD>" , true
729 DatabaseSelection.TypeKeys "<DOWN>" , true
731 DatabaseSelection.TypeKeys "<DOWN>" , true
733 DatabaseSelection.TypeKeys "<ADD>" , true
735 DatabaseSelection.TypeKeys "<DOWN>" , true
739 iNoTable = DatabaseSelection.getItemCount
741 ii = DatabaseSelection.GetSelIndex
742 for i = ii to iNoTable
743 DatabaseSelection.Select i
744 if DatabaseSelection.getText = sTableName1 then
751 fFindTableInBeamer = bfindTable
754 '-------------------------------------------------------------------------
755 function fStartTableWizard()
756 '/// start the table wizard
757 '/// <u>parameter:</u>
760 if ( Database.NotExists(3) ) then
761 fStartTableWizard = false
765 Database.MouseDown(50,50)
766 Database.MouseUp(50,50)
775 fStartTableWizard = true