Date: prev next · Thread: first prev next last
2012 Archives by date, by thread · List index



Hello, 

I have a basic script that I think would probably be easier and faster to
modify using Python.  It's pretty simple, it involves a MySQL database and a
base form. 

I suppose the confusing bit would be where the UNO components come in, how
are these handled in Python?

And just for reference here is the basic macro I'm toying around with:

' TODO Move this RSR stuff to the
'      library RSR Commons, as it
'      is duplicate code.
Type RSR
        DatabaseContext As Object
        DataSource As Object
        Connection As Object
        InteractionHandler As Object
        Statement As Object
        ResultSet As Object
        Query As String
End Type


Function RecordSetRetrievorFactrory()

        DIM registered_db_name As String
        Dim recSetRet As RSR

    registered_db_name = "DESP"
        recSetRet.DatabaseContext =
createUnoService("com.sun.star.sdb.DatabaseContext")

        recSetRet.DataSource =
recSetRet.DatabaseContext.getByName(registered_db_name)
        
        If Not recSetRet.DataSource.IsPasswordRequired Then
                recSetRet.Connection = recSetRet.DataSource.GetConnection("","")
        Else
                recSetRet.InteractionHandler =
createUnoService("com.sun.star.sdb.InteractionHandler")
                recSetRet.Connection =
recSetRet.DataSource.ConnectWithCompletion(recSetRet.InteractionHandler)
        End If

        RecordSetRetrievorFactrory = recSetRet
End Function

' Inputs
Global featureName As String ' The Feature Name to Update
Global despName As String        ' The DESP to Update 
Global setToState As Integer ' The state to set it to:
                                                                ' 5 Awaiting Response
                                                                ' 6 Supports It
                                                                ' 7 Will Support It
                                                                ' 8 Does NOT Support It


' UI Document and Form
Global Doc As Object
Global TheForm As Object

' UI Components
Global CbBoxFeature As Object
Global CbBoxDESPName As Object
Global cbUpdateToState As Object
Global txtDetails As Object
Global lblStatus As Object

Global formReady As Boolean 



' This method gets run when the page is loaded.
Sub Init(oEv)
  formReady = False
  
  ' Doc = ThisComponent
  ' TheForm = Doc.DrawPage.Forms.GetByName("Form")
  TheForm = oEv.Source
 
  CbBoxFeature = TheForm.getByName("CbBoxFeature")
  CbBoxDESPName = TheForm.getByName("CbBoxDESPName")
  cbUpdateToState = TheForm.getByName("cbUpdateToState")
  txtDetails = TheForm.getByName("txtDetails")
  lblStatus = TheForm.getByName("lblStatus")

  formReady = True

End Sub

Sub Main

  ' Init()

  Dim tempStateName As String
        
   ' Get Input Fields
   featureName = CbBoxFeature.text
   despName =  CbBoxDESPName.text
   tempStateName = cbUpdateToState.text
   setToState = getStateId(tempStateName)
   
   If setToState = -1 Then
        MsgBox "Error Retrieving Requestd Update State."
        Exit Sub
   End If

        Dim fk_desp_id AS Integer
        Dim fk_dfws_id As Integer
        Dim Feature_f_id As Integer
        
        IF getRecordForUpdate(fk_desp_id, fk_dfws_id, Feature_f_id) THEN
                'MsgBox "Got Record for Update: fk_desp_id: " & fk_desp_id & ",
fk_dfws_id: " & fk_dfws_id & ", Feature_f_id: " & Feature_f_id

                IF updateTheRecord(fk_desp_id, fk_dfws_id, Feature_f_id) THEN 
                        lblStatus.label = "Update Successful"
                Else
                        lblStatus.label = "Update Failed"
                End If
        ELSE
                lblStatus.label = "Error retrieving record for Update: fk_desp_id: " &
fk_desp_id & ", fk_dfws_id: " & fk_dfws_id & ", Feature_f_id: " &
Feature_f_id
                
                ' Missing Record? Add missing record?
                If fk_desp_id = 0 AND fk_dfws_id = 0 AND Feature_f_id = 0 THEN
                        
                End If
        End If
End Sub

Function getStateId(stateName As String) As Integer
        Dim rsr As Object
        
        listLength = 0
        rsr = RecordSetRetrievorFactrory()
        
        rsr.Query = "SELECT dfws_id FROM DESP_Feature_Weak_State WHERE name=?"
        
        rsr.Statement = rsr.Connection.prepareStatement(rsr.Query)
        rsr.Statement.setString(1, stateName)   
        rsr.ResultSet = rsr.Statement.executeQuery()
        
        Dim loopIterations As Integer
        loopIterations = 0
        
        If Not isNULL(rsr.ResultSet) Then
                While rsr.ResultSet.next()
        
                        getStateId   = rsr.ResultSet.getShort(1)
                        
                        loopIterations = loopIterations + 1
                Wend
                
        Else
                MsgBox "No Records were returned for stateName: " & stateName 
                
                getStateId = -1
        End If
        
        rsr.Connection.Close()
        
        if loopIterations > 1 Then
                MsgBox "Error more than one record returned: stateName: " & stateName
                getStateId = -1
        Else 
                IF loopIterations = 1  THEN

                End If
        End If  

