1 <?xml version=
"1.0" encoding=
"UTF-8"?>
2 <!DOCTYPE script:module PUBLIC
"-//OpenOffice.org//DTD OfficeDocument 1.0//EN" "module.dtd">
3 <script:module xmlns:
script=
"http://openoffice.org/2000/script" script:
name=
"SF_Register" script:
language=
"StarBasic" script:
moduleType=
"normal">REM =======================================================================================================================
4 REM === The ScriptForge library and its associated libraries are part of the LibreOffice project. ===
5 REM === The SFDatabases library is one of the associated libraries. ===
6 REM === Full documentation is available on https://help.libreoffice.org/ ===
7 REM =======================================================================================================================
12 '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
13 ''' SF_Register
14 ''' ===========
15 ''' The ScriptForge framework includes
16 ''' the master ScriptForge library
17 ''' a number of
"associated
" libraries SF*
18 ''' any user/contributor extension wanting to fit into the framework
20 ''' The main methods in this module allow the current library to cling to ScriptForge
21 ''' - RegisterScriptServices
22 ''' Register the list of services implemented by the current library
23 '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
25 REM ================================================================== EXCEPTIONS
27 Private Const BASEDOCUMENTOPENERROR =
"BASEDOCUMENTOPENERROR
"
29 REM ============================================================== PUBLIC METHODS
31 REM -----------------------------------------------------------------------------
32 Public Sub RegisterScriptServices() As Variant
33 ''' Register into ScriptForge the list of the services implemented by the current library
34 ''' Each library pertaining to the framework must implement its own version of this method
36 ''' It consists in successive calls to the RegisterService() and RegisterEventManager() methods
37 ''' with
2 arguments:
38 ''' ServiceName: the name of the service as a case-insensitive string
39 ''' ServiceReference: the reference as an object
40 ''' If the reference refers to a module, then return the module as an object:
41 ''' GlobalScope.Library.Module
42 ''' If the reference is a class instance, then return a string referring to the method
43 ''' containing the New statement creating the instance
44 ''' "libraryname.modulename.function
"
46 With GlobalScope.ScriptForge.SF_Services
47 .RegisterService(
"Database
",
"SFDatabases.SF_Register._NewDatabase
")
' Reference to the function initializing the service
48 .RegisterService(
"DatabaseFromDocument
",
"SFDatabases.SF_Register._NewDatabaseFromSource
")
49 .RegisterService(
"Datasheet
",
"SFDatabases.SF_Register._NewDatasheet
")
52 End Sub
' SFDatabases.SF_Register.RegisterScriptServices
54 REM =========================================================== PRIVATE FUNCTIONS
56 REM -----------------------------------------------------------------------------
57 Public Function _NewDatabase(Optional ByVal pvArgs As Variant) As Object
58 ''' Create a new instance of the SF_Database class
59 ''' Args:
60 ''' FileName : the name of the file (compliant with the SF_FileSystem.FileNaming notation)
61 ''' RegistrationName: mutually exclusive with FileName. Used when database is registered
62 ''' ReadOnly : (boolean). Default = True
63 ''' User : connection parameters
64 ''' Password
65 ''' Returns:
66 ''' The instance or Nothing
67 ''' Exceptions:
68 ''' BASEDOCUMENTOPENERROR The database file could not be opened or connected
70 Dim oDatabase As Object
' Return value
71 Dim vFileName As Variant
' alias of pvArgs(
0)
72 Dim vRegistration As Variant
' Alias of pvArgs(
1)
73 Dim vReadOnly As Variant
' Alias of pvArgs(
2)
74 Dim vUser As Variant
' Alias of pvArgs(
3)
75 Dim vPassword As Variant
' Alias of pvArgs(
4)
76 Dim oDBContext As Object
' com.sun.star.sdb.DatabaseContext
77 Const cstService =
"SFDatabases.Database
"
78 Const cstGlobal =
"GlobalScope
"
80 If ScriptForge.SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
83 If IsMissing(pvArgs) Or IsEmpty(pvArgs) Then pvArgs = Array()
84 If UBound(pvArgs)
>=
0 Then vFileName = pvArgs(
0) Else vFileName =
""
85 If IsEmpty(vFileName) Then vFileName =
""
86 If UBound(pvArgs)
>=
1 Then vRegistration = pvArgs(
1) Else vRegistration =
""
87 If IsEmpty(vRegistration) Then vRegistration =
""
88 If UBound(pvArgs)
>=
2 Then vReadOnly = pvArgs(
2) Else vReadOnly = True
89 If IsEmpty(vReadOnly) Then vReadOnly = True
90 If UBound(pvArgs)
>=
3 Then vUser = pvArgs(
3) Else vUser =
""
91 If IsEmpty(vUser) Then vUser =
""
92 If UBound(pvArgs)
>=
4 Then vPassword = pvArgs(
4) Else vPassword =
""
93 If IsEmpty(vPassword) Then vPassword =
""
94 If Not ScriptForge.SF_Utils._Validate(vFileName,
"FileName
", V_STRING) Then GoTo Finally
95 If Not ScriptForge.SF_Utils._Validate(vRegistration,
"RegistrationName
", V_STRING) Then GoTo Finally
96 If Not ScriptForge.SF_Utils._Validate(vReadOnly,
"ReadOnly
", ScriptForge.V_BOOLEAN) Then GoTo Finally
97 If Not ScriptForge.SF_Utils._Validate(vUser,
"User
", V_STRING) Then GoTo Finally
98 If Not ScriptForge.SF_Utils._Validate(vPassword,
"Password
", V_STRING) Then GoTo Finally
99 Set oDatabase = Nothing
101 ' Check the existence of FileName
103 Set oDBContext = .SF_Utils._GetUNOService(
"DatabaseContext
")
104 If Len(vFileName) =
0 Then
' FileName has precedence over RegistrationName
105 If Len(vRegistration) =
0 Then GoTo CatchError
106 If Not oDBContext.hasRegisteredDatabase(vRegistration) Then GoTo CatchError
107 vFileName = .SF_FileSystem._ConvertFromUrl(oDBContext.getDatabaseLocation(vRegistration))
109 If Not .SF_FileSystem.FileExists(vFileName) Then GoTo CatchError
113 ' Create the database Basic object and initialize attributes
114 Set oDatabase = New SF_Database
116 Set .[Me] = oDatabase
117 ._Location = ConvertToUrl(vFileName)
118 Set ._DataSource = oDBContext.getByName(._Location)
119 Set ._Connection = ._DataSource.getConnection(vUser, vPassword)
120 ._ReadOnly = vReadOnly
121 Set ._MetaData = ._Connection.MetaData
122 ._URL = ._MetaData.URL
126 Set _NewDatabase = oDatabase
131 ScriptForge.SF_Exception.RaiseFatal(BASEDOCUMENTOPENERROR,
"FileName
", vFileName,
"RegistrationName
", vRegistration)
133 End Function
' SFDatabases.SF_Register._NewDatabase
135 REM -----------------------------------------------------------------------------
136 Public Function _NewDatabaseFromSource(Optional ByVal pvArgs As Variant) As Object
137 ' ByRef poDataSource As Object _
138 ' , ByVal psUser As String _
139 ' , ByVal psPassword As String _
141 ''' Create a new instance of the SF_Database class from the given datasource
142 ''' established in the SFDocuments.Base service
143 ''' THIS SERVICE MUST NOT BE CALLED FROM A USER SCRIPT
144 ''' Args:
145 ''' DataSource: com.sun.star.sdbc.XDataSource
146 ''' User, Password : connection parameters
147 ''' Returns:
148 ''' The instance or Nothing
149 ''' Exceptions:
150 ''' managed in the calling routines when Nothing is returned
152 Dim oDatabase As Object
' Return value
153 Dim oConnection As Object
' com.sun.star.sdbc.XConnection
154 Dim oDataSource As Object
' Alias of pvArgs(
0)
155 Dim sUser As String
' Alias of pvArgs(
1)
156 Dim sPassword As String
' Alias of pvArgs(
2)
158 If ScriptForge.SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
159 Set oDatabase = Nothing
163 Set oDataSource = pvArgs(
0)
165 sPassword = pvArgs(
2)
167 ' Setup the connection
168 If oDataSource.IsPasswordRequired Then
169 Set oConnection = oDataSource.getConnection(sUser, sPassword)
171 Set oConnection = oDataSource.getConnection(
"",
"")
174 ' Create the database Basic object and initialize attributes
175 If Not IsNull(oConnection) Then
176 Set oDatabase = New SF_Database
178 Set .[Me] = oDatabase
179 ._Location =
""
180 Set ._DataSource = oDataSource
181 Set ._Connection = oConnection
182 ._ReadOnly = oConnection.isReadOnly()
183 Set ._MetaData = oConnection.MetaData
184 ._URL = ._MetaData.URL
189 Set _NewDatabaseFromSource = oDatabase
193 End Function
' SFDatabases.SF_Register._NewDatabaseFromSource
195 REM -----------------------------------------------------------------------------
196 Public Function _NewDatasheet(Optional ByVal pvArgs As Variant) As Object
197 ' Optional ByRef poComponent As Object _
198 ' , Optional ByRef poParent As Object _
200 ''' Create a new instance of the SF_Datasheet class
201 ''' Called from
202 ''' base.Datasheets()
203 ''' base.OpenTable()
204 ''' base.OpenQuery()
205 ''' database.OpenTable()
206 ''' database.OpenQuery()
207 ''' database.OpenSql()
208 ''' Args:
209 ''' Component: the component of the new datasheet
210 ''' com.sun.star.lang.XComponent - org.openoffice.comp.dbu.ODatasourceBrowser
211 ''' Parent: the parent SF_Database or SF_Base instance having produced the new datasheet
212 ''' When absent, the SF_Database instance will be derived from the component
213 ''' Returns:
214 ''' The instance or Nothing
216 Dim oDatasheet As Object
' Return value
217 Dim oParent As Object
' The parent SF_Database or SF_Base instance having produced the new datasheet
218 Dim oComponent As Object
' The component of the new datasheet
219 Dim oWindow As Object
' ui.Window user-defined type
220 Dim oUi As Object : Set oUi = ScriptForge.SF_Services.CreateScriptService(
"ScriptForge.UI
")
222 Const TABLEDATA =
"TableData
"
223 Const QUERYDATA =
"QueryData
"
224 Const SQLDATA =
"SqlData
"
226 If ScriptForge.SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
227 Set oDatasheet = Nothing
230 ' Get, check and assign arguments
231 If Not IsArray(pvArgs) Then GoTo Catch
232 If UBound(pvArgs)
>=
0 Then
233 Set oComponent = pvArgs(
0)
235 If UBound(pvArgs) =
0 Then
236 Set oParent = Nothing
237 ElseIf UBound(pvArgs) =
1 Then
238 Set oParent = pvArgs(
1)
243 ' Check the validity of the proposed window: is it really a datasheet ? Otherwise, do nothing
244 If IsNull(oComponent) Then GoTo Catch
245 Set oWindow = oUi._IdentifyWindow(oComponent)
247 If .DocumentType
<> TABLEDATA And .DocumentType
<> QUERYDATA And .DocumentType
<> SQLDATA Then GoTo Catch
249 If IsEmpty(oComponent.Selection) Then GoTo Catch
252 Set oDatasheet = New SF_Datasheet
254 Set .[Me] = oDatasheet
255 Set .[_Parent] = oParent
256 Set ._Component = oComponent
257 ' Achieve the initialization
262 Set _NewDatasheet = oDatasheet
265 Set oDatasheet = Nothing
267 End Function
' SFDatabases.SF_Register._NewDatasheet
269 REM ============================================== END OF SFDATABASES.SF_REGISTER