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      private static final Logger LOGGER = LogManager.getRootLogger();
18      
19      private final String packageInteraction;
20  
21      /**
22       * Observer pattern.<br>
23       * Receive an update order from the model:<br>
24       * - Use the Request message to get the Interaction class,<br>
25       * - Pass the parameters to that class.
26       */
27      private Subscription subscription;
28      
29      public SubscriberInteraction(String packageInteraction) {
30          this.packageInteraction = packageInteraction;
31      }
32      
33      @Override
34      public void onSubscribe(Subscription subscription) {
35          this.subscription = subscription;
36          subscription.request(1);
37      }
38  
39      @Override
40      public void onNext(Request request) {
41          this.subscription.request(1);
42          if (Interaction.UNSUBSCRIBE.equals(request.getMessage())) {
43              this.subscription.cancel();
44              return;
45          }
46          
47          // Display model thread name in logs instead of the observer name
48          String nameThread = Thread.currentThread().getName();
49          
50          SwingUtilities.invokeLater(() -> {
51              Thread.currentThread().setName("from " + nameThread);
52              try {
53                  Class<?> cl = Class.forName(this.packageInteraction +"."+ request.getMessage());
54                  var types = new Class[]{ Object[].class };
55                  Constructor<?> constructor = cl.getConstructor(types);
56                  @SuppressWarnings({"java:S1905", "java:S3878"})  // rules opposite by intellij and sonar
57                  InteractionCommand interactionCommand = (InteractionCommand) constructor.newInstance(
58                      new Object[] {  request.getParameters() }
59                  );
60                  interactionCommand.execute();
61              } catch (ClassNotFoundException e) {
62                  LOGGER.log(LogLevelUtil.IGNORE, e);  // Ignore unused interaction message
63              } catch (
64                  InstantiationException
65                  | IllegalAccessException
66                  | NoSuchMethodException
67                  | SecurityException
68                  | IllegalArgumentException
69                  | InvocationTargetException
70                  e
71              ) {
72                  LOGGER.log(LogLevelUtil.CONSOLE_JAVA, e, e);
73              }
74          });
75      }
76  
77      @Override
78      public void onError(Throwable e) {
79          LOGGER.log(LogLevelUtil.CONSOLE_JAVA, e, e);
80      }
81  
82      @Override
83      public void onComplete() {
84          // Nothing
85      }
86  }