/**
 * This file is part of the Harmony package.
 *
 * (c) Mickael Gaillard <mickael.gaillard@tactfactory.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */
package com.tactfactory.mda.orm;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import com.tactfactory.mda.plateforme.BaseAdapter;
import com.tactfactory.mda.template.TagConstant;

/** Entity class metadata */
public class ClassMetadata {
	/** Used for join tables (ManyToMany relations)*/
	public boolean isAssociationClass = false;
	
	/** Namespace of entity class */
	public String space = "";
	
	/** Name of entity class */
	public String name = "";
	
	/** List of fields of entity class*/
	public HashMap<String, FieldMetadata> fields = new HashMap<String, FieldMetadata>();

	/** List of ids of entity class*/
	public HashMap<String, FieldMetadata> ids = new HashMap<String, FieldMetadata>();
	
	/** List of relations of entity class*/
	public HashMap<String, FieldMetadata> relations = new HashMap<String, FieldMetadata>();
		
	/** Class inherited by the entity class or null if none*/
	public String exts = null;
	
	/** Implemented class list of the entity class */
	public ArrayList<String> impls = new ArrayList<String>();
	
	/** Implemented class list of the entity class */
	public ArrayList<MethodMetadata> methods = new ArrayList<MethodMetadata>();

	/** Imports of the class */
	public ArrayList<String> imports = new ArrayList<String>();
	
	
	/**
	 * Transform the class to a map of strings and maps (for each field) given an adapter
	 * @param adapter The adapter used to customize the fields
	 * @return the map
	 */
	public Map<String, Object> toMap(BaseAdapter adapter){
		HashMap<String, Object> model = new HashMap<String, Object>();
		
		model.put(TagConstant.SPACE,	this.space);
		model.put(TagConstant.NAME,		this.name);
		model.put(TagConstant.LOCAL_NAMESPACE, adapter.getNameSpaceEntity(this, adapter.getController()));
		model.put("isAssociationClass","false");
		if(isAssociationClass)
			model.put("isAssociationClass","true");
		//model.put(TagConstant.ALIAS, SqliteAdapter.generateColumnName(this));
		model.put(TagConstant.FIELDS,	this.toFieldArray(this.fields.values(), adapter));
		model.put(TagConstant.IDS,		this.toFieldArray(this.ids.values(), adapter));
		model.put(TagConstant.RELATIONS,this.toFieldArray(this.relations.values(), adapter));
		
		return model;
	}
	
	private ArrayList<Map<String,Object>> toFieldArray(Collection<FieldMetadata> c, BaseAdapter adapter){
		ArrayList<Map<String,Object>> result = new ArrayList<Map<String,Object>>();
		Map<String, Object> subField = null;
		
		for (FieldMetadata field : c) {
			field.customize(adapter);
			
			subField = new HashMap<String, Object>();
			subField = field.toMap();
			
			result.add(subField);
		}
		
		return result;
	}
}

@@ -355,16 +355,48 @@
 ashMap;%0A
+import java.util.LinkedHashMap;%0A
 import j
@@ -824,32 +824,38 @@
 class*/%0A%09public 
+Linked
 HashMap%3CString, 
@@ -874,32 +874,38 @@
 a%3E fields = new 
+Linked
 HashMap%3CString, 
@@ -958,32 +958,38 @@
 class*/%0A%09public 
+Linked
 HashMap%3CString, 
@@ -1005,32 +1005,38 @@
 data%3E ids = new 
+Linked
 HashMap%3CString, 
@@ -1104,16 +1104,22 @@
 %09public 
+Linked
 HashMap%3C
@@ -1153,24 +1153,30 @@
 tions = new 
+Linked
 HashMap%3CStri
@@ -2117,119 +2117,8 @@
 ));%0A
-%09%09model.put(%22isAssociationClass%22,%22false%22);%0A%09%09if(isAssociationClass)%0A%09%09%09model.put(%22isAssociationClass%22,%22true%22);%0A
 %09%09//
@@ -2436,16 +2436,127 @@
 pter));%0A
+%09%09model.put(%22isAssociationClass%22,%22false%22);%0A%09%09if(isAssociationClass)%0A%09%09%09model.put(%22isAssociationClass%22,%22true%22);%0A
 %09%09%0A%09%09ret

Fix #1233