package modules;

import java.util.Map;
import java.util.TreeMap;

import org.skyve.domain.Bean;
import org.skyve.domain.PersistentBean;
import org.skyve.metadata.customer.Customer;
import org.skyve.metadata.model.Attribute;
import org.skyve.metadata.model.document.Document;
import org.skyve.metadata.model.document.Reference;
import org.skyve.metadata.model.document.Relation;
import org.skyve.wildcat.bind.BindUtil;
import org.skyve.wildcat.util.BeanVisitor;
import org.skyve.wildcat.util.JSONUtil;

class AuditJSONGenerator extends BeanVisitor {
	private Map<String, Object> audit = new TreeMap<>();
	private Customer customer;
	
	AuditJSONGenerator(Customer customer) {
		this.customer = customer;
	}
	
	String toJSON() throws Exception {
		return JSONUtil.marshall(customer, audit, null);
	}

	@Override
	protected boolean accept(String binding,
								Document document,
								Document owningDocument,
								Reference owningReference,
								Bean bean,
								boolean visitingInheritedDocument)
	throws Exception {
		Map<String, Object> node = new TreeMap<>();

		node.put(Bean.DOCUMENT_ID, bean.getBizId());
		
		for (Attribute attribute : document.getAttributes()) {
			if (! (attribute instanceof Relation)) {
				String name = attribute.getName();
				node.put(name, BindUtil.getSerialized(customer, bean, name));
			}
		}

		if (bean instanceof PersistentBean) {
			node.put(Bean.BIZ_KEY, ((PersistentBean) bean).getBizKey());
		}
		
		audit.put(binding, node);
		
		return true;
	}
}

@@ -496,16 +496,23 @@
 NUtil;%0A%0A
+public 
 class Au
@@ -635,16 +635,23 @@
 mer;%0A%09%0A%09
+public 
 AuditJSO
@@ -716,16 +716,23 @@
 ;%0A%09%7D%0A%09%0A%09
+public 
 String t

Make modules.AuditJSONGenerator class public for use by projects.