/**
 * 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.Harmony;
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, toFieldArray(this.fields.values(), adapter));
		model.put(TagConstant.IDS, toFieldArray(this.ids.values(), adapter));
		model.put(TagConstant.RELATIONS, toFieldArray(this.relations.values(), adapter));
		
		return model;
	}
	
	private ArrayList<Map<String,Object>> toFieldArray(Collection<FieldMetadata> c, BaseAdapter adapter){
		ArrayList<Map<String,Object>> SubFields = new ArrayList<Map<String,Object>>();
		for (FieldMetadata field : c) {
			Map<String, Object> subField = new HashMap<String, Object>();
			field.customize(adapter);
			subField = field.toMap();
			SubFields.add(subField);
		}
		return SubFields;
	}
}

@@ -382,44 +382,8 @@
 p;%0A%0A
-import com.tactfactory.mda.Harmony;%0A
 impo
@@ -1854,16 +1854,19 @@
 ect%3E();%0A
+%09%09%0A
 %09%09model.
@@ -1887,17 +1887,17 @@
 t.SPACE,
- 
+%09
 this.spa
@@ -1930,17 +1930,18 @@
 nt.NAME,
- 
+%09%09
 this.nam
@@ -2261,17 +2261,22 @@
 .FIELDS,
- 
+%09this.
 toFieldA
@@ -2341,17 +2341,23 @@
 ant.IDS,
- 
+%09%09this.
 toFieldA
@@ -2425,17 +2425,21 @@
 LATIONS,
- 
+this.
 toFieldA
@@ -2638,25 +2638,22 @@
 bject%3E%3E 
-SubFields
+result
  = new A
@@ -2684,16 +2684,58 @@
 ct%3E%3E();%0A
+%09%09Map%3CString, Object%3E subField = null;%0A%09%09%0A
 %09%09for (F
@@ -2763,36 +2763,49 @@
 c) %7B%0A%09%09%09
-Map%3CString, Object%3E 
+field.customize(adapter);%0A%09%09%09%0A%09%09%09
 subField
@@ -2842,37 +2842,8 @@
 ();%0A
-%09%09%09field.customize(adapter);%0A
 %09%09%09s
@@ -2870,25 +2870,26 @@
 p();%0A%09%09%09
-SubFields
+%0A%09%09%09result
 .add(sub
@@ -2900,16 +2900,19 @@
 d);%0A%09%09%7D%0A
+%09%09%0A
 %09%09return
@@ -2916,17 +2916,14 @@
 urn 
-SubFields
+result
 ;%0A%09%7D

Clean code