/**
 * The MIT License (MIT)
 * Copyright (c) 2013 OMG BPMN Model Interchange Working Group
 *
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 *
 */
package org.omg.bpmn.miwg.mvn;

import java.io.File;
import java.io.FileWriter;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;

import org.apache.maven.model.Resource;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;
import org.json.simple.JSONObject;
import org.omg.bpmn.miwg.api.AnalysisJob;
import org.omg.bpmn.miwg.facade.AnalysisFacade;
import org.omg.bpmn.miwg.facade.scan.BpmnFileScanner;
import org.omg.bpmn.miwg.facade.scan.StandardScanParameters;
import org.omg.bpmn.miwg.submission.RepoScanner;

/**
 * Goal which scans project for BPMN files and tests them for interoperability.
 */
@Mojo(name = "test", defaultPhase = LifecyclePhase.PROCESS_SOURCES)
public class ModelInterchangeMojo extends AbstractMojo {
	/**
	 * Project instance Used to add new source directory to the build.
	 */
	@Parameter(defaultValue = "${project}", required = true)
	protected MavenProject project;

	/**
	 * BPMN Application under test.
	 */
	@Parameter(defaultValue = "${project.bpmn.application}", property = "application", required = false)
	protected String application;

	/**
	 * Location to output interoperability results.
	 */
	@Parameter(defaultValue = "${project.reporting.outputDirectory}", property = "outputDir", required = true)
	protected File outputDirectory;

	/**
	 * The list of resource definitions to be included in the project jar. List
	 * of Resource objects for the current build, containing directory,
	 * includes, and excludes.
	 *
	 */
	@Parameter(defaultValue = "${project.resources}", readonly = true, required = true)
	protected List<Resource> resources;

	public void execute() throws MojoExecutionException {
		getLog().info("Running BPMN-MIWG test suite...");

		if (!outputDirectory.exists()) {
			outputDirectory.mkdirs();
		}

		try {
			Collection<String> inputFolders = new LinkedList<String>();
			if (!resources.isEmpty()) {
				for (Resource resource : resources) {
					String folderName = resource.getDirectory();
					inputFolders.add(folderName);
				}
			}

			Collection<AnalysisJob> jobs = BpmnFileScanner
					.buildListOfAnalysisJobs(new StandardScanParameters(application,
							null, inputFolders, outputDirectory.getCanonicalPath()));
			AnalysisFacade.executeAnalysisJobs(jobs,
					outputDirectory.getAbsolutePath());
		} catch (Exception e) {
			e.printStackTrace();
		}

        //Generate the submissions.json
        try (FileWriter out = new FileWriter(new File(outputDirectory, "submissions.json"))) {
            JSONObject submissions = new RepoScanner().getSubmissionsFromRepo("bpmn-miwg/bpmn-miwg-test-suite");
            submissions.writeJSONString(out);
        } catch (Exception e) {
            e.printStackTrace();
        }
	}

}

@@ -3764,33 +3764,125 @@
  %7B%0A%09
-%09%09e.printStackTrace();%0A%09%09
+            getLog().error(%22Unable to perform analysis, cause: %22%0A%09                    + e.getMessage(), e);%0A%09        
 %7D%0A%0A 
@@ -4222,41 +4222,119 @@
     
-e.printStackTrace();%0A        %7D
+getLog().error(%22Unable to generate submissions summary, cause: %22%0A                    + e.getMessage(), e);
 %0A%09%7D%0A
+%09%7D
 %0A%7D%0A

use mvn log instead of console