OpenOffice.org Forum at OOoForum.orgThe OpenOffice.org Forum
 
 [Home]   [FAQ]   [Search]   [Memberlist]   [Usergroups]   [Register
 [Profile]   [Log in to check your private messages]   [Log in

BASE: loadComponentFromURL hidden and than?!

 
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Macros and API
View previous topic :: View next topic  
Author Message
ejomi
Newbie
Newbie


Joined: 18 Apr 2012
Posts: 4
Location: Germany

PostPosted: Wed Apr 18, 2012 9:28 am    Post subject: BASE: loadComponentFromURL hidden and than?! Reply with quote

Hello everybody!

First: My english is not that good - so please be merciful, thank you!

I know how to open a hidden form via the "StarDesktop"-Object with its properties and I know how how to set it visible later using the "CurrentController.Frame.ContainerWindow". But if I load a hidden form via the "DataSource"-methode, the "CurrentController"-object is missing, so there is no way to reach the "ContainerWindow" and its methodes! Trying to set the properties "Hidden" to "FALSE" later, doesnt work.

How can I manage this problem?

Details:
The "DataSource"-methode is used, because I want the forms to be connected to an external database-server (PostgreSQL) during the runtime of the script only. This can be reached by using a datasource-object with username and password under the condition, that the database is registered with OpenOffice locally on the client-computer.

The reason for hiding the form while loading are some adjustments like positioning and resizing on the screen, customizing some labels etc. This causes unsightly effects on the screen, especially when you are loading a form with datas out of a external data-source.

Of course: instead opening a form with the "Hidden"-Argument, you can use the "Frame.ContainerWindow"-command at the very beginning too - but the bad visual effects on the screen are the same!

To be better understood, I will qoute two code-snipplets below, where you can see the problem.

1. Opening a form, using "StartDesktop":

Code:

Dim oDoc As Object
Dim aMediaDesc(0) as New com.sun.star.beans.PropertyValue
Dim sURL As String

sURL = "FORMNAME"

aMediaDesc(0).Name  = "Hidden"
aMediaDesc(0).Value = TRUE

oDoc = StarDesktop.loadComponentFromURL(sURL, "_default", 0, aMediaDesc)

' Now, the document is loaded hidden, so we can do something
' with it, before we show the ready product to the public
' ...
' ...

' We are finish - show the document:
Doc.getCurrentController().getFrame().getContainerWindow().setVisible(True)


2. Opening a form, using "DataSource":

Code:

Dim aMediaDesc(2) as New com.sun.star.beans.PropertyValue
Dim dbContext As Object, oDataSource As Object, oConnection As Object
Dim sURL As String

sURL = "FORMNAME"

' Look for the context of all local registered databases:
dbContext = createUnoService("com.sun.star.sdb.DatabaseContext")

' Get the datasource via its registered name:
oDataSource = dbContext.GetByName(REGISTEREDNAME)
   
' Connect temporarily to the database with username and password:
oConnection = oDataSource.GetConnection(USERNAME, PASSWORD)

' Get random access to the record-set:
oStatement = oConnection.createStatement()
oStatement.ResultSetType = 1004

' Get the formcontainer object:
oForms = oDataSource.DatabaseDocument.FormDocuments

' Customize the MediaDescriptor(s):
aMediaDesc(0).Name  = "OpenMode"
aMediaDesc(0).Value = "open"
aMediaDesc(1).Name  = "ActiveConnection"
aMediaDesc(1).Value = oConnection
aMediaDesc(2).Name  = "Hidden"
aMediaDesc(2).Value = True

' Open the form:
oDoc = oForms.loadComponentFromURL(sURL, "_default", 0, aMediaDesc)

' Now, the document is loaded hidden
' ... and still hidden !
' ... and forever hidden !!

' --- T H I S  D O E S N T  W O R K - N O  CURRENTCONTROLLER ---
oDoc.CurrentController.Frame.ContainerWindow.Visible = True


Wich is strange to me:
If I set the Property "Hidden" to "FALSE", there is no problem to reach the ContainerWindow and change its visibility!
But - as I said before - this methode causes flickering on the screen and makes me unhappy!

So - hopefully, everybody understands, what a have written here! Embarassed

Greatings from Dresden (Germany), yours:
"ejomi"
Back to top
View user's profile Send private message
RPG
Super User
Super User


Joined: 24 Apr 2008
Posts: 2696
Location: Apeldoorn, Netherland

PostPosted: Wed Apr 18, 2012 11:17 am    Post subject: Reply with quote

Hello

It seems to me you want open a form from a databasedocument while this databasedocument is not open. When you want follow this method you get a lot of problems so I think use stand alone forms. On the other forum are two sub forum for Base one for Tutorials and for for examples examples. I think study there the tutorials of Villeroy and or DACM.

Stand alone forms are normal writer documents.

Romke
_________________
OOo 3.4.5 on openSUSE 12.1
Use this forum : http://user.services.openoffice.org/en/forum
Back to top
View user's profile Send private message
RPG
Super User
Super User


Joined: 24 Apr 2008
Posts: 2696
Location: Apeldoorn, Netherland

PostPosted: Wed Apr 18, 2012 1:15 pm    Post subject: Reply with quote

Hello

Code:
sub OpenFormHiddenState

Dim aMediaDesc(2) as New com.sun.star.beans.PropertyValue
Dim dbContext As Object, oDataSource As Object, oConnection
Dim sURL As String
dim REGISTEREDNAME
dim USERNAME,PASSWORD
dim oForms,oDoc
USERNAME=""
PASSWORD=""
sURL = "notes"
REGISTEREDNAME="medic"
' Look for the context of all local registered databases:
dbContext = createUnoService("com.sun.star.sdb.DatabaseContext")

' Get the datasource via its registered name:
oDataSource = dbContext.GetByName(REGISTEREDNAME)
   
' Connect temporarily to the database with username and password:
oConnection = oDataSource.GetConnection(USERNAME, PASSWORD)

' Get random access to the record-set:
'oStatement = oConnection.createStatement()
'oStatement.ResultSetType = 1004

' Get the formcontainer object:
oForms = oDataSource.DatabaseDocument.FormDocuments

' Customize the MediaDescriptor(s):
aMediaDesc(0).Name  = "OpenMode"
aMediaDesc(0).Value = "open"
aMediaDesc(1).Name  = "ActiveConnection"
aMediaDesc(1).Value = oConnection
aMediaDesc(2).Name  = "Hidden"
aMediaDesc(2).Value = True

' Open the form:
oDoc = oForms.loadComponentFromURL(sURL, "_default", 0, aMediaDesc)
wait 500

aMediaDesc(0).Name  = "OpenMode"
aMediaDesc(0).Value = "open"
aMediaDesc(1).Name  = "ActiveConnection"
aMediaDesc(1).Value = oConnection
aMediaDesc(2).Name  = "Hidden"
aMediaDesc(2).Value = false

oDoc = oForms.loadComponentFromURL(sURL, "_default", 0, aMediaDesc)

end sub
You can use nearly the same code for opening the file in not hidden state. I did not expect it was possible but it works.

Define the mediadescriptor array again before the second call. It is possible some values can be changed.

The fact that you can open a form in a databasedocument without opening the databasedocument means not is good idea to do it. Real often we see new data is not stored in the database. So I think use stand alone forms.
_________________
OOo 3.4.5 on openSUSE 12.1
Use this forum : http://user.services.openoffice.org/en/forum
Back to top
View user's profile Send private message
ejomi
Newbie
Newbie


Joined: 18 Apr 2012
Posts: 4
Location: Germany

PostPosted: Thu Apr 19, 2012 3:58 am    Post subject: OpenFormHiddenState Reply with quote

RPG wrote:
Hello


Thank you for your quick reply!

Yes - your method works somehow, but it doesn't make much sense: for any customizing of the window-frame (e.g. resizing, repositioning, customizing menubars, switching scrollbars, update labels etc.) you need the frame-object and all its children. After reloading the form with the argument "Hidden = FALSE" of course you will have those missing obects - but everthing now ist visible again and therefore also our "flickering" while customizing the form ... and we have achieved nothing!

Please let me explain the background of my trouble:
Assuming, it is not possible to get through this above drescribed problem and in consideration of your objection, that it might be not a good idea, to open a form in a databasedocument without opening it, I have to think about alternatives. It seems, that there is no way than to connect to the database first before opening forms (there are a lot in my project). Only after this action its possible hiding the forms while customizing them.

Now the nucleus:
When the connection to a database once is established, EVERY form can be opend by the user without a new login (username + password). My idea was, to connect specific forms whith an autoexec-script which connects with a secret password during the runtime only. So, the user does not know the "real" login-information for the database but all his actions can be controlled by the script (e.g. restrictions for opening other forms ect.).

While I have to refuse my "terrific" idea, to open a form in a disconnected database I now have a conceptional question: How can I manage a form-collection that is easy to edit for the administrator (by logging in manualy) but not to open for "normal" users without the necessary login-infos?

Or maybe there is another workarround: Is it possible, to connect/disconnect the database-container before/after closing a form by script?

Thank you in advance for your nice encouragement!

Greatings from Dresden out of "good old germany":
ejomi

PS:
Yes, I know, that there could be also the possibility, to add different user-restrictions within the database-server. But for some reasons this method is not accessible with my existing server-configuration - sorry!
Back to top
View user's profile Send private message
RPG
Super User
Super User


Joined: 24 Apr 2008
Posts: 2696
Location: Apeldoorn, Netherland

PostPosted: Thu Apr 19, 2012 5:01 am    Post subject: Reply with quote

Hello

What you describe is to difficult for me. So maybe other people can answer this question. When I find some things I will post it.

When nobody other answer this question, read posts of Villeroy and or DACM special on the other forum. If I remember well some of this question are asked and answered sometime on the other forum

Maybe think to use an other tool than OOo-base.

Romke
_________________
OOo 3.4.5 on openSUSE 12.1
Use this forum : http://user.services.openoffice.org/en/forum
Back to top
View user's profile Send private message
caravas
General User
General User


Joined: 05 Sep 2011
Posts: 31

PostPosted: Sun Apr 22, 2012 2:34 pm    Post subject: Reply with quote

Hi!

I kind of have struggled with almost the same question as yours. I do recommend, as suggested by RPG, reading out the base forum, specially DACM and Villeroy Topics.

I have a small HSQLDB(2.2.Cool database running in multiple users environment. My forms are standalone(writer documents) and they use Base as a bridge to the HSQLDB database. Actually, base forms are just writer documents embedded in the Base file.
I need to control users access to the database and, as you've already realized, openoffice keeps the connections properties until is totally closed. So, if other user try to access the database from the same machine, he will achieve it easily without any login Name or passwords.
Using Stardesktop.Terminate (), to close openoffice, is a critical solution and I do not recommend it.
I tried to make a login procedure and setup the Datasource and Connection properties by macro, but doing this after/ while load process makes the forms ActiveConnection property left in blank. So, the user could not see any data, even after the login process. I reached some success using the following code:

Code:
REM OPEN DIALOG BOX
Sub load

   oDlgEntra = CreateUnoDialog(DialogLibraries.Library1.DlgEntra)
   oDlgEntra.setTitle("Get User Name and Password!")
   oDlgEntra.Execute()
   
End Sub

REM #####################################################################
REM MACRO CALLED BY DIALOG BOX
Sub Login
on error goto erro

      oUser = oDlgEntra.GetControl("UserName").Text
      oPassword = oDlgEntra.GetControl("UserPassword").Text
        
      if oUser = ""  or oPassword = "" then
         msgbox "Informe Usuário e Senha!"
         exit sub
      end if

  Database = CreateUnoService("com.sun.star.sdb.DatabaseContext")
  If Database.hasByName("MyDb") Then
      oSource = Database.getByName("MyDb")
  Else
     msgbox "Database does not exist!"
     oDoc = Thiscomponent
     oDoc.Close(True)
     exit sub
  End If
 
oSource = Database.getByName("MyDb")
oSource.User = oUser
oConnection = oSource.GetConnection("" & oUser & "", ""& oPassword & "")

oConnection.Parent.Password = oPassword
oForm = ThisComponent.Drawpage.Forms.GetByName("MainForm")
oSub = oForm.GetByName("SubForm01")
oSub2 = oForm.GetByName("SubForm02")
oForm.DataSourceName = "MyDb"
oForm.Command = "SELECT * FROM ""Ponto"" WHERE ""Matricula"" = " & "'" & oUser & "'" & " AND YEAR(""Entrada"") = YEAR(CURRENT_TIMESTAMP)"
oSub.Command = "SELECT * FROM ""HorasTrabalhadas"" WHERE ""Matricula"" = " & "'" & oUser & "'" & " AND YEAR(""Data"") = YEAR(CURRENT_TIMESTAMP)"
oSub2.Command = "SELECT * FROM ""OutrasHoras"" WHERE ""Matricula"" = " & "'" & oUser & "'" & " AND YEAR(""DataInicio"") = YEAR(CURRENT_TIMESTAMP)"

Doc = ThisComponent
Controller = Doc.CurrentController       REM Activate and Deactivate form design mode
Controller.setFormDesignMode(true)   REM forces the ActiveConnection to be updated.
Controller.setFormDesignMode(false)

oDlgEntra.EndExecute() 

exit sub

erro:

 if Err = 1 and InStr(Error$, "invalid authorization specification") > 0 then
    msgbox "Wrong User Name or Password!"
   oDlg.SetVisible(false)
   exit sub
 else
    msgbox "Connection Failure!"
  end if

End Sub
Back to top
View user's profile Send private message MSN Messenger
ejomi
Newbie
Newbie


Joined: 18 Apr 2012
Posts: 4
Location: Germany

PostPosted: Mon Apr 23, 2012 12:52 am    Post subject: Reply with quote

Thanks a lot folks, for your engagement.

I must say, that my method, connecting a form within a disconnected base works fine to me (Environment: WindowsXP-pro, OO-3.2.1, Posgres on a W2k3-Server connected with the old SDBC-Driver of OO.org). The only reason, looking for alternatives is the (may be) requirement, to be platform-independent. Therefor I want to have a stable coding - thats all.

However: It seems to me, that the only (secure) way to manage disconected forms is the use of outsourced (writer-)documents. I didnt work with this concept so far and will now start up a new project with this conditions - see how it works Rolling Eyes .

So long: Have a good time. I will post my (negative/positive) experience in a while.

ejomi
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Macros and API All times are GMT - 8 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group