on 10-19-2020 1:43 PM
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
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thanks for the hint, Pierre.
That helps.
The relevant part of the documentation is here.
kind regards
Reinhard
User | Count |
---|---|
87 | |
10 | |
10 | |
10 | |
7 | |
6 | |
6 | |
5 | |
5 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.