Search in sources :

Example 1 with ActionConsumer

use of org.finos.symphony.toolkit.workflow.actions.consumers.ActionConsumer 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 ActionConsumer

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

the class PresentationMLHandler method accept.

@Override
public void accept(V4Event t) {
    try {
        V4MessageSent ms = t.getPayload().getMessageSent();
        if ((ms != null) && (!ms.getMessage().getUser().getEmail().equals(botIdentity.getEmail()))) {
            // ok, this is a message, and it's from a third party.  Parse it.
            EntityJson ej = jsonConverter.readValue(ms.getMessage().getData());
            Message words = messageParser.parse(ms.getMessage().getMessage(), ej);
            Addressable rr = ruBuilder.loadRoomById(ms.getMessage().getStream().getStreamId());
            User u = ruBuilder.loadUserById(ms.getMessage().getUser().getUserId());
            // TODO: multi-user chat (not room)
            rr = rr == null ? u : rr;
            SimpleMessageAction sma = new SimpleMessageAction(rr, u, words, ej);
            try {
                Action.CURRENT_ACTION.set(sma);
                for (ActionConsumer c : messageConsumers) {
                    c.accept(sma);
                }
            } finally {
                Action.CURRENT_ACTION.set(Action.NULL_ACTION);
            }
        }
    } 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) Message(org.finos.symphony.toolkit.workflow.content.Message) ActionConsumer(org.finos.symphony.toolkit.workflow.actions.consumers.ActionConsumer) SimpleMessageAction(org.finos.symphony.toolkit.workflow.actions.SimpleMessageAction) V4MessageSent(com.symphony.api.model.V4MessageSent) Addressable(org.finos.symphony.toolkit.workflow.content.Addressable)

Aggregations

EntityJson (org.finos.symphony.toolkit.json.EntityJson)2 ActionConsumer (org.finos.symphony.toolkit.workflow.actions.consumers.ActionConsumer)2 Addressable (org.finos.symphony.toolkit.workflow.content.Addressable)2 User (org.finos.symphony.toolkit.workflow.content.User)2 V4MessageSent (com.symphony.api.model.V4MessageSent)1 V4SymphonyElementsAction (com.symphony.api.model.V4SymphonyElementsAction)1 Map (java.util.Map)1 FormAction (org.finos.symphony.toolkit.workflow.actions.FormAction)1 SimpleMessageAction (org.finos.symphony.toolkit.workflow.actions.SimpleMessageAction)1 Message (org.finos.symphony.toolkit.workflow.content.Message)1 Button (org.finos.symphony.toolkit.workflow.form.Button)1 ErrorMap (org.finos.symphony.toolkit.workflow.form.ErrorMap)1 WorkResponse (org.finos.symphony.toolkit.workflow.response.WorkResponse)1 Errors (org.springframework.validation.Errors)1