package no.uio.ifi.trackfind.backend.services;

import com.google.gson.Gson;
import no.uio.ifi.trackfind.backend.configuration.TrackFindProperties;
import no.uio.ifi.trackfind.backend.dao.Dataset;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.function.Function;

/**
 * Service for querying remote GSuite service for performing JSON to GSuite conversion.
 */
// TODO: cover with tests
@Service
public class GSuiteService implements Function<Collection<Dataset>, String> {

    private TrackFindProperties properties;
    private Gson gson;
    private SchemaService schemaService;
    private RestTemplate restTemplate;

    /**
     * Convert datasets to GSuite.
     *
     * @param datasets Datasets to convert.
     * @return GSuite string.
     */
    @SuppressWarnings("unchecked")
    @Override
    public String apply(Collection<Dataset> datasets) {
        sortJSON(datasets);
        HttpEntity<Collection<Dataset>> request = new HttpEntity<>(datasets);
        return restTemplate.postForObject("http://gsuite/togsuite", request, String.class);
    }

    protected void sortJSON(Collection<Dataset> datasets) {
        for (Dataset dataset : datasets) {
            dataset.setFairContent(sortJSON(dataset.getFairContent()));
        }
    }

    protected String sortJSON(String fairContent) {
        Map mapContent = gson.fromJson(fairContent, Map.class);
        mapContent = sortMap("", mapContent);
        System.out.println("before = " + fairContent);
        String after = gson.toJson(mapContent);
        System.out.println("after = " + after);
        return after;
    }

    @SuppressWarnings("unchecked")
    protected Map sortMap(String path, Map mapContent) {
        TreeMap sortedMap = new TreeMap((o1, o2) -> {
            String key1 = path + o1.toString();
            String key2 = path + o2.toString();
            List<String> attributes = schemaService.getAttributes();
            String fullKey1 = attributes.stream().filter(a -> a.startsWith(key1)).findFirst().orElse(null);
            String fullKey2 = attributes.stream().filter(a -> a.startsWith(key2)).findFirst().orElse(null);
            long index1 = fullKey1 == null ? Long.MAX_VALUE : attributes.indexOf(fullKey1);
            long index2 = fullKey2 == null ? Long.MAX_VALUE : attributes.indexOf(fullKey2);
            return Long.compare(index1, index2);
        });
        for (Object key : mapContent.keySet()) {
            Object value = mapContent.get(key);
            if (value instanceof Map) {
                sortedMap.put(key, sortMap(path + key + properties.getLevelsSeparator(), (Map) value));
            } else {
                sortedMap.put(key, value);
            }
        }
        return sortedMap;
    }

    @Autowired
    public void setProperties(TrackFindProperties properties) {
        this.properties = properties;
    }

    @Autowired
    public void setGson(Gson gson) {
        this.gson = gson;
    }

    @Autowired
    public void setSchemaService(SchemaService schemaService) {
        this.schemaService = schemaService;
    }

    @Autowired
    public void setRestTemplate(RestTemplate restTemplate) {
        this.restTemplate = restTemplate;
    }

}

@@ -1700,171 +1700,38 @@
     
-System.out.println(%22before = %22 + fairContent);%0A        String after = gson.toJson(mapContent);%0A        System.out.println(%22after = %22 + after);%0A        return after
+return gson.toJson(mapContent)
 ;%0A  

Remove logging.