package org.openhealthdata.validator.listeners;

import java.util.LinkedList;
import java.util.List;

import org.drools.definition.rule.Rule;
import org.drools.event.knowledgebase.AfterFunctionRemovedEvent;
import org.drools.event.knowledgebase.AfterKnowledgeBaseLockedEvent;
import org.drools.event.knowledgebase.AfterKnowledgeBaseUnlockedEvent;
import org.drools.event.knowledgebase.AfterKnowledgePackageAddedEvent;
import org.drools.event.knowledgebase.AfterKnowledgePackageRemovedEvent;
import org.drools.event.knowledgebase.AfterRuleAddedEvent;
import org.drools.event.knowledgebase.AfterRuleRemovedEvent;
import org.drools.event.knowledgebase.BeforeFunctionRemovedEvent;
import org.drools.event.knowledgebase.BeforeKnowledgeBaseLockedEvent;
import org.drools.event.knowledgebase.BeforeKnowledgeBaseUnlockedEvent;
import org.drools.event.knowledgebase.BeforeKnowledgePackageAddedEvent;
import org.drools.event.knowledgebase.BeforeKnowledgePackageRemovedEvent;
import org.drools.event.knowledgebase.BeforeRuleAddedEvent;
import org.drools.event.knowledgebase.BeforeRuleRemovedEvent;
import org.drools.event.knowledgebase.KnowledgeBaseEventListener;
import org.openhealthdata.validation.result.RuleType;

/**
 * This Listener creates and manages a <code>RuleType</code> list 
 * for the ValdiationManager
 * @author ohdohd
 *
 */
public class ValidationKnowledgeBaseEventListener implements
		KnowledgeBaseEventListener {

	List<RuleType> rules = new LinkedList<RuleType>();
	
	public List<RuleType> getRules() {
		return rules;
	}
	
	public void afterFunctionRemoved(AfterFunctionRemovedEvent arg0) {
		// Nothing needed to be done
	}

	public void afterKnowledgeBaseLocked(AfterKnowledgeBaseLockedEvent arg0) {
		// Nothing needed to be done
	}

	public void afterKnowledgeBaseUnlocked(AfterKnowledgeBaseUnlockedEvent arg0) {
		// Nothing needed to be done
	}

	public void afterKnowledgePackageAdded(AfterKnowledgePackageAddedEvent arg0) {
		// Nothing needed to be done
	}

	public void afterKnowledgePackageRemoved(
			AfterKnowledgePackageRemovedEvent arg0) {
		// Nothing needed to be done
	}

	public void afterRuleAdded(AfterRuleAddedEvent rae) {
		// TODO Auto-generated method stub
		RuleType rt = new RuleType();
		Rule r = rae.getRule();
		rt.setName(r.getName());
		rt.setPackage(r.getPackageName());
		// Check for the appropriate meta-attributes
		String profile = r.getMetaAttribute("profile");
		if (profile != null){
			rt.setProfile(profile);
		}
		String testid = r.getMetaAttribute("testid");
		if(testid != null){
			rt.setId(testid);
		}
		String title = r.getMetaAttribute("title");
		if(title != null){
			rt.setTitle(title);
		}
		String description = r.getMetaAttribute("description");
		if (description != null){
			rt.setDescription(description);
		}
		String source = r.getMetaAttribute("source");
		if (source != null){
			rt.setSource(source);
		}
		String author = r.getMetaAttribute("author");
		if (author !=null){
			rt.setAuthor(author);
		}
		rules.add(rt);
	}

	public void afterRuleRemoved(AfterRuleRemovedEvent rre) {
		// TODO Increase performance
		// Assume small number of rules
		for (int i=0;i<rules.size();i++){
			if (rules.get(i).getName() == rre.getRule().getName()){
				rules.remove(i);
				break;
			}
		}
	}

	public void beforeFunctionRemoved(BeforeFunctionRemovedEvent arg0) {
		// Nothing needed to be done
	}

	public void beforeKnowledgeBaseLocked(BeforeKnowledgeBaseLockedEvent arg0) {
		// Nothing needed to be done
	}

	public void beforeKnowledgeBaseUnlocked(
			BeforeKnowledgeBaseUnlockedEvent arg0) {
		// Nothing needed to be done
	}

	public void beforeKnowledgePackageAdded(
			BeforeKnowledgePackageAddedEvent arg0) {
		// Nothing needed to be done
	}

	public void beforeKnowledgePackageRemoved(
			BeforeKnowledgePackageRemovedEvent arg0) {
		// Nothing needed to be done
	}

	public void beforeRuleAdded(BeforeRuleAddedEvent arg0) {
		// Nothing needed to be done
	}

	public void beforeRuleRemoved(BeforeRuleRemovedEvent arg0) {
		// Nothing needed to be done
	}


}

@@ -93,16 +93,38 @@
 il.List;
+%0Aimport java.util.Map;
 %0A%0Aimport
@@ -2384,45 +2384,109 @@
 tes%0A
-%09%09String profile = r.getMetaAttribute
+        Map%3CString,Object%3E metaData = r.getMetaData();%0A        String profile = (String) metaData.get
 (%22pr
@@ -2567,34 +2567,37 @@
 estid = 
-r.getMetaAttribute
+(String) metaData.get
 (%22testid
@@ -2664,34 +2664,37 @@
 title = 
-r.getMetaAttribute
+(String) metaData.get
 (%22title%22
@@ -2767,34 +2767,37 @@
 ption = 
-r.getMetaAttribute
+(String) metaData.get
 (%22descri
@@ -2890,34 +2890,37 @@
 ource = 
-r.getMetaAttribute
+(String) metaData.get
 (%22source
@@ -2997,26 +2997,29 @@
 r = 
-r.getMetaAttribute
+(String) metaData.get
 (%22au

change deprecated API usage