001/* 002 * JBoss, Home of Professional Open Source 003 * 004 * Distributable under LGPL license. 005 * See terms of license at gnu.org. 006 */ 007 008package org.granite.util; 009 010import java.lang.reflect.Field; 011import java.lang.reflect.Method; 012import java.lang.reflect.Type; 013 014import javax.persistence.EmbeddedId; 015import javax.persistence.Id; 016import javax.persistence.Version; 017 018 019 020/** 021 * A wrapper for a entity, This code was pulled from Entity.java 022 * in the seam project www.seamframework.org jboss-seam-2.0.0.GA author Gavin King 023 * @author gavin king 024 */ 025 026public class Entity { 027 028 private Class<?> entityClass; 029 private Method identifierGetter; 030 private Field identifierField; 031 private Method versionGetter; 032 private Field versionField; 033 private Object wrappedEntity; 034 private String name; 035 036 037 public Entity(Object entity) { 038 if (entity instanceof Class<?>) 039 this.entityClass = (Class<?>)entity; 040 else { 041 this.entityClass = entity.getClass(); 042 this.wrappedEntity = entity; 043 } 044 045 if (entityClass.isAnnotationPresent(javax.persistence.Entity.class)) { 046 if (!"".equals(entityClass.getAnnotation(javax.persistence.Entity.class).name())) 047 name = entityClass.getAnnotation(javax.persistence.Entity.class).name(); 048 else 049 name = entityClass.getName(); 050 } 051 052 for (Class<?> clazz = entityClass; clazz != Object.class; clazz = clazz.getSuperclass()) { 053 for (Method method : clazz.getDeclaredMethods()) { 054 if (method.isAnnotationPresent(Id.class) || method.isAnnotationPresent(EmbeddedId.class)) 055 identifierGetter = method; 056 057 if (method.isAnnotationPresent(Version.class)) 058 versionGetter = method; 059 } 060 061 } 062 063 if (identifierGetter == null) { 064 for (Class<?> clazz = entityClass; clazz != Object.class; clazz = clazz.getSuperclass()) { 065 for (Field field : clazz.getDeclaredFields()) { 066 if (field.isAnnotationPresent(Id.class) || field.isAnnotationPresent(EmbeddedId.class)) { 067 identifierField = field; 068 if (!field.isAccessible()) 069 field.setAccessible(true); 070 } 071 072 if (field.isAnnotationPresent(Version.class)) { 073 versionField = field; 074 if (!field.isAccessible()) 075 field.setAccessible(true); 076 } 077 } 078 } 079 } 080 } 081 082 083 084 public Object getIdentifier() { 085 if (wrappedEntity == null) 086 throw new IllegalStateException("No entity instance defined"); 087 088 return getIdentifier(wrappedEntity); 089 } 090 091 public Object getIdentifier(Object entity) { 092 if (identifierGetter != null) 093 return Reflections.invokeAndWrap(identifierGetter, entity); 094 else if (identifierField != null) 095 return Reflections.getAndWrap(identifierField, entity); 096 else 097 throw new IllegalStateException("@Id attribute not found for entity class: " + entity.getClass().getName()); 098 } 099 100 public Object getVersion() { 101 if (wrappedEntity == null) 102 throw new IllegalStateException("No entity instance defined"); 103 104 return getVersion(wrappedEntity); 105 } 106 107 public Object getVersion(Object entity) { 108 if (versionGetter != null) 109 return Reflections.invokeAndWrap(versionGetter, entity); 110 else if (versionField != null) 111 return Reflections.getAndWrap(versionField, entity); 112 return null; 113 } 114 115 116 public Method getIdentifierGetter() { 117 return identifierGetter; 118 } 119 120 public Field getIdentifierField() { 121 return identifierField; 122 } 123 124 public Type getIdentifierType() { 125 if (identifierGetter != null) 126 return identifierGetter.getGenericReturnType(); 127 else if (identifierField != null) 128 return identifierField.getGenericType(); 129 else 130 throw new IllegalStateException("@Id attribute not found for entity class: " + entityClass.getName()); 131 } 132 133 134 public Method getVersionGetter() { 135 return versionGetter; 136 } 137 138 public Field getVersionField() { 139 return versionField; 140 } 141 142 public boolean isVersioned() { 143 return versionGetter != null || versionField != null; 144 } 145 146 147 public String getName() { 148 return name; 149 } 150}