Search in sources :

Example 11 with SymphonyUser

use of org.finos.symphony.toolkit.workflow.sources.symphony.content.SymphonyUser in project spring-bot by finos.

the class MessageMLParser method parse.

public Message parse(String message, EntityJson jsonObjects) {
    message = (!message.contains("<messageML>")) ? "<messageML>" + message + "</messageML>" : message;
    Content[] out = { null };
    try {
        SAXParser saxParser = factory.newSAXParser();
        saxParser.parse(new InputSource(new StringReader(message)), new DefaultHandler2() {

            Frame<?> top = null;

            @Override
            public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
                if (top instanceof CodeBlockFrame) {
                    push(new IgnoredFrame(qName));
                } else if (isStartCodeBlock(qName, attributes)) {
                    push(new CodeBlockFrame(qName));
                } else if (isStartTag(qName, attributes)) {
                    String dataEntityId = attributes.getValue("data-entity-id");
                    Object o = jsonObjects.get(dataEntityId);
                    if (o instanceof SymphonyUser) {
                        push(new TagFrame<SymphonyUser>((SymphonyUser) o));
                    } else if (o instanceof HashTag) {
                        push(new TagFrame<HashTag>((HashTag) o));
                    } else if (o instanceof CashTag) {
                        push(new TagFrame<CashTag>((CashTag) o));
                    } else if (o instanceof Taxonomy && !((Taxonomy) o).getId().isEmpty() && ((Taxonomy) o).getId().get(0) instanceof HashTag) {
                        push(new TagFrame<HashTag>((HashTag) ((Taxonomy) o).getId().get(0)));
                    } else {
                        throw new UnsupportedOperationException();
                    }
                } else if (isStartTable(qName, attributes)) {
                    push(new TableFrame());
                } else if (isStartParaListItemOrCell(qName, attributes)) {
                    push(new ParagraphFrame());
                } else if (isStartList(qName, attributes)) {
                    push(new ListFrame(qName));
                } else if (isStartRow(qName, attributes)) {
                    if (top instanceof TableFrame) {
                        ((TableFrame) top).newRow();
                    } else {
                        throw new UnsupportedOperationException();
                    }
                } else if (isStartMessage(qName, attributes)) {
                    if (top == null) {
                        push(new MessageFrame());
                    }
                }
            }

            private boolean isStartMessage(String qName, Attributes attributes) {
                return "messageML".equals(qName) || ("div".equals(qName) && "PresentationML".equals(attributes.getValue("data-format")));
            }

            private boolean isStartList(String qName, Attributes attributes) {
                return "ul".equals(qName) || "ol".equals(qName);
            }

            private boolean isStartRow(String qName, Attributes attributes) {
                return "tr".equals(qName);
            }

            private boolean isStartCodeBlock(String qName, Attributes attributes) {
                return "pre".equals(qName) || "code".equals(qName);
            }

            private <X extends Frame<?>> X push(X newFrame) {
                newFrame.parent = top;
                top = newFrame;
                return newFrame;
            }

            @Override
            public void startEntity(String name) throws SAXException {
            // do nothing
            }

            @Override
            public void endEntity(String name) throws SAXException {
            // do nothing
            }

            private boolean isStartTable(String qName, Attributes attributes) {
                return "table".equals(qName);
            }

            private boolean isStartTag(String qName, Attributes attributes) {
                return "span".equals(qName) && (attributes.getValue("class") != null) && (attributes.getValue("class").contains("entity"));
            }

            private boolean isStartParaListItemOrCell(String qName, Attributes attributes) {
                return "p".equals(qName) || "td".equals(qName) || "li".equals(qName) || "th".equals(qName);
            }

            @Override
            public void endElement(String uri, String localName, String qName) throws SAXException {
                if (top.isEnding(qName)) {
                    Frame<?> parent = top.parent;
                    Content c = top.getContents();
                    if (parent == null) {
                        out[0] = c;
                    } else {
                        top = parent;
                        top.push(c);
                    }
                }
            }

            @Override
            public void characters(char[] ch, int start, int length) throws SAXException {
                if (top instanceof TextFrame) {
                    ((TextFrame<?>) top).push(ch, start, length);
                } else {
                    String content = new String(ch, start, length);
                    if (!content.trim().isEmpty()) {
                        throw new UnsupportedOperationException("Wasn't expecting text: " + content);
                    }
                }
            }

            @Override
            public void warning(SAXParseException e) throws SAXException {
                LOG.error("SAX warning: ", e);
            }

            @Override
            public void error(SAXParseException e) throws SAXException {
                LOG.error("SAX error: ", e);
            }

            @Override
            public void fatalError(SAXParseException e) throws SAXException {
                LOG.error("SAX fatal error: ", e);
            }
        });
    } catch (Exception e) {
        throw new RuntimeException("Couldn't parse message: " + message, e);
    }
    return (Message) out[0];
}
Also used : InputSource(org.xml.sax.InputSource) Message(org.finos.symphony.toolkit.workflow.content.Message) Taxonomy(org.symphonyoss.Taxonomy) HashTag(org.finos.symphony.toolkit.workflow.sources.symphony.content.HashTag) Attributes(org.xml.sax.Attributes) CashTag(org.finos.symphony.toolkit.workflow.sources.symphony.content.CashTag) SAXException(org.xml.sax.SAXException) SAXParseException(org.xml.sax.SAXParseException) StringReader(java.io.StringReader) SAXParser(javax.xml.parsers.SAXParser) SymphonyUser(org.finos.symphony.toolkit.workflow.sources.symphony.content.SymphonyUser) DefaultHandler2(org.xml.sax.ext.DefaultHandler2) SAXParseException(org.xml.sax.SAXParseException) SAXException(org.xml.sax.SAXException) Content(org.finos.symphony.toolkit.workflow.content.Content) OrderedContent(org.finos.symphony.toolkit.workflow.content.OrderedContent)