End Function

Function getRecordForUpdate(ByRef fk_desp_id AS Integer, _
                            ByRef fk_dfws_id As Integer, _
                            ByRef Feature_f_id As Integer) _ 
  As Boolean
  
        Dim rsr As Object
        
        listLength = 0
        rsr = RecordSetRetrievorFactrory()
        
        rsr.Query ="SELECT dfw.fk_desp_id, dfw.fk_dfws_id, dfw.Features_f_id,
desp.name AS DESP_Name, f.name AS FeatureName, dfws.name As ResponseState "
& _
"FROM " & _
"DESP_Feature_Weak_State dfws INNER JOIN (DigitalEditionsSolutionProvider
desp INNER JOIN (Features f INNER JOIN DESP_Features_Weak dfw ON f.f_id =
dfw.Features_f_id) ON desp.desp_id = dfw.fk_desp_id) ON dfws.dfws_id =
dfw.fk_dfws_id " & _
"WHERE f.name=? AND desp.name=? " & _
"ORDER BY fk_desp_id;"

        'MsgBox featureName & featureName
        rsr.Statement = rsr.Connection.prepareStatement(rsr.Query)
        rsr.Statement.setString(1, featureName)
        rsr.Statement.setString(2, despName)
        rsr.ResultSet = rsr.Statement.executeQuery()
        
        Dim loopIterations As Integer
        loopIterations = 0
        
        If Not isNULL(rsr.ResultSet) Then
                While rsr.ResultSet.next()
        
                        fk_desp_id   = rsr.ResultSet.getShort(1)
                        fk_dfws_id   = rsr.ResultSet.getShort(2)
                        Feature_f_id = rsr.ResultSet.getShort(3)
                        
                        loopIterations = loopIterations + 1
                Wend
        Else
                MsgBox "No Records were returned for featureName: " & featureName & ",
despName: " & despName
                
                getRecordForUpdate = False
        End If
        
        rsr.Connection.Close()
        
        if loopIterations > 1 Then
                MsgBox "Error more than one record returned: featureName: " & featureName
& ", despName: " & despName
                getRecordForUpdate = False
        Else 
                IF loopIterations = 1  THEN
                        getRecordForUpdate = True
                End If
        End If
        
        
End Function

Function updateTheRecord(fk_desp_id AS Integer, _
                         fk_dfws_id As Integer, _
                         Feature_f_id As Integer) As Boolean
        Dim rsr As Object
        
        rsr = RecordSetRetrievorFactrory()
        rsr.Query = "UPDATE DESP_Features_Weak SET fk_dfws_id=?, details=? WHERE
fk_desp_id=? AND fk_dfws_id=? AND Features_f_id=?"
        
        rsr.Statement = rsr.Connection.prepareStatement(rsr.Query)
        rsr.Statement.setShort(1, setToState)
        rsr.Statement.setString(2, txtDetails.text)
        rsr.Statement.setShort(3, fk_desp_id)
        rsr.Statement.setShort(4, fk_dfws_id)
        rsr.Statement.setShort(5, Feature_f_id)
        
        updateTheRecord = rsr.Statement.executeUpdate()
        
        rsr.Connection.close()
End Function

Sub clearResults
        'IF formReady THEN
                lblStatus.label = ""
        'End IF
End Sub


Thank you, 
    Andrew J. Leer

--
View this message in context: 
http://nabble.documentfoundation.org/How-to-convert-a-basic-script-into-Python-tp3986621.html
Sent from the Users mailing list archive at Nabble.com.

-- 
For unsubscribe instructions e-mail to: users+help@global.libreoffice.org
Problems? http://www.libreoffice.org/get-help/mailing-lists/how-to-unsubscribe/
Posting guidelines + more: http://wiki.documentfoundation.org/Netiquette
List archive: http://listarchives.libreoffice.org/global/users/
All messages sent to this list will be publicly archived and cannot be deleted

Context


Privacy Policy | Impressum (Legal Info) | Copyright information: Unless otherwise specified, all text and images on this website are licensed under the Creative Commons Attribution-Share Alike 3.0 License. This does not include the source code of LibreOffice, which is licensed under the Mozilla Public License (MPLv2). "LibreOffice" and "The Document Foundation" are registered trademarks of their corresponding registered owners or are in actual use as trademarks in one or more countries. Their respective logos and icons are also subject to international copyright laws. Use thereof is explained in our trademark policy.