001/**
002 *   GRANITE DATA SERVICES
003 *   Copyright (C) 2006-2013 GRANITE DATA SERVICES S.A.S.
004 *
005 *   This file is part of the Granite Data Services Platform.
006 *
007 *   Granite Data Services is free software; you can redistribute it and/or
008 *   modify it under the terms of the GNU Lesser General Public
009 *   License as published by the Free Software Foundation; either
010 *   version 2.1 of the License, or (at your option) any later version.
011 *
012 *   Granite Data Services is distributed in the hope that it will be useful,
013 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
014 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
015 *   General Public License for more details.
016 *
017 *   You should have received a copy of the GNU Lesser General Public
018 *   License along with this library; if not, write to the Free Software
019 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
020 *   USA, or see <http://www.gnu.org/licenses/>.
021 */
022/*
023 * JBoss, Home of Professional Open Source
024 *
025 * Distributable under LGPL license.
026 * See terms of license at gnu.org.
027 */
028
029package org.granite.util;
030
031import java.lang.reflect.Field;
032import java.lang.reflect.Method;
033import java.lang.reflect.Type;
034
035import javax.persistence.EmbeddedId;
036import javax.persistence.Id;
037import javax.persistence.Version;
038
039
040
041/**
042 * A wrapper for a entity, This code was pulled from Entity.java 
043 * in the seam project www.seamframework.org jboss-seam-2.0.0.GA author Gavin King
044 * @author gavin king
045 */
046
047public class Entity {
048        
049        private Class<?> entityClass;
050        private Method identifierGetter;
051        private Field identifierField;
052        private Method versionGetter;
053        private Field versionField;
054        private Object wrappedEntity;
055        private String name;
056        
057        
058        public Entity(Object entity) {
059                if (entity instanceof Class<?>)
060                        this.entityClass = (Class<?>)entity;
061                else {
062                        this.entityClass = entity.getClass();
063                        this.wrappedEntity = entity;
064                }
065                
066        if (entityClass.isAnnotationPresent(javax.persistence.Entity.class)) {
067                if (!"".equals(entityClass.getAnnotation(javax.persistence.Entity.class).name()))
068                    name = entityClass.getAnnotation(javax.persistence.Entity.class).name();
069                else
070                    name = entityClass.getName();
071        }
072                
073            for (Class<?> clazz = entityClass; clazz != Object.class; clazz = clazz.getSuperclass())  {
074            for (Method method : clazz.getDeclaredMethods()) {
075                            if (method.isAnnotationPresent(Id.class) || method.isAnnotationPresent(EmbeddedId.class))
076                                identifierGetter = method;
077                            
078                            if (method.isAnnotationPresent(Version.class))
079                                versionGetter = method;
080                        }
081            
082            }
083            
084        if (identifierGetter == null) {
085            for (Class<?> clazz = entityClass; clazz != Object.class; clazz = clazz.getSuperclass())      {
086               for (Field field : clazz.getDeclaredFields()) {
087                   if (field.isAnnotationPresent(Id.class) || field.isAnnotationPresent(EmbeddedId.class)) {
088                           identifierField = field;
089                       if (!field.isAccessible())
090                           field.setAccessible(true);
091                   }
092              
093                   if (field.isAnnotationPresent(Version.class)) {
094                           versionField = field;
095                       if (!field.isAccessible())
096                           field.setAccessible(true);
097                   }
098               }
099           }
100        }
101        }
102        
103        
104        
105        public Object getIdentifier() {
106                if (wrappedEntity == null)
107                        throw new IllegalStateException("No entity instance defined");
108                
109                return getIdentifier(wrappedEntity);
110        }
111        
112        public Object getIdentifier(Object entity) {            
113            if (identifierGetter != null)
114                return Reflections.invokeAndWrap(identifierGetter, entity);
115            else if (identifierField != null)
116                return Reflections.getAndWrap(identifierField, entity);
117            else
118                throw new IllegalStateException("@Id attribute not found for entity class: " + entity.getClass().getName());
119        }
120        
121        public Object getVersion() {
122                if (wrappedEntity == null)
123                        throw new IllegalStateException("No entity instance defined");
124                
125                return getVersion(wrappedEntity);
126        }
127        
128        public Object getVersion(Object entity) {
129            if (versionGetter != null)
130                return Reflections.invokeAndWrap(versionGetter, entity);
131            else if (versionField != null)
132                return Reflections.getAndWrap(versionField, entity);
133            return null;
134        }
135
136        
137        public Method getIdentifierGetter() {
138                return identifierGetter;
139        }
140
141        public Field getIdentifierField() {
142                return identifierField;
143        }
144
145        public Type getIdentifierType() {
146            if (identifierGetter != null)
147                return identifierGetter.getGenericReturnType();
148            else if (identifierField != null)
149                return identifierField.getGenericType();
150            else
151                throw new IllegalStateException("@Id attribute not found for entity class: " + entityClass.getName());
152        }
153        
154        
155        public Method getVersionGetter() {
156                return versionGetter;
157        }
158
159        public Field getVersionField() {
160                return versionField;
161        }
162        
163        public boolean isVersioned() {
164                return versionGetter != null || versionField != null;
165        }
166
167
168        public String getName() {
169                return name;
170        }
171}