FIXTradingCommunity / fix-orchestra

Machine readable rules of engagement
Apache License 2.0
71 stars 34 forks source link

[Question] Accessing messages tutorial #95

Closed nngakosso closed 4 years ago

nngakosso commented 4 years ago

Hello, I'm following the hands on tutorial on accessing messages. For my class, I got this:

package com.example.orchestra.sample;

import java.io.InputStream;
import java.io.PrintStream;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import io.fixprotocol._2020.orchestra.repository.Repository;

public class RepositoryReporter {

    public void report(InputStream is, PrintStream os) {
        try {
            // parse the XML
            Repository repository = unmarshal(is);
            // report messages
            List<MessageType> messageList = repository.getMessages().getMessage();
            reportMessages(messageList, os);
            // TODO
        } catch (JAXBException e) {
            // print exception
            os.format("ERROR: %s%n", e.getMessage());
        }
    }
    void reportMessages(List<MessageType> messageList, PrintStream os) {
        // iterate all messages in the file
        for (MessageType message: messageList) {
            os.format("Message name: %s scenario: %s MsgType: %s%n",
                    message.getName(), message.getScenario(), message.getMsgType());
            // report message members
            List<Object> members = message.getStructure().getComponentRefOrGroupRefOrFieldRef();
            reportMembers(members, os);
            // TODO
        }
    }
    void reportMembers(List<Object> members, PrintStream os) {
        for (Object member : members) {
            if (member instanceof FieldRefType) {
                FieldRefType fieldRef = (FieldRefType)member;
                os.format("\tFieldRef id: %d scenario: %s%n", fieldRef.getId(), fieldRef.getScenario());
            } else if (member instanceof ComponentRefType) {
                ComponentRefType componentRef = (ComponentRefType)member;
                os.format("\tComponentRef id: %d scenario: %s%n", componentRef.getId(), componentRef.getScenario());
            } else if (member instanceof GroupRefType) {
                GroupRefType groupRef = (GroupRefType)member;
                os.format("\tGroupRef id: %d scenario: %s%n", groupRef.getId(), groupRef.getScenario());
            }
        }
    }

    private Repository unmarshal(InputStream is) throws JAXBException {
        final JAXBContext jaxbContext = JAXBContext.newInstance(Repository.class);
        final Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
        return (Repository) jaxbUnmarshaller.unmarshal(is);
    }

}

First does it look correct to? I'm not a dev but as I need to get my hands on orchestra I'm willing to build an app.

Second, I know I need to change package com.example.orchestra.sample; , but precisely does it refer to a package inside an orchestra file, I didnt get this part right.

Thanks

donmendelson commented 4 years ago

Your code appears to compile with the addition of a few missing import statements.

See Naming a Package for tutorial on package names and organization.

nngakosso commented 4 years ago

Thank you, I wanted to clarify whether or not I needed to create a new package to import.