Search in sources :

Example 1 with ChatVariable

use of org.finos.symphony.toolkit.workflow.annotations.ChatVariable in project spring-bot by finos.

the class AbstractClassWorkflowResolver method canResolve.

@Override
public boolean canResolve(MethodParameter mp) {
    ChatVariable chatVariable = mp.getParameterAnnotation(ChatVariable.class);
    if (chatVariable != null) {
        return false;
    }
    Type t = mp.getGenericParameterType();
    if (isOptional(t)) {
        t = getOptionalType(t);
    }
    if (t instanceof Class<?>) {
        return canResolve((Class<?>) t);
    }
    return false;
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) ChatVariable(org.finos.symphony.toolkit.workflow.annotations.ChatVariable)

Example 2 with ChatVariable

use of org.finos.symphony.toolkit.workflow.annotations.ChatVariable in project spring-bot by finos.

the class ChatRequestChatHandlerMapping method createMappingRegistration.

@Override
protected MappingRegistration<ChatRequest> createMappingRegistration(ChatRequest mapping, ChatHandlerMethod handlerMethod) {
    List<WildcardContent> wildcards = createWildcardContent(mapping, handlerMethod);
    List<MessageMatcher> matchers = createMessageMatchers(mapping, wildcards);
    return new MappingRegistration<ChatRequest>(mapping, handlerMethod) {

        @Override
        public ChatHandlerExecutor getExecutor(Action a) {
            if (a instanceof SimpleMessageAction) {
                if (!canBePerformedHere((SimpleMessageAction) a)) {
                    return null;
                }
                return matchesSimpleMessageAction((SimpleMessageAction) a);
            }
            if (a instanceof FormAction) {
                if (Objects.nonNull(((FormAction) a).getData().get("form")) && ((FormAction) a).getData().get("form").getClass() != HelpPage.class) {
                    return null;
                }
                if (!canBePerformedHere((FormAction) a)) {
                    return null;
                }
                return matchesFormAction((FormAction) a);
            }
            return null;
        }

        private boolean canBePerformedHere(Action a) {
            ChatRequest cb = getMapping();
            return canBePerformed(a.getAddressable(), a.getUser(), cb);
        }

        private ChatHandlerExecutor matchesSimpleMessageAction(SimpleMessageAction a) {
            return pathMatches(a.getMessage(), a);
        }

        private ChatHandlerExecutor matchesFormAction(FormAction a) {
            return pathMatches(Message.of(Word.build(a.getAction())), a);
        }

        private ChatHandlerExecutor pathMatches(Message words, Action a) {
            MappingRegistration<?> me = this;
            ChatHandlerExecutor bestMatch = null;
            for (MessageMatcher messageMatcher : matchers) {
                Map<ChatVariable, Object> map = new HashMap<>();
                if (messageMatcher.consume(words, map)) {
                    if ((bestMatch == null) || (bestMatch.getReplacements().size() < map.size())) {
                        bestMatch = new AbstractHandlerExecutor(wrf, rh, converters) {

                            @Override
                            public Map<ChatVariable, Object> getReplacements() {
                                return map;
                            }

                            @Override
                            public Action action() {
                                return a;
                            }

                            @Override
                            public ChatMapping<?> getOriginatingMapping() {
                                return me;
                            }
                        };
                    }
                }
            }
            return bestMatch;
        }

        @Override
        public boolean isButtonFor(Object o, WorkMode m) {
            return false;
        }
    };
}
Also used : Action(org.finos.symphony.toolkit.workflow.actions.Action) SimpleMessageAction(org.finos.symphony.toolkit.workflow.actions.SimpleMessageAction) FormAction(org.finos.symphony.toolkit.workflow.actions.FormAction) Message(org.finos.symphony.toolkit.workflow.content.Message) HashMap(java.util.HashMap) ChatVariable(org.finos.symphony.toolkit.workflow.annotations.ChatVariable) HelpPage(org.finos.symphony.toolkit.workflow.help.HelpPage) SimpleMessageAction(org.finos.symphony.toolkit.workflow.actions.SimpleMessageAction) FormAction(org.finos.symphony.toolkit.workflow.actions.FormAction) ChatRequest(org.finos.symphony.toolkit.workflow.annotations.ChatRequest) HashMap(java.util.HashMap) Map(java.util.Map) WorkMode(org.finos.symphony.toolkit.workflow.annotations.WorkMode)

Aggregations

ChatVariable (org.finos.symphony.toolkit.workflow.annotations.ChatVariable)2 ParameterizedType (java.lang.reflect.ParameterizedType)1 Type (java.lang.reflect.Type)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Action (org.finos.symphony.toolkit.workflow.actions.Action)1 FormAction (org.finos.symphony.toolkit.workflow.actions.FormAction)1 SimpleMessageAction (org.finos.symphony.toolkit.workflow.actions.SimpleMessageAction)1 ChatRequest (org.finos.symphony.toolkit.workflow.annotations.ChatRequest)1 WorkMode (org.finos.symphony.toolkit.workflow.annotations.WorkMode)1 Message (org.finos.symphony.toolkit.workflow.content.Message)1 HelpPage (org.finos.symphony.toolkit.workflow.help.HelpPage)1