N°6324 - CRUD Event for one time treatment before creation and before update

This commit is contained in:
Eric Espie
2023-05-30 11:29:24 +02:00
parent e3dd805f24
commit c3bb995407
5 changed files with 92 additions and 155 deletions

View File

@@ -5874,54 +5874,28 @@ JS
*/
final protected function FireEventCheckToWrite(): void
{
$this->FireEvent(EVENT_DB_CHECK_TO_WRITE);
$this->FireEvent(EVENT_DB_CHECK_TO_WRITE, ['is_new' => $this->IsNew()]);
}
final protected function FireEventBeforeObjectCreate()
final protected function FireEventBeforeWrite()
{
$this->FireEvent(EVENT_DB_BEFORE_CREATE);
}
/**
* @return void
* @throws \CoreException
*
* @since 3.1.0
*/
final protected function FireEventCreateDone(): void
{
$this->NotifyAttachedObjectsOnLinkClassModification();
$this->FireEventDbLinksChangedForCurrentObject();
$this->FireEvent(EVENT_DB_CREATE_DONE);
}
/////////////
/// UPDATE
///
/**
* @return void
* @throws \CoreException
*/
final protected function FireEventBeforeObjectUpdate()
{
$this->FireEvent(EVENT_DB_BEFORE_UPDATE);
$this->FireEvent(EVENT_DB_BEFORE_WRITE, ['is_new' => $this->IsNew()]);
}
/**
* @param array $aChanges
* @param bool $bIsNew
*
* @return void
* @throws \ArchivedObjectException
* @throws \CoreException
* @since 3.1.0
*/
final protected function FireEventUpdateDone(array $aChanges): void
final protected function FireEventAfterWrite(array $aChanges, bool $bIsNew): void
{
$this->NotifyAttachedObjectsOnLinkClassModification();
$this->FireEventDbLinksChangedForCurrentObject();
$this->FireEvent(EVENT_DB_UPDATE_DONE, ['changes' => $aChanges]);
$this->FireEvent(EVENT_DB_AFTER_WRITE, ['is_new' => $bIsNew, 'changes' => $aChanges]);
}
//////////////
@@ -5950,7 +5924,7 @@ JS
{
$this->NotifyAttachedObjectsOnLinkClassModification();
$this->FireEventDbLinksChangedForCurrentObject();
$this->FireEvent(EVENT_DB_DELETE_DONE);
$this->FireEvent(EVENT_DB_AFTER_DELETE);
}
/**

View File

@@ -186,6 +186,27 @@
</menu>
</menus>
<events>
<event id="EVENT_DB_BEFORE_WRITE" _delta="define">
<description>An object is about to be written into the database. The object can be modified.</description>
<sources>
<source id="cmdbAbstractObject">cmdbAbstractObject</source>
</sources>
<replaces>DBObject::OnInsert</replaces>
<event_data>
<event_datum id="object">
<description>The object inserted</description>
<type>DBObject</type>
</event_datum>
<event_datum id="is_new">
<description>Creation flag</description>
<type>boolean</type>
</event_datum>
<event_datum id="debug_info">
<description>Debug string</description>
<type>string</type>
</event_datum>
</event_data>
</event>
<event id="EVENT_DB_CHECK_TO_WRITE" _delta="define">
<description>Check an object before it is written into the database (no change possible). Call DBObject::AddCheckIssue() to signal an issue</description>
<sources>
@@ -197,22 +218,9 @@
<description>The object to check</description>
<type>DBObject</type>
</event_datum>
<event_datum id="debug_info">
<description>Debug string</description>
<type>string</type>
</event_datum>
</event_data>
</event>
<event id="EVENT_DB_BEFORE_CREATE" _delta="define">
<description>An object is about to be created into the database. The object can be modified.</description>
<sources>
<source id="cmdbAbstractObject">cmdbAbstractObject</source>
</sources>
<replaces>DBObject::OnInsert</replaces>
<event_data>
<event_datum id="object">
<description>The object inserted</description>
<type>DBObject</type>
<event_datum id="is_new">
<description>Creation flag</description>
<type>boolean</type>
</event_datum>
<event_datum id="debug_info">
<description>Debug string</description>
@@ -220,8 +228,8 @@
</event_datum>
</event_data>
</event>
<event id="EVENT_DB_CREATE_DONE" _delta="define">
<description>An object has been created into the database. The modifications can be propagated to other objects.</description>
<event id="EVENT_DB_AFTER_WRITE" _delta="define">
<description>An object has been written into the database. The modifications can be propagated to other objects.</description>
<sources>
<source id="cmdbAbstractObject">cmdbAbstractObject</source>
</sources>
@@ -231,39 +239,13 @@
<description>The object inserted</description>
<type>DBObject</type>
</event_datum>
<event_datum id="debug_info">
<description>Debug string</description>
<type>string</type>
<event_datum id="is_new">
<description>Creation flag</description>
<type>boolean</type>
</event_datum>
</event_data>
</event>
<event id="EVENT_DB_BEFORE_UPDATE" _delta="define">
<description>An object is about to be updated into the database. The object can be modified.</description>
<sources>
<source id="cmdbAbstractObject">cmdbAbstractObject</source>
</sources>
<replaces>DBObject::OnUpdate</replaces>
<event_data>
<event_datum id="object">
<description>The object updated</description>
<type>DBObject</type>
</event_datum>
<event_datum id="debug_info">
<description>Debug string</description>
<type>string</type>
</event_datum>
</event_data>
</event>
<event id="EVENT_DB_UPDATE_DONE" _delta="define">
<description>An object has been updated into the database and reloaded.</description>
<sources>
<source id="cmdbAbstractObject">cmdbAbstractObject</source>
</sources>
<replaces>DBObject::AfterUpdate</replaces>
<event_data>
<event_datum id="object">
<description>The object updated</description>
<type>DBObject</type>
<event_datum id="changes">
<description>For updates, the list of changes done during this operation</description>
<type>array</type>
</event_datum>
<event_datum id="debug_info">
<description>Debug string</description>
@@ -288,7 +270,7 @@
</event_datum>
</event_data>
</event>
<event id="EVENT_DB_DELETE_DONE" _delta="define">
<event id="EVENT_DB_AFTER_DELETE" _delta="define">
<description>An object has been deleted into the database</description>
<sources>
<source id="cmdbAbstractObject">cmdbAbstractObject</source>

View File

@@ -3066,7 +3066,7 @@ abstract class DBObject implements iDisplay
$this->DoComputeValues();
$this->OnInsert();
$this->FireEventBeforeObjectCreate();
$this->FireEventBeforeWrite();
// If not automatically computed, then check that the key is given by the caller
if (!MetaModel::IsAutoIncrementKey($sRootClass)) {
@@ -3167,7 +3167,7 @@ abstract class DBObject implements iDisplay
MetaModel::StartReentranceProtection($this);
try {
$this->FireEventCreateDone();
$this->FireEventAfterWrite([], true);
$this->AfterInsert();
// Activate any existing trigger
@@ -3278,7 +3278,7 @@ abstract class DBObject implements iDisplay
$this->ComputeStopWatchesDeadline(false);
$this->OnUpdate();
$this->FireEventBeforeObjectUpdate();
$this->FireEventBeforeWrite();
// Freeze the changes at this point
$this->InitPreviousValuesForUpdatedAttributes();
@@ -3457,7 +3457,7 @@ abstract class DBObject implements iDisplay
$this->m_aModifiedAtt = array();
$bModifiedByUpdateDone = false;
try {
$this->FireEventUpdateDone($aChanges);
$this->FireEventAfterWrite($aChanges, false);
$this->AfterUpdate();
// Save the status as it is reset just after...
$bModifiedByUpdateDone = $this->IsModified();
@@ -6061,37 +6061,17 @@ abstract class DBObject implements iDisplay
* @return void
* @since 3.1.0
*/
protected function FireEventBeforeObjectCreate()
protected function FireEventBeforeWrite()
{
}
/**
* @return void
* @since 3.1.0
*/
protected function FireEventCreateDone(): void
{
}
/////////////
/// UPDATE
///
/**
* @return void
* @since 3.1.0
*/
protected function FireEventBeforeObjectUpdate()
{
}
/**
* @param array $aChanges
* @param bool $bIsNew
*
* @return void
* @since 3.1.0
*/
protected function FireEventUpdateDone(array $aChanges): void
protected function FireEventAfterWrite(array $aChanges, bool $bIsNew): void
{
}

