package hu.elte.txtuml.export.cpp.wizardz;

import java.io.File;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.emf.common.util.URI;
import org.eclipse.uml2.uml.Model;
import org.eclipse.uml2.uml.UMLPackage;

import hu.elte.txtuml.api.deployment.Configuration;
import hu.elte.txtuml.export.ExportUtils;
import hu.elte.txtuml.export.Uml2Utils;
import hu.elte.txtuml.export.cpp.Shared;
import hu.elte.txtuml.export.cpp.Uml2ToCppExporter;
import hu.elte.txtuml.export.cpp.thread.ThreadDescriptionExporter;
import hu.elte.txtuml.export.uml2.UML2.ExportMode;
import hu.elte.txtuml.utils.eclipse.ClassLoaderProvider;
import hu.elte.txtuml.utils.eclipse.Dialogs;

class TxtUMLToCppGovernor {

	private static final String GeneratedCPPFolderName = "cpp-gen";
	private static final String UmlFilesFolderName = "model";

	private final boolean testing;

	TxtUMLToCppGovernor(boolean testing) {
		this.testing = testing;
	}

	@SuppressWarnings("unchecked")
	void uml2ToCpp(String txtUMLProject, String txtUMLModel, String threadManagmentDescription,
			boolean addRuntimeOption) throws Exception {
		String projectFolder = ResourcesPlugin.getWorkspace().getRoot().getProject(txtUMLProject).getLocation().toFile()
				.getAbsolutePath();
		String umlFilesFolder = txtUMLProject + File.separator + GeneratedCPPFolderName + File.separator + txtUMLModel
				+ File.separator + UmlFilesFolderName;
		String umlFileLocation = umlFilesFolder + File.separator + txtUMLModel + ".uml";

		try {
			ExportUtils.exportTxtUMLModelToUML2(txtUMLProject, txtUMLModel, umlFilesFolder,
					ExportMode.ExportActionCode);
		} catch (Exception e) {
			if (!testing) {
				Dialogs.errorMsgb("txtUML export Error", e.getClass() + ":" + System.lineSeparator() + e.getMessage(),
						e);
			}
			throw e;
		}

		Model model = Uml2Utils.loadModel(URI.createPlatformResourceURI(umlFileLocation, false));

		URLClassLoader loader = ClassLoaderProvider.getClassLoaderForProject(txtUMLProject,
				ThreadDescriptionExporter.class.getClassLoader());
		Class<? extends Configuration> txtUMLThreadDescription;
		try {
			txtUMLThreadDescription = (Class<? extends Configuration>) loader.loadClass(threadManagmentDescription);
		} catch (ClassNotFoundException e) {
			if (!testing) {
				Dialogs.errorMsgb("Description Class Error",
						e.getClass() + ":" + System.lineSeparator() + e.getMessage(), e);
			}
			throw e;
		}

		List<org.eclipse.uml2.uml.Class> classList = new ArrayList<org.eclipse.uml2.uml.Class>();
		Shared.getTypedElements(classList, model.getOwnedElements(), UMLPackage.Literals.CLASS);
		Set<String> allClass = new HashSet<String>();
		for (org.eclipse.uml2.uml.Class cls : classList) {
			allClass.add(cls.getName());
		}

		ThreadDescriptionExporter exporter = new ThreadDescriptionExporter(allClass);
		exporter.exportDescription(txtUMLThreadDescription);

		if (!exporter.warningListIsEmpty()) {
			StringBuilder warnings = new StringBuilder("");
			for (String warning : exporter.getWarnings()) {
				warnings.append(warning + "\n");
			}
			warnings.append("\nWould you like to continue the generation?\n ");
			if (testing || !Dialogs.WarningConfirm("Description export reported warnings", warnings.toString())) {
				throw new Exception("Export was cancelled by user because of warnings");
			}
		}

		Uml2ToCppExporter cppExporter = new Uml2ToCppExporter(model, exporter.getConfigMap(), addRuntimeOption);
		try {
			cppExporter.buildCppCode(
					projectFolder + File.separator + GeneratedCPPFolderName + File.separator + txtUMLModel);
		} catch (Exception e) {
			if (!testing) {
				Dialogs.errorMsgb("Compilation failed", e.getClass() + ":" + System.lineSeparator() + e.getMessage(),
						e);
			}
			throw e;
		}
	}

}

@@ -245,48 +245,8 @@
 in;%0A
-import org.eclipse.emf.common.util.URI;%0A
 impo
@@ -373,90 +373,8 @@
 on;%0A
-import hu.elte.txtuml.export.ExportUtils;%0Aimport hu.elte.txtuml.export.Uml2Utils;%0A
 impo
@@ -563,16 +563,72 @@
 rt.uml2.
+TxtUMLToUML2;%0Aimport hu.elte.txtuml.export.uml2.TxtUMLTo
 UML2.Exp
@@ -1468,138 +1468,67 @@
 me;%0A
+%0A
 %09%09
-String umlFileLocation = umlFilesFolder + File.separator + txtUMLModel + %22.uml%22;%0A%0A%09%09try %7B%0A%09%09%09ExportUtils.exportTxtUMLModelToUML2
+Model model;%0A%09%09try %7B%0A%09%09%09model = TxtUMLToUML2.exportModel
 (txt
@@ -1567,22 +1567,17 @@
 sFolder,
-%0A%09%09%09%09%09
+ 
 ExportMo
@@ -1624,36 +1624,16 @@
 on e) %7B%0A
-%09%09%09if (!testing) %7B%0A%09
 %09%09%09Dialo
@@ -1729,32 +1729,21 @@
 ssage(),
-%0A%09%09%09%09%09%09
+ 
 e);%0A
-%09%09%09%7D%0A
 %09%09%09throw
@@ -1755,101 +1755,8 @@
 %09%7D%0A%0A
-%09%09Model model = Uml2Utils.loadModel(URI.createPlatformResourceURI(umlFileLocation, false));%0A%0A
 %09%09UR

Fix merge problems