1616 */
1717package io .github .project .openubl .xmlsender .events ;
1818
19+ import com .fasterxml .jackson .core .JsonProcessingException ;
20+ import com .fasterxml .jackson .databind .ObjectMapper ;
21+ import io .github .project .openubl .xmlsender .idm .DocumentRepresentation ;
1922import io .github .project .openubl .xmlsender .models .DocumentEvent ;
23+ import io .github .project .openubl .xmlsender .models .jpa .DocumentRepository ;
24+ import io .github .project .openubl .xmlsender .models .jpa .entities .DocumentEntity ;
25+ import io .github .project .openubl .xmlsender .models .utils .EntityToRepresentation ;
2026import org .eclipse .microprofile .config .inject .ConfigProperty ;
2127
2228import javax .enterprise .context .ApplicationScoped ;
2329import javax .enterprise .event .Observes ;
2430import javax .enterprise .event .TransactionPhase ;
2531import javax .inject .Inject ;
2632import javax .jms .*;
33+ import java .lang .IllegalStateException ;
2734
2835@ ApplicationScoped
2936public class JMSEventManager {
@@ -43,33 +50,47 @@ public class JMSEventManager {
4350 @ Inject
4451 ConnectionFactory connectionFactory ;
4552
53+ @ Inject
54+ DocumentRepository documentRepository ;
55+
4656 public void onDocumentCreate (
4757 @ Observes (during = TransactionPhase .AFTER_SUCCESS )
4858 @ EventProvider (EventProvider .Type .jms ) DocumentEvent .Created event
4959 ) {
50- produceMessage (sendFileQueue , event .getId ());
60+ produceMessage (sendFileQueue , String . valueOf ( event .getId () ));
5161 }
5262
5363 public void onDocumentRequireCheckTicket (
5464 @ Observes (during = TransactionPhase .AFTER_SUCCESS )
5565 @ EventProvider (EventProvider .Type .jms ) DocumentEvent .RequireCheckTicket event
5666 ) {
57- produceMessage (ticketQueue , event .getId ());
67+ produceMessage (ticketQueue , String . valueOf ( event .getId () ));
5868 }
5969
6070 public void onDocumentDelivered (
6171 @ Observes (during = TransactionPhase .AFTER_SUCCESS )
6272 @ EventProvider (EventProvider .Type .jms ) DocumentEvent .Delivered event
6373 ) {
64- produceMessage (callbackQueue , event .getId ());
74+ DocumentEntity documentEntity = documentRepository .findById (event .getId ());
75+ DocumentRepresentation representation = EntityToRepresentation .toRepresentation (documentEntity );
76+
77+ String callbackObj ;
78+ try {
79+ ObjectMapper mapper = new ObjectMapper ();
80+ callbackObj = mapper .writeValueAsString (representation );
81+ } catch (JsonProcessingException e ) {
82+ throw new IllegalStateException (e );
83+ }
84+
85+ produceMessage (callbackQueue , callbackObj );
6586 }
6687
67- private void produceMessage (String queueName , Long documentId ) {
88+ private void produceMessage (String queueName , String messageBody ) {
6889 try (JMSContext context = connectionFactory .createContext (Session .AUTO_ACKNOWLEDGE )) {
6990 JMSProducer producer = context .createProducer ();
7091 producer .setDeliveryDelay (messageDelay );
7192 Queue queue = context .createQueue (queueName );
72- Message message = context .createTextMessage (documentId . toString () );
93+ Message message = context .createTextMessage (messageBody );
7394 producer .send (queue , message );
7495 }
7596 }
0 commit comments