View File

@@ -47,7 +47,7 @@ class CRUDEventTest extends ItopDataTestCase
}
/**
* Check that the 3 events EVENT_DB_COMPUTE_VALUES, EVENT_DB_CHECK_TO_WRITE and EVENT_DB_CREATE_DONE are called on insert
* Check that the 3 events EVENT_DB_COMPUTE_VALUES, EVENT_DB_CHECK_TO_WRITE and EVENT_DB_AFTER_WRITE are called on insert
*
* @return void
* @throws \Exception
@@ -61,12 +61,13 @@ class CRUDEventTest extends ItopDataTestCase
$this->assertIsObject($oOrg);
$this->assertEquals(1, self::$aEventCalls[EVENT_DB_COMPUTE_VALUES]);
$this->assertEquals(1, self::$aEventCalls[EVENT_DB_CHECK_TO_WRITE]);
$this->assertEquals(1, self::$aEventCalls[EVENT_DB_CREATE_DONE]);
$this->assertEquals(3, self::$iEventCalls);
$this->assertEquals(1, self::$aEventCalls[EVENT_DB_BEFORE_WRITE]);
$this->assertEquals(1, self::$aEventCalls[EVENT_DB_AFTER_WRITE]);
$this->assertEquals(4, self::$iEventCalls);
}
/**
* Check that the 3 events EVENT_DB_COMPUTE_VALUES, EVENT_DB_CHECK_TO_WRITE and EVENT_DB_UPDATE_DONE are called on update
* Check that the 3 events EVENT_DB_COMPUTE_VALUES, EVENT_DB_CHECK_TO_WRITE and EVENT_DB_AFTER_WRITE are called on update
*
* @return void
* @throws \Exception
@@ -84,8 +85,9 @@ class CRUDEventTest extends ItopDataTestCase
$this->assertEquals(1, self::$aEventCalls[EVENT_DB_COMPUTE_VALUES]);
$this->assertEquals(1, self::$aEventCalls[EVENT_DB_CHECK_TO_WRITE]);
$this->assertEquals(1, self::$aEventCalls[EVENT_DB_UPDATE_DONE]);
$this->assertEquals(3, self::$iEventCalls);
$this->assertEquals(1, self::$aEventCalls[EVENT_DB_BEFORE_WRITE]);
$this->assertEquals(1, self::$aEventCalls[EVENT_DB_AFTER_WRITE]);
$this->assertEquals(4, self::$iEventCalls);
}
/**
@@ -210,7 +212,7 @@ class CRUDEventTest extends ItopDataTestCase
}
/**
* Modify one object during EVENT_DB_CREATE_DONE
* Modify one object during EVENT_DB_AFTER_WRITE
* Check that all the events are sent (CREATE + UPDATE)
* Check that the modification is saved in DB
*
@@ -221,7 +223,7 @@ class CRUDEventTest extends ItopDataTestCase
{
$oEventReceiver = new CRUDEventReceiver();
// Set the person's first name during Compute Values
$oEventReceiver->AddCallback(EVENT_DB_CREATE_DONE, Person::class, 'SetPersonFirstName');
$oEventReceiver->AddCallback(EVENT_DB_AFTER_WRITE, Person::class, 'SetPersonFirstName');
$oEventReceiver->RegisterCRUDListeners();
$oPerson = $this->CreatePerson(1);
@@ -229,9 +231,9 @@ class CRUDEventTest extends ItopDataTestCase
$this->assertEquals(2, self::$aEventCalls[EVENT_DB_COMPUTE_VALUES]);
$this->assertEquals(2, self::$aEventCalls[EVENT_DB_CHECK_TO_WRITE]);
$this->assertEquals(1, self::$aEventCalls[EVENT_DB_CREATE_DONE]);
$this->assertEquals(1, self::$aEventCalls[EVENT_DB_UPDATE_DONE]);
$this->assertEquals(6, self::$iEventCalls);
$this->assertEquals(2, self::$aEventCalls[EVENT_DB_BEFORE_WRITE]);
$this->assertEquals(2, self::$aEventCalls[EVENT_DB_AFTER_WRITE]);
$this->assertEquals(8, self::$iEventCalls);
// Read the object explicitly from the DB to check that the first name has been set
$oSet = new DBObjectSet(DBSearch::FromOQL('SELECT Person WHERE id=:id'), [], ['id' => $oPerson->GetKey()]);
@@ -240,7 +242,7 @@ class CRUDEventTest extends ItopDataTestCase
}
/**
* Modify one object during EVENT_DB_UPDATE_DONE
* Modify one object during EVENT_DB_AFTER_WRITE
* Check that all the events are sent (UPDATE + UPDATE again)
* Check that the modification is saved in DB
*
@@ -254,7 +256,7 @@ class CRUDEventTest extends ItopDataTestCase
$oEventReceiver = new CRUDEventReceiver();
// Set the person's first name during Compute Values
$oEventReceiver->AddCallback(EVENT_DB_UPDATE_DONE, Person::class, 'SetPersonFirstName');
$oEventReceiver->AddCallback(EVENT_DB_AFTER_WRITE, Person::class, 'SetPersonFirstName');
$oEventReceiver->RegisterCRUDListeners();
$oPerson->Set('function', 'test'.rand());
@@ -262,8 +264,9 @@ class CRUDEventTest extends ItopDataTestCase
$this->assertEquals(2, self::$aEventCalls[EVENT_DB_COMPUTE_VALUES]);
$this->assertEquals(2, self::$aEventCalls[EVENT_DB_CHECK_TO_WRITE]);
$this->assertEquals(2, self::$aEventCalls[EVENT_DB_UPDATE_DONE]);
$this->assertEquals(6, self::$iEventCalls);
$this->assertEquals(2, self::$aEventCalls[EVENT_DB_BEFORE_WRITE]);
$this->assertEquals(2, self::$aEventCalls[EVENT_DB_AFTER_WRITE]);
$this->assertEquals(8, self::$iEventCalls);
// Read the object explicitly from the DB to check that the first name has been set
$oSet = new DBObjectSet(DBSearch::FromOQL('SELECT Person WHERE id=:id'), [], ['id' => $oPerson->GetKey()]);
@@ -272,8 +275,8 @@ class CRUDEventTest extends ItopDataTestCase
}
/**
* Modify one object during EVENT_DB_UPDATE_DONE
* Check that the CRUD is protected against infinite loops (when modifying an object in its EVENT_DB_UPDATE_DONE)
* Modify one object during EVENT_DB_AFTER_WRITE
* Check that the CRUD is protected against infinite loops (when modifying an object in its EVENT_DB_AFTER_WRITE)
*
*
* @return void
@@ -288,8 +291,8 @@ class CRUDEventTest extends ItopDataTestCase
$oEventReceiver = new CRUDEventReceiver();
// Set the person's first name during Compute Values
$oEventReceiver->AddCallback(EVENT_DB_UPDATE_DONE, Person::class, 'SetPersonFirstName', 100);
$oEventReceiver->RegisterCRUDListeners(EVENT_DB_UPDATE_DONE);
$oEventReceiver->AddCallback(EVENT_DB_AFTER_WRITE, Person::class, 'SetPersonFirstName', 100);
$oEventReceiver->RegisterCRUDListeners(EVENT_DB_AFTER_WRITE);
$oPerson->Set('function', 'test'.rand());
$oPerson->DBUpdate();
@@ -338,8 +341,9 @@ class CRUDEventTest extends ItopDataTestCase
// 1 insert for Team, 3 insert for lnkPersonToTeam
$this->assertEquals(4, self::$aEventCalls[EVENT_DB_COMPUTE_VALUES]);
$this->assertEquals(4, self::$aEventCalls[EVENT_DB_CHECK_TO_WRITE]);
$this->assertEquals(4, self::$aEventCalls[EVENT_DB_CREATE_DONE]);
$this->assertEquals(12, self::$iEventCalls);
$this->assertEquals(4, self::$aEventCalls[EVENT_DB_BEFORE_WRITE]);
$this->assertEquals(4, self::$aEventCalls[EVENT_DB_AFTER_WRITE]);
$this->assertEquals(16, self::$iEventCalls);
}
/**
@@ -374,7 +378,7 @@ class CRUDEventTest extends ItopDataTestCase
$this->debug("\n-------------> Test Starts HERE\n");
$oEventReceiver = new CRUDEventReceiver();
// Create a new role and add it to the newly created lnkPersonToTeam
$oEventReceiver->AddCallback(EVENT_DB_CREATE_DONE, lnkPersonToTeam::class, 'AddRoleToLink');
$oEventReceiver->AddCallback(EVENT_DB_AFTER_WRITE, lnkPersonToTeam::class, 'AddRoleToLink');
$oEventReceiver->RegisterCRUDListeners();
// Create the team
@@ -385,9 +389,9 @@ class CRUDEventTest extends ItopDataTestCase
// 1 for Team, 1 for lnkPersonToTeam, 1 for ContactType and 1 for the update of lnkPersonToTeam
$this->assertEquals(4, self::$aEventCalls[EVENT_DB_COMPUTE_VALUES]);
$this->assertEquals(4, self::$aEventCalls[EVENT_DB_CHECK_TO_WRITE]);
$this->assertEquals(3, self::$aEventCalls[EVENT_DB_CREATE_DONE]);
$this->assertEquals(1, self::$aEventCalls[EVENT_DB_UPDATE_DONE]);
$this->assertEquals(12, self::$iEventCalls);
$this->assertEquals(4, self::$aEventCalls[EVENT_DB_BEFORE_WRITE]);
$this->assertEquals(4, self::$aEventCalls[EVENT_DB_AFTER_WRITE]);
$this->assertEquals(16, self::$iEventCalls);
// Read the object explicitly from the DB to check that the role has been set
$oSet = new DBObjectSet(DBSearch::FromOQL('SELECT Team WHERE id=:id'), [], ['id' => $oTeam->GetKey()]);
@@ -400,7 +404,7 @@ class CRUDEventTest extends ItopDataTestCase
}
/**
* Check that updates during EVENT_DB_CREATE_DONE are postponed to the end of all events and only one update is done
* Check that updates during EVENT_DB_AFTER_WRITE are postponed to the end of all events and only one update is done
*
* @return void
* @throws \Exception
@@ -409,24 +413,21 @@ class CRUDEventTest extends ItopDataTestCase
{
$oEventReceiver = new CRUDEventReceiver();
// Set the person's function after the creation
$oEventReceiver->AddCallback(EVENT_DB_CREATE_DONE, Person::class, 'SetPersonFunction');
$oEventReceiver->RegisterCRUDListeners(EVENT_DB_CREATE_DONE);
$oEventReceiver->AddCallback(EVENT_DB_AFTER_WRITE, Person::class, 'SetPersonFunction');
$oEventReceiver->RegisterCRUDListeners(EVENT_DB_AFTER_WRITE);
// Intentionally register twice so 2 modifications will be done
$oEventReceiver = new CRUDEventReceiver();
$oEventReceiver->AddCallback(EVENT_DB_CREATE_DONE, Person::class, 'SetPersonFirstName');
$oEventReceiver->RegisterCRUDListeners(EVENT_DB_CREATE_DONE);
// Used to count the updates
$oEventReceiver->RegisterCRUDListeners(EVENT_DB_UPDATE_DONE);
$oEventReceiver->AddCallback(EVENT_DB_AFTER_WRITE, Person::class, 'SetPersonFirstName');
$oEventReceiver->RegisterCRUDListeners(EVENT_DB_AFTER_WRITE);
self::$iEventCalls = 0;
$oPerson = $this->CreatePerson(1);
$this->assertIsObject($oPerson);
// 2 for insert => 2 modifications generate ONE update
// 1 for update (if 2 updates were done 2 events were counted)
$this->assertEquals(2, self::$aEventCalls[EVENT_DB_CREATE_DONE]);
$this->assertEquals(1, self::$aEventCalls[EVENT_DB_UPDATE_DONE]);
$this->assertEquals(3, self::$iEventCalls);
// 2 for update (if 2 updates were done then 4 events would have been counted)
$this->assertEquals(4, self::$aEventCalls[EVENT_DB_AFTER_WRITE]);
$this->assertEquals(4, self::$iEventCalls);
}
@@ -457,16 +458,16 @@ class CRUDEventTest extends ItopDataTestCase
$this->assertTrue(CRUDEventReceiver::$bIsObjectInCrudStack);
$oEventReceiver->CleanCallbacks();
$oEventReceiver->AddCallback(EVENT_DB_CREATE_DONE, Person::class, 'CheckCrudStack');
$oEventReceiver->RegisterCRUDListeners(EVENT_DB_CREATE_DONE);
$oEventReceiver->AddCallback(EVENT_DB_AFTER_WRITE, Person::class, 'CheckCrudStack');
$oEventReceiver->RegisterCRUDListeners(EVENT_DB_AFTER_WRITE);
$this->CreatePerson(3);
$this->assertTrue(CRUDEventReceiver::$bIsObjectInCrudStack);
$oEventReceiver->CleanCallbacks();
// Insert a Team with new lnkPersonToTeam - in the lnkPersonToTeam event we check that Team CRUD operation is ongoing
$oEventReceiver->AddCallback(EVENT_DB_CREATE_DONE, Person::class, 'CheckUpdateInLnk');
$oEventReceiver->AddCallback(EVENT_DB_AFTER_WRITE, Person::class, 'CheckUpdateInLnk');
$sLinkedClass = lnkPersonToTeam::class;
$oEventReceiver->RegisterCRUDListeners(EVENT_DB_CREATE_DONE, $sLinkedClass);
$oEventReceiver->RegisterCRUDListeners(EVENT_DB_AFTER_WRITE, $sLinkedClass);
// Prepare the link for the insertion with the team
$aLinkedObjectsArray = [];
$oSet = DBObjectSet::FromArray($sLinkedClass, $aLinkedObjectsArray);
@@ -552,9 +553,9 @@ class CRUDEventReceiver extends ClassesWithDebug
EventService::RegisterListener(EVENT_DB_COMPUTE_VALUES, [$this, 'OnEvent']);
EventService::RegisterListener(EVENT_DB_CHECK_TO_WRITE, [$this, 'OnEvent']);
EventService::RegisterListener(EVENT_DB_CHECK_TO_DELETE, [$this, 'OnEvent']);
EventService::RegisterListener(EVENT_DB_CREATE_DONE, [$this, 'OnEvent']);
EventService::RegisterListener(EVENT_DB_UPDATE_DONE, [$this, 'OnEvent']);
EventService::RegisterListener(EVENT_DB_DELETE_DONE, [$this, 'OnEvent']);
EventService::RegisterListener(EVENT_DB_BEFORE_WRITE, [$this, 'OnEvent']);
EventService::RegisterListener(EVENT_DB_AFTER_WRITE, [$this, 'OnEvent']);
EventService::RegisterListener(EVENT_DB_AFTER_DELETE, [$this, 'OnEvent']);
return;
}

View File

@@ -92,7 +92,7 @@
<class id="ClassWithEventListeners">
<event_listeners>
<event_listener id="OnUserRequestUpdateDone" _delta="define">
<event>EVENT_DB_UPDATE_DONE</event>
<event>EVENT_DB_AFTER_UPDATE</event>
<callback>OnUpdateDone</callback>
<rank>0</rank>
</event_listener>