package com.gentics.mesh.changelog.changes;

import java.io.File;
import java.io.IOException;

import com.gentics.mesh.changelog.AbstractChange;

public class ReindexDatabase extends AbstractChange {
	@Override
	public void actualApply() {
		// Move old config files away which will no longer work with orientdb 3.0.x
		moveOldConfig("default-distributed-db-config.json");
		moveOldConfig("hazelcast.xml");
		moveOldConfig("orientdb-server-config.xml");
		try {
			getDb().initConfigurationFiles();
		} catch (IOException e) {
			throw new RuntimeException("Error while generating new configuration files", e);
		}
		getDb().reindex();
	}

	private void moveOldConfig(String confName) {
		File oldConf = new File("config", confName);
		if (oldConf.exists()) {
			log.info("Moving old OrientDB server configuration file away.");
			File newConf = new File("config", confName + ".org.bak");
			if (!oldConf.renameTo(newConf)) {
				throw new RuntimeException("Failed to move {" + oldConf + "} to {" + newConf + "}");
			}
		}
	}

	@Override
	protected boolean applyInTx() {
		return false;
	}

	@Override
	public String getDescription() {
		return "Repopulates the database indices. This is necessary when upgrading to OrientDB 3." +
			" This change is executed before all other changes to ensure that the changes" +
			" can access the database correctly.";
	}

	@Override
	public String getUuid() {
		return "DCF025827B8F44BBB025827B8FA4BB94";
	}

	@Override
	public String getName() {
		return "Invoke database reindex";
	}
}

@@ -42,29 +42,8 @@
 s;%0A%0A
-import java.io.File;%0A
 impo
@@ -172,16 +172,17 @@
 hange %7B%0A
+%0A
 %09@Overri
@@ -219,789 +219,187 @@
 %7B%0A%09%09
-// Move old config files away which will no longer work with orientdb 3.0.x%0A%09%09moveOldConfig(%22default-distributed-db-config.json%22);%0A%09%09moveOldConfig(%22hazelcast.xml%22);%0A%09%09moveOldConfig(%22orientdb-server-config.xml%22);%0A%09%09try %7B%0A%09%09%09getDb().initConfigurationFiles();%0A%09%09%7D catch (IOException e) %7B%0A%09%09%09throw new RuntimeException(%22Error while generating new configuration files%22, e);%0A%09%09%7D%0A%09%09getDb().reindex();%0A%09%7D%0A%0A%09private void moveOldConfig(String confName) %7B%0A%09%09File oldConf = new File(%22config%22, confName);%0A%09%09if (oldConf.exists()) %7B%0A%09%09%09log.info(%22Moving old OrientDB server configuration file away.%22);%0A%09%09%09File newConf = new File(%22config%22, confName + %22.org.bak%22);%0A%09%09%09if (!oldConf.renameTo(newConf)) %7B%0A%09%09%09%09throw new RuntimeException(%22Failed to move %7B%22 + oldConf + %22%7D to %7B%22 + newConf + %22%7D%22);%0A%09%09%09%7D%0A%09%09%7D
+try %7B%0A%09%09%09getDb().initConfigurationFiles();%0A%09%09%7D catch (IOException e) %7B%0A%09%09%09throw new RuntimeException(%22Error while generating new configuration files%22, e);%0A%09%09%7D%0A%09%09getDb().reindex();
 %0A%09%7D%0A

Remove config migration handling