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          this.subscription = subscription;
39          subscription.request(1);
40      }
41  
42      @Override
43      public void onNext(Request request) {
44          this.subscription.request(1);
45          if (Interaction.UNSUBSCRIBE.equals(request.getMessage())) {
46              this.subscription.cancel();
47              return;
48          }
49          
50          // Display model thread name in logs instead of the observer name
51          String nameThread = Thread.currentThread().getName();
52          
53          SwingUtilities.invokeLater(() -> {
54              Thread.currentThread().setName("from " + nameThread);
55              try {
56                  Class<?> cl = Class.forName(this.packageInteraction +"."+ request.getMessage());
57                  var types = new Class[]{ Object[].class };
58                  Constructor<?> constructor = cl.getConstructor(types);
59                  @SuppressWarnings({"java:S1905", "java:S3878"})  // rules opposite by intellij and sonar
60                  InteractionCommand interactionCommand = (InteractionCommand) constructor.newInstance(
61                      new Object[] {  request.getParameters() }
62                  );
63                  interactionCommand.execute();
64              } catch (ClassNotFoundException e) {
65                  LOGGER.log(LogLevelUtil.IGNORE, e);  // Ignore unused interaction message
66              } catch (
67                  InstantiationException
68                  | IllegalAccessException
69                  | NoSuchMethodException
70                  | SecurityException
71                  | IllegalArgumentException
72                  | InvocationTargetException
73                  e
74              ) {
75                  LOGGER.log(LogLevelUtil.CONSOLE_JAVA, e, e);
76              }
77          });
78      }
79  
80      @Override
81      public void onError(Throwable e) {
82          LOGGER.log(LogLevelUtil.CONSOLE_JAVA, e, e);
83      }
84  
85      @Override
86      public void onComplete() {
87          // Nothing
88      }
89  }