Search in sources :

Example 1 with FormAction

use of org.finos.symphony.toolkit.workflow.actions.FormAction in project spring-bot by finos.

the class ElementsHandler method accept.

@SuppressWarnings("unchecked")
@Override
public void accept(V4Event t) {
    try {
        V4SymphonyElementsAction action = t.getPayload().getSymphonyElementsAction();
        if (action != null) {
            Map<String, Object> formValues = (Map<String, Object>) action.getFormValues();
            String verb = (String) formValues.get("action");
            String formId = action.getFormId();
            Object currentForm;
            if (hasFormData(formValues)) {
                currentForm = formConverter.convert(formValues, formId);
            } else {
                currentForm = null;
            }
            EntityJson data = retrieveData(action.getFormMessageId());
            Addressable rr = ruBuilder.loadRoomById(action.getStream().getStreamId());
            User u = ruBuilder.loadUserById(t.getInitiator().getUser().getUserId());
            // if we're not in a room, address the user directly.
            rr = rr == null ? u : rr;
            Errors e = ErrorHelp.createErrorHolder();
            if (validated(currentForm, e)) {
                FormAction ea = new FormAction(rr, u, currentForm, verb, data);
                try {
                    Action.CURRENT_ACTION.set(ea);
                    for (ActionConsumer c : elementsConsumers) {
                        try {
                            c.accept(ea);
                        } catch (Exception ee) {
                            LOG.error("Failed to handle consumer " + c, ee);
                        }
                    }
                } finally {
                    Action.CURRENT_ACTION.set(Action.NULL_ACTION);
                }
            } else {
                WorkResponse fr = new WorkResponse(rr, currentForm, WorkMode.EDIT, ButtonList.of(new Button(verb, Button.Type.ACTION, "Retry")), convertErrorsToMap(e));
                rh.accept(fr);
            }
        }
    } catch (Exception e) {
        LOG.error("Couldn't handle event " + t, e);
    }
}
Also used : EntityJson(org.finos.symphony.toolkit.json.EntityJson) User(org.finos.symphony.toolkit.workflow.content.User) ActionConsumer(org.finos.symphony.toolkit.workflow.actions.consumers.ActionConsumer) Errors(org.springframework.validation.Errors) Button(org.finos.symphony.toolkit.workflow.form.Button) V4SymphonyElementsAction(com.symphony.api.model.V4SymphonyElementsAction) FormAction(org.finos.symphony.toolkit.workflow.actions.FormAction) Addressable(org.finos.symphony.toolkit.workflow.content.Addressable) WorkResponse(org.finos.symphony.toolkit.workflow.response.WorkResponse) Map(java.util.Map) ErrorMap(org.finos.symphony.toolkit.workflow.form.ErrorMap)

Example 2 with FormAction

use of org.finos.symphony.toolkit.workflow.actions.FormAction in project spring-bot by finos.

the class TestTableEdit method add.

protected void add(String testStem, Object toChange, Object newRow, String spel) throws Exception {
    EntityJson ej = new EntityJson();
    ej.put(WorkResponse.OBJECT_KEY, toChange);
    FormAction ea = new FormAction(room, u, toChange, spel + TableAddRow.ACTION_SUFFIX, ej);
    addRows.accept(ea);
    // should get a new form back.
    String jsonStr = testTemplating("abc123", testStem + "1.ml", testStem + "1.json");
    EntityJson json = ejc.readValue(jsonStr);
    ea = new FormAction(room, u, newRow, spel + TableAddRow.DO_SUFFIX, json);
    Mockito.reset(messagesApi);
    addRows.accept(ea);
    testTemplating("abc123", testStem + "2.ml", testStem + "2.json");
}
Also used : EntityJson(org.finos.symphony.toolkit.json.EntityJson) FormAction(org.finos.symphony.toolkit.workflow.actions.FormAction)

Example 3 with FormAction

use of org.finos.symphony.toolkit.workflow.actions.FormAction in project spring-bot by finos.

the class TestTableEdit method testDeleteRows.

