Skip to content

Commit

Permalink
Bugfix/remove duplicates from postgis due to multiple subscriptions (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
saschadoemer authored Jun 18, 2024
1 parent ee2daf1 commit 44c283d
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 6 deletions.
26 changes: 26 additions & 0 deletions src/main/java/de/app/fivegla/business/SubscriptionService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package de.app.fivegla.business;

import de.app.fivegla.integration.fiware.SubscriptionIntegrationService;
import de.app.fivegla.persistence.entity.Tenant;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

@Slf4j
@Service
@RequiredArgsConstructor
public class SubscriptionService {

private final SubscriptionIntegrationService subscriptionIntegrationService;

/**
* Delete all existing subscriptions.
*
* @param tenant the tenant
*/
public void deleteAllSubscriptions(Tenant tenant) {
log.info("Deleting all subscriptions");
subscriptionIntegrationService.removeAll(tenant);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,5 @@ public class BaseMappings {
public static final String THIRD_PARTY_API_CONFIGURATION = SECURED_BY_TENANT + "/3rd-party-api-configuration";
public static final String DEVICE_POSITION = SECURED_BY_TENANT + "/device-position";
public static final String GROUPS = SECURED_BY_TENANT + "/groups";
public static final String SUBSCRIPTION = SECURED_BY_TENANT + "/subscription";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package de.app.fivegla.controller.tenant;

import de.app.fivegla.api.Response;
import de.app.fivegla.business.SubscriptionService;
import de.app.fivegla.business.TenantService;
import de.app.fivegla.config.security.marker.TenantCredentialApiAccess;
import de.app.fivegla.controller.api.BaseMappings;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.security.Principal;

/**
* The SubscriptionController class handles subscription-related operations.
*/
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping(BaseMappings.SUBSCRIPTION)
public class SubscriptionController implements TenantCredentialApiAccess {

private final TenantService tenantService;
private final SubscriptionService subscriptionService;

@Operation(
operationId = "delete.all.subscriptions",
description = "Delete all subscriptions.",
tags = BaseMappings.SUBSCRIPTION
)
@ApiResponse(
responseCode = "200",
description = "All subscriptions have been deleted.",
content = @Content(
mediaType = MediaType.APPLICATION_JSON_VALUE,
schema = @Schema(implementation = Response.class)
)
)
@ApiResponse(
responseCode = "400",
description = "The request is invalid.",
content = @Content(
mediaType = MediaType.APPLICATION_JSON_VALUE,
schema = @Schema(implementation = Response.class)
)
)
@DeleteMapping(value = "/delete", produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<? extends Response> deleteAllSubscriptions(Principal principal) {
var tenant = validateTenant(tenantService, principal);
subscriptionService.deleteAllSubscriptions(tenant);
return ResponseEntity.ok().body(new Response());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public SubscriptionIntegrationService(String contextBrokerUrl, List<String> noti
* each representing a different type.
*/
public void subscribe(Tenant tenant, EntityType... entityTypes) {
removeAll(tenant); // FIXME adjust and update subscriptions instead of removing all
var httpClient = HttpClient.newHttpClient();
var subscriptions = createSubscriptions(entityTypes);
for (var subscription : subscriptions) {
Expand Down Expand Up @@ -115,10 +116,9 @@ private List<Entity> createSubscriptionEntities(EntityType... entityTypes) {
* Removes all subscriptions of the specified type.
*
* @param tenant the tenant to remove subscriptions for
* @param type the type of subscriptions to be removed
*/
public void removeAll(Tenant tenant, EntityType type) {
findAll(tenant, type).forEach(subscription -> removeSubscription(tenant, subscription));
public void removeAll(Tenant tenant) {
findAll(tenant).forEach(subscription -> removeSubscription(tenant, subscription));
}

private void removeSubscription(Tenant tenant, Subscription subscription) {
Expand Down Expand Up @@ -156,6 +156,18 @@ private void removeSubscription(Tenant tenant, Subscription subscription) {
* @return A list of Subscription objects matching the given entityType.
*/
public List<Subscription> findAll(Tenant tenant, EntityType entityType) {
return findAll(tenant).stream().filter(subscription -> subscription.getSubject().getEntities()
.stream()
.anyMatch(entity -> entity.getType().equals(entityType.getKey()))).toList();
}

/**
* Finds all subscriptions of a given entityType.
*
* @param tenant The tenant to find subscriptions for.
* @return A list of Subscription objects matching the given entityType.
*/
public List<Subscription> findAll(Tenant tenant) {
var httpClient = HttpClient.newHttpClient();
var httpRequest = HttpRequest.newBuilder()
.header(CustomHeader.FIWARE_SERVICE, tenant.getTenantId())
Expand All @@ -172,9 +184,7 @@ public List<Subscription> findAll(Tenant tenant, EntityType entityType) {
.build());
} else {
log.info("Subscription found successfully.");
return toListOfObjects(response.body()).stream().filter(subscription -> subscription.getSubject().getEntities()
.stream()
.anyMatch(entity -> entity.getType().equals(entityType.getKey()))).toList();
return toListOfObjects(response.body());
}
} catch (Exception e) {
log.error("Could not find subscriptions.", e);
Expand Down

0 comments on commit 44c283d

Please sign in to comment.