SF_Register.xba 13 KB

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