/*
 * Copyright (C) 2009-2011 Open Wide
 * Contact: contact@openwide.fr
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package fr.openwide.core.wicket.more.model;

import java.io.Serializable;

import org.apache.wicket.injection.web.InjectorHolder;
import org.apache.wicket.model.LoadableDetachableModel;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.hibernate.Hibernate;

import fr.openwide.core.jpa.business.generic.model.GenericEntity;
import fr.openwide.core.jpa.business.generic.service.IEntityService;

public class GenericEntityModel<K extends Serializable & Comparable<K>, E extends GenericEntity<K, ?>> extends LoadableDetachableModel<E> {
	
	private static final long serialVersionUID = 1L;

	@SpringBean
	private IEntityService entityService;
	
	private K id;
	
	private Class<E> clazz;
	
	@SuppressWarnings("unchecked")
	public GenericEntityModel(E entity) {
		super(entity);
		InjectorHolder.getInjector().inject(this);
		
		if (entity != null) {
			clazz = (Class<E>) Hibernate.getClass(entity);
			id = entity.getId();
		}
	}

	@Override
	protected E load() {
		E entity = null;
		if (clazz != null && id != null) {
			entity = entityService.getEntity(clazz, id);
		}
		return entity;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public void setObject(E entity) {
		if (entity != null) {
			clazz = (Class<E>) Hibernate.getClass(entity);
			id = entity.getId();
		} else {
			id = null;
			clazz = null;
		}
		super.setObject(entity);
	}
	
	protected K getId() {
		return id;
	}

}

@@ -1302,74 +1302,117 @@
 ate 
-K id;%0A%09%0A%09private Class%3CE%3E clazz
+Class%3CE%3E clazz;%0A%09%0A%09private transient boolean attached = false;%0A%0A%09private K id
 ;%0A
-%09
 %0A%09
-@SuppressWarnings(%22unchecked%22)
+private E transientEntity;%0A%09
 %0A%09pu
@@ -1455,22 +1455,20 @@
 %09%09super(
-entity
+null
 );%0A%09%09Inj
@@ -1516,107 +1516,26 @@
 %09%0A%09%09
-if (entity != null) %7B%0A%09%09%09clazz = (Class%3CE%3E) Hibernate.getClass(entity);%0A%09%09%09id = entity.getId();%0A%09%09%7D
+setObject(entity);
 %0A%09%7D%0A
@@ -1572,22 +1572,22 @@
 ) %7B%0A%09%09E 
-entity
+result
  = null;
@@ -1597,25 +1597,8 @@
 if (
-clazz != null && 
 id !
@@ -1610,22 +1610,22 @@
 l) %7B%0A%09%09%09
-entity
+result
  = entit
@@ -1662,24 +1662,83 @@
 %0A%09%09%7D
-%0A%09%09return entity
+ else %7B%0A%09%09%09result = transientEntity;%0A%09%09%7D%0A%09%09attached = true;%0A%09%09return result
 ;%0A%09%7D
@@ -1898,28 +1898,137 @@
 ;%0A%09%09
-%09id = entity.getId()
+%7D else %7B%0A%09%09%09clazz = null;%0A%09%09%7D%0A%09%09if (entity != null && entity.getId() != null) %7B%0A%09%09%09id = entity.getId();%0A%09%09%09transientEntity = null
 ;%0A%09%09
@@ -2053,34 +2053,49 @@
 ull;%0A%09%09%09
-clazz = null
+transientEntity = entity
 ;%0A%09%09%7D%0A
+%09%09%0A
 %09%09super.
@@ -2113,16 +2113,35 @@
 ntity);%0A
+%09%09attached = true;%0A
 %09%7D%0A%09%0A%09pr
@@ -2177,10 +2177,121 @@
 id;%0A%09%7D%0A%0A
+%09@Override%0A%09public void detach() %7B%0A%09%09if (!attached) %7B%0A%09%09%09return;%0A%09%09%7D%0A%09%09super.detach();%0A%09%09attached = false;%0A%09%7D%0A%0A
 %7D%0A

version du genericentitymodel qui peut utiliser des objets « transient »