/***********************************************************************************************************************
 *
 * FixBro - an open source FIX broker (sell side) simulator
 * =========================================================
 *
 * Copyright (C) 2016 by Emilio Bosch Ferrando
 * https://github.com/embosfer
 *
 ***********************************************************************************************************************
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the License.
 *
 ***********************************************************************************************************************/
package com.embosfer.fixbro.controller.qfj;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.embosfer.fixbro.controller.sender.MessageSender;
import com.embosfer.fixbro.model.messages.NewOrderSingle;

import quickfix.DoNotSend;
import quickfix.FieldNotFound;
import quickfix.IncorrectDataFormat;
import quickfix.IncorrectTagValue;
import quickfix.Message;
import quickfix.RejectLogon;
import quickfix.SessionID;
import quickfix.UnsupportedMessageType;
import quickfix.field.SenderCompID;

public class QFJApplicationIn extends quickfix.fix44.MessageCracker implements quickfix.Application {

	private static final Logger logger = LoggerFactory.getLogger(QFJApplicationIn.class);
	private final MessageSender messageSender;

	public QFJApplicationIn(MessageSender messageSender) {
		this.messageSender = messageSender;
	}

	// callback notifying of every "admin" message (eg. logon, heartbeat)
	// received from the outside world
	@Override
	public void fromAdmin(Message msg, SessionID sessionID)
			throws FieldNotFound, IncorrectDataFormat, IncorrectTagValue, RejectLogon {
		if (logger.isDebugEnabled())
			logger.debug("fromAdmin on session {}. Msg {}", sessionID, msg);
	}

	// callback notifying of every "app" message (eg. NOS, ER) received from the
	// outside world
	@Override
	public void fromApp(Message msg, SessionID sessionID)
			throws FieldNotFound, IncorrectDataFormat, IncorrectTagValue, UnsupportedMessageType {
		if (logger.isDebugEnabled())
			logger.debug("fromApp on session {}. Msg {}", sessionID, msg);
		crack(msg, sessionID);
	}

	@Override
	public void onCreate(SessionID session) {
		logger.info("creating {}", session);
	}

	@Override
	public void onLogon(SessionID sessionID) {
		logger.info("loging on {}", sessionID);
	}

	@Override
	public void onLogout(SessionID sessionID) {
		logger.info("loging out {}", sessionID);
	}

	@Override
	public void toAdmin(Message msg, SessionID sessionID) {
		logger.info("toAdmin {}", sessionID);
	}

	@Override
	public void toApp(Message msg, SessionID sessionID) throws DoNotSend {
		logger.info("toApp on session {}. Msg {}", sessionID, msg);
	}

	@Override
	public void onMessage(quickfix.fix44.NewOrderSingle qfjNos, SessionID sessionID)
			throws FieldNotFound, UnsupportedMessageType, IncorrectTagValue {
		logger.info("NewOrderSingle received on session {} => {}", sessionID, qfjNos);
		NewOrderSingle.Builder builder = new NewOrderSingle.Builder();
		// mandatory fields
		builder.senderCompID(qfjNos.getHeader().getField(new SenderCompID()).getValue())
				.clOrdID(qfjNos.getClOrdID().getValue()).orderQty(qfjNos.getOrderQty().getValue())
				.ordType(qfjNos.getOrdType().getValue()).side(qfjNos.getSide().getValue())
				.symbol(qfjNos.getInstrument().getSymbol().getValue())
				.transactTime(qfjNos.getTransactTime().getValue());

		// non mandatory fields
		if (qfjNos.isSetPrice())
			builder.price(qfjNos.getPrice().getValue());
		NewOrderSingle nos = builder.build();
		logger.info("Transformed {}", nos);
		messageSender.sendNewOrderSingle(nos);
	}

}

@@ -1627,16 +1627,188 @@
 ompID;%0A%0A
+/**%0A * This component listens to FIX messages coming from clients, transforms them%0A * into FixBro datamodel and sends them in for processing%0A * %0A * @author embosfer%0A *%0A */%0A
 public c
@@ -2545,17 +2545,24 @@
 g. NOS, 
-E
+OCR, OCR
 R) recei
@@ -2556,32 +2556,36 @@
 , OCRR) received
+%0A%09//
  from the%0A%09// ou
@@ -2577,20 +2577,16 @@
 from the
-%0A%09//
  outside
@@ -4355,10 +4355,264 @@
 s);%0A%09%7D%0A%0A
+%09@Override%0A%09public void onMessage(quickfix.fix44.OrderCancelRequest qfjOcr, SessionID sessionID)%0A%09%09%09throws FieldNotFound, UnsupportedMessageType, IncorrectTagValue %7B%0A%09%09logger.info(%22OrderCancelRequest received on session %7B%7D =%3E %7B%7D%22, sessionID, qfjOcr);%0A%09%7D%0A
 %7D%0A

Add more documentation and add OrderCancelRequest callback