Merge branch 'mkoller/atfxadapter' into dev

Change-Id: Ibd25785778afbf8a5a4e3b652a0754c1d88541b7
diff --git a/src/main/java/org/eclipse/mdm/api/dflt/EntityManager.java b/src/main/java/org/eclipse/mdm/api/dflt/EntityManager.java
index 1f42019..2a6b7b0 100644
--- a/src/main/java/org/eclipse/mdm/api/dflt/EntityManager.java
+++ b/src/main/java/org/eclipse/mdm/api/dflt/EntityManager.java
@@ -23,8 +23,12 @@
 import org.eclipse.mdm.api.base.model.ContextType;
 import org.eclipse.mdm.api.base.model.Entity;
 import org.eclipse.mdm.api.base.model.StatusAttachable;
+import org.eclipse.mdm.api.base.model.Test;
+import org.eclipse.mdm.api.base.model.TestStep;
 import org.eclipse.mdm.api.base.query.DataAccessException;
 import org.eclipse.mdm.api.dflt.model.Status;
+import org.eclipse.mdm.api.dflt.model.TemplateTest;
+import org.eclipse.mdm.api.dflt.model.TemplateTestStep;
 import org.eclipse.mdm.api.dflt.model.Versionable;
 
 /**
@@ -144,4 +148,20 @@
 	}
 
 	<T extends StatusAttachable> List<T> loadAll(Class<T> entityClass, Status status, String pattern);
+
+	/**
+	 * Loads the refereced {@link TemplateTest} for a {@link Test}.
+	 * 
+	 * @param test {@link Test} referencing the desired template
+	 * @return {@link Optional} with the loaded {@link TemplateTest}
+	 */
+	Optional<TemplateTest> loadTemplate(Test test);
+
+	/**
+	 * Loads the referenced {@link TemplateTestStep} for a {@link TestStep}
+	 * 
+	 * @param testStep {@link TestStep} referencing the desired template
+	 * @return {@link Optional} with the loaded {@link TemplateTestStep}
+	 */
+	Optional<TemplateTestStep> loadTemplate(TestStep testStep);
 }
diff --git a/src/main/java/org/eclipse/mdm/api/dflt/model/EntityFactory.java b/src/main/java/org/eclipse/mdm/api/dflt/model/EntityFactory.java
index f67d546..ef133b4 100644
--- a/src/main/java/org/eclipse/mdm/api/dflt/model/EntityFactory.java
+++ b/src/main/java/org/eclipse/mdm/api/dflt/model/EntityFactory.java
@@ -798,6 +798,31 @@
 		return valueListValue;
 	}
 
+	/**
+	 * Creates a new {@link Classification} for given {@link Domain},
+	 * {@link ProjectDomain} and {@link Status}.
+	 * 
+	 * @param domain        The {@link Domain} for the {@link Classification}
+	 * @param projectDomain The {@link ProjectDomain} for the {@link Classification}
+	 * @param status        The {@link Status} for the {@link Classification}
+	 * @return The created {@link Classification} is returned.
+	 */
+	public Classification createClassification(Domain domain, ProjectDomain projectDomain, Status status) {
+		String name = String.format("ProjDomainId_%s.DomainId_%s.StatusId_%s", projectDomain.getID(), domain.getID(),
+				status.getID());
+		return createClassification(name, status, projectDomain, domain);
+	}
+
+	/**
+	 * Creates a new {@link Classification} for given {@link Domain},
+	 * {@link ProjectDomain} and {@link Status}.
+	 * 
+	 * @param name          The name for the {@link Classification}
+	 * @param status        The {@link Status} for the {@link Classification}
+	 * @param projectDomain The {@link ProjectDomain} for the {@link Classification}
+	 * @param domain        The {@link Domain} for the {@link Classification}
+	 * @return The created {@link Classification} is returned.
+	 */
 	public Classification createClassification(String name, Status status, ProjectDomain projectDomain, Domain domain) {
 		Classification classification = new Classification(createCore(Classification.class));
 
@@ -811,6 +836,12 @@
 		return classification;
 	}
 
+	/**
+	 * Creates a new {@link ProjectDomain} with given name.
+	 * 
+	 * @param name Name of the {@link ProjectDomain}
+	 * @return The created {@link ProjectDomain} is returned.
+	 */
 	public ProjectDomain createProjectDomain(String name) {
 		ProjectDomain projectDomain = new ProjectDomain(createCore(ProjectDomain.class));
 		// properties
@@ -818,6 +849,12 @@
 		return projectDomain;
 	}
 
