package org.orienteer.core.method.filters;

import java.util.ArrayList;
import java.util.List;

import org.orienteer.core.method.IMethodEnvironmentData;
import org.orienteer.core.method.IMethodFilter;
import com.orientechnologies.orient.core.db.ODatabaseRecordThreadLocal;
import com.orientechnologies.orient.core.metadata.schema.OClass;
import com.orientechnologies.orient.core.record.impl.ODocument;

/**
 * 
 * OFilter by OClass of current ODocument or OClass
 * Support multiple values splitted by "|"
 *
 */

public class OEntityFilter implements IMethodFilter {

	private List<OClass> oClasses;
	
	@Override
	public IMethodFilter setFilterData(String filterData) {
		String[] strOClasses = filterData.split("\\|");
		if (strOClasses.length>0){
			oClasses = new ArrayList<OClass>(strOClasses.length);
			for (String strClass : strOClasses) {
				OClass oClass = ODatabaseRecordThreadLocal.INSTANCE.get().getMetadata().getSchema().getClass(strClass);
				if (oClass!=null){
					oClasses.add(oClass);
				}
			}
		}
		return this;
	}

	@Override
	public boolean isSupportedMethod(IMethodEnvironmentData dataObject) {
		OClass oclass = getOClass(dataObject);
		if (oclass!=null){
			for (OClass oClass : oClasses) {
				if (oclass.isSubClassOf(oClass)){
					return true;
				}
			}
		}
		return false;
	}
	
	private OClass getOClass(IMethodEnvironmentData dataObject){
		if (dataObject.getDisplayObjectModel()!=null){
			Object obj = dataObject.getDisplayObjectModel().getObject();
			if (obj instanceof OClass){
				return (OClass) obj;
			}else if (obj instanceof ODocument){
				return ((ODocument)obj).getSchemaClass();
			}
		}
		return null;
	}
}

@@ -89,16 +89,67 @@
 .List;%0A%0A
+import org.orienteer.core.OrienteerWebApplication;%0A
 import o
@@ -249,80 +249,8 @@
 er;%0A
-import com.orientechnologies.orient.core.db.ODatabaseRecordThreadLocal;%0A
 impo
@@ -573,16 +573,45 @@
 lasses;%0A
+%09private String filterData; %0A
 %09%0A%09@Over
@@ -672,16 +672,129 @@
 Data) %7B%0A
+%09%09this.filterData = filterData;%0A%09%09return this;%0A%09%7D%0A%09%0A%09private List%3COClass%3E getOClasses()%7B%0A%09%09if (oClasses==null)%7B%0A%09
 %09%09String
@@ -833,24 +833,25 @@
 t(%22%5C%5C%7C%22);%0A%09%09
+%09
 if (strOClas
@@ -865,16 +865,17 @@
 gth%3E0)%7B%0A
+%09
 %09%09%09oClas
@@ -922,24 +922,25 @@
 length);%0A%09%09%09
+%09
 for (String 
@@ -969,16 +969,17 @@
 ) %7B%0A%09%09%09%09
+%09
 OClass o
@@ -991,46 +991,48 @@
  = O
-DatabaseRecordThreadLocal.INSTANCE.get
+rienteerWebApplication.get().getDatabase
 ().g
@@ -1072,24 +1072,25 @@
 (strClass);%0A
+%09
 %09%09%09%09if (oCla
@@ -1101,24 +1101,25 @@
 null)%7B%0A%09%09%09%09%09
+%09
 oClasses.add
@@ -1136,37 +1136,140 @@
 %09%09%09%09
+%09
 %7D%0A
+%09
 %09%09%09%7D%0A
+%09
 %09%09%7D
-%0A%09%09return thi
+else%7B%0A%09%09%09%09//TODO insert empty list class here%0A%09%09%09%09oClasses = new ArrayList%3COClass%3E(0);%0A%09%09%09%7D%0A%09%09%7D%0A%09%09return oClasse
 s;%0A%09%7D%0A
+%09
 %0A%09@O
@@ -1433,24 +1433,29 @@
 Class : 
-o
+getO
 Classes
+()
 ) %7B%0A%09%09%09%09

add OEntityFilter lazy loading