@Test
public void testDeleteRows() throws Exception {
    EntityJson ej = new EntityJson();
    ej.put(WorkResponse.OBJECT_KEY, to);
    Map<String, Object> selects = Collections.singletonMap("items", Collections.singletonList(Collections.singletonMap("selected", "true")));
    FormSubmission uc = new FormSubmission(TestObjects.class.getCanonicalName(), selects);
    FormAction ea = new FormAction(room, u, uc, "items." + TableDeleteRows.ACTION_SUFFIX, ej);
    deleteRows.accept(ea);
    testTemplating("abc123", "testDeleteRows.ml", "testDeleteRows.json");
}
Also used : EntityJson(org.finos.symphony.toolkit.json.EntityJson) TestObjects(org.finos.symphony.toolkit.workflow.fixture.TestObjects) FormSubmission(org.finos.symphony.toolkit.workflow.form.FormSubmission) FormAction(org.finos.symphony.toolkit.workflow.actions.FormAction) TestObject(org.finos.symphony.toolkit.workflow.fixture.TestObject) Test(org.junit.jupiter.api.Test)

Example 4 with FormAction

use of org.finos.symphony.toolkit.workflow.actions.FormAction 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 5 with FormAction

use of org.finos.symphony.toolkit.workflow.actions.FormAction in project spring-bot by finos.

the class TestHandlerMapping method pressButton.

private void pressButton(String s) throws Exception {
    EntityJson jsonObjects = new EntityJson();
    jsonObjects.put("1", new SymphonyUser(123l, "gaurav", "gaurav@example.com"));
    jsonObjects.put("2", new HashTag("SomeTopic"));
    Chat r = new SymphonyRoom("The Room Where It Happened", "abc123");
    User author = new SymphonyUser(ROB_EXAMPLE_ID, ROB_NAME, ROB_EXAMPLE_EMAIL);
    Object fd = new StartClaim();
    Action a = new FormAction(r, author, fd, s, jsonObjects);
    Action.CURRENT_ACTION.set(a);
    mc.accept(a);
}
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) EntityJson(org.finos.symphony.toolkit.json.EntityJson) User(org.finos.symphony.toolkit.workflow.content.User) SymphonyUser(org.finos.symphony.toolkit.workflow.sources.symphony.content.SymphonyUser) StartClaim(org.finos.symphony.toolkit.workflow.fixture.StartClaim) HashTag(org.finos.symphony.toolkit.workflow.sources.symphony.content.HashTag) Chat(org.finos.symphony.toolkit.workflow.content.Chat) FormAction(org.finos.symphony.toolkit.workflow.actions.FormAction) SymphonyUser(org.finos.symphony.toolkit.workflow.sources.symphony.content.SymphonyUser) SymphonyRoom(org.finos.symphony.toolkit.workflow.sources.symphony.content.SymphonyRoom)

Aggregations

FormAction (org.finos.symphony.toolkit.workflow.actions.FormAction)7 EntityJson (org.finos.symphony.toolkit.json.EntityJson)5 Map (java.util.Map)3 Action (org.finos.symphony.toolkit.workflow.actions.Action)2 SimpleMessageAction (org.finos.symphony.toolkit.workflow.actions.SimpleMessageAction)2 User (org.finos.symphony.toolkit.workflow.content.User)2 V4SymphonyElementsAction (com.symphony.api.model.V4SymphonyElementsAction)1 HashMap (java.util.HashMap)1 Optional (java.util.Optional)1 ActionConsumer (org.finos.symphony.toolkit.workflow.actions.consumers.ActionConsumer)1 ChatRequest (org.finos.symphony.toolkit.workflow.annotations.ChatRequest)1 ChatVariable (org.finos.symphony.toolkit.workflow.annotations.ChatVariable)1 WorkMode (org.finos.symphony.toolkit.workflow.annotations.WorkMode)1 Addressable (org.finos.symphony.toolkit.workflow.content.Addressable)1 Chat (org.finos.symphony.toolkit.workflow.content.Chat)1 Message (org.finos.symphony.toolkit.workflow.content.Message)1 StartClaim (org.finos.symphony.toolkit.workflow.fixture.StartClaim)1 TestObject (org.finos.symphony.toolkit.workflow.fixture.TestObject)1 TestObjects (org.finos.symphony.toolkit.workflow.fixture.TestObjects)1 Button (org.finos.symphony.toolkit.workflow.form.Button)1