+	/**
+	 * Creates a new {@link Domain} with given name.
+	 * 
+	 * @param name Name of the {@link Domain}
+	 * @return The created {@link Domain} is returned.
+	 */
 	public Domain createDomain(String name) {
 		Domain domain = new Domain(createCore(Domain.class));
 
@@ -863,6 +900,96 @@
 	}
 
 	/**
+	 * Creates a new {@link Test} for given {@link Pool} using given name,
+	 * {@link TemplateTest} and {@link Classification}.
+	 * 
+	 * @param name           Name of the created {@code Test}.
+	 * @param pool           The parent {@code Pool}.
+	 * @param templateTest   The template the returned {@code Test} will be derived
+	 *                       from.
+	 * @param classification The {@link Classification} for the created
+	 *                       {@link Test}.
+	 * @param withTestSteps  If true, {@link TestStep}s are automatically created
+	 *                       based on configured {@link TemplateTestStepUsage}.
+	 * @return The created {@code Test} is returned.
+	 */
+	public Test createTest(String name, Pool pool, TemplateTest templateTest, Classification classification,
+			boolean withTestSteps) {
+		// TODO
+		Test test = createTest(name, pool);
+
+		// relations
+		if (templateTest != null) {
+			getCore(test).getMutableStore().set(templateTest);
+		}
+		getCore(test).getMutableStore().set(classification);
+
+		if (withTestSteps && templateTest != null) {
+			// create default active and mandatory test steps according to the
+			// template
+			templateTest.getTemplateTestStepUsages().stream().filter(TemplateTestStepUsage.IS_IMPLICIT_CREATE)
+					.map(TemplateTestStepUsage::getTemplateTestStep).forEach(templateTestStep -> {
+						createTestStep(test, templateTestStep, classification);
+					});
+		}
+
+		return test;
+	}
+
+	/**
+	 * Creates a new {@link TestStep} for given {@link Test} using given
+	 * {@link TemplateTestStep} and {@link Classification}.
+	 * 
+	 * @param test             The parent {@code Test}.
+	 * @param templateTestStep The template the returned {@code TestStep} will be
+	 *                         derived from.
+	 * @param classification   The {@link Classification} for the created
+	 *                         {@link TestStep}.
+	 * @return The created {@code TestStep} is returned.
+	 */
+	public TestStep createTestStep(Test test, TemplateTestStep templateTestStep, Classification classification) {
+		TestStep testStep = createTestStep(test, templateTestStep);
+		getCore(testStep).getMutableStore().set(classification);
+		return testStep;
+	}
+
+	/**
+	 * Creates a new {@link TestStep} for given {@link Test} using given
+	 * {@link TemplateTestStep} and {@link Classification}.
+	 * 
+	 * @param name           Name of the created {@code TestStep}.
+	 * @param test           The parent {@code Test}.
+	 * @param classification The {@link Classification} for the created
+	 *                       {@link TestStep}.
+	 * @return The created {@code TestStep} is returned.
+	 */
+	public TestStep createTestStep(String name, Test test, Classification classification) {
+		TestStep testStep = createTestStep(name, test);
+		getCore(testStep).getMutableStore().set(classification);
+		return testStep;
+	}
+
+	/**
+	 * Creates a new {@link TestStep} for given {@link Test} using given name,
+	 * {@link TemplateTestStep} and {@link Classification}.
+	 * 
+	 * @param name             Name of the created {@code TestStep}.
+	 * @param test             The parent {@code Test}.
+	 * @param templateTestStep The template the returned {@code TestStep} will be
+	 *                         derived from.
+	 * @param classification   The {@link Classification} for the created
+	 *                         {@link TestStep}.
+	 * @return The created {@code TestStep} is returned.
+	 */
+	public TestStep createTestStep(String name, Test test, TemplateTestStep templateTestStep,
+			Classification classification) {
+		TestStep testStep = createTestStep(test, templateTestStep);
+		getCore(testStep).getMutableStore().set(classification);
+		testStep.setName(name);
+		return testStep;
+	}
+
+	/**
 	 * Creates a new {@link Test} for given {@link Pool}.
 	 *
 	 * @param name   Name of the created {@code Test}.
@@ -878,10 +1005,6 @@
 		getCore(test).getPermanentStore().set(pool);
 		getCore(pool).getChildrenStore().add(test);
 
-		if (status != null) {
-			status.assign(test);
-		}
-
 		return test;
 	}
 
diff --git a/src/main/java/org/eclipse/mdm/api/dflt/model/Status.java b/src/main/java/org/eclipse/mdm/api/dflt/model/Status.java
index 387c482..041a156 100644
--- a/src/main/java/org/eclipse/mdm/api/dflt/model/Status.java
+++ b/src/main/java/org/eclipse/mdm/api/dflt/model/Status.java
@@ -23,6 +23,7 @@
 import org.eclipse.mdm.api.base.model.StatusAttachable;
 import org.eclipse.mdm.api.base.model.Test;
 import org.eclipse.mdm.api.base.model.TestStep;
+import org.eclipse.mdm.api.base.query.DataAccessException;
 
 /**
  * Implementation of the status entity type. A status may be attached to
@@ -68,7 +69,12 @@
 	 * @param statusAttachable This status will be assigned to it.
 	 */
 	public <T extends StatusAttachable> void assign(T statusAttachable) {
-		getCore(statusAttachable).getMutableStore().set(this);
+		Classification classification = getCore(statusAttachable).getMutableStore().get(Classification.class);
+		if (classification == null) {
+			throw new DataAccessException("Mandatory element classification not found!");
+		}
+
+		getCore(classification).getMutableStore().set(this);
 	}
 
 	/**