// Generated by Haxe 4.3.3
package transform;

import haxe.root.*;
import transform.MLDToDiagram_addBinaryDFAssoc_79__Fun;
import transform.MLDToDiagram_relationsToAssociations_44__Fun;
import transform.MLDToDiagram_relationsToEntities_23__Fun;
import transform.MLDToDiagram;

@SuppressWarnings(value={"rawtypes", "unchecked"})
public class MLDToDiagram extends haxe.lang.HxObject
{
	public MLDToDiagram(haxe.lang.EmptyObject empty)
	{
	}
	
	
	public MLDToDiagram()
	{
		//line 7 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/transform/MLDToDiagram.hx"
		MLDToDiagram.__hx_ctor_transform_MLDToDiagram(this);
	}
	
	
	protected static void __hx_ctor_transform_MLDToDiagram(MLDToDiagram __hx_this)
	{
	}
	
	
	public static model.Diagram transform(model.MLD mld)
	{
		//line 9 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/transform/MLDToDiagram.hx"
		model.Diagram diagram = new model.Diagram();
		//line 10 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/transform/MLDToDiagram.hx"
		MLDToDiagram.relationsToEntities(mld, diagram);
		//line 11 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/transform/MLDToDiagram.hx"
		MLDToDiagram.relationsToAssociations(mld, diagram);
		//line 12 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/transform/MLDToDiagram.hx"
		return diagram;
	}
	
	
	public static void relationsToEntities(model.MLD mld, model.Diagram diagram)
	{
		//line 16 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/transform/MLDToDiagram.hx"
		java.lang.Object r = new haxe.ds._StringMap.StringMapValueIterator<model.Relation>(((haxe.ds.StringMap<model.Relation>) (mld.relations) ));
		//line 16 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/transform/MLDToDiagram.hx"
		while (haxe.lang.Runtime.toBool(((java.lang.Boolean) (haxe.lang.Runtime.callField(r, "hasNext", null)) )))
		{
			//line 16 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/transform/MLDToDiagram.hx"
			model.Relation r1 = ((model.Relation) (haxe.lang.Runtime.callField(r, "next", null)) );
			//line 17 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/transform/MLDToDiagram.hx"
			if ((  ! (MLDToDiagram.allForeignKeysBelongToKey(r1))  || ( ((model.Relation) (haxe.lang.Runtime.getField(r1.foreignKeys.__get(0), "relation", true)) ).key.length == r1.foreignKeys.length ) )) 
			{
				//line 19 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/transform/MLDToDiagram.hx"
				model.Set e = new model.Set(haxe.lang.Runtime.toString(r1.name), ((model.Set) (null) ), ((java.lang.Object) (null) ), ((java.lang.Object) (null) ), ((java.lang.Object) (null) ));
				//line 20 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/transform/MLDToDiagram.hx"
				diagram.sets.set(e.name, e);
				//line 21 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/transform/MLDToDiagram.hx"
				e.fields = new haxe.root.Array<model.Field>();
				//line 22 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/transform/MLDToDiagram.hx"
				{
					//line 22 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/transform/MLDToDiagram.hx"
					int _g = 0;
					//line 22 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/transform/MLDToDiagram.hx"
					haxe.root.Array<model.Field> _g1 = r1.fields;
					//line 22 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/transform/MLDToDiagram.hx"
					while (( _g < _g1.length ))
					{
						//line 22 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/transform/MLDToDiagram.hx"
						model.Field f = _g1.__get(_g);
						//line 22 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/transform/MLDToDiagram.hx"
						 ++ _g;
						//line 23 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/transform/MLDToDiagram.hx"
						java.lang.Object fk = ((java.lang.Object) (haxe.root.Lambda.find(((java.lang.Object) (r1.foreignKeys) ), ((haxe.lang.Function) (new MLDToDiagram_relationsToEntities_23__Fun(f)) ))) );
						//line 24 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/transform/MLDToDiagram.hx"
						if (( fk == null )) 
						{
							//line 25 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/transform/MLDToDiagram.hx"
							e.fields.push(f);
							//line 26 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/transform/MLDToDiagram.hx"
							if (( r1.key.indexOf(f.name, null) != -1 )) 
							{
								//line 27 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/transform/MLDToDiagram.hx"
								e.id.push(f.name);
							}
							
						}
						
					}
					
				}
				
			}
			
		}
		
	}
	
	
	public static void relationsToAssociations(model.MLD mld, model.Diagram diagram)
	{
		//line 36 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/transform/MLDToDiagram.hx"
		haxe.root.Array<model.Relation> relations = new haxe.root.Array<model.Relation>();
		//line 37 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/transform/MLDToDiagram.hx"
		{
			//line 37 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/transform/MLDToDiagram.hx"
			java.lang.Object r = new haxe.ds._StringMap.StringMapValueIterator<model.Relation>(((haxe.ds.StringMap<model.Relation>) (mld.relations) ));
			//line 37 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/transform/MLDToDiagram.hx"
			while (haxe.lang.Runtime.toBool(((java.lang.Boolean) (haxe.lang.Runtime.callField(r, "hasNext", null)) )))
			{
				//line 37 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/transform/MLDToDiagram.hx"
				model.Relation r1 = ((model.Relation) (haxe.lang.Runtime.callField(r, "next", null)) );
				//line 38 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/transform/MLDToDiagram.hx"
				relations.push(r1);
			}
			
		}
		
		//line 40 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/transform/MLDToDiagram.hx"
		while (( relations.length != 0 ))
		{
			//line 41 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/transform/MLDToDiagram.hx"
			model.Relation r2 = relations.pop();
			//line 42 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/transform/MLDToDiagram.hx"
			if (MLDToDiagram.allForeignKeysBelongToKey(r2)) 
			{
				//line 43 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/transform/MLDToDiagram.hx"
				if (( ((model.Relation) (haxe.lang.Runtime.getField(r2.foreignKeys.__get(0), "relation", true)) ).key.length != r2.foreignKeys.length )) 
				{
					//line 44 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/transform/MLDToDiagram.hx"
					if (haxe.lang.Runtime.toBool(((java.lang.Boolean) (haxe.root.Lambda.exists(((java.lang.Object) (r2.foreignKeys) ), ((haxe.lang.Function) (new MLDToDiagram_relationsToAssociations_44__Fun(diagram)) ))) ))) 
					{
						//line 45 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/transform/MLDToDiagram.hx"
						relations.push(r2);
					}
					else
					{
						//line 47 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/transform/MLDToDiagram.hx"
						MLDToDiagram.addNNAssociation(diagram, r2);
					}
					
				}
				else
				{
					//line 50 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/transform/MLDToDiagram.hx"
					model.Set e = diagram.sets.get(r2.name);
					//line 51 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/transform/MLDToDiagram.hx"
					model.Set superset = diagram.sets.get(((model.Relation) (haxe.lang.Runtime.getField(r2.foreignKeys.__get(0), "relation", true)) ).name);
					//line 52 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/transform/MLDToDiagram.hx"
					superset.subsets.push(e);
					//line 53 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/transform/MLDToDiagram.hx"
					e.superset = superset;
				}
				
			}
			else
			{
				//line 56 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/transform/MLDToDiagram.hx"
				int _g = 0;
				//line 56 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/transform/MLDToDiagram.hx"
				haxe.root.Array<java.lang.Object> _g1 = r2.mergeForeignKeys();
				//line 56 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/transform/MLDToDiagram.hx"
				while (( _g < _g1.length ))
				{
					//line 56 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/transform/MLDToDiagram.hx"
					java.lang.Object mfk = _g1.__get(_g);
					//line 56 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/transform/MLDToDiagram.hx"
					 ++ _g;
					//line 57 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/transform/MLDToDiagram.hx"
					model.Set target = diagram.getObj(((model.Relation) (haxe.lang.Runtime.getField(mfk, "relation", true)) ).name);
					//line 58 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/transform/MLDToDiagram.hx"
					if (( target != null )) 
					{
						//line 59 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/transform/MLDToDiagram.hx"
						MLDToDiagram.addBinaryDFAssoc(diagram, r2, mfk, target);
					}
					else
					{
						//line 61 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/transform/MLDToDiagram.hx"
						relations.push(r2);
					}
					
				}
				
			}
			
		}
		
	}
	
	
	public static void addBinaryDFAssoc(model.Diagram diagram, model.Relation r, java.lang.Object mfk, model.Set target)
	{
		//line 69 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/transform/MLDToDiagram.hx"
		java.lang.String rName = ( r.name + ((model.Relation) (haxe.lang.Runtime.getField(mfk, "relation", true)) ).name );
		//line 70 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/transform/MLDToDiagram.hx"
		while (( diagram.associations.get(rName) != null ))
		{
			//line 70 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/transform/MLDToDiagram.hx"
			rName += "1";
		}
		
		//line 71 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/transform/MLDToDiagram.hx"
		model.Association a = new model.Association(haxe.lang.Runtime.toString(rName));
		//line 72 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/transform/MLDToDiagram.hx"
		diagram.associations.set(a.name, a);
		//line 73 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/transform/MLDToDiagram.hx"
		boolean isCompound = ( r.key.indexOf(((haxe.root.Array<java.lang.String>) (haxe.lang.Runtime.getField(mfk, "names", true)) ).__get(0), null) != -1 );
		//line 74 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/transform/MLDToDiagram.hx"
		model.Set source1 = diagram.sets.get(r.name);
		//line 75 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/transform/MLDToDiagram.hx"
		a.links.push(new model.Link(((model.Set) (source1) ), haxe.lang.Runtime.toString(""), haxe.lang.Runtime.toString("0"), haxe.lang.Runtime.toString("1"), haxe.lang.Runtime.toBool(((java.lang.Boolean) (isCompound) )), haxe.lang.Runtime.toBool(((java.lang.Boolean) (false) )), haxe.lang.Runtime.toBool(((java.lang.Boolean) (false) )), ((int) (model.Field.ACCESS_UNSET) )));
		//line 76 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/transform/MLDToDiagram.hx"
		if (isCompound) 
		{
			//line 77 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/transform/MLDToDiagram.hx"
			source1.compound = ((model.Set) (target) );
		}
		
		//line 79 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/transform/MLDToDiagram.hx"
		if (( (  ! (isCompound)  &&  ! (haxe.lang.Runtime.valEq(((model.Relation) (haxe.lang.Runtime.getField(mfk, "relation", true)) ).name, r.name))  ) && haxe.lang.Runtime.toBool(((java.lang.Boolean) (haxe.root.Lambda.exists(((java.lang.Object) (((model.Relation) (haxe.lang.Runtime.getField(mfk, "relation", true)) ).foreignKeys) ), ((haxe.lang.Function) (new MLDToDiagram_addBinaryDFAssoc_79__Fun(r)) ))) )) )) 
		{
			//line 81 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/transform/MLDToDiagram.hx"
			a.links.push(new model.Link(((model.Set) (target) ), haxe.lang.Runtime.toString(""), haxe.lang.Runtime.toString("0"), haxe.lang.Runtime.toString("1"), haxe.lang.Runtime.toBool(((java.lang.Boolean) (false) )), haxe.lang.Runtime.toBool(((java.lang.Boolean) (false) )), haxe.lang.Runtime.toBool(((java.lang.Boolean) (false) )), ((int) (model.Field.ACCESS_UNSET) )));
		}
		else
		{
			//line 83 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/transform/MLDToDiagram.hx"
			a.links.push(new model.Link(((model.Set) (target) ), haxe.lang.Runtime.toString(""), haxe.lang.Runtime.toString("0"), haxe.lang.Runtime.toString("N"), haxe.lang.Runtime.toBool(((java.lang.Boolean) (false) )), haxe.lang.Runtime.toBool(((java.lang.Boolean) (false) )), haxe.lang.Runtime.toBool(((java.lang.Boolean) (false) )), ((int) (model.Field.ACCESS_UNSET) )));
		}
		
	}
	
	
	public static void addNNAssociation(model.Diagram diagram, model.Relation r)
	{
		//line 88 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/transform/MLDToDiagram.hx"
		model.Association a = new model.Association(haxe.lang.Runtime.toString(r.name));
		//line 89 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/transform/MLDToDiagram.hx"
		diagram.associations.set(a.name, a);
		//line 90 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/transform/MLDToDiagram.hx"
		{
			//line 90 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/transform/MLDToDiagram.hx"
			int _g = 0;
			//line 90 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/transform/MLDToDiagram.hx"
			haxe.root.Array<java.lang.Object> _g1 = r.mergeForeignKeys();
			//line 90 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/transform/MLDToDiagram.hx"
			while (( _g < _g1.length ))
			{
				//line 90 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/transform/MLDToDiagram.hx"
				java.lang.Object mfk = _g1.__get(_g);
				//line 90 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/transform/MLDToDiagram.hx"
				 ++ _g;
				//line 91 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/transform/MLDToDiagram.hx"
				a.links.push(new model.Link(((model.Set) (diagram.getObj(((model.Relation) (haxe.lang.Runtime.getField(mfk, "relation", true)) ).name)) ), haxe.lang.Runtime.toString(""), haxe.lang.Runtime.toString("0"), haxe.lang.Runtime.toString("N"), haxe.lang.Runtime.toBool(((java.lang.Boolean) (false) )), haxe.lang.Runtime.toBool(((java.lang.Boolean) (false) )), haxe.lang.Runtime.toBool(((java.lang.Boolean) (false) )), ((int) (model.Field.ACCESS_UNSET) )));
			}
			
		}
		
		//line 93 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/transform/MLDToDiagram.hx"
		{
			//line 93 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/transform/MLDToDiagram.hx"
			int _g2 = 0;
			//line 93 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/transform/MLDToDiagram.hx"
			haxe.root.Array<model.Field> _g3 = r.fields;
			//line 93 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/transform/MLDToDiagram.hx"
			while (( _g2 < _g3.length ))
			{
				//line 93 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/transform/MLDToDiagram.hx"
				model.Field f = _g3.__get(_g2);
				//line 93 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/transform/MLDToDiagram.hx"
				 ++ _g2;
				//line 94 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/transform/MLDToDiagram.hx"
				if (( r.key.indexOf(f.name, null) == -1 )) 
				{
					//line 95 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/transform/MLDToDiagram.hx"
					a.fields.push(f);
				}
				
			}
			
		}
		
	}
	
	
	public static boolean allForeignKeysBelongToKey(model.Relation r)
	{
		//line 101 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/transform/MLDToDiagram.hx"
		boolean result = false;
		//line 102 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/transform/MLDToDiagram.hx"
		if (( r.key.length == r.foreignKeys.length )) 
		{
			//line 103 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/transform/MLDToDiagram.hx"
			result = true;
			//line 104 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/transform/MLDToDiagram.hx"
			int i = 0;
			//line 105 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/transform/MLDToDiagram.hx"
			while (( result && ( i < r.foreignKeys.length ) ))
			{
				//line 106 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/transform/MLDToDiagram.hx"
				if (( r.key.indexOf(haxe.lang.Runtime.toString(haxe.lang.Runtime.getField(r.foreignKeys.__get(i), "name", true)), null) == -1 )) 
				{
					//line 107 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/transform/MLDToDiagram.hx"
					result = false;
				}
				
				//line 109 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/transform/MLDToDiagram.hx"
				 ++ i;
			}
			
		}
		
		//line 112 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/transform/MLDToDiagram.hx"
		return result;
	}
	
	
}


