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