/** This utility will ensure that the smartobject the ui event is linked to is the same smartobject
    that the object instance is linked to
   
   ------------------------------------------------------------------------------------------------------ **/
disable triggers for dump of ryc_ui_event.
disable triggers for load of ryc_ui_event.  

DEFINE VARIABLE lError  AS LOGICAL    NO-UNDO.

DEFINE BUFFER ryc_ui_event        FOR ryc_ui_event.
DEFINE BUFFER buf_ui_event        FOR ryc_ui_event.
DEFINE BUFFER ryc_object_instance FOR ryc_object_instance.
DEFINE BUFFER ryc_smartobject     FOR ryc_smartobject.
DEFINE BUFFER buf_smartobject     FOR ryc_smartobject.

PUBLISH "DCU_WriteLog":U ("UiEvent smartobject_obj fix started.").

FOR EACH ryc_ui_event EXCLUSIVE-LOCK,
   FIRST ryc_object_instance NO-LOCK
   WHERE ryc_object_instance.object_instance_obj = ryc_ui_event.object_instance_obj
     AND ryc_object_instance.smartobject_obj    <> ryc_ui_event.smartobject_obj,
   FIRST ryc_smartobject NO-LOCK
   WHERE ryc_smartobject.smartobject_obj = ryc_object_instance.smartobject_obj:

  /* If we are going to violate the index, it means the ui event exists properly - against
     the correct smartobject. In this case, get rid of the 'duff' ui event */
  IF CAN-FIND(FIRST buf_ui_event
              WHERE buf_ui_event.object_type_obj     = ryc_smartobject.object_type_obj
                AND buf_ui_event.smartobject_obj     = ryc_smartobject.smartobject_obj
                AND buf_ui_event.object_instance_obj = ryc_ui_event.object_instance_obj
                AND buf_ui_event.render_type_obj     = ryc_ui_event.render_type_obj
                AND buf_ui_event.event_name          = ryc_ui_event.event_name) THEN
  DO:
    DELETE ryc_ui_event NO-ERROR.

    IF ERROR-STATUS:ERROR THEN
    DO:
      ASSIGN lError = TRUE.
      PUBLISH "DCU_WriteLog":U ("Unable to delete UiEvent with ui_event_obj '" + STRING(ryc_ui_event.ui_event_obj) + "'":U).

      ERROR-STATUS:ERROR = FALSE.
    END.
  END.
  ELSE
  DO:
    /* Fix the ui event */
    ASSIGN
        ryc_ui_event.smartobject_obj = ryc_object_instance.smartobject_obj
        ryc_ui_event.object_type_obj = ryc_smartobject.object_type_obj NO-ERROR.

    IF ERROR-STATUS:ERROR THEN
    DO:
      ASSIGN lError = TRUE.
      PUBLISH "DCU_WriteLog":U ("Unable to assign smartobject_obj to '" + STRING(ryc_object_instance.smartobject_obj) +
                                "' for UiEvent with ui_event_obj '" + STRING(ryc_ui_event.ui_event_obj) + "'":U).
  
      ERROR-STATUS:ERROR = FALSE.
    END.
  END.
END.

PUBLISH "DCU_WriteLog":U ("UiEvent smartobject_obj fix completed " + (IF lError THEN "with errors!" ELSE "successfully.")).
