Search in sources :

Example 1 with WorkMode

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

the class WorkResponseConverter method convert.

@Override
public Response convert(Object source, ChatHandlerExecutor creator) {
    Addressable a = creator.action().getAddressable();
    ChatResponseBody wr = creator.getOriginatingMapping().getHandlerMethod().getMethodAnnotation(ChatResponseBody.class);
    WorkMode wm = WorkMode.VIEW;
    if (wr != null) {
        String template = wr.template();
        WorkMode wmAnnotation = wr.workMode();
        if (wmAnnotation == WorkMode.EDIT) {
            wm = wmAnnotation;
        }
        if (StringUtils.hasText(template)) {
            Map<String, Object> entityMap = WorkResponse.createEntityMap(source, null, null);
            return new WorkResponse(a, entityMap, template, wm, source.getClass());
        }
    }
    return new WorkResponse(a, source, wm, null, null);
}
Also used : Addressable(org.finos.symphony.toolkit.workflow.content.Addressable) WorkResponse(org.finos.symphony.toolkit.workflow.response.WorkResponse) WorkMode(org.finos.symphony.toolkit.workflow.annotations.WorkMode) ChatResponseBody(org.finos.symphony.toolkit.workflow.annotations.ChatResponseBody)

Example 2 with WorkMode

use of org.finos.symphony.toolkit.workflow.annotations.WorkMode 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)

Example 3 with WorkMode

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

the class ButtonsResponseHandler method accept.

@Override
public void accept(Response t) {
    if (t instanceof WorkResponse) {
        Object o = ((WorkResponse) t).getFormObject();
        WorkMode wm = ((WorkResponse) t).getMode();
        ButtonList obl = (ButtonList) ((WorkResponse) t).getData().get(ButtonList.KEY);
        if ((obl != null) && (obl.getContents().size() > 0)) {
            return;
        }
        obl = new ButtonList();
        ((WorkResponse) t).getData().put(ButtonList.KEY, obl);
        final ButtonList bl = obl;
        initExposedHandlerMappings();
        exposedHandlerMappings.stream().flatMap(hm -> hm.getAllHandlers(t.getAddress(), null).stream()).filter(cm -> exposedMatchesObject(cm.getMapping(), o)).filter(cm -> cm.isButtonFor(o, wm)).forEach(cm -> {
            ChatButton e = cm.getMapping();
            String value = cm.getUniqueName();
            String text = e.buttonText();
            text = StringUtils.hasText(text) ? text : formatFieldName(cm.getHandlerMethod().getMethod().getName());
            bl.add(new Button(value, Type.ACTION, text));
        });
        Collections.sort((List<Button>) bl.getContents());
    }
}
Also used : Arrays(java.util.Arrays) ButtonList(org.finos.symphony.toolkit.workflow.form.ButtonList) Type(org.finos.symphony.toolkit.workflow.form.Button.Type) WorkMode(org.finos.symphony.toolkit.workflow.annotations.WorkMode) Response(org.finos.symphony.toolkit.workflow.response.Response) BeansException(org.springframework.beans.BeansException) Button(org.finos.symphony.toolkit.workflow.form.Button) ChatHandlerMapping(org.finos.symphony.toolkit.workflow.java.mapping.ChatHandlerMapping) Collectors(java.util.stream.Collectors) ApplicationContext(org.springframework.context.ApplicationContext) List(java.util.List) Optional(java.util.Optional) ChatButton(org.finos.symphony.toolkit.workflow.annotations.ChatButton) WorkResponse(org.finos.symphony.toolkit.workflow.response.WorkResponse) ResolvableType(org.springframework.core.ResolvableType) Collections(java.util.Collections) ApplicationContextAware(org.springframework.context.ApplicationContextAware) StringUtils(org.springframework.util.StringUtils) Button(org.finos.symphony.toolkit.workflow.form.Button) ChatButton(org.finos.symphony.toolkit.workflow.annotations.ChatButton) WorkResponse(org.finos.symphony.toolkit.workflow.response.WorkResponse) ChatButton(org.finos.symphony.toolkit.workflow.annotations.ChatButton) ButtonList(org.finos.symphony.toolkit.workflow.form.ButtonList) WorkMode(org.finos.symphony.toolkit.workflow.annotations.WorkMode)

Aggregations

WorkMode (org.finos.symphony.toolkit.workflow.annotations.WorkMode)3 WorkResponse (org.finos.symphony.toolkit.workflow.response.WorkResponse)2 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Optional (java.util.Optional)1 Collectors (java.util.stream.Collectors)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 ChatButton (org.finos.symphony.toolkit.workflow.annotations.ChatButton)1 ChatRequest (org.finos.symphony.toolkit.workflow.annotations.ChatRequest)1 ChatResponseBody (org.finos.symphony.toolkit.workflow.annotations.ChatResponseBody)1 ChatVariable (org.finos.symphony.toolkit.workflow.annotations.ChatVariable)1 Addressable (org.finos.symphony.toolkit.workflow.content.Addressable)1 Message (org.finos.symphony.toolkit.workflow.content.Message)1 Button (org.finos.symphony.toolkit.workflow.form.Button)1 Type (org.finos.symphony.toolkit.workflow.form.Button.Type)1