001    /*
002      GRANITE DATA SERVICES
003      Copyright (C) 2007-2010 ADEQUATE SYSTEMS SARL
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.Id;
034    import javax.persistence.MappedSuperclass;
035    import javax.persistence.Version;
036    
037    import org.granite.generator.as3.reflect.JavaEntityBean;
038    import org.granite.generator.as3.reflect.JavaFieldProperty;
039    import org.granite.generator.as3.reflect.JavaProperty;
040    import org.granite.generator.as3.reflect.JavaType;
041    import org.granite.generator.as3.reflect.JavaTypeFactory;
042    
043    
044    public class DefaultEntityFactory implements EntityFactory {
045    
046            public boolean isEntity(Class<?> clazz) {
047                    return clazz.isAnnotationPresent(Entity.class) ||
048                    clazz.isAnnotationPresent(MappedSuperclass.class) ||
049                    clazz.isAnnotationPresent(PersistenceCapable.class);
050            }
051            
052            public JavaType newEntity(JavaTypeFactory provider, Class<?> type, URL url) {
053                    return new JavaEntityBean(provider, type, url);
054            }
055            
056            public boolean isId(JavaFieldProperty fieldProperty) {
057            Field field = fieldProperty.getMember();
058            Method getter = (fieldProperty.getReadMethod() != null ? fieldProperty.getReadMethod().getMember() : null);
059            Method setter = (fieldProperty.getWriteMethod() != null ? fieldProperty.getWriteMethod().getMember() : null);
060    
061            if (field.isAnnotationPresent(Persistent.class)) {
062                    Annotation annotation = field.getAnnotation(Persistent.class);
063                    if (annotation instanceof Persistent) {
064                            Persistent persistAnnotation = (Persistent)annotation;
065                            String pk = persistAnnotation.primaryKey();
066                            if (pk != null && pk.toLowerCase().equals("true"))
067                                    return true;
068                    }
069                    if (field.isAnnotationPresent(PrimaryKey.class))
070                            return true;
071            }
072            
073            return
074                (field.isAnnotationPresent(Id.class) || field.isAnnotationPresent(EmbeddedId.class)) ||
075                (getter != null && (getter.isAnnotationPresent(Id.class) || getter.isAnnotationPresent(EmbeddedId.class))) ||
076                (setter != null && (setter.isAnnotationPresent(Id.class) || setter.isAnnotationPresent(EmbeddedId.class)));
077            }
078            
079            public boolean isVersion(JavaProperty property) {
080                    if (property.isAnnotationPresent(Version.class))
081                            return true;
082                    
083                if (property.getType().isAnnotationPresent(javax.jdo.annotations.Version.class)) {
084                    // Get JDO version field using specific DataNucleus extension
085                    javax.jdo.annotations.Version versionAnnotation = property.getType().getAnnotation(javax.jdo.annotations.Version.class);
086                    javax.jdo.annotations.Extension[] extensions = versionAnnotation.extensions();
087                    for (javax.jdo.annotations.Extension extension : extensions) {
088                            if ("datanucleus".equals(extension.vendorName()) && "field-name".equals(extension.key()))
089                                    return property.getName().equals(extension.value());
090                    }
091                }
092                return false;
093            }
094    }