package ru.ydn.wicket.wicketorientdb.model;

import org.apache.wicket.model.IObjectClassAwareModel;
import org.apache.wicket.model.LoadableDetachableModel;

import ru.ydn.wicket.wicketorientdb.OrientDbWebSession;

import com.orientechnologies.orient.core.db.document.ODatabaseDocument;
import com.orientechnologies.orient.core.exception.ORecordNotFoundException;
import com.orientechnologies.orient.core.id.ORID;
import com.orientechnologies.orient.core.record.impl.ODocument;

/**
 * Model for storing of {@link ODocument}
 */
public class ODocumentModel extends LoadableDetachableModel<ODocument> implements IObjectClassAwareModel<ODocument>
{
	private static final long serialVersionUID = 1L;
	private ORID orid;
	private ODocument savedDocument;
	
	private boolean autoSave=false;
	
	public ODocumentModel() {
		this((ODocument)null);
	}
	
	public ODocumentModel(ODocument iDocument) {
		super(iDocument);
		if(iDocument!=null) orid=iDocument.getIdentity();
	}

	public ODocumentModel(ORID iRID) {
		this.orid = iRID;
	}

	@Override
	public Class<ODocument> getObjectClass() {
		return ODocument.class;
	}
	
	/**
	 * Get {@link ORID} of a stored {@link ODocument}
	 * @return identifactor {@link ORID}
	 */
	public ORID getIdentity() {
		if(orid!=null) return orid;
		ODocument doc = getObject();
		return doc!=null?doc.getIdentity():null;
	}
	
	public boolean isAutoSave() {
		return autoSave;
	}
	
	public ODocumentModel setAutoSave(boolean autoSave) {
		this.autoSave = autoSave;
		return this;
	}
	
	@Override
	protected ODocument load() {
		if(orid!=null && orid.isValid())
		{
			try {
				ODatabaseDocument db = OrientDbWebSession.get().getDatabase();
				return db.load(orid);
			} catch (ORecordNotFoundException e) {
				return null;
			}
		}
		else
		{
			return savedDocument;
		}
	}
	
	@Override
    public void detach()
    {
		if(isAttached())
		{
			ODocument doc = getObject();
			if(doc!=null)
			{
				if(autoSave) doc.save();
		        this.orid = doc.getIdentity();
		        if(orid!=null && orid.isValid())
		        {
		        	savedDocument=null;
		        }
		        else
		        {
		        	orid=null;
		        	savedDocument = doc;
		        }
			}
			else
			{
				orid=null;
				savedDocument=null;
			}
		}
		super.detach();
    }
	
	

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + (autoSave ? 1231 : 1237);
		result = prime * result + ((orid == null) ? 0 : orid.hashCode());
		result = prime * result
				+ ((savedDocument == null) ? 0 : savedDocument.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		ODocumentModel other = (ODocumentModel) obj;
		if (autoSave != other.autoSave)
			return false;
		if (orid == null) {
			if (other.orid != null)
				return false;
		} else if (!orid.equals(other.orid))
			return false;
		if (savedDocument == null) {
			if (other.savedDocument != null)
				return false;
		} else if (!savedDocument.equals(other.savedDocument))
			return false;
		return true;
	}

	@Override
	public String toString() {
		return "ODocumentModel [orid=" + orid + "]";
	}

}

@@ -67,31 +67,206 @@
 ket.
-model.IObjectClassAware
+Component;%0Aimport org.apache.wicket.model.IComponentInheritedModel;%0Aimport org.apache.wicket.model.IModel;%0Aimport org.apache.wicket.model.IObjectClassAwareModel;%0Aimport org.apache.wicket.model.IWrap
 Mode
@@ -811,16 +811,53 @@
 ocument%3E
+, IComponentInheritedModel%3CODocument%3E
 %0A%7B%0A%09priv
@@ -3454,10 +3454,626 @@
 %5D%22;%0A%09%7D%0A%0A
+%09@Override%0A%09public %3CW%3E IWrapModel%3CW%3E wrapOnInheritance(final Component component) %7B%0A%09%09return new IWrapModel%3CW%3E() %7B%0A%0A%09%09%09@Override%0A%09%09%09public W getObject() %7B%0A%09%09%09%09ODocument doc = ODocumentModel.this.getObject();%0A%09%09%09%09return doc!=null?(W)doc.field(component.getId()):null;%0A%09%09%09%7D%0A%0A%09%09%09@Override%0A%09%09%09public void setObject(W object) %7B%0A%09%09%09%09ODocument doc = ODocumentModel.this.getObject();%0A%09%09%09%09if(doc!=null) doc.field(component.getId(), object);%0A%09%09%09%7D%0A%0A%09%09%09@Override%0A%09%09%09public void detach() %7B%0A%09%09%09%09ODocumentModel.this.detach();%0A%09%09%09%7D%0A%0A%09%09%09@Override%0A%09%09%09public IModel%3C?%3E getWrappedModel() %7B%0A%09%09%09%09return ODocumentModel.this;%0A%09%09%09%7D%0A%09%09%7D;%0A%09%7D%0A%0A
 %7D%0A

Make ODocumentModel compound model