From ef50504737313a7f50f9e3864f6d27b081587aab Mon Sep 17 00:00:00 2001
From: Michal Badin <xbadin@fi.muni.cz>
Date: Sat, 6 May 2023 13:46:16 +0200
Subject: [PATCH] refactoring: Extracted receivers emails into
 application.properties

---
 .../pa165/service/ApplicationService.java     | 21 ++++++++-----------
 .../src/main/resources/application.properties |  5 ++++-
 .../pa165/service/CarComponentService.java    | 17 +++++++--------
 .../src/main/resources/application.properties |  5 ++++-
 4 files changed, 25 insertions(+), 23 deletions(-)

diff --git a/application/src/main/java/cz/muni/pa165/service/ApplicationService.java b/application/src/main/java/cz/muni/pa165/service/ApplicationService.java
index 1a1ce76..cfdd2fd 100644
--- a/application/src/main/java/cz/muni/pa165/service/ApplicationService.java
+++ b/application/src/main/java/cz/muni/pa165/service/ApplicationService.java
@@ -7,7 +7,7 @@ import cz.muni.pa165.exceptions.ResourceNotFoundException;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.core.ParameterizedTypeReference;
+import org.springframework.beans.factory.annotation.Value;
 import org.springframework.core.io.Resource;
 import org.springframework.http.MediaType;
 import org.springframework.stereotype.Service;
@@ -17,10 +17,7 @@ import org.springframework.web.reactive.function.client.WebClient;
 import org.springframework.web.reactive.function.client.WebClientException;
 
 import java.time.LocalDate;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-import java.util.Optional;
