Search This Blog

Tuesday 15 December 2009

Open Office Macro

 Here comes a macro for Open Office Writer, written in StarBasic.
It aims at realizing a sort of constraint logic between controls in a form.

In the first part there is a data structure which specify all the constraints
on three controls, where the first two a comboboxes and the last is a text area.

In the second part there is the handler for the "onChange" event,
which loads data according to the specifications in the data structure.


REM  *****  BASIC  *****
'------------------------------------------------------------------------
'------------------------------------------------------------------------
'------------------------------------------------------------------------
'data block
'------------------------------------------------------------------------
'------------------------------------------------------------------------
'------------------------------------------------------------------------
' tabella dependenciesTable(objName, value, dependentObj, valueSetID)
' Primary Key: objName, value, dependentOb
' associa per ogni oggetto 'objName'
' un oggetto dipendente 'dependentObj' che assumerĂ 
' il set di valori identificato da 'valueSetID' (vedere tabella valueSets)
' quando l'oggetto 'objName' assumerĂ  il valore 'value'
Global dependenciesTable(2, 3) As String

' tabella valueSets(valueSetID, value)
' Primary Key: valueSetID, value
' associa per ogni valueSetID un set di valori
Global valueSets(6, 1) As String
'------------------------------------------------------------------------
Sub Init
   
    dependenciesTable(0, 0) = "cb1"
    dependenciesTable(0, 1) = "uno"
    dependenciesTable(0, 2) = "cb2"
    dependenciesTable(0, 3) = "1"
   
    dependenciesTable(1, 0) = "cb1"
    dependenciesTable(1, 1) = "due"
    dependenciesTable(1, 2) = "cb2"
    dependenciesTable(1, 3) = "2"
   
    dependenciesTable(2, 0) = "cb1"
    dependenciesTable(2, 1) = "tre"
    dependenciesTable(2, 2) = "cb3"
    dependenciesTable(2, 3) = "3"
   

   
   
    valueSets(0, 0) = "1"
    valueSets(0, 1) = "obj1ValPerUno1"
   
    valueSets(1, 0) = "1"
    valueSets(1, 1) = "obj1ValPerUno2"
   
    valueSets(2, 0) = "1"
    valueSets(2, 1) = "obj1ValPerUno3"
   
    valueSets(3, 0) = "2"
    valueSets(3, 1) = "obj1ValPerDue1"
   
    valueSets(4, 0) = "2"
    valueSets(4, 1) = "obj1ValPerDue2"
   
    valueSets(5, 0) = "2"
    valueSets(5, 1) = "obj1ValPerDue3"
   
    valueSets(6, 0) = "3"
    valueSets(6, 1) = "selezionato il valore tre"

End Sub
'------------------------------------------------------------------------
'------------------------------------------------------------------------
'------------------------------------------------------------------------
'end of data block
'------------------------------------------------------------------------
'------------------------------------------------------------------------
'------------------------------------------------------------------------

'------------------------------------------------------------------------
'------------------------------------------------------------------------
'------------------------------------------------------------------------
'engine block
'------------------------------------------------------------------------
'------------------------------------------------------------------------
'------------------------------------------------------------------------
Sub Main
    Init()
End Sub
'------------------------------------------------------------------------
' ritorna nel parametro values il set dei valori
' da assegnare a un oggetto dependentObj
' quando l'oggetto ctrlName vale selectedVal
' il set di valori ritornati dipendono da che valore ha assunto il controllo che ha scatenato l'evento
' secondo le tabelle dependenciesTable e valueSets
Sub getValueSet(ctrlName As String, selectedVal As String, dependentObj As String, values)
    Dim valueId As String
   
    Dim firstTime As Integer
    firstTime = 1
   
    Dim last As Integer
    last = 0
   
    'selezione dell'id dei valori, nella tabella valueSets
    For I = 0 To UBound(dependenciesTable)
        If dependenciesTable(I, 0) = ctrlName AND dependenciesTable(I, 1) = selectedVal AND dependenciesTable(I, 2) = dependentObj Then
            valueId = dependenciesTable(I, 3) ' id dei valori selezionato
            Exit For
        End If
    Next

    ' ricerca dei valori in valueSets in base all'id
    For I = 0 To UBound(valueSets)
        If valueSets(I, 0) = valueId Then
            If FirstTime = 0 Then
                last = last + 1
                Redim Preserve values(last)
            End If
            values(last) = valueSets(I, 1)
        End If
        firstTime = 0
    Next
