001    /*
002     * JBoss, Home of Professional Open Source
003     *
004     * Distributable under LGPL license.
005     * See terms of license at gnu.org.
006     */
007    
008    package org.granite.util;
009    
010    import java.lang.reflect.Field;
011    import java.lang.reflect.Method;
012    import java.lang.reflect.Type;
013    
014    import javax.persistence.EmbeddedId;
015    import javax.persistence.Id;
016    import 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    
026    public 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                if (identifierGetter != null)
089                    return Reflections.invokeAndWrap(identifierGetter, wrappedEntity);
090                else if (identifierField != null)
091                    return Reflections.getAndWrap(identifierField, wrappedEntity);
092                else
093                    throw new IllegalStateException("@Id attribute not found for entity class: " + wrappedEntity.getClass().getName());
094            }
095            
096            public Object getVersion() {
097                if (versionGetter != null)
098                    return Reflections.invokeAndWrap(versionGetter, wrappedEntity);
099                else if (versionField != null)
100                    return Reflections.getAndWrap(versionField, wrappedEntity);
101                return null;
102            }
103    
104            
105            public Method getIdentifierGetter() {
106                    return identifierGetter;
107            }
108    
109            public Field getIdentifierField() {
110                    return identifierField;
111            }
112    
113            public Type getIdentifierType() {
114                if (identifierGetter != null)
115                    return identifierGetter.getGenericReturnType();
116                else if (identifierField != null)
117                    return identifierField.getGenericType();
118                else
119                    throw new IllegalStateException("@Id attribute not found for entity class: " + entityClass.getName());
120            }
121            
122            
123            public Method getVersionGetter() {
124                    return versionGetter;
125            }
126    
127            public Field getVersionField() {
128                    return versionField;
129            }
130            
131            public boolean isVersioned() {
132                    return versionGetter != null || versionField != null;
133            }
134    
135    
136            public String getName() {
137                    return name;
138            }
139    }