+import java.util.*;
 
 /**
  * @author Michal Badin
@@ -30,12 +27,13 @@ public class ApplicationService {
     private static final Logger log = LoggerFactory.getLogger(ApplicationService.class);
     private static final String NOTIFICATION_MODULE_DOCKER = "http://notification:8083";
     private static final String NOTIFICATION_MODULE_LOCALHOST = "http://localhost:8083";
-    private String NOTIFICATION_MODULE_URL;
     private static final String NOTIFICATION_MODULE_URI_NEW = "/notification/application/new";
     private static final String NOTIFICATION_MODULE_URI_STATUS = "/notification/application/status";
-    private static final List<String> NOTIFICATION_RECEIVERS = List.of("formula.team.management@gmail.com");
+    @Value("${notification.receivers}")
+    private final List<String> notificationReceivers = new ArrayList<>();
     private final ApplicationRepository applicationRepository;
     private final WebClient webClient;
+    private String NOTIFICATION_MODULE_URL;
 
     @Autowired
     public ApplicationService(ApplicationRepository applicationRepository, WebClient.Builder webClientBuilder) {
@@ -44,8 +42,7 @@ public class ApplicationService {
         //if running in docker, modules cannot communicate through localhost
         if (System.getenv("DOCKER") != null) {
             NOTIFICATION_MODULE_URL = NOTIFICATION_MODULE_DOCKER;
-        }
-        else {
+        } else {
             NOTIFICATION_MODULE_URL = NOTIFICATION_MODULE_LOCALHOST;
         }
         this.webClient = webClientBuilder.baseUrl(NOTIFICATION_MODULE_URL).build();
@@ -63,8 +60,8 @@ public class ApplicationService {
         var savedApplication = applicationRepository.save(application);
 
         try {
-            sendPostRequest(savedApplication, NOTIFICATION_MODULE_URI_NEW, List.of(savedApplication.getEmail()));
-        } catch (WebClientException  e) {
+            sendPostRequest(savedApplication, NOTIFICATION_MODULE_URI_NEW, notificationReceivers);
+        } catch (WebClientException e) {
             log.debug(String.format("The notification module is not reachable on the URL: %s, exception %s", NOTIFICATION_MODULE_URL + NOTIFICATION_MODULE_URI_NEW, e));
         }
 
@@ -90,7 +87,7 @@ public class ApplicationService {
 
         if (savedApplication.getStatus() != ApplicationStatusEnum.PENDING) {
             try {
-                sendPostRequest(savedApplication, NOTIFICATION_MODULE_URI_STATUS, NOTIFICATION_RECEIVERS);
+                sendPostRequest(savedApplication, NOTIFICATION_MODULE_URI_STATUS, List.of(savedApplication.getEmail()));
             } catch (WebClientException e) {
                 log.debug(String.format("The notification module is not reachable on the URL: %s, exception %s", NOTIFICATION_MODULE_URL + NOTIFICATION_MODULE_URI_STATUS, e));
             }
diff --git a/application/src/main/resources/application.properties b/application/src/main/resources/application.properties
index 698b657..a41e2a3 100644
--- a/application/src/main/resources/application.properties
+++ b/application/src/main/resources/application.properties
@@ -16,4 +16,7 @@ appconfig.enablecache=false
 management.endpoints.web.exposure.include=health,metrics,loggers,beans,prometheus
 management.endpoint.health.show-details=always
 management.endpoint.health.show-components=always
-management.endpoint.health.probes.enabled=true
\ No newline at end of file
+management.endpoint.health.probes.enabled=true
+
+#notification receivers
+notification.receivers=formula.team.management@gmail.com
\ No newline at end of file
diff --git a/core/src/main/java/cz/muni/pa165/service/CarComponentService.java b/core/src/main/java/cz/muni/pa165/service/CarComponentService.java
index f16c674..6fd2777 100644
--- a/core/src/main/java/cz/muni/pa165/service/CarComponentService.java
+++ b/core/src/main/java/cz/muni/pa165/service/CarComponentService.java
@@ -7,12 +7,11 @@ import cz.muni.pa165.exceptions.ResourceNotFoundException;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.core.ParameterizedTypeReference;
+import org.springframework.beans.factory.annotation.Value;
 import org.springframework.core.io.Resource;
 import org.springframework.http.MediaType;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
-import org.springframework.web.client.*;
 import org.springframework.web.reactive.function.client.WebClient;
 import org.springframework.web.reactive.function.client.WebClientException;
 
@@ -22,13 +21,14 @@ import java.util.*;
 public class CarComponentService extends DomainService<CarComponent> {
 
     private static final Logger log = LoggerFactory.getLogger(CarComponentService.class);
-    private final CarComponentRepository componentRepository;
-    private final WebClient webClient;
     private static final String NOTIFICATION_MODULE_DOCKER = "http://notification:8083";
     private static final String NOTIFICATION_MODULE_LOCALHOST = "http://localhost:8083";
-    private String NOTIFICATION_MODULE_URL;
     private static final String NOTIFICATION_MODULE_URI = "/notification/component";
-    private static final List<String> NOTIFICATION_RECEIVERS = List.of("formula.team.management@gmail.com");
+    private final CarComponentRepository componentRepository;
+    private final WebClient webClient;
+    @Value("${notification.receivers}")
+    private final List<String> notificationReceivers = new ArrayList<>();
+    private String NOTIFICATION_MODULE_URL;
 
     @Autowired
     public CarComponentService(CarComponentRepository repository, WebClient.Builder webClientBuilder) {
@@ -38,8 +38,7 @@ public class CarComponentService extends DomainService<CarComponent> {
         //if running in docker, modules cannot communicate through localhost
         if (System.getenv("DOCKER") != null) {
             NOTIFICATION_MODULE_URL = NOTIFICATION_MODULE_DOCKER;
-        }
-        else {
+        } else {
             NOTIFICATION_MODULE_URL = NOTIFICATION_MODULE_LOCALHOST;
         }
         this.webClient = webClientBuilder.baseUrl(NOTIFICATION_MODULE_URL).build();
@@ -83,7 +82,7 @@ public class CarComponentService extends DomainService<CarComponent> {
     private void sendPostRequest(CarComponent component) {
         Map<String, Object> notification = new HashMap<>();
         notification.put("carComponent", component);
-        notification.put("receivers", NOTIFICATION_RECEIVERS);
+        notification.put("receivers", notificationReceivers);
 
         webClient.post()
                 .uri(NOTIFICATION_MODULE_URI)
diff --git a/core/src/main/resources/application.properties b/core/src/main/resources/application.properties
index d0fa9b6..f2c74fa 100644
--- a/core/src/main/resources/application.properties
+++ b/core/src/main/resources/application.properties
@@ -34,4 +34,7 @@ management.endpoint.health.probes.enabled=true
 management.info.env.enabled=true
 info.app.encoding=UTF-8
 info.app.java.source=17
-info.app.java.target=17
\ No newline at end of file
+info.app.java.target=17
+
+#notification receivers
+notification.receivers=formula.team.management@gmail.com
\ No newline at end of file
-- 
GitLab