001/**
002 *   GRANITE DATA SERVICES
003 *   Copyright (C) 2006-2014 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 */
022package org.granite.generator.as3;
023
024import java.lang.annotation.Annotation;
025import java.lang.reflect.Field;
026import java.lang.reflect.Method;
027import java.net.URL;
028
029import javax.jdo.annotations.PersistenceCapable;
030import javax.jdo.annotations.Persistent;
031import javax.jdo.annotations.PrimaryKey;
032import javax.persistence.EmbeddedId;
033import javax.persistence.Entity;
034import javax.persistence.FetchType;
035import javax.persistence.Id;
036import javax.persistence.ManyToMany;
037import javax.persistence.ManyToOne;
038import javax.persistence.MappedSuperclass;
039import javax.persistence.OneToMany;
040import javax.persistence.OneToOne;
041import javax.persistence.Version;
042
043import org.granite.generator.as3.reflect.JavaBean;
044import org.granite.generator.as3.reflect.JavaEntityBean;
045import org.granite.generator.as3.reflect.JavaFieldProperty;
046import org.granite.generator.as3.reflect.JavaProperty;
047import org.granite.generator.as3.reflect.JavaType;
048import org.granite.generator.as3.reflect.JavaTypeFactory;
049
050
051public class DefaultEntityFactory implements EntityFactory {
052
053        @Override
054        public boolean isEntity(Class<?> clazz) {
055                return clazz.isAnnotationPresent(Entity.class) ||
056                clazz.isAnnotationPresent(MappedSuperclass.class) ||
057                clazz.isAnnotationPresent(PersistenceCapable.class);
058        }
059        
060        @Override
061        public JavaType newBean(JavaTypeFactory provider, Class<?> type, URL url) {
062                return new JavaBean(provider, type, url);
063        }
064        
065        @Override
066        public JavaType newEntity(JavaTypeFactory provider, Class<?> type, URL url) {
067                return new JavaEntityBean(provider, type, url);
068        }
069        
070        @Override
071        public boolean isId(JavaFieldProperty fieldProperty) {
072        Field field = fieldProperty.getMember();
073        Method getter = (fieldProperty.getReadMethod() != null ? fieldProperty.getReadMethod().getMember() : null);
074        Method setter = (fieldProperty.getWriteMethod() != null ? fieldProperty.getWriteMethod().getMember() : null);
075
076        if (field.isAnnotationPresent(Persistent.class)) {
077                Annotation annotation = field.getAnnotation(Persistent.class);
078                if (annotation instanceof Persistent) {
079                        Persistent persistAnnotation = (Persistent)annotation;
080                        String pk = persistAnnotation.primaryKey();
081                        if (pk != null && pk.toLowerCase().equals("true"))
082                                return true;
083                }
084                if (field.isAnnotationPresent(PrimaryKey.class))
085                        return true;
086        }
087        
088        return
089            (field.isAnnotationPresent(Id.class) || field.isAnnotationPresent(EmbeddedId.class)) ||
090            (getter != null && (getter.isAnnotationPresent(Id.class) || getter.isAnnotationPresent(EmbeddedId.class))) ||
091            (setter != null && (setter.isAnnotationPresent(Id.class) || setter.isAnnotationPresent(EmbeddedId.class)));
092        }
093        
094        @Override
095        public boolean isVersion(JavaProperty property) {
096                if (property.isAnnotationPresent(Version.class))
097                        return true;
098                
099            if (property.getType().isAnnotationPresent(javax.jdo.annotations.Version.class)) {
100                // Get JDO version field using specific DataNucleus extension
101                javax.jdo.annotations.Version versionAnnotation = property.getType().getAnnotation(javax.jdo.annotations.Version.class);
102                javax.jdo.annotations.Extension[] extensions = versionAnnotation.extensions();
103                for (javax.jdo.annotations.Extension extension : extensions) {
104                        if ("datanucleus".equals(extension.vendorName()) && "field-name".equals(extension.key()))
105                                return property.getName().equals(extension.value());
106                }
107            }
108            return false;
109        }
110        
111        @Override
112        public boolean isLazy(JavaProperty property) {
113                if (property.isAnnotationPresent(ManyToOne.class)) {
114                        ManyToOne manyToOne = property.getAnnotation(ManyToOne.class);
115                        return manyToOne.fetch().equals(FetchType.LAZY);
116                }
117                else if (property.isAnnotationPresent(OneToOne.class)) {
118                        OneToOne oneToOne = property.getAnnotation(OneToOne.class);
119                        return oneToOne.fetch().equals(FetchType.LAZY);
120                }
121                else if (property.isAnnotationPresent(OneToMany.class)) {
122                        OneToMany oneToMany = property.getAnnotation(OneToMany.class);
123                        return oneToMany.fetch().equals(FetchType.LAZY);
124                }
125                else if (property.isAnnotationPresent(ManyToMany.class)) {
126                        ManyToMany manyToMany = property.getAnnotation(ManyToMany.class);
127                        return manyToMany.fetch().equals(FetchType.LAZY);
128                }
129                return false;
130        }
131}