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:

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$METHODSCase CURPTHWhen "DOCLINE" : Gosub DOCLINE...EndcaseReturn$DOCLINECase CURPTHWhen "ADDLINE_BEFORE" : Gosub ADDLINE_BEFORE_DOCLINE...EndcaseReturn$ADDLINE_BEFORE_DOCLINELocal Integer PREV_POS, NEXT_POS, ILocal Decimal TOTAMOUNT# If the position of insertion is not the last one, we need to test the previous and next lineIf 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-1PREV_POS=IIf NEXT_POS<>0 : Break : EndifElsif This.DOCLINE(I)<>Null and ALINE=This.DOCLINE(I).AORDERNEXT_POS=IIf PREV_POS<>0 : Break : EndifEndifNext I# Now let's test the valuesIf 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)ReturnEndifEndif# The second test is simplerTOTAMOUNT=0For I=1 to maxtab(This.DOCLINE)If This.DOCLINE(I)<>Null and find(This.DOCLINE(I).ASTATUS, [V]CST_ADEL,[V]CST_ANEWDEL)=0TOTAMOUNT+=This.DOCLINE(I).AMOUNTEndifNext IIf TOTAMOUNT>30000[L]ASTATUS=Fmet this.ASETERROR("","Maximum amount reached, no more line can be inserted",[V]CST_AERROR)ReturnEndif# Now we can return without having set ASTATUS to an error value# The insertion is possibleReturn