package ru.ydn.wicket.wicketorientdb.model;

import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;

import com.orientechnologies.orient.core.type.ODocumentWrapper;

/**
 * {@link IModel} for a {@link ODocumentWrapper}s
 *
 * @param <T> {@link ODocumentWrapper} type
 */
public class ODocumentWrapperModel<T extends ODocumentWrapper> extends Model<T> {
	private static final long serialVersionUID = 1L;
	
	private boolean needToReload=false;

	public ODocumentWrapperModel() {
		super();
	}

	public ODocumentWrapperModel(T object) {
		super(object);
		needToReload=false;
	}
	

	@Override
	public T getObject() {
		T ret = super.getObject();
		if(ret != null && needToReload) {
			ret.load();
			needToReload = false;
		}
		return ret;
	}

	@Override
	public void setObject(T object) {
		super.setObject(object);
		needToReload = false;
	}

	@Override
	public void detach() {
		T ret = getObject();
		if (ret != null && !ret.getDocument().getIdentity().isNew()) {
			needToReload = true;
		}
		super.detach();
	}
	
}

@@ -34,24 +34,101 @@
 tdb.model;%0A%0A
+import com.orientechnologies.orient.core.exception.ORecordNotFoundException;%0A
 import org.a
@@ -702,24 +702,33 @@
 tObject() %7B%0A
+%09%09try %7B%0A%09
 %09%09T ret = su
@@ -740,24 +740,25 @@
 etObject();%0A
+%09
 %09%09if(ret != 
@@ -784,16 +784,17 @@
 d) %7B%0A%09%09%09
+%09
 ret.load
@@ -797,16 +797,17 @@
 load();%0A
+%09
 %09%09%09needT
@@ -823,22 +823,24 @@
  false;%0A
+%09
 %09%09%7D%0A%09%09
+%09
 return r
@@ -843,16 +843,77 @@
 rn ret;%0A
+%09%09%7D catch (ORecordNotFoundException e) %7B%0A%09%09%09return null;%0A%09%09%7D%0A
 %09%7D%0A%0A%09@Ov

Return null if record doesn't exists in database