package org.openlca.jsonld.output;

import java.util.Collection;
import java.util.Date;
import java.util.List;

import org.openlca.core.model.RootEntity;
import org.openlca.jsonld.Dates;
import org.openlca.jsonld.Enums;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;

class Out {

	static final boolean WRITE_NULL_VALUES = false;
	static final boolean WRITE_EMPTY_COLLECTIONS = false;

	private Out() {
	}
	static void put(JsonObject json, String property, RootEntity value,
			ExportConfig conf) {
		put(json, property, value, conf, false);
	}

	static void put(JsonObject json, String property, RootEntity value,
			ExportConfig conf, boolean forceExport) {
		if (!isValidInput(value))
			return;
		JsonObject ref = References.create(value, conf, forceExport);
		json.add(property, ref);
	}
	static void put(JsonObject json, String property,
			List<? extends RootEntity> values, ExportConfig conf) {
		put(json, property, values, conf, false);
	}

	static void put(JsonObject json, String property,
			List<? extends RootEntity> values, ExportConfig conf, boolean forceExport) {
		if (!isValidInput(values))
			return;
		JsonArray array = new JsonArray();
		for (RootEntity value : values)
			array.add(References.create(value, conf, forceExport));
		json.add(property, array);
	}

	static <T extends Enum<T>> void put(JsonObject json, String property,
			Enum<T> value) {
		if (!isValidInput(value))
			return;
		json.addProperty(property, Enums.getLabel(value));
	}

	static void put(JsonObject json, String property, JsonElement value) {
		if (!isValidInput(value))
			return;
		json.add(property, value);
	}

	static void put(JsonObject json, String property, String value) {
		if (!isValidInput(value))
			return;
		json.addProperty(property, value);
	}

	static void put(JsonObject json, String property, Number value) {
		if (!isValidInput(value))
			return;
		json.addProperty(property, value);
	}

	static void put(JsonObject json, String property, Boolean value) {
		if (!isValidInput(value))
			return;
		json.addProperty(property, value);
	}

	static void put(JsonObject json, String property, Character value) {
		if (!isValidInput(value))
			return;
		json.addProperty(property, value);
	}

	static void put(JsonObject json, String property, Date value) {
		if (!isValidInput(value))
			return;
		json.addProperty(property, Dates.toString(value));
	}

	private static boolean isValidInput(Object value) {
		if (Collection.class.isInstance(value))
			return WRITE_EMPTY_COLLECTIONS
					|| !Collection.class.cast(value).isEmpty();
		if (JsonArray.class.isInstance(value))
			return WRITE_EMPTY_COLLECTIONS
					|| JsonArray.class.cast(value).size() > 0;
		return WRITE_NULL_VALUES || value != null;
	}

}

@@ -454,24 +454,25 @@
  Out() %7B%0A%09%7D%0A
+%0A
 %09static void
@@ -846,16 +846,17 @@
 ef);%0A%09%7D%0A
+%0A
 %09static 
@@ -1104,33 +1104,36 @@
 portConfig conf,
- 
+%0A%09%09%09
 boolean forceExp
@@ -2727,16 +2727,107 @@
 () %3E 0;%0A
+%09%09if (value instanceof Boolean)%0A%09%09%09return WRITE_NULL_VALUES %7C%7C Boolean.TRUE.equals(value);%0A
 %09%09return

Treating false boolean values as "null"