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

net.mossol.bot.context.TextContextConfiguration Maven / Gradle / Ivy

There is a newer version: 0.0.3.8
Show newest version
package net.mossol.bot.context;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.linecorp.centraldogma.client.CentralDogma;
import com.linecorp.centraldogma.client.Watcher;
import com.linecorp.centraldogma.common.Query;
import net.mossol.bot.model.RegexText;
import net.mossol.bot.model.SimpleText;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

@Configuration
public class TextContextConfiguration {
    private static final Logger logger = LoggerFactory.getLogger(TextContextConfiguration.class);
    private static final String CENTRAL_DOGMA_PROJECT = "mossol";
    private static final String CENTRAL_DOGMA_REPOSITORY = "main";

    @Resource
    private CentralDogma centralDogma;

    @Resource
    private ObjectMapper objectMapper;

    private Map convertToSimpleText(JsonNode jsonNode) {
        try {
            List info = objectMapper.readValue(objectMapper.treeAsTokens(jsonNode),
                    new TypeReference>(){});
            return info.stream().collect(Collectors.toMap(SimpleText::getMessage, Function.identity()));
        } catch (IOException e) {
            logger.error("Converting Json to SimpleText Map Failed", e);
            return null;
        }
    }

    private List convertToRegexText(JsonNode jsonNode) {
        try {
            List regexTexts = objectMapper.readValue(objectMapper.treeAsTokens(jsonNode), new TypeReference>(){});
            regexTexts = regexTexts.stream().map(RegexText::compilePattern).collect(Collectors.toList());
            return regexTexts;
        } catch (IOException e) {
            logger.error("Converting Json to RegexText Map Failed", e);
            return null;
        }
    }

    @Bean
    public Watcher> simpleTextWatcher() {
        return centralDogma.fileWatcher(CENTRAL_DOGMA_PROJECT, CENTRAL_DOGMA_REPOSITORY,
                Query.ofJsonPath("/simpleText.json"),
                this::convertToSimpleText);
    }

    @Bean
    public Watcher> regexTextWatcher() {
        return centralDogma.fileWatcher(CENTRAL_DOGMA_PROJECT, CENTRAL_DOGMA_REPOSITORY,
                                        Query.ofJsonPath("/regexText.json"),
                                        this::convertToRegexText);
    }

    @Bean
    public Watcher> KSRegexTextWatcher() {
        return centralDogma.fileWatcher(CENTRAL_DOGMA_PROJECT, CENTRAL_DOGMA_REPOSITORY,
                                        Query.ofJsonPath("/KSRegexText.json"),
                                        this::convertToRegexText);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy