/**
 * generated by Xtext
 */
package org.xtext.example.domij.validation;

import org.eclipse.xtext.validation.Check;
import org.xtext.example.domij.validation.AbstractErDslValidator;

import er.Categories;
import er.ERModel;
import er.Entity;
import er.ErPackage;
import er.ISA;
import er.Key;
import er.ModellingConcept;
import er.RelationshipConcept;

/**
 * Custom validation rules.
 * 
 * see http://www.eclipse.org/Xtext/documentation.html#validation
 */
@SuppressWarnings("all")
public class ErDslValidator extends AbstractErDslValidator {
	
	@Check
	public void checkDuplicateKeyAttribute (Key key){
		for(int i=0;i<key.getKeyAttributes().size();i++)
			for(int j=i+1;j<key.getKeyAttributes().size();j++){
				if(key.getKeyAttributes().get(i).equals(key.getKeyAttributes().get(j))){
					error("Duplicated attribute in the key.",ErPackage.Literals.KEY__KEY_ATTRIBUTES);
					return;
				}
			}
	}
	
	@Check
	public void checkDuplicateIsaSubtype (ISA isa){
		for(int i=0;i<isa.getSubtypes().size();i++)
			for(int j=i+1;j<isa.getSubtypes().size();j++){
				if(isa.getSubtypes().get(i).equals(isa.getSubtypes().get(j))){
					error("Duplicated entity in the subtypes.",ErPackage.Literals.ISA__SUBTYPES);
					return;
				}
			}
	}

	@Check
	public void checkDuplicateCategorisingEntities (Categories categories){
		for(int i=0;i<categories.getEntities().size();i++)
			for(int j=i+1;j<categories.getEntities().size();j++){
				if(categories.getEntities().get(i).equals(categories.getEntities().get(j))){
					error("Duplicated entity concept in the categorising entities.",ErPackage.Literals.CATEGORIES__ENTITIES);
					return;
				}
			}
	}
	
	@Check
	public void checkPrimaryKeyEntity (Entity entity){
		if(entity.getPrimaryKey() == null){
			for(RelationshipConcept rc:((ERModel)entity.eContainer()).getRelationships())
				if(rc instanceof ISA)
					for (Entity e:((ISA) rc).getSubtypes())
						if(e.equals(entity))
							return;
			error("Missing primary key in entity.",ErPackage.Literals.ENTITY__PRIMARY_KEY);
		}			
		
	}
	
	
}
