Module cCJGridDataSource.pkg

     1Use ui
     2Use cCJGridDataSourceColumn.pkg
     3
     4Struct tDataSourceRow
     5    RowID riID
     6    Boolean bNewRow
     7    Variant vTag
     8    String[] sValue
     9End_Struct
    10
    11Function ComparetDataSourceRowId DESKTOP tDataSourceRow Row1 tDataSourceRow Row2 Returns Integer
    12    Function_Return (If(IsSameRowID(Row1.riID,Row2.riID),EQ,NE))    
    13End_Function
    14
    15Use cCJGridSortHandler.pkg
    16
    17Class cCJGridDataSource is a cObject
    18
    19    Procedure Construct_Object
    20        Forward Send Construct_Object
    21
    22        // all private
    23        { Visibility=Private }
    24        Property tDataSourceRow[] pDataSource
    25        { Visibility=Private }
    26        Property tDataSourceRow   pCurrentDataSourceRow
    27        { Visibility=Private }
    28        Property Boolean pbRowChanged False
    29        { Visibility=Private }
    30        Property Boolean[] pbCurrentColumnChanged
    31        { Visibility=Private }
    32        Property Handle[] pDataSourceColumnObjects
    33        { Visibility=Private }
    34        Property Integer piSelectedRow -1
    35        { Visibility=Private }
    36        Property Boolean pbDataSourceSynchRequired  False 
    37
    38    End_Procedure
    39
    40    // returns true if all data is loaded
    41    Function AllDataIsLoaded Returns Boolean
    42        Integer iRowCount
    43        Get RowCount to iRowCount
    44        Function_Return (iRowCount>0)
    45    End_Function
    46    
    47    // returns true if data is static meaning that once loaded there is no need to go
    48    // back to the external datasource to look for new or deleted rows
    49    Function DataIsStatic Returns Boolean
    50        Function_Return True
    51    End_Function
    52
    53    // This signifies that the datasource needs adjusting and that grid needs updating  
    54    Function DataSourceSynchRequired Returns Boolean
    55        Boolean bRequired
    56        Get pbDataSourceSynchRequired to bRequired
    57        Function_Return bRequired
    58    End_Function
    59    
    60    // Adds column metadata information to the datasource
    61    Procedure AddColumn Handle hoColumn
    62        Handle[] DataSourceColumnObjects
    63        Get pDataSourceColumnObjects to DataSourceColumnObjects
    64        Move hoColumn to DataSourceColumnObjects[SizeOfArray(DataSourceColumnObjects)]
    65        Set pDataSourceColumnObjects to DataSourceColumnObjects
    66    End_Procedure
    67    
    68    // returns entire datasource
    69    Function DataSource Returns tDataSourceRow[]
    70        tDataSourceRow[] DataSource
    71        Get pDataSource to DataSource
    72        Function_Return DataSource
    73    End_Function
    74        
    75    Function RowTag Integer iRow Returns RowID
    76        tDataSourceRow[] DataSource
    77        Get pDataSource to DataSource
    78        Function_Return DataSource[iRow].riID
    79    End_Function
    80
    81    Function RowCount Returns Integer
    82        tDataSourceRow[] DataSource
    83        Get pDataSource to DataSource
    84        Function_Return (SizeOfArray(DataSource))
    85    End_Function
    86
    87    { Visibility=Private }
    88    Function ColumnCount Returns Integer
    89        Handle[] DataSourceColumnObjects
    90        Get pDataSourceColumnObjects to DataSourceColumnObjects
    91        Function_Return (SizeOfArray(DataSourceColumnObjects))
    92    End_Function
    93
    94    Function DataForCell Integer iRow Integer iColumn Returns String
    95        tDataSourceRow[] DataSource
    96        tDataSourceRow CurrentDataSource
    97        Integer iSize iSel
    98        String sValue
    99        Get SelectedRow to iSel
   100        If (iSel=iRow) Begin
   101            Get pCurrentDataSourceRow to CurrentDataSource
   102            If (SizeOfArray(CurrentDataSource.sValue)>iColumn) Begin
   103                Move CurrentDataSource.sValue[iColumn] to sValue
   104            End
   105        End
   106        Else Begin
   107            Get pDataSource to DataSource
   108            Move (SizeOfArray(DataSource)) to iSize
   109            If (iRow>=0 and iRow<iSize and (SizeOfArray(DataSource[iRow].sValue)>iColumn)) Begin
   110                Move DataSource[iRow].sValue[iColumn] to sValue
   111            End
   112        End
   113        Function_Return sValue
   114    End_Function
   115
   116    Function NormalizeDataSource Integer iTopRow Returns Integer
   117        Set pbDataSourceSynchRequired to False
   118    End_Procedure
   119
   120    Procedure PageInRows Integer ByRef iFirstRow Integer ByRef iLastRow
   121    End_Procedure
   122    
   123    Function PageInMatchingRow RowID riId Returns Integer
   124    End_Function
   125   
   126    Procedure PageInLastRow
   127    End_Procedure
   128
   129    Procedure PageInFirstRow
   130    End_Procedure
   131
   132    Procedure SynchronizeDataForSelectedRow Boolean bPushData Integer iFlags 
   133    End_Procedure
   134            
   135    Procedure UpdateDataForSelectedRow Integer iColumn String sValue Boolean bUpdateExternalData
   136        tDataSourceRow DataSourceRow
   137        Boolean[] ColsChanged
   138        String sOldValue
   139        Integer iRow
   140        Handle[] DataSourceColumnObjects
   141        Get SelectedRow to iRow
   142        If (iRow>=0) Begin
   143            Get pCurrentDataSourceRow to DataSourceRow
   144            Get pDataSourceColumnObjects to DataSourceColumnObjects
   145            Move DataSourceRow.sValue[iColumn]  to sOldValue
   146            Move sValue to DataSourceRow.sValue[iColumn] 
   147            Set pCurrentDataSourceRow to DataSourceRow
   148            Set pbRowChanged to True
   149            Get pbCurrentColumnChanged to ColsChanged
   150            Move True to ColsChanged[iColumn]
   151            Set pbCurrentColumnChanged to ColsChanged
   152            Send NotifySelectedRowChanged of DataSourceColumnObjects[iColumn] sOldValue sValue
   153        End
   154    End_Procedure        
   155        
   156    Procedure SelectRow Integer iRow Boolean bUpdateExternalData
   157        tDataSourceRow[] DataSource
   158        tDataSourceRow DataSourceCurrent
   159        Boolean bOk
   160        Integer iRowCount iSelected
   161        
   162        Get SelectedRow to iSelected
   163        If (iSelected<>-1) Begin
   164            Send StoreSelectedRow
   165        End
   166
   167        Get RowCount to iRowCount
   168        Get SelectedRow to iSelected
   169        If (iSelected<>iRow) Begin
   170            If (iRow=-1 or (iRow<0) or (iRow>=iRowCount)) Begin
   171                Set piSelectedRow to -1
   172            End
   173            Else Begin
   174                Get pDataSource to DataSource
   175                Move DataSource[iRow] to DataSourceCurrent
   176                Set piSelectedRow to iRow
   177            End
   178            Send ClearChangedStates
   179            Set pCurrentDataSourceRow to DataSourceCurrent
   180        End
   181    End_Procedure
   182    
   183    { Visibility=Private }
   184    Procedure ClearChangedStates
   185        Boolean[] ColsChanged
   186        Integer iColCount
   187        Get ColumnCount to iColCount
   188        Set pbCurrentColumnChanged to (ResizeArray(ColsChanged,iColCount,False))
   189        Set pbRowChanged to False
   190    End_Procedure
   191    
   192    { Visibility=Private }
   193    Procedure StoreSelectedRow
   194        tDataSourceRow[] DataSource
   195        tDataSourceRow CurrentDataSourceRow
   196        Boolean bChange
   197        Integer iRow
   198        Get SelectedRow to iRow
   199        If (iRow>=0) Begin
   200            Get IsSelectedRowChanged to bChange
   201            If bChange Begin
   202                Get pDataSource to DataSource
   203                Get pCurrentDataSourceRow to CurrentDataSourceRow
   204                Move CurrentDataSourceRow to DataSource[iRow]
   205                Set pDataSource to DataSource
   206                Send ClearChangedStates
   207            End
   208        End
   209    End_Procedure
   210
   211  
   212    Procedure ResetSelectedRow
   213        tDataSourceRow[] DataSource
   214        Integer iRow
   215        Boolean bChange
   216        Get SelectedRow to iRow
   217        If (iRow>=0) Begin
   218            Get IsSelectedRowChanged to bChange
   219            If bChange Begin
   220                Get pDataSource to DataSource
   221                Set pCurrentDataSourceRow to DataSource[iRow]
   222                Send ClearChangedStates
   223            End
   224        End
   225    End_Procedure
   226    
   227    Function SelectedRow Returns Integer
   228        Integer iIndex
   229        Get piSelectedRow to iIndex
   230        Function_Return iIndex
   231    End_Function
   232
   233    Function SaveSelectedRow Returns Boolean
   234        tDataSourceRow[] DataSource
   235        tDataSourceRow CurrentDataSourceRow
   236        Integer iRow
   237        Get SelectedRow to iRow
   238        If (iRow>=0) Begin
   239            Get pDataSource to DataSource
   240            Get pCurrentDataSourceRow to CurrentDataSourceRow
   241            Move False to CurrentDataSourceRow.bNewRow
   242            Set pCurrentDataSourceRow to CurrentDataSourceRow
   243            Send StoreSelectedRow
   244        End
   245        Function_Return False
   246    End_Function
   247
   248    // deletes the row from the back end
   249    Function DeleteSelectedRow Returns Boolean
   250    End_Function
   251    
   252    
   253    Function ValidateSelectedRow Returns Handle
   254        Integer iCols iCol
   255        Boolean bError bCancel
   256        Handle[] DataSourceColumnObjects
   257        Get pDataSourceColumnObjects to DataSourceColumnObjects
   258        Move (SizeOfArray(DataSourceColumnObjects)) to iCols
   259        For iCol from 0 to (iCols-1)
   260            Get Validating of DataSourceColumnObjects[iCol] to bError
   261            If bError Begin
   262                Function_Return DataSourceColumnObjects[iCol]    
   263            End
   264        Loop
   265        Function_Return 0
   266    End_Function
   267
   268    Procedure RemoveRow Integer iIndex
   269        tDataSourceRow[] DataSource
   270        Integer iRowCount iSelect
   271        Send SelectRow -1 False
   272        Get pDataSource to DataSource
   273        Get RowCount to iRowCount
   274        If (iIndex>=0 and iIndex<iRowCount) Begin
   275            Move (RemoveFromArray(DataSource,iIndex)) to DataSource
   276        End
   277        Set pDataSource to DataSource
   278        Set pbDataSourceSynchRequired to True
   279    End_Procedure
   280
   281    Procedure InsertRow Integer iIndex
   282        tDataSourceRow[] DataSource
   283        tDataSourceRow DataSourceRow
   284        Integer iRowCount iSelect
   285        Send SelectRow -1 False
   286        Get pDataSource to DataSource
   287        Get CreateClearedDataSourceRow to DataSourceRow
   288        Move True to DataSourceRow.bNewRow
   289        Get RowCount to iRowCount
   290        If (iIndex>=0 and iIndex<iRowCount) Begin
   291            Move (InsertInArray(DataSource,iIndex,DataSourceRow)) to DataSource
   292        End
   293        Else If (iIndex=iRowCount) Begin
   294            Move DataSourceRow to DataSource[iRowCount]
   295        End
   296        Set pDataSource to DataSource
   297        Set pbDataSourceSynchRequired to True
   298    End_Procedure
   299    
   300        
   301    // Clears all data from datasource
   302    Procedure Reset
   303        tDataSourceRow[] DataSource
   304        Send SelectRow -1 False
   305        Set pDataSource to DataSource
   306        Set pbDataSourceSynchRequired to True
   307    End_Procedure
   308
   309    // resets everything - columns and data
   310    Procedure ResetAll
   311        Handle[] DataSourceColumnObjects
   312        Send Reset
   313        Set pDataSourceColumnObjects to DataSourceColumnObjects
   314    End_Procedure
   315                
   316    // created this to initialize the data. It can be left empty or used to load data up front        
   317    Procedure InitializeDataSource tDataSourceRow[] DataSource
   318        Send Reset
   319        Set pDataSource to DataSource
   320        Set pbDataSourceSynchRequired to False
   321    End_Procedure
   322    
   323    // called to fill a row with data. Often used to fill data from an external source.
   324    // The column object is called with the assumption that the external source (i.e., file buffers)
   325    // contain the data needed. Used by sub-classes
   326    Function CreateDataSourceRow Returns tDataSourceRow
   327        tDataSourceRow DataSourceRow
   328        Handle[] DataSourceColumnObjects
   329        Integer iColumn iColumns
   330        String sValue
   331        Get pDataSourceColumnObjects to DataSourceColumnObjects
   332        Move (SizeOfArray(DataSourceColumnObjects)) to iColumns
   333        For iColumn from 0 to (iColumns-1)
   334            Get InitialValue of DataSourceColumnObjects[iColumn] to sValue
   335            Move sValue to DataSourceRow.sValue[iColumn]
   336        Loop
   337        Function_Return DataSourceRow
   338    End_Function
   339    
   340    // Creates a cleared row. Used by subclasses
   341    { Visibility=Private }
   342    Function CreateClearedDataSourceRow Returns tDataSourceRow
   343        tDataSourceRow DataSourceRow
   344        Get CreateDataSourceRow to DataSourceRow
   345        Function_Return DataSourceRow
   346    End_Function
   347    
   348    Function IsSelectedRowNew Returns Boolean
   349        tDataSourceRow DataSourceRow
   350        Get pCurrentDataSourceRow to DataSourceRow
   351        Function_Return DataSourceRow.bNewRow    
   352    End_Function
   353
   354    Function IsSelectedRowChanged Returns Boolean
   355        Boolean bChanged
   356        Get pbRowChanged to bChanged
   357        Function_Return bChanged
   358    End_Function
   359
   360    Function ShouldSaveSelectedRow Returns Boolean
   361        Boolean bChanged bNewRow
   362        Get IsSelectedRowChanged to bChanged
   363        Get IsSelectedRowNew to bNewRow
   364        Function_Return (bChanged or bNewRow)
   365    End_Function
   366
   367    Function CanSaveRow Returns Boolean
   368        Function_Return True
   369    End_Function
   370
   371    Function CanDeleteRow Returns Boolean
   372        Function_Return True
   373    End_Function
   374    
   375    Function CanAddRow Returns Boolean
   376        Function_Return True
   377    End_Function
   378    
   379    // sort datasource data based on rules in GridSortRules. This only sorts loaded data
   380    // and it assumes that sorting it does not invalidate it some way
   381    Procedure SortDataSource tGridSortRules GridSortRules
   382        Handle hoGridSortHelper
   383        Integer iSel 
   384        tDataSourceRow[] DataSource
   385        tDataSourceRow[] NewDataSource
   386        Get pDataSource to DataSource
   387        Get SelectedRow to iSel
   388        Send SelectRow -1 False
   389        Get Create (RefClass(cGridSortHandler)) to hoGridSortHelper
   390        Get SortDataSource of hoGridSortHelper GridSortRules DataSource (&iSel) to NewDataSource 
   391        Send Destroy of hoGridSortHelper
   392        Set pDataSource to NewDataSource
   393        Send SelectRow iSel False
   394    End_Procedure
   395    
   396    // Find the value for this column in the datasource.
   397    // This is meant to be used when all data is loaded. bFindGE determines if this is a EQ (false)
   398    // or GE (True) find. iStartRow determines the row to start the search at.
   399    // This is datatype aware and if performs the proper search for the expected type.
   400    Function FindColumnValue Integer iColumn String sValue Boolean bFindGE Integer iStartRow Boolean bSearchUp Returns Integer
   401        String sValue2
   402        Date dDate1 dDate2
   403        DateTime dtDateTime1 dtDateTime2
   404        Number nNum1 nNum2
   405        Integer i iIndex iRows eType
   406        Handle[] DataSourceColumnObjects
   407        Handle hoCol 
   408        tDataSourceRow[] DataSource
   409        
   410        Get pDataSourceColumnObjects to DataSourceColumnObjects
   411        Move DataSourceColumnObjects[iColumn] to hoCol
   412        Get DataType of hoCol to eType
   413        
   414        If (eType=Ascii_Window or eType=Mask_Window) Begin
   415            Move (Uppercase(sValue)) to sValue
   416        End
   417        Else If (eType=Mask_Date_Window or eType=Date_Window) Begin
   418            Move sValue to dDate1
   419        End
   420        Else If (eType=Mask_Datetime_Window) Begin
   421            Move sValue to dtDateTime1
   422        End
   423        Else Begin
   424            Move sValue to nNum1
   425        End
   426        
   427        Get pDataSource to DataSource
   428        Move (SizeOfArray(DataSource)) to iRows
   429        Decrement iRows
   430        If (bSearchUp and iStartRow>0) Begin 
   431            Move (iStartRow min iRows) to iRows
   432            Move 0 to iStartRow
   433        End
   434        For i from iStartRow to iRows
   435        
   436            If bSearchUp Begin
   437                Move (iRows-i) to iIndex
   438            End
   439            Else Begin
   440                Move i to iIndex
   441            End
   442
   443            If (SizeOfArray(DataSource[iIndex].sValue)>iColumn) Begin
   444                Move DataSource[iIndex].sValue[iColumn] to sValue2
   445            End
   446            Else Begin
   447                Move "" to sValue2
   448            End
   449            
   450            If (eType=Ascii_Window or eType=Mask_Window) Begin
   451                Move (Uppercase(sValue2)) to sValue2
   452                If ((sValue=sValue2) or (bFindGe and (sValue<sValue2))) Begin
   453                    Function_Return iIndex
   454                End
   455            End
   456            Else If (eType=Mask_Date_Window or eType=Date_Window) Begin
   457                Move sValue2 to dDate2
   458                If ((dDate1=dDate2) or (bFindGe and (dDate1<dDate2))) Begin
   459                    Function_Return iIndex
   460                End
   461            End
   462            Else If (eType=Mask_Datetime_Window) Begin
   463                Move sValue2 to dtDateTime2
   464                If ((dtDateTime1=dtDateTime2) or (bFindGe and (dtDateTime1<dtDateTime2))) Begin
   465                    Function_Return iIndex
   466                End
   467            End
   468            Else Begin
   469                Move sValue2 to nNum2
   470                If ((nNum1=nNum2) or (bFindGe and (nNum1<nNum2))) Begin
   471                    Function_Return iIndex
   472                End
   473            End
   474        Loop
   475        Function_Return -1
   476    End_Function
   477    
   478    
   479    
   480End_Class
   481
   482