Fixes for running generation with a facet override and other generation improvements.

- Facet can only be applied once project artifacts are discovered, otherwise
facet thinks no artifacts are valid.  In future we should update artifact
session code to update the applied facet lookup maps each time an artifact
is added to the session to account for this.
- In plugin run report, give resolved generator version instead of the plugins
configured mininmum version.
- In case of error running generation, fail generation right away, to make it
easier to identify the source of failure, instead of needing to scroll back
through logs to try to find where an error was printed.  Especially helpful
when running multi-threaded project generation.
- In case plugin cannot be loaded, possibly because minimum required version not
present, fail generation right away instead of  silently skipping it.

Change-Id: Id5983d1b089e9dd68aa515302f2805284d0b543d
diff --git a/core/org.eclipse.tigerstripe.core/src/main/java/org/eclipse/tigerstripe/runner/ProjectGenerator.java b/core/org.eclipse.tigerstripe.core/src/main/java/org/eclipse/tigerstripe/runner/ProjectGenerator.java
index b0105fd..15dc149 100644
--- a/core/org.eclipse.tigerstripe.core/src/main/java/org/eclipse/tigerstripe/runner/ProjectGenerator.java
+++ b/core/org.eclipse.tigerstripe.core/src/main/java/org/eclipse/tigerstripe/runner/ProjectGenerator.java
@@ -145,16 +145,6 @@
         LOG.info("Running generation on model: {}", project.getName());
         IProgressMonitor monitor = new RedirectMonitor(LOG, arguments.isDebug());
 
-        File facetFile = arguments.getActiveFacet();
-        if (facetFile != null && facetFile.exists()) {
-            LOG.info("Setting project facet for: {} to: {}", project.getName(), facetFile.getAbsolutePath());
-            FacetReference facet = new FacetReference(facetFile.toURI(), project);
-            project.setActiveFacet(facet, monitor);
-            runConfig.setUseCurrentFacet(true);
-        } else if (arguments.isIgnoreFacet()) {
-            runConfig.setIgnoreFacets(true);
-        }
-
         IArtifactManagerSession session = project.getArtifactManagerSession();
         session.refreshAll(monitor);
         if (LOG.isDebugEnabled()) {
@@ -166,6 +156,18 @@
                             artifact.getFullyQualifiedName()));
         }
 
+        // NOTE: Must set active facet only AFTER artifacts have been discovered from the project.
+        File facetFile = arguments.getActiveFacet();
+        if (facetFile != null && facetFile.exists()) {
+            LOG.info("Setting project facet for: {} to: {}", project.getName(), facetFile.getAbsolutePath());
+            FacetReference facet = new FacetReference(facetFile.toURI(), project);
+            facet.resolve();
+            project.setActiveFacet(facet, monitor);
+            runConfig.setUseCurrentFacet(true);
+        } else if (arguments.isIgnoreFacet()) {
+            runConfig.setIgnoreFacets(true);
+        }
+
         verifyResults(project.generate(runConfig, monitor));
         
         project.dispose();
diff --git a/core/org.eclipse.tigerstripe.core/src/main/java/org/eclipse/tigerstripe/workbench/generation/PluginRunStatus.java b/core/org.eclipse.tigerstripe.core/src/main/java/org/eclipse/tigerstripe/workbench/generation/PluginRunStatus.java
index e3d0d74..e853795 100644
--- a/core/org.eclipse.tigerstripe.core/src/main/java/org/eclipse/tigerstripe/workbench/generation/PluginRunStatus.java
+++ b/core/org.eclipse.tigerstripe.core/src/main/java/org/eclipse/tigerstripe/workbench/generation/PluginRunStatus.java
@@ -16,6 +16,7 @@
 import java.util.Iterator;
 import java.util.List;
 
+import org.apache.commons.lang.StringUtils;
 import org.eclipse.core.runtime.IStatus;
 import org.eclipse.core.runtime.MultiStatus;
 import org.eclipse.tigerstripe.workbench.TigerstripeException;
@@ -121,23 +122,31 @@
 			if (hasError && includeHTML)
 				res.append("<b>");
 			res.append("[");
-			res.append(getMessage());
 
+			res.append(projectType + ": ");
 			if (project != null) {
-				res.append(projectType + ": " + project.getModelId()
-						+ " version="
+				res.append(project.getModelId()
+						+ "-"
 						+ project.getProjectDetails().getVersion());
+			} else {
+			    res.append("<none>");
 			}
-			if (pluginConfig != null) {
-				res.append(", Plugin: "
-						+ ((PluginConfig) pluginConfig).isResolvedTo()
-						+ " version="
-						+ ((PluginConfig) pluginConfig).getVersion());
-			}
-
+            if (pluginConfig != null) {
+                res.append(", Plugin: "
+                        + ((PluginConfig) pluginConfig).getPluginId()
+                        + "-"
+                        + ((PluginConfig) pluginConfig).getResolvedVersion());
+            }
 			if (facetRef != null && facetRef.canResolve()) {
 				String facetName = facetRef.resolve().getName();
-				res.append(", Facet: " + facetName);
+				res.append(", Facet: " + facetName
+				        + " (file="
+				        + facetRef.getProjectRelativePath()
+				        + ")");
+			}
+			if (StringUtils.isNotEmpty(getMessage())) {
+			    res.append(", Notice: ");
+			    res.append(getMessage());
 			}
 
 			res.append("]");
diff --git a/core/org.eclipse.tigerstripe.core/src/main/java/org/eclipse/tigerstripe/workbench/internal/contract/predicate/FacetPredicate.java b/core/org.eclipse.tigerstripe.core/src/main/java/org/eclipse/tigerstripe/workbench/internal/contract/predicate/FacetPredicate.java
index 883b576..7f1ae3c 100644
--- a/core/org.eclipse.tigerstripe.core/src/main/java/org/eclipse/tigerstripe/workbench/internal/contract/predicate/FacetPredicate.java
+++ b/core/org.eclipse.tigerstripe.core/src/main/java/org/eclipse/tigerstripe/workbench/internal/contract/predicate/FacetPredicate.java
@@ -172,7 +172,7 @@
 
         // Here we need to bypass the IArtifactManagerSession to ensure we are
         // not going to hit an active facet
-        Collection<IAbstractArtifact> artifacts =  tsProject
+        Collection<IAbstractArtifact> artifacts = tsProject
                 .getArtifactManagerSession().getArtifactManager()
                 .getAllArtifacts(true, true, monitor);
 
diff --git a/core/org.eclipse.tigerstripe.core/src/main/java/org/eclipse/tigerstripe/workbench/internal/contract/segment/FacetReference.java b/core/org.eclipse.tigerstripe.core/src/main/java/org/eclipse/tigerstripe/workbench/internal/contract/segment/FacetReference.java
index d61f343..fc602e8 100644
--- a/core/org.eclipse.tigerstripe.core/src/main/java/org/eclipse/tigerstripe/workbench/internal/contract/segment/FacetReference.java
+++ b/core/org.eclipse.tigerstripe.core/src/main/java/org/eclipse/tigerstripe/workbench/internal/contract/segment/FacetReference.java
@@ -14,6 +14,7 @@
 import java.net.URI;
 import java.net.URISyntaxException;
 
+import org.apache.commons.io.FilenameUtils;
 //import org.eclipse.core.resources.IProject;
 //import org.eclipse.core.resources.IResource;
 //import org.eclipse.core.resources.IWorkspace;
@@ -96,7 +97,7 @@
     public FacetReference(URI facetURI, ITigerstripeModelProject tsProject) {
         this.facetURI = facetURI;
         this.project = null;
-        this.projectRelativePath = null;
+        this.projectRelativePath = tsProject.getBaseDir().toURI().relativize(facetURI).getPath();
         this.tsProject = tsProject;
     }
 
diff --git a/core/org.eclipse.tigerstripe.core/src/main/java/org/eclipse/tigerstripe/workbench/internal/core/generation/BaseM1Generator.java b/core/org.eclipse.tigerstripe.core/src/main/java/org/eclipse/tigerstripe/workbench/internal/core/generation/BaseM1Generator.java
index 891a05f..e9b4388 100644
--- a/core/org.eclipse.tigerstripe.core/src/main/java/org/eclipse/tigerstripe/workbench/internal/core/generation/BaseM1Generator.java
+++ b/core/org.eclipse.tigerstripe.core/src/main/java/org/eclipse/tigerstripe/workbench/internal/core/generation/BaseM1Generator.java
@@ -193,13 +193,10 @@
             IFacetReference origFacet = project.getActiveFacet();

 

             if (config.isUseCurrentFacet()) {

-                project.resetActiveFacet();

-                if (origFacet.needsToBeEvaluated()) {

-                    origFacet.computeFacetPredicate(monitor);

-                    reportFacetInconsistencies(origFacet, overallResult);

+                if (project.getActiveFacet().needsToBeEvaluated()) {

+                    project.getActiveFacet().computeFacetPredicate(monitor);

+                    reportFacetInconsistencies(project.getActiveFacet(), overallResult);

                 }

-                facetsToGenerate.add(origFacet);

-

             } else if (config.isUseProjectFacets() && project.getFacetReferences().length > 0) {

                 if (config.isMergeFacets()) {

                     IFacetReference mergedFacet = new MultiFacetReference(project.getFacetReferences(), project);

@@ -216,7 +213,7 @@
                 project.resetActiveFacet();

             }

 

-            if (facetsToGenerate.isEmpty()) {

+            if (facetsToGenerate.isEmpty() || config.isUseCurrentFacet()) {

                 PluginRunStatus[] subResult = internalRun(monitor, config);

                 overallResult.addAll(Arrays.asList(subResult));

                 if (config.isProcessUseCases()) {

@@ -233,7 +230,7 @@
                 }

             }

 

-            if (origFacet != null) {

+            if (origFacet != null && !config.isUseCurrentFacet()) {

                 monitor.setTaskName("Reverting to active facet (" + origFacet.resolve().getName() + ")");

                 project.setActiveFacet(origFacet, monitor);

             }

@@ -448,7 +445,7 @@
             pluginLogger.setUpForRun(ref, config);

             PluginLog.setThreadAwarePluginLogger(pluginLogger);

 

-            PluginRunStatus pluginResult = new PluginRunStatus(ref, project, project.getActiveFacet(), ref.getLabel());

+            PluginRunStatus pluginResult = new PluginRunStatus(ref, project, project.getActiveFacet());

             try {

                 monitor.worked(1);

                 monitor.setTaskName("Running: " + ref.getLabel());

@@ -456,17 +453,22 @@
                 // Bug #741. Need to resolve the ref in case the underlying body

                 // changed.

                 ref.resolve();

-                config.setMonitor(monitor);

-

-                ref.trigger(config, pluginLogger);

-

-                IPluginReport rep = ref.getReport();

-                if (rep != null) {

-                    pluginResult.setReport(rep);

-                    reports.add(rep);

+                if (ref.isResolved()) {

+                    config.setMonitor(monitor);

+    

+                    ref.trigger(config, pluginLogger);

+    

+                    IPluginReport rep = ref.getReport();

+                    if (rep != null) {

+                        pluginResult.setReport(rep);

+                        reports.add(rep);

+                    }

+                    monitor.worked(1);

+                } else {

+                    throw new TigerstripeException(String.format(

+                            "Could not resolve plugin %s-%s, check it is installed and that the version meets or exceeds minimum version configured: %s. Consider setting plugin version to '0.0.0' in tigerstripe.xml to simply use current avialable version without enforcing a minimum.",

+                            ref.getPluginId(), ref.getVersion(), ref.getVersion()));

                 }

-

-                monitor.worked(1);

             } catch (TigerstripeException e) {

                 String failureMessage = "An error was detected while triggering '" + ref.getLabel()

                         + "' plugin. Generation may be incomplete.";

@@ -481,13 +483,14 @@
                 } else {

                     pluginLogger.log(LogLevel.ERROR, failureMessage, e);

                 }

+                throw e;

             } catch (Exception e) {

                 String failureMessage = "An error was detected while triggering '" + ref.getLabel()

                         + "' plugin. Generation may be incomplete.";

                 IStatus error = new Status(IStatus.ERROR, TigerstripeCoreBundleData.CORE_STATUS_ID, failureMessage, e);

                 pluginLogger.reportStatus(error);

-

                 pluginLogger.log(LogLevel.ERROR, failureMessage, e);

+                throw new TigerstripeException(failureMessage, e);

             } finally {

                 for (IStatus status : pluginLogger.getReportedStatuses()) {

                     if (status.getSeverity() == IStatus.ERROR) {

diff --git a/core/org.eclipse.tigerstripe.core/src/main/java/org/eclipse/tigerstripe/workbench/internal/core/model/AbstractArtifact.java b/core/org.eclipse.tigerstripe.core/src/main/java/org/eclipse/tigerstripe/workbench/internal/core/model/AbstractArtifact.java
index b260b0c..c501d95 100644
--- a/core/org.eclipse.tigerstripe.core/src/main/java/org/eclipse/tigerstripe/workbench/internal/core/model/AbstractArtifact.java
+++ b/core/org.eclipse.tigerstripe.core/src/main/java/org/eclipse/tigerstripe/workbench/internal/core/model/AbstractArtifact.java
@@ -1881,25 +1881,26 @@
             return getStereotypeInstances();
         }
 
-        Collection<IStereotypeInstance> allStereos = new ArrayList<>();
-        allStereos.addAll(getStereotypeInstances());
-        Collection<IStereotypeInstance> inherited = getInheritedStereotypes();
-        for (IStereotypeInstance inh : inherited) {
+        Collection<IStereotypeInstance> myStereotypes = getStereotypeInstances();
+        Collection<IStereotypeInstance> allStereotypes = new ArrayList<>();
+        allStereotypes.addAll(myStereotypes);
+        Collection<IStereotypeInstance> inheritedStereotypes = getInheritedStereotypes();
+        for (IStereotypeInstance inh : inheritedStereotypes) {
             boolean handled = false;
-            for (IStereotypeInstance base : getStereotypeInstances()) {
+            for (IStereotypeInstance base : myStereotypes) {
                 if (base.getCharacterizingStereotype().getName().equals(inh.getName())) {
-                    allStereos.remove(base);
-                    allStereos.add(new OverridenStereotypeInstance(base,
+                    allStereotypes.remove(base);
+                    allStereotypes.add(new OverridenStereotypeInstance(base,
                             ((IInheritedStereotypeInstance) inh).getDefiningComponent()));
                     handled = true;
                 }
             }
             if (!handled) {
-                allStereos.add(inh);
+                allStereotypes.add(inh);
             }
         }
 
-        return Collections.unmodifiableCollection(allStereos);
+        return Collections.unmodifiableCollection(allStereotypes);
     }
 
     public Collection<IStereotypeInstance> getInheritedStereotypes() {
diff --git a/core/org.eclipse.tigerstripe.core/src/main/java/org/eclipse/tigerstripe/workbench/internal/core/plugin/PluginConfig.java b/core/org.eclipse.tigerstripe.core/src/main/java/org/eclipse/tigerstripe/workbench/internal/core/plugin/PluginConfig.java
index 9e7f326..49f394a 100644
--- a/core/org.eclipse.tigerstripe.core/src/main/java/org/eclipse/tigerstripe/workbench/internal/core/plugin/PluginConfig.java
+++ b/core/org.eclipse.tigerstripe.core/src/main/java/org/eclipse/tigerstripe/workbench/internal/core/plugin/PluginConfig.java
@@ -169,6 +169,13 @@
     public String getVersion() {
         return this.version;
     }
+    
+    public String getResolvedVersion() {
+        if (housing != null) {
+            return housing.getVersion();
+        }
+        return getVersion();
+    }
 
     @Override
     public String toString() {
@@ -207,14 +214,6 @@
         }
     }
 
-    public String isResolvedTo() {
-        if (this.housing != null) {
-            return this.housing.getPluginId();
-        } 
-        return "Not resolved";
-
-    }
-
     /**
      * Returns true if this plugin reference has been resolved.
      * 
diff --git a/plugins/org.eclipse.tigerstripe.workbench.base/src/java/org/eclipse/tigerstripe/workbench/internal/builder/CommonArtifactAuditor.java b/plugins/org.eclipse.tigerstripe.workbench.base/src/java/org/eclipse/tigerstripe/workbench/internal/builder/CommonArtifactAuditor.java
index 376f947..6f6a5ba 100644
--- a/plugins/org.eclipse.tigerstripe.workbench.base/src/java/org/eclipse/tigerstripe/workbench/internal/builder/CommonArtifactAuditor.java
+++ b/plugins/org.eclipse.tigerstripe.workbench.base/src/java/org/eclipse/tigerstripe/workbench/internal/builder/CommonArtifactAuditor.java
@@ -181,7 +181,7 @@
 
             if (instance instanceof IOverridenStereotypeInstance
                     && !instance.getCharacterizingStereotype().canOverride()) {
-                TigerstripeProjectAuditor.reportError("Overrride of stereotype ("
+                TigerstripeProjectAuditor.reportError("Override of stereotype ("
                         + instance.getCharacterizingStereotype().getName() + ") not allowed here ",
                         getIResource(project, artifact), STEREOTYPE_OVERRIDE_NOT_ALLOWED_VIOLATION);
             }