cancel
Showing results for 
Search instead for 
Did you mean: 

Implementing a beforeUpdate handler

0 Kudos

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

Accepted Solutions (1)

Accepted Solutions (1)

pierre_dominique2
Contributor
0 Kudos

Hi,

I don't use CAP Java but have you read the official documentation?

EDIT: there are some examples on GitHub that might help you.

@Before(event = DraftService.EVENT_DRAFT_CANCEL, entity = OrderItems_.CDS_NAME)	public void cancelOrderItems(DraftCancelEventContext context) {

String orderItemId = (String) analyzer.analyze(context.getCqn()).targetKeys().get(OrderItems.ID);

Thanks,

Pierre

0 Kudos

Thanks for the hint, Pierre.

That helps.

The relevant part of the documentation is here.

kind regards

Reinhard

Answers (0)