package ru.ydn.wicket.wicketorientdb.proto;

import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import ru.ydn.wicket.wicketorientdb.OrientDbWebSession;

import com.orientechnologies.orient.core.metadata.schema.OClass;
import com.orientechnologies.orient.core.metadata.schema.OSchema;
import com.orientechnologies.orient.core.metadata.schema.clusterselection.OClusterSelectionFactory;
import com.orientechnologies.orient.core.metadata.schema.clusterselection.OClusterSelectionStrategy;

/**
 * Prototyper for {@link OClass}
 */
public class OClassPrototyper extends AbstractPrototyper<OClass> {
	
	private static final long serialVersionUID = 1L;
	public static final String NAME = "name";
	public static final String SHORT_NAME = "shortName";
	public static final String SUPER_CLASSES = "superClasses";
	public static final String OVER_SIZE = "overSize";
	public static final String STRICT_MODE = "strictMode";
	public static final String ABSTRACT = "abstract";
	public static final String CLUSTER_SELECTION = "clusterSelection";
	public static final String JAVA_CLASS = "javaClass";
	
	public static final List<String> OCLASS_ATTRS = Arrays.asList(NAME, SHORT_NAME, SUPER_CLASSES, OVER_SIZE, STRICT_MODE, ABSTRACT, JAVA_CLASS, CLUSTER_SELECTION);
	

	private OClassPrototyper()
	{
		values.put("overSize", (float)0);
	}

	@Override
	protected OClass createInstance(OClass proxy) {
		OSchema schema = OrientDbWebSession.get().getDatabase().getMetadata().getSchema();
		OClass oClass = schema.createClass(proxy.getName());
		values.remove("name");
		return oClass;
	}

	@Override
	protected Class<OClass> getMainInterface() {
		return OClass.class;
	}
	
	public static OClass newPrototype()
	{
		return newPrototype(new OClassPrototyper());
	}
	
	@Override
	protected Object handleSet(String propName, Object value) {
		if("clusterSelection".equals(propName))
		{
			if(value instanceof CharSequence)
			{
				value = new OClusterSelectionFactory().newInstance(value.toString());
			}
			if(value instanceof OClusterSelectionStrategy)
			{
				return super.handleSet(propName, value);
			}
			else return null;
			
		}
		//Default
		return super.handleSet(propName, value);
	}

	@Override
	protected Object handleCustom(Object proxy, Method method, Object[] args) {
		String methodName = method.getName();
		if("properties".equals(methodName) || "declaredProperties".equals(methodName))
		{
			return Collections.EMPTY_SET;
		}
		else
		{
			return super.handleCustom(proxy, method, args);
		}
	}

	@Override
	public String toString() {
		return "Prototype for '"+getMainInterface().getName()+"'";
	}

}

@@ -42,177 +42,8 @@
 o;%0A%0A
-import java.lang.reflect.Method;%0Aimport java.util.Arrays;%0Aimport java.util.Collections;%0Aimport java.util.List;%0A%0Aimport ru.ydn.wicket.wicketorientdb.OrientDbWebSession;%0A%0A
 impo
@@ -103,16 +103,16 @@
 OClass;%0A
+
 import c
@@ -369,16 +369,126 @@
 trategy;
+%0Aimport ru.ydn.wicket.wicketorientdb.OrientDbWebSession;%0A%0Aimport java.lang.reflect.Method;%0Aimport java.util.*;
 %0A%0A/**%0A *
@@ -1517,16 +1517,94 @@
 ame());%0A
+%09%09oClass.setSuperClasses((List%3C? extends OClass%3E) values.get(SUPER_CLASSES));%0A
 %09%09values
@@ -2459,24 +2459,24 @@
 dName))%0A%09%09%7B%0A
-
 %09%09%09return Co
@@ -2500,16 +2500,324 @@
 ET;%0A%09%09%7D%0A
+%09%09else if(%22addSuperClass%22.equals(methodName))%0A%09%09%7B%0A%09%09%09Object superClasses = values.get(SUPER_CLASSES);%0A%09%09%09if (superClasses == null)%0A%09%09%09%7B%0A%09%09%09%09superClasses = new ArrayList%3COClass%3E();%0A%09%09%09%09values.put(SUPER_CLASSES, superClasses);%0A%09%09%09%7D%0A%0A%09%09%09((List%3COClass%3E) superClasses).add((OClass) args%5B0%5D);%0A%09%09%09return proxy;%0A%09%09%7D%0A
 %09%09else%0A%09

Add option to set superclasses for OClass proxy, part of #270.