/**
 * 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.template;

import java.util.HashMap;
import java.util.Map;

import com.tactfactory.mda.ConsoleUtils;
import com.tactfactory.mda.orm.ClassMetadata;
import com.tactfactory.mda.plateforme.BaseAdapter;
import com.tactfactory.mda.utils.PackageUtils;

public class TestGenerator extends BaseGenerator {
	protected String localNameSpace;
	protected Map<String, Object> entities;
	
	public TestGenerator(BaseAdapter adapter) throws Exception {
		super(adapter);
	}
	
	public void generateAll() {
		ConsoleUtils.display(">> Generate Repository test...");
		
		this.initTestAndroid();
		
		for(ClassMetadata cm : this.appMetas.entities.values()){
			this.datamodel = (HashMap<String, Object>) cm.toMap(this.adapter);
			this.localNameSpace = this.adapter.getNameSpace(cm, this.adapter.getTest());
			
			this.generate(null);
		}
	}
	
	/**  
	 * Generate DataBase Test  
	 */ 
	private void generate(String entity) {
		// Info
				ConsoleUtils.display(">>> Generate Repository test for " +  this.datamodel.get(TagConstant.NAME));
		
		try {			
			this.makeSourceTest(
					"TemplateTestDBBase.java", 
					"%sTestDBBase.java",
					true);
			
			this.makeSourceTest(
					"TemplateTestDB.java", 
					"%sTestDB.java",
					false);

		} catch (Exception e) {
			ConsoleUtils.displayError(e.getMessage());
		}
	}
	
	/** 
	 * Make Java Source Code
	 * 
	 * @param template Template path file. <br/>For list activity is "TemplateListActivity.java"
	 * @param filename
	 */
	private void makeSourceTest(String template, String filename, boolean override) {
		String fullFilePath = String.format("%s%s/%s",
						this.adapter.getTestPath(),
						PackageUtils.extractPath(String.format(
								"%s/%s", this.adapter.getSource(), this.localNameSpace)).toLowerCase(),
						String.format(filename, this.datamodel.get(TagConstant.NAME)));
		
		String fullTemplatePath = String.format("%s%s",
					this.adapter.getTemplateTestsPath(),
					template);
		
		super.makeSource(fullTemplatePath, fullFilePath, override);
	}
	
	/**
	 * Initialize Test Android Project folders and files
	 * @return success of Test Android project initialization
	 */
	public boolean initTestAndroid() {
		ConsoleUtils.display("> Init Test Project Google Android");

		boolean result = false;

		try {
			if(new TestProjectGenerator(this.adapter).makeProject()){
				ConsoleUtils.displayDebug("Init Test Android Project Success!");
				
				result = true;
			} else {
				ConsoleUtils.displayError("Init Test Android Project Fail!");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
}
@@ -903,16 +903,48 @@
 ues())%7B%0A
+%09%09%09if (!cm.fields.isEmpty()) %7B%0A%09
 %09%09%09this.
@@ -1005,16 +1005,17 @@
 apter);%0A
+%09
 %09%09%09this.
@@ -1085,25 +1085,27 @@
 Test());%0A%09%09%09
-%0A
+%09%0A%09
 %09%09%09this.gene
@@ -1116,16 +1116,21 @@
 (null);%0A
+%09%09%09%7D%0A
 %09%09%7D%0A%09%7D%0A%09

Fix #1261