Skip to Content
0
Oct 19, 2020 at 12:43 PM

Implementing a beforeUpdate handler

82 Views

Hey Capire Experts,

I have a basic issue in understanding how an UPDATE (Http PATCH) call works and how to implement a corresponding BEFORE/ON Handler.

A Http PATCH sends the changed Data to the backend only, which seems reasonable. The primary key of the entity to be changed is part of the URL (e.g.: " ... PATCH Item(7addeaa8-71f0-4e1e-93ce-f9e71e6414c5) ... " ).

The request payload to change the "item_text" of an entity called "item" looks like this:

--batch_id-1603101583126-422
Content-Type:application/http
Content-Transfer-Encoding:binary

PATCH Item(7addeaa8-71f0-4e1e-93ce-f9e71e6414c5) HTTP/1.1

Accept:application/json;odata.metadata=minimal;IEEE754Compatible=true
Accept-Language:de-DE
X-CSRF-Token: (....)
Content-Type:application/json;charset=UTF-8;IEEE754Compatible=true

{"item_text":"Changed Item Data"}

--batch_id-1603101583126-422--

However, when implementing an UPDATE Handler using the POJO Interface like this:

	@Before(event = CdsService.EVENT_UPDATE, entity = Item_.CDS_NAME)
	public void beforeUpdate( Item item ) {
		log.debug( "Item primary key: " + item.getId( ) );
		if ( item.getItemText( ).equals( CHANGED_ITEM_TEXT ) ) {
			item.setItemText( DERIVED_ITEM_TEXT );
		}
	}

there seems no straightforward way to find out the ID (7addeaa8-71f0-4e1e-93ce-f9e71e6414c5) of the item being updated . All fields of "item" are null, except for "item_text"; so "item.getId( )" will fail.

When I try to use the "CdsUpdateEventContext" I also receive "modifiedAt" and "modifiedBy" , but again no primary key:

	@Before(event = CdsService.EVENT_UPDATE, entity = Item_.CDS_NAME)
	public void beforeUpdate( CdsUpdateEventContext ctx ) {
		
		Item item = db.run( ctx.getCqn( ).asUpdate( ) ).single( Item.class );
		log.debug( "Item primary key: " + item.getId( ) );
	}

Am I doing this completely wrong?

How is this supposed to work?

How do I find out the primary key of the entity ?

kind regards

Reinhard Jaeschke