Tip handling or restricting the insertion of lines into an array

If you are in a CRUD management, with header and lines to manage, you do not want a user to insert a line at a given position in the lines array. How can you do this?

The incorrect way to do this is to search a way to disable the "insert line" link in the user interface. This is incorrect despite the fact that it is how it happens in version 6.

Why is this incorrect? Because the code we have to implement must be robust and able to work in service mode, and thus must not consider that the user interface requests are authorized for the only reason that they could be sent.

The correct way is to control, at the instant when an insert line request is received, if the conditions are fulfilled. Otherwise the system should display an error.

This can be done by using the ADDLINE_BEFORE event. In this event:

  • ALINE contains the position where the insertion can be done.
  • CURPTH contains the path of the collection code.
  • this is the class in which the collection is found.

If an error displays, no insertion will occur.

The way to prevent from inserting a line is as follows:
# Code executed on $METHODS label, when ACTION is "ADDLINE_BEFORE" and CURPTH is the right collection
  [L]ASTATUS=Fmet This.ASETERROR("","You cannot insert lines in the collection"-CURPTH,[V]CST_AERROR)
  Return

Example

In a header and line entity you do not accept an insertion of a line in the following cases:

1) If a maximum cumulated amount has already been reached.

2) If the insertion is done between two lines having the same value for a given property (MYPROP).

The code that performs this control is the following:

# Code found in the DOCUMENT_CSTD script associated with DOCUMENT class
$METHODS
Case CURPTH
  When "DOCLINE" : Gosub DOCLINE
  ...
Endcase
Return
$DOCLINE
  Case CURPTH
     When "ADDLINE_BEFORE" : Gosub ADDLINE_BEFORE_DOCLINE
     ...
  Endcase
Return
$ADDLINE_BEFORE_DOCLINE
Local Integer PREV_POS, NEXT_POS, I
Local Decimal TOTAMOUNT
 # If the position of insertion is not the last one, we need to test the previous and next line
 If ALINE<>[V]CST_ALASTPOST and ALINE<>[V]CST_AFIRSTPOS
   # Search the index of the previous and the next line (in AORDER rank)
   For I=1 To maxtab(This.DOCLINE)
     If This.DOCLINE(I)<>Null and ALINE=This.DOCLINE(I).AORDER-1
        PREV_POS=I
        If NEXT_POS<>0 : Break : Endif
     Elsif This.DOCLINE(I)<>Null and ALINE=This.DOCLINE(I).AORDER
        NEXT_POS=I
        If PREV_POS<>0 : Break : Endif
     Endif
   Next I
   # Now let's test the values
   If This.DOCLINE(PREV_POS).MYPROP=This.DOCLINE(NEXT_POS).MYPROP
     [L]ASTATUS=Fmet this.ASETERROR("","No insert between 2 lines with the code "-This.DOCLINE(PREV_POS).MYPROP,[V]CST_AERROR)
     Return
   Endif
 Endif
 # The second test is simpler
 TOTAMOUNT=0
 For I=1 to maxtab(This.DOCLINE)
   If This.DOCLINE(I)<>Null and find(This.DOCLINE(I).ASTATUS, [V]CST_ADEL,[V]CST_ANEWDEL)=0
     TOTAMOUNT+=This.DOCLINE(I).AMOUNT
   Endif
 Next I
 If TOTAMOUNT>30000
   [L]ASTATUS=Fmet this.ASETERROR("","Maximum amount reached, no more line can be inserted",[V]CST_AERROR)
   Return
 Endif
 # Now we can return without having set ASTATUS to an error value
 # The insertion is possible
 Return