End Sub
'------------------------------------------------------------------------
' ritorna nel parametro res l'elenco degli oggetti dipendenti
' dal controllo ctrlName quando il suo valore è ctrlValue
' i controlli di questo elenco dipendono da che valore ha assunto il controllo che ha scatenato l'evento
' secondo la tabella dependenciesTable
Sub getDependentObjsId(ctrlName As String, ctrlValue As String, res)
    Dim firstTime As Integer
    firstTime = 1
   
    Dim last As Integer
    last = 0
   
    For I = 0 To UBound(dependenciesTable)
        If dependenciesTable(I, 0) = ctrlName AND dependenciesTable(I, 1) = ctrlValue AND res(last) <> dependenciesTable(I, 2) Then 'last clause helps in preventing from inserting duplicates (not guaranteeing)
            If FirstTime = 0 Then
                last = last + 1
                Redim Preserve res(last)
            End If
            res(last) = dependenciesTable(I, 2)
        End If
        firstTime = 0
    Next
   
End Sub
'------------------------------------------------------------------------
 Sub OnChangeValue(oEvent)
    oControl = oEvent.Source.Model
     oForm = oControl.Parent
    Dim valueId As String
   
    Dim depObjs(0) As String
   
    'popola l'array depObjs con l'elenco dei controlli dipendenti dall'evento corrente
    getDependentObjsId(oControl.Name,  oControl.Text, depObjs)
   
    Dim textVar As String
   
    For I = 0 To UBound(depObjs) ' per ogni elemento dipendente
        Dim listValues(0)
        listValues(0) = ""
       
        getValueSet(oControl.Name, oControl.Text, depObjs(I), listValues) 'valori
       
        For K = 0 to oForm.Count - 1
            If oForm.getByIndex(K).Name = depObjs(I) Then
                If hasProperty(oForm.getByIndex(K), "StringItemList") = 1 Then 'ComboBox
                    oForm.getByIndex(K).StringItemList = listValues
                    oForm.getByIndex(K).Text = ""
                Else 'altro caso
                    textVar = ""
                    For valIdx = 0 To UBound(listValues)
                        textVar = "" & textVar & listValues(valIdx)
                    Next
                    oForm.getByIndex(K).Text = textVar
                End If
                Exit For
             EndIf
        Next K
    Next

 End Sub
 '------------------------------------------------------------------------
 Function hasProperty( oObj, prop )
   oIntrospection = createUnoService( "com.sun.star.beans.Introspection" )
   oObjInfo = oIntrospection.inspect( oObj )
  
   oProperties = oObjInfo.getProperties( com.sun.star.beans.PropertyConcept.ALL )
   
   For i = LBound( oProperties ) To UBound( oProperties )
      oProperty = oProperties( i )
      cPropertyName = oProperty.Name
      If cPropertyName = prop Then
          hasProperty() = 1
          Exit Function
      End If
   Next
  
   hasProperty() = 0
  
End Function
'------------------------------------------------------------------------
'------------------------------------------------------------------------
'------------------------------------------------------------------------
'end of engine block
'------------------------------------------------------------------------
'------------------------------------------------------------------------
'------------------------------------------------------------------------

Saturday 12 December 2009

Linux keyboard remapping on X server

  • use xev to retrieve keycodes
  • use xmodmap  to load a remapping file
  • use the command setxkbmap to restore a keyboard setting, ex.
    the command "setxkbmap it" restore the it keyboard

Thursday 10 December 2009

Oracle parse number

how to parse as a number the string 1670.0000
TO_NUMBER('1670.0000','999999999999D0000','NLS_NUMERIC_CHARACTERS=''.,''')

note that the substring ., is enclosed within single quotes

Wednesday 9 December 2009

inspect Oracle db

to get a full list of all the objects in the data dictionary:

select * from dictionary order by table_name










show database version and parameters
select * from v$version
select * from nls_database_parameters

V$ Views
http://www.adp-gmbh.ch/ora/misc/dynamic_performance_views.html

Thursday 3 December 2009

Linux, information about the distribution

suppose you are asked to take care of a Linux system you've never seen before, and nobody is able to tell anything about it, at first you can get some information about the distribution and the kernel by using the commands


cat /etc/*-release
uname -a