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

net.mossol.bot.service.impl.RegexMatcherServiceImpl Maven / Gradle / Ivy

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

import static net.mossol.bot.model.TextType.ADD_MENU_D;
import static net.mossol.bot.model.TextType.ADD_MENU_J;
import static net.mossol.bot.model.TextType.ADD_MENU_K;
import static net.mossol.bot.model.TextType.DEL_MENU_D;
import static net.mossol.bot.model.TextType.DEL_MENU_J;
import static net.mossol.bot.model.TextType.DEL_MENU_K;
import static net.mossol.bot.model.TextType.TEXT;

import java.util.List;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import net.mossol.bot.model.RegexText;
import net.mossol.bot.model.ReplyMessage;
import net.mossol.bot.service.MatcherService;
import net.mossol.bot.service.MenuServiceHandler;
import net.mossol.bot.service.MenuServiceHandler.FoodType;

import com.linecorp.centraldogma.client.Watcher;

@Service
@Order(2)
public class RegexMatcherServiceImpl implements MatcherService {
    private static final Logger logger = LoggerFactory.getLogger(RegexMatcherServiceImpl.class);
    private volatile List regexTextContext;

    @Resource
    private Watcher> regexTextWatcher;

    @Resource
    private MenuServiceHandler menuServiceHandler;

    @PostConstruct
    private void init() {
        regexTextWatcher.watch((revision, context) -> {
            if (context == null)  {
                logger.warn("RegexText Watch Failed");
                return;
            }
            logger.info("RegexText Updated : " + context);
            regexTextContext = context;
        });
    }

    private ReplyMessage regexTextHandle(String message) {
        for (RegexText regex : regexTextContext) {
            List result = regex.match(message);
            if (!CollectionUtils.isEmpty(result)) {
                logger.debug("Regex Matched : message{}, match{}", message, result);
                switch (regex.getType()) {
                    case ADD_MENU_K:
                        return new ReplyMessage(ADD_MENU_K, null,
                                                menuServiceHandler.addMenu(result, FoodType.KOREA_FOOD));
                    case ADD_MENU_J:
                        return new ReplyMessage(ADD_MENU_J, null,
                                                menuServiceHandler.addMenu(result, FoodType.JAPAN_FOOD));
                    case ADD_MENU_D:
                        return new ReplyMessage(ADD_MENU_D, null,
                                                menuServiceHandler.addMenu(result, FoodType.DRINK_FOOD));
                    case DEL_MENU_K:
                        return new ReplyMessage(DEL_MENU_K, null,
                                                menuServiceHandler.removeMenu(result, FoodType.KOREA_FOOD));
                    case DEL_MENU_J:
                        return new ReplyMessage(DEL_MENU_J, null,
                                                menuServiceHandler.removeMenu(result, FoodType.JAPAN_FOOD));
                    case DEL_MENU_D:
                        return new ReplyMessage(DEL_MENU_D, null,
                                                menuServiceHandler.removeMenu(result, FoodType.DRINK_FOOD));
                    case TEXT:
                        return new ReplyMessage(TEXT, null, regex.getResponse());
                }
            }
        }
        logger.debug("There is no matched regex for the message : " + message);
        return null;
    }

    @Override
    public ReplyMessage match(String requestMessage) {
        return regexTextHandle(requestMessage.replaceAll("\\s+", ""));
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy