package com.gentics.mesh.core.verticle.migration.impl;

import static com.gentics.mesh.Events.MESH_MIGRATION;
import static com.gentics.mesh.core.rest.admin.migration.MigrationStatus.COMPLETED;
import static com.gentics.mesh.core.rest.admin.migration.MigrationStatus.FAILED;

import java.lang.management.ManagementFactory;

import javax.management.MBeanServer;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;

import com.gentics.mesh.core.data.job.Job;
import com.gentics.mesh.core.data.release.ReleaseVersionEdge;
import com.gentics.mesh.core.rest.admin.migration.MigrationStatus;
import com.gentics.mesh.core.rest.admin.migration.MigrationType;
import com.gentics.mesh.core.verticle.migration.MigrationStatusHandler;
import com.syncleus.ferma.tx.Tx;

import io.vertx.core.Vertx;
import io.vertx.core.json.JsonObject;
import io.vertx.core.logging.Logger;
import io.vertx.core.logging.LoggerFactory;

/**
 * The migration status class keeps track of the status of a migration and manages also the errors and event handling.
 */
public class MigrationStatusHandlerImpl implements MigrationStatusHandler {

	private static final Logger log = LoggerFactory.getLogger(MigrationStatusHandlerImpl.class);

	private MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();

	private Vertx vertx;

	private ReleaseVersionEdge versionEdge;

	private Job job;

	private long completionCount = 0;

	private MigrationStatus status;

	public MigrationStatusHandlerImpl(Job job, Vertx vertx, MigrationType type) {
		this.vertx = vertx;
		this.job = job;
		status = job.getStatus();
	}

	@Override
	public MigrationStatusHandler commitStatus() {
		if (versionEdge != null) {
			versionEdge.setMigrationStatus(status);
		}
		job.setCompletionCount(completionCount);
		job.setStatus(status);

		Tx.getActive().getGraph().commit();
		return this;

	}

	private ObjectName startJMX() throws MalformedObjectNameException {
		String JMX_MBEAN_NAME = "com.gentics.mesh:type=NodeMigration";
		ObjectName statusMBeanName = new ObjectName(JMX_MBEAN_NAME + ",name=" + "bogus");
		try {
			mbs.registerMBean(this, statusMBeanName);
		} catch (Exception e1) {
		}
		return statusMBeanName;
	}

	private MigrationStatusHandler stopJMX(ObjectName statusMBeanName) {
		try {
			mbs.unregisterMBean(statusMBeanName);
		} catch (Exception e1) {
		}
		return this;
	}

	/**
	 * This method will:
	 * <ul>
	 * <li>Log the success</li>
	 * <li>Reply to the invoker of the migration</li>
	 * <li>Send an event to other potential consumers on the eventbus</li>
	 * <li>Update the job and potential version edge</li>
	 * </ul>
	 */
	public MigrationStatusHandler done() {
		setStatus(COMPLETED);
		log.info("Migration completed without errors.");
		commitStatus();
		JsonObject result = new JsonObject().put("type", "completed");
		vertx.eventBus().publish(MESH_MIGRATION, result);
		if (versionEdge != null) {
			versionEdge.setMigrationStatus(status);
		}
		job.setStopTimestamp();
		job.setStatus(status);
		job.setCompletionCount(completionCount);
		return this;
	}

	/**
	 * This method will:
	 * <ul>
	 * <li>Log the error</li>
	 * <li>Reply to the invoker of the migration</li>
	 * <li>Send an event to other potential consumers on the eventbus</li>
	 * </ul>
	 * 
	 * @param error
	 * @param failureMessage
	 */
	public MigrationStatusHandler error(Throwable error, String failureMessage) {
		setStatus(FAILED);
		log.error("Error handling migration", error);

		vertx.eventBus().publish(MESH_MIGRATION, new JsonObject().put("type", status.name()));
		if (versionEdge != null) {
			versionEdge.setMigrationStatus(status);
		}
		job.setStopTimestamp();
		job.setStatus(status);
		job.setError(error);
		commitStatus();
		return this;
	}

	@Override
	public void setVersionEdge(ReleaseVersionEdge versionEdge) {
		this.versionEdge = versionEdge;
	}

	@Override
	public void setCompletionCount(long completionCount) {
		this.completionCount = completionCount;
	}

	@Override
	public void setStatus(MigrationStatus status) {
		this.status = status;
	}

	@Override
	public void incCompleted() {
		completionCount++;
	}

}

@@ -2748,34 +2748,16 @@
 ors.%22);%0A
-%09%09commitStatus();%0A
 %09%09JsonOb
@@ -2869,84 +2869,8 @@
 t);%0A
-%09%09if (versionEdge != null) %7B%0A%09%09%09versionEdge.setMigrationStatus(status);%0A%09%09%7D%0A
 %09%09jo
@@ -2889,38 +2889,37 @@
 estamp();%0A%09%09
-job.se
+commi
 tStatus(
 status);%0A%09%09j
@@ -2910,57 +2910,8 @@
 tus(
-status);%0A%09%09job.setCompletionCount(completionCount
 );%0A%09
@@ -3421,130 +3421,29 @@
 ;%0A%09%09
-if (versionEdge != null) %7B%0A%09%09%09versionEdge.setMigrationStatus(status);%0A%09%09%7D%0A%09%09job.setStopTimestamp();%0A%09%09job.setStatus(status
+job.setStopTimestamp(
 );%0A%09

Remove bogus code