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