Example 12 with SymphonyUser

use of org.finos.symphony.toolkit.workflow.sources.symphony.content.SymphonyUser in project spring-bot by finos.

the class TestFormMessageML method createWeirdFieldsWorkResponse.

protected WorkResponse createWeirdFieldsWorkResponse(WorkMode wm) {
    SymphonyRoom theRoom = new SymphonyRoom("tesxt room", "abc123");
    SymphonyUser someUser = new SymphonyUser(2678l, "bob", "bob@example.com");
    WeirdObject to4 = new WeirdObject();
    to4.setTheId(new HashTag("adf360dd-06fe-43a4-9a62-2c17fe2deefa"));
    to4.setC(Choice.C);
    to4.setSomeUser(someUser);
    to4.setCashTag(new CashTag("rameses"));
    to4.setB(true);
    to4.setC(Choice.B);
    Button submit = new Button("submit", Type.ACTION, "GO");
    WorkResponse wr = new WorkResponse(theRoom, to4, wm);
    ButtonList bl = (ButtonList) wr.getData().get(ButtonList.KEY);
    bl.add(submit);
    return wr;
}
Also used : WeirdObject(org.finos.symphony.toolkit.workflow.fixture.WeirdObject) Button(org.finos.symphony.toolkit.workflow.form.Button) HashTag(org.finos.symphony.toolkit.workflow.sources.symphony.content.HashTag) WorkResponse(org.finos.symphony.toolkit.workflow.response.WorkResponse) CashTag(org.finos.symphony.toolkit.workflow.sources.symphony.content.CashTag) SymphonyUser(org.finos.symphony.toolkit.workflow.sources.symphony.content.SymphonyUser) ButtonList(org.finos.symphony.toolkit.workflow.form.ButtonList) SymphonyRoom(org.finos.symphony.toolkit.workflow.sources.symphony.content.SymphonyRoom)

Example 13 with SymphonyUser

use of org.finos.symphony.toolkit.workflow.sources.symphony.content.SymphonyUser in project spring-bot by finos.

the class TestFormMessageML method createNestedWeirdFieldsWorkResponse.

protected WorkResponse createNestedWeirdFieldsWorkResponse(WorkMode wm) {
    SymphonyUser someUser = new SymphonyUser(2678l, "bob", "bob@example.com");
    WeirdObject to4 = new WeirdObject();
    to4.setB(true);
    to4.setC(Choice.B);
    to4.setSomeUser(someUser);
    to4.setTheId(new HashTag("adf360dd-06fe-43a4-9a62-2c17fe2deefa"));
    WeirdObjectCollection ob5 = new WeirdObjectCollection();
    ob5.setOb4(to4);
    WorkResponse wr = createWorkAddSubmit(wm, ob5);
    return wr;
}
Also used : WeirdObject(org.finos.symphony.toolkit.workflow.fixture.WeirdObject) HashTag(org.finos.symphony.toolkit.workflow.sources.symphony.content.HashTag) WeirdObjectCollection(org.finos.symphony.toolkit.workflow.fixture.WeirdObjectCollection) WorkResponse(org.finos.symphony.toolkit.workflow.response.WorkResponse) SymphonyUser(org.finos.symphony.toolkit.workflow.sources.symphony.content.SymphonyUser)

Example 14 with SymphonyUser

use of org.finos.symphony.toolkit.workflow.sources.symphony.content.SymphonyUser in project spring-bot by finos.

the class SymphonyConversationsImpl method ensureChat.

@Override
public SymphonyRoom ensureChat(Chat r, List<User> users, Map<String, Object> meta) {
    String description = "";
    String name = r.getName();
    boolean isPublic = false;
    description = (String) meta.getOrDefault(ROOM_DESCRIPTION, "");
    isPublic = (boolean) meta.getOrDefault(ROOM_PUBLIC, false);
    SymphonyRoom theRoom = null;
    if (r instanceof SymphonyRoom) {
        if (((SymphonyRoom) r).getStreamId() != null) {
            theRoom = (SymphonyRoom) r;
        } else {
            theRoom = loadRoomByName(name);
        }
    }
    if (theRoom == null) {
        // create the room
        V3RoomAttributes ra = new V3RoomAttributes().name(name).description(description)._public(isPublic).discoverable(isPublic);
        V3RoomDetail detail = streamsApi.v3RoomCreatePost(ra, null);
        String streamId = detail.getRoomSystemInfo().getId();
        theRoom = new SymphonyRoom(name, streamId);
        // next, we need to make sure that all of the admins are members of the room and owners.
        List<Long> adminIds = getDefaultAdministrators().stream().filter(u -> u instanceof SymphonyUser).map(u -> (SymphonyUser) u).map(su -> Long.parseLong(su.getUserId())).filter(id -> id != null).collect(Collectors.toList());
        for (Long user : adminIds) {
            UserId u = new UserId().id(user);
            rmApi.v1RoomIdMembershipAddPost(u, null, streamId);
            rmApi.v1RoomIdMembershipPromoteOwnerPost(u, null, streamId);
        }
        LOG.info("Created room {} with admins {} ", theRoom, getDefaultAdministrators());
    }
    // next, ensure that all the users are in the room
    String streamId = theRoom.getStreamId();
    users.stream().filter(u -> u instanceof SymphonyUser).map(u -> (SymphonyUser) u).forEach(u -> rmApi.v1RoomIdMembershipAddPost(new UserId().id(Long.parseLong(u.getUserId())), null, streamId));
    return theRoom;
}
Also used : V2RoomSearchCriteria(com.symphony.api.model.V2RoomSearchCriteria) User(org.finos.symphony.toolkit.workflow.content.User) SymphonyIdentity(com.symphony.api.id.SymphonyIdentity) TypeEnum(com.symphony.api.model.StreamType.TypeEnum) LoggerFactory(org.slf4j.LoggerFactory) V3RoomDetail(com.symphony.api.model.V3RoomDetail) StreamsApi(com.symphony.api.pod.StreamsApi) InitializingBean(org.springframework.beans.factory.InitializingBean) V3RoomAttributes(com.symphony.api.model.V3RoomAttributes) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Map(java.util.Map) StreamList(com.symphony.api.model.StreamList) UserV2(com.symphony.api.model.UserV2) V3RoomSearchResults(com.symphony.api.model.V3RoomSearchResults) Logger(org.slf4j.Logger) UsersApi(com.symphony.api.pod.UsersApi) SymphonyUser(org.finos.symphony.toolkit.workflow.sources.symphony.content.SymphonyUser) AbstractStreamResolving(org.finos.symphony.toolkit.workflow.sources.symphony.streams.AbstractStreamResolving) MembershipList(com.symphony.api.model.MembershipList) SymphonyApiProperties(org.finos.symphony.toolkit.spring.api.properties.SymphonyApiProperties) RoomMembershipApi(com.symphony.api.pod.RoomMembershipApi) Set(java.util.Set) Chat(org.finos.symphony.toolkit.workflow.content.Chat) Collectors(java.util.stream.Collectors) List(java.util.List) Addressable(org.finos.symphony.toolkit.workflow.content.Addressable) StreamAttributes(com.symphony.api.model.StreamAttributes) StreamID(com.symphony.user.StreamID) SymphonyRoom(org.finos.symphony.toolkit.workflow.sources.symphony.content.SymphonyRoom) StreamFilter(com.symphony.api.model.StreamFilter) Collections(java.util.Collections) StreamType(com.symphony.api.model.StreamType) UserId(com.symphony.api.model.UserId) V3RoomDetail(com.symphony.api.model.V3RoomDetail) UserId(com.symphony.api.model.UserId) V3RoomAttributes(com.symphony.api.model.V3RoomAttributes) SymphonyUser(org.finos.symphony.toolkit.workflow.sources.symphony.content.SymphonyUser) SymphonyRoom(org.finos.symphony.toolkit.workflow.sources.symphony.content.SymphonyRoom)

