package nu.studer.teamcity.buildscan.internal;

import jetbrains.buildServer.serverSide.CustomDataStorage;
import jetbrains.buildServer.serverSide.SBuild;
import jetbrains.buildServer.util.StringUtil;
import nu.studer.teamcity.buildscan.BuildScanReference;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

public final class DefaultBuildScanDataStore implements BuildScanDataStore {

    private static final String BUILD_SCAN_STORAGE_ID = "nu.studer.teamcity.buildscan.DefaultBuildScanDataStore";
    private static final String BUILD_SCAN_URLS_SEPARATOR = "|";

    @Override
    public void mark(SBuild build) {
        String buildId = String.valueOf(build.getBuildId());
        CustomDataStorage customDataStorage = getCustomDataStorage(build);
        String existing = customDataStorage.getValue(buildId);

        if (existing == null) {
            customDataStorage.putValue(buildId, "");
            customDataStorage.flush();
        }
    }

    @Override
    public void store(SBuild build, String buildScanUrl) {
        String buildId = String.valueOf(build.getBuildId());
        CustomDataStorage customDataStorage = getCustomDataStorage(build);
        String existing = customDataStorage.getValue(buildId);

        if (existing == null || existing.isEmpty()) {
            customDataStorage.putValue(buildId, buildScanUrl);
            customDataStorage.flush();
        } else {
            List<String> scans = StringUtil.split(existing, BUILD_SCAN_URLS_SEPARATOR);
            scans.add(buildScanUrl);

            customDataStorage.putValue(buildId, StringUtil.join(BUILD_SCAN_URLS_SEPARATOR, scans));
            customDataStorage.flush();
        }
    }

    @Override
    public List<BuildScanReference> fetch(SBuild build) {
        String buildId = String.valueOf(build.getBuildId());
        CustomDataStorage customDataStorage = getCustomDataStorage(build);
        String existing = customDataStorage.getValue(buildId);

        if (existing == null) {
            return null;
        } else {
            List<BuildScanReference> buildScanReferences = new ArrayList<>();
            List<String> scans = StringUtil.split(existing, BUILD_SCAN_URLS_SEPARATOR);
            for (String scanUrl : scans) {
                if (!scanUrl.isEmpty()) {
                    buildScanReferences.add(new BuildScanReference(Util.getBuildScanId(scanUrl), scanUrl));
                }
            }

            return buildScanReferences;
        }
    }

    @NotNull
    private CustomDataStorage getCustomDataStorage(SBuild build) {
        //noinspection ConstantConditions
        return build.getBuildType().getCustomDataStorage(BUILD_SCAN_STORAGE_ID);
    }

}

@@ -250,16 +250,48 @@
 erence;%0A
+import org.apache.log4j.Logger;%0A
 import o
@@ -324,16 +324,16 @@
 otNull;%0A
-
 %0Aimport 
@@ -635,16 +635,111 @@
 = %22%7C%22;%0A%0A
+    private static final Logger LOGGER = Logger.getLogger(%22jetbrains.buildServer.BUILDSCAN%22);%0A%0A
     @Ove
@@ -1441,32 +1441,131 @@
 ng.isEmpty()) %7B%0A
+            LOGGER.info(%22Storing build scan URL: %22 + buildScanUrl + %22, for build id: %22 + buildId);%0A
             cust
@@ -1809,43 +1809,32 @@
     
-customDataStorage.putValue(buildId,
+String buildScanString =
  Str
@@ -1879,16 +1879,184 @@
 , scans)
+;%0A            LOGGER.info(%22Storing build scan URLs: %22 + buildScanString + %22, for build id: %22 + buildId);%0A            customDataStorage.putValue(buildId, buildScanString
 );%0A     

Add logging