package org.taverna.component.validator;

import static java.lang.System.out;

import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;

public abstract class AssertionReporter {
	public boolean reportAssertions(List<Assertion> assertions) {
		int sat = 0;
		for (Assertion a : assertions)
			if (a.satisfied)
				sat++;
		return sat == assertions.size();
	}

	public static class StdoutReporter extends AssertionReporter {
		@Override
		public boolean reportAssertions(List<Assertion> assertions) {
			boolean satisfied = super.reportAssertions(assertions);
			int sat = 0;
			for (Assertion a : assertions)
				if (a.satisfied)
					sat++;
			if (satisfied)
				out.println("SATISFIED (" + sat + "/" + assertions.size() + ")");
			else
				out.println("NOT SATISFIED (" + sat + "/" + assertions.size()
						+ ")");
			out.println("");
			for (Assertion a : assertions)
				if (!a.satisfied)
					out.println("[N] " + a.text);
			for (Assertion a : assertions)
				if (a.satisfied && a.warning)
					out.println("[w] " + a.text);
			for (Assertion a : assertions)
				if (a.satisfied && !a.warning)
					out.println("[Y] " + a.text);
			return satisfied;
		}
	}

	public static class JSONReporter extends AssertionReporter {
		@Override
		public boolean reportAssertions(List<Assertion> assertions) {
			boolean sat = super.reportAssertions(assertions);
			JSONArray ary = new JSONArray();
			for (Assertion a : assertions)
				ary.put(new JSONObject().put(
						"type",
						a.satisfied ? a.warning ? "warning" : "satisifed"
								: "failed").put("message", a.text));
			out.println(new JSONObject().put("allSatisfied", sat).put("assertions", ary));
			return sat;
		}
	}
}
@@ -1407,16 +1407,44 @@
 rray();%0A
+%09%09%09int f = 0, w = 0, s = 0;%0A
 %09%09%09for (
@@ -1465,24 +1465,49 @@
 assertions)%0A
+%09%09%09%09if (!a.satisfied) %7B%0A%09
 %09%09%09%09ary.put(
@@ -1531,29 +1531,117 @@
 put(
-%0A%09%09%09%09%09%09%22type%22,%0A%09%09%09%09%09%09
+%22type%22, %22failed%22).put(%0A%09%09%09%09%09%09%09%22message%22, a.text));%0A%09%09%09%09%09f++;%0A%09%09%09%09%7D%0A%09%09%09for (Assertion a : assertions)%0A%09%09%09%09if (
 a.sa
@@ -1648,17 +1648,18 @@
 tisfied 
-?
+&&
  a.warni
@@ -1664,50 +1664,235 @@
 ning
- ? %22warning%22 : %22satisifed%22%0A%09%09%09%09%09%09%09%09: %22fail
+) %7B%0A%09%09%09%09%09ary.put(new JSONObject().put(%22type%22, %22warning%22).put(%0A%09%09%09%09%09%09%09%22message%22, a.text));%0A%09%09%09%09%09w++;%0A%09%09%09%09%7D%0A%09%09%09for (Assertion a : assertions)%0A%09%09%09%09if (a.satisfied && !a.warning) %7B%0A%09%09%09%09%09ary.put(new JSONObject().put(%22type%22, %22satisif
 ed%22)
@@ -1896,16 +1896,24 @@
 d%22).put(
+%0A%09%09%09%09%09%09%09
 %22message
@@ -1925,16 +1925,32 @@
 text));%0A
+%09%09%09%09%09s++;%0A%09%09%09%09%7D%0A
 %09%09%09out.p
@@ -1997,16 +1997,22 @@
 d%22, sat)
+%0A%09%09%09%09%09
 .put(%22as
@@ -2026,16 +2026,127 @@
 s%22, ary)
+.put(%22numSatisfied%22, s)%0A%09%09%09%09%09.put(%22numWarning%22, w).put(%22numFailed%22, f)%0A%09%09%09%09%09.put(%22numTotal%22, assertions.size())
 );%0A%09%09%09re

Improve the reporting.