diff --git a/app/server/src/main/java/com/mobtools/server/ServerApplication.java b/app/server/src/main/java/com/mobtools/server/ServerApplication.java index ac7a801d35..339a23ca73 100644 --- a/app/server/src/main/java/com/mobtools/server/ServerApplication.java +++ b/app/server/src/main/java/com/mobtools/server/ServerApplication.java @@ -2,12 +2,14 @@ package com.mobtools.server; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import reactor.core.publisher.Hooks; @SpringBootApplication public class ServerApplication { public static void main(String[] args) { SpringApplication.run(ServerApplication.class, args); + Hooks.onOperatorDebug(); } } diff --git a/app/server/src/main/java/com/mobtools/server/configurations/SecurityConfig.java b/app/server/src/main/java/com/mobtools/server/configurations/SecurityConfig.java index 6fd7199b9e..0489254750 100644 --- a/app/server/src/main/java/com/mobtools/server/configurations/SecurityConfig.java +++ b/app/server/src/main/java/com/mobtools/server/configurations/SecurityConfig.java @@ -28,6 +28,7 @@ public class SecurityConfig { /** * This configuration enables CORS requests for the most common HTTP Methods + * * @return */ @Bean diff --git a/app/server/src/main/java/com/mobtools/server/controllers/TenantController.java b/app/server/src/main/java/com/mobtools/server/controllers/TenantController.java index a629268fd4..f227109837 100644 --- a/app/server/src/main/java/com/mobtools/server/controllers/TenantController.java +++ b/app/server/src/main/java/com/mobtools/server/controllers/TenantController.java @@ -2,15 +2,10 @@ package com.mobtools.server.controllers; import com.mobtools.server.constants.Url; import com.mobtools.server.domains.Tenant; -import com.mobtools.server.dtos.ResponseDto; import com.mobtools.server.services.TenantService; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.http.HttpStatus; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; -import reactor.core.publisher.Mono; @RestController @RequestMapping(Url.TENANT_URL) @@ -22,10 +17,4 @@ public class TenantController extends BaseController> getByName(@PathVariable String name) { - return service.getByName(name) - .map(widget -> new ResponseDto<>(HttpStatus.OK.value(), widget, null)); - } - } diff --git a/app/server/src/main/java/com/mobtools/server/services/TenantServiceImpl.java b/app/server/src/main/java/com/mobtools/server/services/TenantServiceImpl.java index 17589e91cd..65f368344a 100644 --- a/app/server/src/main/java/com/mobtools/server/services/TenantServiceImpl.java +++ b/app/server/src/main/java/com/mobtools/server/services/TenantServiceImpl.java @@ -13,8 +13,8 @@ import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; import reactor.core.scheduler.Scheduler; -import javax.validation.constraints.NotNull; -import java.util.stream.Stream; +import java.util.ArrayList; +import java.util.List; @Slf4j @Service @@ -39,29 +39,44 @@ public class TenantServiceImpl extends BaseService create(@NotNull Tenant tenant) { - - //Embed the setting object into tenantSetting. This is done only for settings for which the values deviate from the default. - //It is assumed that the tenant create API body would only contain the settings for which the default value and - //true value are different. - if (tenant.getTenantSettings() != null) { - - try (Stream stream = tenant.getTenantSettings().stream()) { - Flux tenantSettingFlux = Flux.fromStream(stream); - tenantSettingFlux.map(this::getAndStoreSettingObjectInTenantSetting).subscribe(); - } - } - - return repository.save(tenant); + public Mono create(Tenant tenant) { + return Mono.just(tenant) + //transform the tenant data to embed setting object in each object in tenantSetting list. + .flatMap(this::enhanceTenantSettingList) + //Call the library function to save the updated tenant + .flatMap(tenantUpdated -> repository.save(tenantUpdated)) + .subscribeOn(scheduler); } - private Object getAndStoreSettingObjectInTenantSetting(TenantSetting tenantSetting) { + private Mono enhanceTenantSettingList(Tenant tenant) { + + if (tenant.getTenantSettings() == null) tenant.setTenantSettings(new ArrayList<>()); + + Flux tenantSettingFlux = Flux.fromIterable(tenant.getTenantSettings()); + // For each tenant setting, fetch and embed the setting, and once all the tenant setting are done, collect it + // back into a single list of tenant settings. + Mono> listMono = tenantSettingFlux.flatMap(this::fetchAndEmbedSetting).collectList(); + return listMono.map(list -> { + tenant.setTenantSettings(list); + return list; + }).thenReturn(tenant); + } + + private Mono fetchAndEmbedSetting(TenantSetting tenantSetting) { + String key = tenantSetting.getSetting().getKey(); Mono setting = settingService.getByKey(key); - return setting.doOnNext(set -> tenantSetting.setSetting(set)) - .thenReturn(tenantSetting) - .subscribe(); + return setting.map(setting1 -> { + tenantSetting.setSetting(setting1); + return tenantSetting; + }); } }