cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

CAP JAVA - Process an array of requests, validate and process correct entries

DC
Explorer
0 Kudos
928

Hi Experts,

We have a requirement to process (bulk load) some requests using customs action in CAP JAVA. We need to validate these requests and process only the correct ones and skipping the errored ones.

We have a validate function which currently collects any messages.errors and returns true or false. we don't throw any errors

requests.forEach(request -> {
  if (validateRequest(request)) {          
      context.getCdsRuntime().changeSetContext().run(ctx -> {                   
         // all good so far                   
         x newRequest = db.run(Insert.into(x_.class).entry(request)).single(x.class); 
         logger.debug("      Created Record " + newRequest.toJson());
         outList.add(newRequest);
         Released event = Released.create();
         event.setId(newRequest.getId()); 
         // set event context 
         ReleasedContext evContext = ReleasedContext.create(); 
         evContext.setData(event); 
         // Emit event 
         adminService.emit(evContext); 
      });
   }
 }); 
messages.throwIfError(); 
context.setResult(outList)



The problem is i don't see this processing all the requests when there are some invalidate ones in the list. it will process some but then cancel some valid ones.


In the validate Request function, we are just collecting messages.Error to use later in the process.Is this the correct way to process multiple requests??? Basically we want to validate and insert only the correct ones and return a response to what happened to each request.

Any suggestions/help appreciated

marcbecker : any suggestions/ help Please.

Cheers

Dharmesh

View Entire Topic
0 Kudos

Can you say something more specific on validateRequest? Does it return false in case there is an error message? If yes it would explain that after the first request which adds to messages.error the loop is exited. Directly afterwards the handler will then throw due to messages.throwIfError().

DC
Explorer
0 Kudos

Hi Matthias

In the validateRequest function, errors are collected and the function return a boolean. We are not calling throwifError() till the loop is completed. Is there a reason why the loop is exited?? do we need to move the call to validateRequest into its own changeContext???

Thanks

Dharmesh

marcbecker
Product and Topic Expert
Product and Topic Expert
0 Kudos

It would also be interesting to get more details about what you mean by "it will process some but then cancel some valid ones". Is the validateRequest unexpectedly returning false? Or is the ChangeSet for this request executed as expected, but then rolled back instead of comitted?

DC
Explorer
0 Kudos

Hi Marc,

The validateRequest function collects errors and success messages and returns a boolean. What seems to happen is it will process some requests successfully then on rolls back. Say we have 4 requests.

Request 1 - returns False form validateRequest. A error Message likely added to the Messages API.

Request 2 - 4 return true. All are processed but request 4 would rollback because request 1 caused an exception?

I have since changed my code to this. This seems to work but open to suggestions/ comments.

Thanks

D

requests.forEach(request -> {
       
      context.getCdsRuntime().changeSetContext().run(ctx -> {     
         if (validateRequest(request)) {                  
               // all good so far                   
               x newRequest = db.run(Insert.into(x_.class).entry(request)).single(x.class); 
               logger.debug("      Created Record " + newRequest.toJson());
               outList.add(newRequest);
               Released event = Released.create();
               event.setId(newRequest.getId()); 
               // set event context 
               ReleasedContext evContext = ReleasedContext.create(); 
               evContext.setData(event); 
               // Emit event 
               adminService.emit(evContext);
         }
      });
   
 }); 
messages.throwIfError(); 
context.setResult(outList)