uf_set_row_status() to replace SetItemStatus()

Share this topic:



Link to this posting

Postby Ursego » 19 Feb 2013, 21:38

Don't use SetItemStatus() function directly - it doesn't change every old status to any new status - you can find the problem combinations in this table:

Image

As you see, there are some item statuses that cannot be set directly; you need a second SetItemStatus() to achieve the goal. One example: changing the row status from New! to NotModified! simply doesn't work. You need to set it to DataModified! first, then you can change it to NotModified!. Some others settings are allowed, but don't work as expected; changing from NewModified! to NotModified! will change the status to New!. To encapsulate all that complexity (and forget about it forever), create a function (named, for example, uf_set_row_status - to show it sets ROW's status, not COLUMN's) in a class of useful functions or another class in your application (maybe in the DW ancestor - in this case remove the argument adw and use this instead):

Code: Select all
/**********************************************************************************************************************
Dscr:   Changes the item status of the passed row in a DataWindow.
-----------------------------------------------------------------------------------------------------------------------
Arg:   DataWindow      adw
   long          al_row
   DWItemStatus   a_new_status
   DWBuffer   a_buf
-----------------------------------------------------------------------------------------------------------------------
Thr:   n_ex // see http://forum.powerbuilder.us/viewtopic.php?f=4&t=9
**********************************************************************************************************************/
long         ll_row_count
boolean         lb_work_done // sometimes we have to call SetItemStatus twice to achieve the goal; this var manages the number of calls
DWItemStatus    l_old_status
DWItemStatus    l_changed_status

// Validate arguments:

choose case a_buf
case Primary!
   ll_row_count = adw.RowCount()
case Filter!
   ll_row_count = adw.FilteredCount()
case Delete!
   ll_row_count = adw.DeleteCount()
case else
   f_throw(PopulateError(1, "Argument a_buf is not supported"))
end choose

choose case true
case not IsValid(adw)
   f_throw(PopulateError(2, "Argument adw is not valid"))
case IsNull(al_row)
   f_throw(PopulateError(3, "Argument al_row is null"))
case al_row < 1
   f_throw(PopulateError(4, "Argument al_row is negative (" + String(al_row) + ")"))
case al_row > ll_row_count
   f_throw(PopulateError(5, "Argument al_row (" + String(al_row) + ") is greater than number of rows in DW (" + String(ll_row_count) + ")"))
end choose

// Set item status:

l_old_status = adw.GetItemStatus(al_row, 0, a_buf)
lb_work_done = false

choose case true
case a_new_status = l_old_status
   return // nothing to do - the row is already in the desired status
case l_old_status = NewModified! and a_new_status = New!
   adw.SetItemStatus(al_row, 0, a_buf, NotModified!) // that changes the row status to New!
   lb_work_done = true // no need in another SetItemStatus
case l_old_status = New! and a_new_status = NotModified!
   adw.SetItemStatus(al_row, 0, a_buf, DataModified!)
case l_old_status = NewModified! and a_new_status = NotModified!
   adw.SetItemStatus(al_row, 0, a_buf, DataModified!)
case l_old_status = DataModified! and a_new_status = New!
   adw.SetItemStatus(al_row, 0, a_buf, NotModified!)
end choose

if not lb_work_done then
   adw.SetItemStatus(al_row, 0, a_buf, a_new_status)
end if

// Validate that the new status is the desired change:

l_changed_status = adw.GetItemStatus(al_row, 0, a_buf)
if l_changed_status <> a_new_status then
   f_throw(PopulateError(6, "Row status changed to undesired status"))
end if

return

Create a similar function for DataStore too. These functions can be also overloaded with a 3-arguments version which will be called when only the Primary buffer is processed:

Code: Select all
/**********************************************************************************************************************
Dscr:   Changes the item tatus of the passed row in a DataWindow.
   Works only on Primary! buffer; to work with other buffers, use the overloaded version.
-----------------------------------------------------------------------------------------------------------------------
Arg:   DataWindow      adw
   long          al_row
   DWItemStatus   a_new_status
-----------------------------------------------------------------------------------------------------------------------
Thr:   n_ex // see http://forum.powerbuilder.us/viewtopic.php?f=4&t=9
**********************************************************************************************************************/
this.uf_set_row_status(adw, al_row, a_new_status, Primary!)
User avatar
Ursego
Site Admin
 
Posts: 112
Joined: 19 Feb 2013, 20:33

Return to Tips and Tricks

Who is online

Users browsing this forum: No registered users and 1 guest


uf_set_row_status() to replace SetItemStatus()

Share this topic:


If you think that this site is not too bad, please LIKE it in Facebook. Thanks!





cron
free counters

eXTReMe Tracker