All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.edurt.datacap.client.cli.service.DataSourceServiceImpl Maven / Gradle / Ivy

There is a newer version: 2024.4.0
Show newest version
package io.edurt.datacap.client.cli.service;

import com.google.gson.Gson;
import com.google.gson.internal.LinkedTreeMap;
import io.edurt.datacap.client.cli.http.HttpCommon;
import io.edurt.datacap.client.cli.http.HttpConfigure;
import io.edurt.datacap.client.cli.http.HttpMethod;
import io.edurt.datacap.client.cli.model.DataSource;
import io.edurt.datacap.client.cli.response.ServerResponse;
import io.edurt.datacap.client.cli.utils.TableUtils;
import org.springframework.shell.table.BeanListTableModel;
import org.springframework.shell.table.Table;
import org.springframework.shell.table.TableBuilder;
import org.springframework.shell.table.TableModel;
import org.springframework.shell.table.TableModelBuilder;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import static java.lang.Math.toIntExact;

@Service
public class DataSourceServiceImpl
        implements DataSourceService
{
    private static final String url = "/api/v1/source";
    private final CacheService cacheService;

    public DataSourceServiceImpl(CacheService cacheService)
    {
        this.cacheService = cacheService;
    }

    private HttpConfigure getConfigure()
    {
        HttpConfigure configure = this.cacheService.getConfigure();
        return configure;
    }

    @Override
    public Table showList()
    {
        HttpConfigure configure = this.getConfigure();
        configure.setUrl(url);
        HttpCommon httpCommon = new HttpCommon(configure);
        ServerResponse serverResponse = httpCommon.withTokenForGet();
        LinkedTreeMap map = (LinkedTreeMap) serverResponse.getData();
        List> list = (List>) map.get("content");
        final List dataSources = new ArrayList<>();
        list.forEach(value -> {
            DataSource dataSource = new DataSource();
            // Double
            dataSource.setId(Integer.parseInt(String.valueOf(value.get("id")).split("\\.")[0]));
            dataSource.setName(String.valueOf(value.get("name")));
            dataSource.setType(String.valueOf(value.get("type")));
            dataSource.setDescription(String.valueOf(value.get("description")));
            dataSources.add(dataSource);
        });
        LinkedHashMap headers = new LinkedHashMap<>();
        headers.put("id", "Id");
        headers.put("name", "Name");
        headers.put("type", "Type");
        headers.put("description", "Description");
        TableModel model = new BeanListTableModel<>(dataSources, headers);
        TableBuilder tableBuilder = new TableBuilder(model);
        TableUtils.applyStyle(tableBuilder);
        return tableBuilder.build();
    }

    @Override
    public Table getInfo()
    {
        HttpConfigure configure = this.getConfigure();
        configure.setUrl(url + "/" + configure.getSourceId());
        HttpCommon httpCommon = new HttpCommon(configure);
        ServerResponse serverResponse = httpCommon.withTokenForGet();
        LinkedTreeMap map = (LinkedTreeMap) serverResponse.getData();
        LinkedHashMap headers = new LinkedHashMap<>();
        headers.put("id", "Id");
        headers.put("name", "Name");
        headers.put("type", "Type");
        headers.put("description", "Description");
        DataSource dataSource = new DataSource();
        dataSource.setId(toIntExact(configure.getSourceId()));
        dataSource.setName(String.valueOf(map.get("name")));
        dataSource.setType(String.valueOf(map.get("type")));
        dataSource.setDescription(String.valueOf(map.get("description")));
        TableModel model = new BeanListTableModel<>(Collections.singleton(dataSource), headers);
        TableBuilder tableBuilder = new TableBuilder(model);
        TableUtils.applyStyle(tableBuilder);
        return tableBuilder.build();
    }

    @Override
    public Table execute(String query)
    {
        HttpConfigure configure = this.getConfigure();
        configure.setUrl("/api/v1/execute");
        configure.setMethod(HttpMethod.POST);
        HttpCommon httpCommon = new HttpCommon(configure);
        Map stringObjectMap = new ConcurrentHashMap<>();
        stringObjectMap.put("content", query);
        stringObjectMap.put("format", "JSON");
        stringObjectMap.put("name", configure.getSourceId());
        ServerResponse serverResponse = httpCommon.withTokenForPost(new Gson().toJson(stringObjectMap));
        LinkedTreeMap map = (LinkedTreeMap) serverResponse.getData();

        LinkedHashMap headers = new LinkedHashMap<>();
        List columns = new ArrayList<>();
        List responseHeaders = (List) map.get("headers");
        List> responseColumns = (List>) map.get("columns");
        if (Boolean.valueOf(String.valueOf(map.get("isSuccessful")))) {
            responseHeaders.forEach(value -> headers.put(value, value));
            responseColumns.forEach(column -> columns.add(column));
        }

        TableModelBuilder builder = new TableModelBuilder<>();
        responseHeaders.forEach(header -> builder.addRow().addValue(header));
        responseColumns.forEach(column -> responseHeaders.forEach(header -> builder.addRow().addValue(column.get(header))));
        TableBuilder tableBuilder = new TableBuilder(builder.build());
        TableUtils.applyStyle(tableBuilder);
        return tableBuilder.build();
    }
}