001    /*
002      GRANITE DATA SERVICES
003      Copyright (C) 2011 GRANITE DATA SERVICES S.A.S.
004    
005      This file is part of Granite Data Services.
006    
007      Granite Data Services is free software; you can redistribute it and/or modify
008      it under the terms of the GNU Library General Public License as published by
009      the Free Software Foundation; either version 2 of the License, or (at your
010      option) any later version.
011    
012      Granite Data Services is distributed in the hope that it will be useful, but
013      WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
014      FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License
015      for more details.
016    
017      You should have received a copy of the GNU Library General Public License
018      along with this library; if not, see <http://www.gnu.org/licenses/>.
019    */
020    
021    package org.granite.generator.as3;
022    
023    import java.lang.annotation.Annotation;
024    import java.lang.reflect.Field;
025    import java.lang.reflect.Method;
026    import java.net.URL;
027    
028    import javax.jdo.annotations.PersistenceCapable;
029    import javax.jdo.annotations.Persistent;
030    import javax.jdo.annotations.PrimaryKey;
031    import javax.persistence.EmbeddedId;
032    import javax.persistence.Entity;
033    import javax.persistence.FetchType;
034    import javax.persistence.Id;
035    import javax.persistence.ManyToMany;
036    import javax.persistence.ManyToOne;
037    import javax.persistence.MappedSuperclass;
038    import javax.persistence.OneToMany;
039    import javax.persistence.OneToOne;
040    import javax.persistence.Version;
041    
042    import org.granite.generator.as3.reflect.JavaBean;
043    import org.granite.generator.as3.reflect.JavaEntityBean;
044    import org.granite.generator.as3.reflect.JavaFieldProperty;
045    import org.granite.generator.as3.reflect.JavaProperty;
046    import org.granite.generator.as3.reflect.JavaType;
047    import org.granite.generator.as3.reflect.JavaTypeFactory;
048    
049    
050    public class DefaultEntityFactory implements EntityFactory {
051    
052            @Override
053            public boolean isEntity(Class<?> clazz) {
054                    return clazz.isAnnotationPresent(Entity.class) ||
055                    clazz.isAnnotationPresent(MappedSuperclass.class) ||
056                    clazz.isAnnotationPresent(PersistenceCapable.class);
057            }
058            
059            @Override
060            public JavaType newBean(JavaTypeFactory provider, Class<?> type, URL url) {
061                    return new JavaBean(provider, type, url);
062            }
063            
064            @Override
065            public JavaType newEntity(JavaTypeFactory provider, Class<?> type, URL url) {
066                    return new JavaEntityBean(provider, type, url);
067            }
068            
069            @Override
070            public boolean isId(JavaFieldProperty fieldProperty) {
071            Field field = fieldProperty.getMember();
072            Method getter = (fieldProperty.getReadMethod() != null ? fieldProperty.getReadMethod().getMember() : null);
073            Method setter = (fieldProperty.getWriteMethod() != null ? fieldProperty.getWriteMethod().getMember() : null);
074    
075            if (field.isAnnotationPresent(Persistent.class)) {
076                    Annotation annotation = field.getAnnotation(Persistent.class);
077                    if (annotation instanceof Persistent) {
078                            Persistent persistAnnotation = (Persistent)annotation;
079                            String pk = persistAnnotation.primaryKey();
080                            if (pk != null && pk.toLowerCase().equals("true"))
081                                    return true;
082                    }
083                    if (field.isAnnotationPresent(PrimaryKey.class))
084                            return true;
085            }
086            
087            return
088                (field.isAnnotationPresent(Id.class) || field.isAnnotationPresent(EmbeddedId.class)) ||
089                (getter != null && (getter.isAnnotationPresent(Id.class) || getter.isAnnotationPresent(EmbeddedId.class))) ||
090                (setter != null && (setter.isAnnotationPresent(Id.class) || setter.isAnnotationPresent(EmbeddedId.class)));
091            }
092            
093            @Override
094            public boolean isVersion(JavaProperty property) {
095                    if (property.isAnnotationPresent(Version.class))
096                            return true;
097                    
098                if (property.getType().isAnnotationPresent(javax.jdo.annotations.Version.class)) {
099                    // Get JDO version field using specific DataNucleus extension
100                    javax.jdo.annotations.Version versionAnnotation = property.getType().getAnnotation(javax.jdo.annotations.Version.class);
101                    javax.jdo.annotations.Extension[] extensions = versionAnnotation.extensions();
102                    for (javax.jdo.annotations.Extension extension : extensions) {
103                            if ("datanucleus".equals(extension.vendorName()) && "field-name".equals(extension.key()))
104                                    return property.getName().equals(extension.value());
105                    }
106                }
107                return false;
108            }
109            
110            @Override
111            public boolean isLazy(JavaProperty property) {
112                    if (property.isAnnotationPresent(ManyToOne.class)) {
113                            ManyToOne manyToOne = property.getAnnotation(ManyToOne.class);
114                            return manyToOne.fetch().equals(FetchType.LAZY);
115                    }
116                    else if (property.isAnnotationPresent(OneToOne.class)) {
117                            OneToOne oneToOne = property.getAnnotation(OneToOne.class);
118                            return oneToOne.fetch().equals(FetchType.LAZY);
119                    }
120                    else if (property.isAnnotationPresent(OneToMany.class)) {
121                            OneToMany oneToMany = property.getAnnotation(OneToMany.class);
122                            return oneToMany.fetch().equals(FetchType.LAZY);
123                    }
124                    else if (property.isAnnotationPresent(ManyToMany.class)) {
125                            ManyToMany manyToMany = property.getAnnotation(ManyToMany.class);
126                            return manyToMany.fetch().equals(FetchType.LAZY);
127                    }
128                    return false;
129            }
130    }