Add a comment to clarify what kind of inputs the class handles
[LibreOffice.git] / wizards / source / sfdatabases / SF_Register.xba
blobe1b752f7f107e349bae97e987faf40a5c18cae16
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 =======================================================================================================================
9 Option Compatible
10 Option Explicit
12 &apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;
13 &apos;&apos;&apos; SF_Register
14 &apos;&apos;&apos; ===========
15 &apos;&apos;&apos; The ScriptForge framework includes
16 &apos;&apos;&apos; the master ScriptForge library
17 &apos;&apos;&apos; a number of &quot;associated&quot; libraries SF*
18 &apos;&apos;&apos; any user/contributor extension wanting to fit into the framework
19 &apos;&apos;&apos;
20 &apos;&apos;&apos; The main methods in this module allow the current library to cling to ScriptForge
21 &apos;&apos;&apos; - RegisterScriptServices
22 &apos;&apos;&apos; Register the list of services implemented by the current library
23 &apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;&apos;
25 REM ================================================================== EXCEPTIONS
27 Private Const BASEDOCUMENTOPENERROR = &quot;BASEDOCUMENTOPENERROR&quot;
28 Private Const DBCONNECTERROR = &quot;DBCONNECTERROR&quot;
30 REM ============================================================== PUBLIC METHODS
32 REM -----------------------------------------------------------------------------
33 Public Sub RegisterScriptServices() As Variant
34 &apos;&apos;&apos; Register into ScriptForge the list of the services implemented by the current library
35 &apos;&apos;&apos; Each library pertaining to the framework must implement its own version of this method
36 &apos;&apos;&apos;
37 &apos;&apos;&apos; It consists in successive calls to the RegisterService() and RegisterEventManager() methods
38 &apos;&apos;&apos; with 2 arguments:
39 &apos;&apos;&apos; ServiceName: the name of the service as a case-insensitive string
40 &apos;&apos;&apos; ServiceReference: the reference as an object
41 &apos;&apos;&apos; If the reference refers to a module, then return the module as an object:
42 &apos;&apos;&apos; GlobalScope.Library.Module
43 &apos;&apos;&apos; If the reference is a class instance, then return a string referring to the method
44 &apos;&apos;&apos; containing the New statement creating the instance
45 &apos;&apos;&apos; &quot;libraryname.modulename.function&quot;
47 With GlobalScope.ScriptForge.SF_Services
48 .RegisterService(&quot;Database&quot;, &quot;SFDatabases.SF_Register._NewDatabase&quot;) &apos; Reference to the function initializing the service
49 .RegisterService(&quot;DatabaseFromDocument&quot;, &quot;SFDatabases.SF_Register._NewDatabaseFromSource&quot;)
50 .RegisterService(&quot;Datasheet&quot;, &quot;SFDatabases.SF_Register._NewDatasheet&quot;)
51 End With
53 End Sub &apos; SFDatabases.SF_Register.RegisterScriptServices
55 REM =========================================================== PRIVATE FUNCTIONS
57 REM -----------------------------------------------------------------------------
58 Public Function _NewDatabase(Optional ByVal pvArgs As Variant) As Object
59 &apos;&apos;&apos; Create a new instance of the SF_Database class
60 &apos;&apos;&apos; Args:
61 &apos;&apos;&apos; FileName : the name of the file (compliant with the SF_FileSystem.FileNaming notation)
62 &apos;&apos;&apos; RegistrationName: mutually exclusive with FileName. Used when database is registered
63 &apos;&apos;&apos; ReadOnly : (boolean). Default = True
64 &apos;&apos;&apos; User : connection parameters
65 &apos;&apos;&apos; Password
66 &apos;&apos;&apos; Returns:
67 &apos;&apos;&apos; The instance or Nothing
68 &apos;&apos;&apos; Exceptions:
69 &apos;&apos;&apos; BASEDOCUMENTOPENERROR The database file could not be opened
70 &apos;&apos;&apos; DBCONNECTERROR The database could not be connected, credentials are probably wrong
72 Dim oDatabase As Object &apos; Return value
73 Dim vFileName As Variant &apos; alias of pvArgs(0)
74 Dim vRegistration As Variant &apos; Alias of pvArgs(1)
75 Dim vReadOnly As Variant &apos; Alias of pvArgs(2)
76 Dim vUser As Variant &apos; Alias of pvArgs(3)
77 Dim vPassword As Variant &apos; Alias of pvArgs(4)
78 Dim oDBContext As Object &apos; com.sun.star.sdb.DatabaseContext
79 Const cstService = &quot;SFDatabases.Database&quot;
80 Const cstGlobal = &quot;GlobalScope&quot;
82 If ScriptForge.SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
84 Check:
85 If IsMissing(pvArgs) Or IsEmpty(pvArgs) Then pvArgs = Array()
86 If UBound(pvArgs) &gt;= 0 Then vFileName = pvArgs(0) Else vFileName = &quot;&quot;
87 If IsEmpty(vFileName) Then vFileName = &quot;&quot;
88 If UBound(pvArgs) &gt;= 1 Then vRegistration = pvArgs(1) Else vRegistration = &quot;&quot;
89 If IsEmpty(vRegistration) Then vRegistration = &quot;&quot;
90 If UBound(pvArgs) &gt;= 2 Then vReadOnly = pvArgs(2) Else vReadOnly = True
91 If IsEmpty(vReadOnly) Then vReadOnly = True
92 If UBound(pvArgs) &gt;= 3 Then vUser = pvArgs(3) Else vUser = &quot;&quot;
93 If IsEmpty(vUser) Then vUser = &quot;&quot;
94 If UBound(pvArgs) &gt;= 4 Then vPassword = pvArgs(4) Else vPassword = &quot;&quot;
95 If IsEmpty(vPassword) Then vPassword = &quot;&quot;
96 If Not ScriptForge.SF_Utils._Validate(vFileName, &quot;FileName&quot;, V_STRING) Then GoTo Finally
97 If Not ScriptForge.SF_Utils._Validate(vRegistration, &quot;RegistrationName&quot;, V_STRING) Then GoTo Finally
98 If Not ScriptForge.SF_Utils._Validate(vReadOnly, &quot;ReadOnly&quot;, ScriptForge.V_BOOLEAN) Then GoTo Finally
99 If Not ScriptForge.SF_Utils._Validate(vUser, &quot;User&quot;, V_STRING) Then GoTo Finally
100 If Not ScriptForge.SF_Utils._Validate(vPassword, &quot;Password&quot;, V_STRING) Then GoTo Finally
101 Set oDatabase = Nothing
103 &apos; Check the existence of FileName
104 With ScriptForge
105 Set oDBContext = .SF_Utils._GetUNOService(&quot;DatabaseContext&quot;)
106 If Len(vFileName) = 0 Then &apos; FileName has precedence over RegistrationName
107 If Len(vRegistration) = 0 Then GoTo CatchError
108 If Not oDBContext.hasRegisteredDatabase(vRegistration) Then GoTo CatchError
109 vFileName = .SF_FileSystem._ConvertFromUrl(oDBContext.getDatabaseLocation(vRegistration))
110 End If
111 If Not .SF_FileSystem.FileExists(vFileName) Then GoTo CatchError
112 End With
114 Try:
115 &apos; Create the database Basic object and initialize attributes
116 Set oDatabase = New SF_Database
117 With oDatabase
118 Set .[Me] = oDatabase
119 ._Location = ConvertToUrl(vFileName)
120 Set ._DataSource = oDBContext.getByName(._Location)
121 If ScriptForge.SF_Utils._ErrorHandling() Then On Local Error GoTo CatchConnect
122 Set ._Connection = ._DataSource.getConnection(vUser, vPassword)
123 If IsNull(._Connection) Then GoTo CatchConnect
124 ._User = vUser
125 ._Password = vPassword
126 ._ReadOnly = vReadOnly
127 Set ._MetaData = ._Connection.MetaData
128 ._URL = ._MetaData.URL
129 End With
131 Finally:
132 Set _NewDatabase = oDatabase
133 Exit Function
134 Catch:
135 GoTo Finally
136 CatchError:
137 ScriptForge.SF_Exception.RaiseFatal(BASEDOCUMENTOPENERROR, &quot;FileName&quot;, vFileName, &quot;RegistrationName&quot;, vRegistration)
138 GoTo Finally
139 CatchConnect:
140 ScriptForge.SF_Exception.RaiseFatal(DBCONNECTERROR, &quot;User&quot;, vUser, &quot;Password&quot;, vPassword, vFileName)
141 GoTo Finally
142 End Function &apos; SFDatabases.SF_Register._NewDatabase
144 REM -----------------------------------------------------------------------------
145 Public Function _NewDatabaseFromSource(Optional ByVal pvArgs As Variant) As Object
146 &apos; ByRef oDataSource As Object _
147 &apos; , ByVal sUser As String _
148 &apos; , ByVal sPassword As String _
149 &apos; ) As Object
150 &apos;&apos;&apos; Create a new instance of the SF_Database class from the given datasource
151 &apos;&apos;&apos; established in the SFDocuments.Base service
152 &apos;&apos;&apos; THIS SERVICE MUST NOT BE CALLED FROM A USER SCRIPT
153 &apos;&apos;&apos; Args:
154 &apos;&apos;&apos; oDataSource: com.sun.star.sdbc.XDataSource
155 &apos;&apos;&apos; sUser, sPassword : connection parameters
156 &apos;&apos;&apos; Returns:
157 &apos;&apos;&apos; The instance or Nothing
158 &apos;&apos;&apos; Exceptions:
159 &apos;&apos;&apos; managed in the calling routines when Nothing is returned
161 Dim oDatabase As Object &apos; Return value
162 Dim oConnection As Object &apos; com.sun.star.sdbc.XConnection
163 Dim oDataSource As Object &apos; Alias of pvArgs(0)
164 Dim sUser As String &apos; Alias of pvArgs(1)
165 Dim sPassword As String &apos; Alias of pvArgs(2)
167 If ScriptForge.SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
168 Set oDatabase = Nothing
170 Try:
171 &apos; Get arguments
172 Set oDataSource = pvArgs(0)
173 sUser = pvArgs(1)
174 sPassword = pvArgs(2)
176 &apos; Setup the connection
177 If oDataSource.IsPasswordRequired Then
178 Set oConnection = oDataSource.getConnection(sUser, sPassword)
179 Else
180 Set oConnection = oDataSource.getConnection(&quot;&quot;, &quot;&quot;)
181 End If
183 &apos; Create the database Basic object and initialize attributes
184 If Not IsNull(oConnection) Then
185 Set oDatabase = New SF_Database
186 With oDatabase
187 Set .[Me] = oDatabase
188 ._Location = &quot;&quot;
189 Set ._DataSource = oDataSource
190 Set ._Connection = oConnection
191 ._ReadOnly = oConnection.isReadOnly()
192 Set ._MetaData = oConnection.MetaData
193 ._URL = ._MetaData.URL
194 End With
195 End If
197 Finally:
198 Set _NewDatabaseFromSource = oDatabase
199 Exit Function
200 Catch:
201 ScriptForge.SF_Exception.Clear()
202 GoTo Finally
203 End Function &apos; SFDatabases.SF_Register._NewDatabaseFromSource
205 REM -----------------------------------------------------------------------------
206 Public Function _NewDatasheet(Optional ByVal pvArgs As Variant) As Object
207 &apos; Optional ByRef poComponent As Object _
208 &apos; , Optional ByRef poParent As Object _
209 &apos; ) As Object
210 &apos;&apos;&apos; Create a new instance of the SF_Datasheet class
211 &apos;&apos;&apos; Called from
212 &apos;&apos;&apos; base.Datasheets()
213 &apos;&apos;&apos; base.OpenTable()
214 &apos;&apos;&apos; base.OpenQuery()
215 &apos;&apos;&apos; database.OpenTable()
216 &apos;&apos;&apos; database.OpenQuery()
217 &apos;&apos;&apos; database.OpenSql()
218 &apos;&apos;&apos; Args:
219 &apos;&apos;&apos; Component: the component of the new datasheet
220 &apos;&apos;&apos; com.sun.star.lang.XComponent - org.openoffice.comp.dbu.ODatasourceBrowser
221 &apos;&apos;&apos; Parent: the parent SF_Database or SF_Base instance having produced the new datasheet
222 &apos;&apos;&apos; When absent, the SF_Database instance will be derived from the component
223 &apos;&apos;&apos; Returns:
224 &apos;&apos;&apos; The instance or Nothing
226 Dim oDatasheet As Object &apos; Return value
227 Dim oParent As Object &apos; The parent SF_Database or SF_Base instance having produced the new datasheet
228 Dim oComponent As Object &apos; The component of the new datasheet
229 Dim oWindow As Object &apos; ui.Window user-defined type
230 Dim oUi As Object : Set oUi = ScriptForge.SF_Services.CreateScriptService(&quot;ScriptForge.UI&quot;)
232 Const TABLEDATA = &quot;TableData&quot;
233 Const QUERYDATA = &quot;QueryData&quot;
234 Const SQLDATA = &quot;SqlData&quot;
236 If ScriptForge.SF_Utils._ErrorHandling() Then On Local Error GoTo Catch
237 Set oDatasheet = Nothing
239 Check:
240 &apos; Get, check and assign arguments
241 If Not IsArray(pvArgs) Then GoTo Catch
242 If UBound(pvArgs) &gt;= 0 Then
243 Set oComponent = pvArgs(0)
244 End If
245 If UBound(pvArgs) = 0 Then
246 Set oParent = Nothing
247 ElseIf UBound(pvArgs) = 1 Then
248 Set oParent = pvArgs(1)
249 Else
250 GoTo Catch
251 End If
253 &apos; Check the validity of the proposed window: is it really a datasheet ? Otherwise, do nothing
254 If IsNull(oComponent) Then GoTo Catch
255 Set oWindow = oUi._IdentifyWindow(oComponent)
256 With oWindow
257 If .DocumentType &lt;&gt; TABLEDATA And .DocumentType &lt;&gt; QUERYDATA And .DocumentType &lt;&gt; SQLDATA Then GoTo Catch
258 End With
259 If IsEmpty(oComponent.Selection) Then GoTo Catch
261 Try:
262 Set oDatasheet = New SF_Datasheet
263 With oDatasheet
264 Set .[Me] = oDatasheet
265 Set .[_Parent] = oParent
266 Set ._Component = oComponent
267 &apos; Achieve the initialization
268 ._Initialize()
269 End With
271 Finally:
272 Set _NewDatasheet = oDatasheet
273 Exit Function
274 Catch:
275 Set oDatasheet = Nothing
276 GoTo Finally
277 End Function &apos; SFDatabases.SF_Register._NewDatasheet
279 REM ============================================== END OF SFDATABASES.SF_REGISTER
280 </script:module>