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.BuildScanDataStore;
import nu.studer.teamcity.buildscan.BuildScanReference;
import org.apache.log4j.Logger;
import org.jetbrains.annotations.NotNull;

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

public final class CustomDataStorageBuildScanDataStore implements BuildScanDataStore {

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

    private static final Logger LOGGER = Logger.getLogger("jetbrains.buildServer.BUILDSCAN");

    @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()) {
            LOGGER.info("Storing build scan URL: " + buildScanUrl + ", for build id: " + buildId);
            customDataStorage.putValue(buildId, buildScanUrl);
            customDataStorage.flush();
        } else {
            List<String> scans = StringUtil.split(existing, BUILD_SCAN_URLS_SEPARATOR);
            scans.add(buildScanUrl);

            String buildScanString = StringUtil.join(BUILD_SCAN_URLS_SEPARATOR, scans);
            LOGGER.info("Storing build scan URLs: " + buildScanString + ", for build id: " + buildId);
            customDataStorage.putValue(buildId, buildScanString);
            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);
    }

}

@@ -310,40 +310,8 @@
 ce;%0A
-import org.apache.log4j.Logger;%0A
 impo
@@ -401,16 +401,90 @@
 .List;%0A%0A
+import static nu.studer.teamcity.buildscan.internal.Util.getBuildScanId;%0A%0A
 public f
@@ -751,1467 +751,358 @@
     
-private static final Logger LOGGER = Logger.getLogger(%22jetbrains.buildServer.BUILDSCAN%22);%0A%0A    @Override%0A    public void mark(SBuild build) %7B%0A        String buildId = String.valueOf(build.getBuildId());%0A        CustomDataStorage customDataStorage = getCustomDataStorage(build);%0A        String existing =
+@Override%0A    public void mark(SBuild build) %7B%0A        throw new UnsupportedOperationException(%22Writing to the
  custom
-DataS
+ data s
 torage
-.getValue(buildId);%0A%0A        if (existing == null) %7B%0A            customDataStorage.putValue(buildId, %22%22);%0A            customDataStorage.flush();%0A        %7D%0A    %7D%0A%0A    @Override%0A    public void store(SBuild build, String buildScanUrl) %7B%0A        String buildId = String.valueOf(build.getBuildId());%0A        CustomDataStorage customDataStorage = getCustomDataStorage(build);%0A        String existing = customDataStorage.getValue(buildId);%0A%0A        if (existing == null %7C%7C existing.isEmpty()) %7B%0A            LOGGER.info(%22Storing build scan URL: %22 + buildScanUrl + %22, for build id: %22 + buildId);%0A            customDataStorage.putValue(buildId, buildScanUrl);%0A           
+ is not supported anymore%22);%0A    %7D%0A%0A    @Override%0A    public void store(SBuild build, String buildScanUrl) %7B%0A        throw new UnsupportedOperationException(%22Writing to the
  custom
-DataStorage.flush();%0A        %7D else %7B%0A            List%3CString%3E scans = StringUtil.split(existing, BUILD_SCAN_URLS_SEPARATOR);%0A            scans.add(buildScanUrl);%0A%0A            String buildScanString = StringUtil.join(BUILD_SCAN_URLS_SEPARATOR, scans);%0A            LOGGER.info(%22Storing build scan URLs: %22 + buildScanString + %22, for build id: %22 + buildId);%0A            customDataStorage.putValue(buildId, buildScanString);%0A            customDataStorage.flush();%0A        %7D
+ data storage is not supported anymore%22);
 %0A   
@@ -1769,21 +1769,16 @@
 ference(
-Util.
 getBuild

Throw exception when writing to the custom data storage is requested