class NTGMessageHelper extends MessageHelper {

	@Override
	public IMessage prepareMessageToEncode(IMessage message, Map<String, String> params) {
		// AML3 - header can be filled like repeating group.
		IMessage header = (IMessage) message.getField(MESSAGE_HEADER);
        NTGMessageMetadata m = getMetadata(message.getName());
        if(header == null) {
            header = getMessageFactory().createMessage(MESSAGE_HEADER, getNamespace());
            // add header to message
            message.addField(MESSAGE_HEADER, header);
        }

        if (!header.isFieldSet(FIELD_MESSAGE_LENGTH)) {
            // special case: handle AML2 actions that doesn't sets
            // MessageLength field
            header.addField(FIELD_MESSAGE_LENGTH, m.messageSize);
        }
        if (!header.isFieldSet(FIELD_START_OF_MESSAGE)) {
            header.addField(FIELD_START_OF_MESSAGE, VALUE_START_OF_MESSAGE);
        }
        if (!header.isFieldSet(MESSAGE_TYPE)) {
            header.addField(MESSAGE_TYPE, m.messageType);
        }


		if (params != null) {
            // AML2 - header filled by actions. Dirty AML2 uses "#nativemsgtype", "#dirtystart", "#dirtymsglen" to override header's fields
		    String value = params.get(FIELD_DIRTY_MSG_LEN);
		    if (value != null) {
                header.addField(FIELD_MESSAGE_LENGTH, Integer.valueOf(value));
		    }
		    
		    value = params.get(FIELD_DIRTY_MSG_TYPE);
            if (value != null) {
                header.addField(MESSAGE_TYPE, value);
            }
            
            value = params.get(FIELD_DIRTY_START);
            if (value != null) {
                header.addField(FIELD_START_OF_MESSAGE, Integer.valueOf(value));
            }
		}
		return message;
	}

}