/*
 * Copyright 2019 EPAM Systems
 *
 * 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.epam.ta.reportportal.core.events;

import com.epam.ta.reportportal.core.events.attachment.DeleteAttachmentEvent;
import com.epam.ta.reportportal.entity.activity.Activity;
import org.springframework.amqp.core.AmqpTemplate;

import static com.epam.ta.reportportal.core.configs.rabbit.InternalConfiguration.*;

public class MessageBusImpl implements MessageBus {

	private final AmqpTemplate amqpTemplate;

	public MessageBusImpl(AmqpTemplate amqpTemplate) {
		this.amqpTemplate = amqpTemplate;
	}

	@Override
	public void publish(String exchange, String route, Object o) {
		this.amqpTemplate.convertAndSend(exchange, route, o);
	}

	@Override
	public void publish(String route, Object o) {
		this.amqpTemplate.convertSendAndReceive(route, o);
	}

	@Override
	public void broadcastEvent(Object o) {
		this.amqpTemplate.convertAndSend(EXCHANGE_EVENTS, "", o);
	}

	/**
	 * Publishes activity to the queue with the following routing key
	 * <pre>{@code activity.<project-id>.<entity-type>.<action>}</pre>
	 * @param event Activity event to be converted to Activity object
	 */
	@Override
	public void publishActivity(ActivityEvent event) {
		final Activity activity = event.toActivity();
		String key = "activity." + activity.getProjectId() + "." + activity.getActivityEntityType() + "." + activity.getAction();
		this.amqpTemplate.convertAndSend(EXCHANGE_ACTIVITY, key, activity);
	}

	@Override
	public void publishDeleteAttachmentEvent(DeleteAttachmentEvent event) {

		amqpTemplate.convertAndSend(EXCHANGE_ATTACHMENT, QUEUE_ATTACHMENT_DELETE, event);

	}
}

@@ -1599,16 +1599,20 @@
 %7D%3C/pre%3E%0A
+%09 *%0A
 %09 * @par
@@ -1670,16 +1670,16 @@
  object%0A
-
 %09 */%0A%09@O
@@ -1786,16 +1786,43 @@
 vity();%0A
+%09%09if (activity != null) %7B%0A%09
 %09%09String
@@ -1939,16 +1939,17 @@
 on();%0A%09%09
+%09
 this.amq
@@ -2008,16 +2008,20 @@
 ivity);%0A
+%09%09%7D%0A
 %09%7D%0A%0A%09@Ov

fix npe in activity