package com.swisscom.cloud.sb.broker.cleanup;

import com.swisscom.cloud.sb.broker.services.inventory.InventoryService;
import org.springframework.data.util.Pair;

public class CleanupInfoService {
    public static final String COMPLETED_STATE = "cleanup_completed";
    public static final String PENDING_STATE = "cleanup_pending";
    public static final String STATE_FIELD_NAME = "cleanup_state";

    private final InventoryService inventoryService;

    public CleanupInfoService(InventoryService inventoryService) {
        this.inventoryService = inventoryService;
    }

    public String getState(String serviceInstanceUuid) {
        return inventoryService.get(serviceInstanceUuid, STATE_FIELD_NAME, PENDING_STATE).getSecond();
    }

    public boolean isCompletedState(String serviceInstanceUuid) {
        return getState(serviceInstanceUuid).equals(COMPLETED_STATE);
    }

    public String setState(String serviceInstanceUuid, String value) {
        inventoryService.set(serviceInstanceUuid, Pair.of(STATE_FIELD_NAME, value));
        return getState(serviceInstanceUuid);
    }

    public String setCompletedState(String serviceInstanceUuid) {
        return setState(serviceInstanceUuid, COMPLETED_STATE);
    }
}

@@ -661,20 +661,40 @@
 yService
+%0A                
 .get
+All
 (service
@@ -727,24 +727,147 @@
 NAME
-, PENDING_STATE)
+)%0A                .stream()%0A                .findFirst()%0A                .orElse(Pair.of(STATE_FIELD_NAME, PENDING_STATE))%0A                
 .get

use none single implementation.