View Javadoc
1   package com.jsql.view.interaction;
2   
3   import com.jsql.model.bean.util.Interaction;
4   import com.jsql.model.bean.util.Request;
5   import com.jsql.util.LogLevelUtil;
6   import org.apache.logging.log4j.LogManager;
7   import org.apache.logging.log4j.Logger;
8   
9   import javax.swing.*;
10  import java.lang.reflect.Constructor;
11  import java.lang.reflect.InvocationTargetException;
12  import java.util.concurrent.Flow.Subscriber;
13  import java.util.concurrent.Flow.Subscription;
14  
15  public class SubscriberInteraction implements Subscriber<Request> {
16  
17      /**
18       * Log4j logger sent to view.
19       */
20      private static final Logger LOGGER = LogManager.getRootLogger();
21      
22      private final String packageInteraction;
23      
24      /**
25       * Observer pattern.<br>
26       * Receive an update order from the model:<br>
27       * - Use the Request message to get the Interaction class,<br>
28       * - Pass the parameters to that class.
29       */
30      private Subscription subscription;
31      
32      public SubscriberInteraction(String packageInteraction) {
33          this.packageInteraction = packageInteraction;
34      }
35      
36      @Override
37      public void onSubscribe(Subscription subscription) {
38          
39          this.subscription = subscription;
40          subscription.request(1);
41      }
42  
43      @Override
44      public void onNext(Request request) {
45          
46          subscription.request(1);
47          
48          if (Interaction.UNSUBSCRIBE.equals(request.getMessage())) {
49              
50              subscription.cancel();
51              return;
52          }
53          
54          // Display model thread name in logs instead of the observer name
55          String nameThread = Thread.currentThread().getName();
56          
57          SwingUtilities.invokeLater(() -> {
58              
59              Thread.currentThread().setName("from " + nameThread);
60              
61              try {
62                  Class<?> cl = Class.forName(this.packageInteraction +"."+ request.getMessage());
63                  var types = new Class[]{ Object[].class };
64                  Constructor<?> constructor = cl.getConstructor(types);
65      
66                  @SuppressWarnings("java:S1905")
67                  InteractionCommand interactionCommand = (InteractionCommand) constructor.newInstance(
68                      (Object[]) new Object[] {  // cast for sonar disambiguation
69                          request.getParameters()
70                      }
71                  );
72                  interactionCommand.execute();
73                  
74              } catch (ClassNotFoundException e) {
75                  LOGGER.log(LogLevelUtil.IGNORE, e);  // Ignore unused interaction message
76              } catch (
77                  InstantiationException
78                  | IllegalAccessException
79                  | NoSuchMethodException
80                  | SecurityException
81                  | IllegalArgumentException
82                  | InvocationTargetException
83                  e
84              ) {
85                  LOGGER.log(LogLevelUtil.CONSOLE_JAVA, e, e);
86              }
87          });
88      }
89  
90      @Override
91      public void onError(Throwable e) {
92          LOGGER.log(LogLevelUtil.CONSOLE_JAVA, e, e);
93      }
94  
95      @Override
96      public void onComplete() {
97          // Nothing
98      }
99  }