Example 15 with SymphonyUser

use of org.finos.symphony.toolkit.workflow.sources.symphony.content.SymphonyUser in project spring-bot by finos.

the class SymphonyConversationsImpl method loadUserByStreamAttributes.

protected SymphonyUser loadUserByStreamAttributes(StreamAttributes si) {
    if (si.getStreamAttributes().getMembers().size() != 2) {
        return null;
    } else {
        Long userId = si.getStreamAttributes().getMembers().stream().filter(id -> id != botUserId).findFirst().orElse(null);
        if (userId == null) {
            return null;
        }
        SymphonyUser out = new SymphonyUser(userId);
        out.getId().add(new StreamID(si.getId()));
        return out;
    }
}
Also used : StreamID(com.symphony.user.StreamID) SymphonyUser(org.finos.symphony.toolkit.workflow.sources.symphony.content.SymphonyUser)

Aggregations

SymphonyUser (org.finos.symphony.toolkit.workflow.sources.symphony.content.SymphonyUser)15 SymphonyRoom (org.finos.symphony.toolkit.workflow.sources.symphony.content.SymphonyRoom)7 HashTag (org.finos.symphony.toolkit.workflow.sources.symphony.content.HashTag)6 Test (org.junit.jupiter.api.Test)5 Message (org.finos.symphony.toolkit.workflow.content.Message)4 User (org.finos.symphony.toolkit.workflow.content.User)4 StreamID (com.symphony.user.StreamID)3 EntityJson (org.finos.symphony.toolkit.json.EntityJson)3 Chat (org.finos.symphony.toolkit.workflow.content.Chat)3 CashTag (org.finos.symphony.toolkit.workflow.sources.symphony.content.CashTag)3 MembershipList (com.symphony.api.model.MembershipList)2 Stream (com.symphony.api.model.Stream)2 StreamAttributes (com.symphony.api.model.StreamAttributes)2 StreamList (com.symphony.api.model.StreamList)2 StreamType (com.symphony.api.model.StreamType)2 UserV2 (com.symphony.api.model.UserV2)2 V3RoomAttributes (com.symphony.api.model.V3RoomAttributes)2 V3RoomDetail (com.symphony.api.model.V3RoomDetail)2 V3RoomSearchResults (com.symphony.api.model.V3RoomSearchResults)2 Action (org.finos.symphony.toolkit.workflow.actions.Action)2