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.reflect;
022
023 import java.net.URL;
024 import java.util.ArrayList;
025 import java.util.Collections;
026 import java.util.List;
027
028 import javax.persistence.IdClass;
029
030 /**
031 * @author Franck WOLFF
032 */
033 public class JavaEntityBean extends JavaBean {
034
035 protected final List<JavaFieldProperty> identifiers;
036 protected final JavaType idClass;
037 protected JavaProperty version;
038 protected final List<JavaProperty> lazyProperties;
039
040 public JavaEntityBean(JavaTypeFactory provider, Class<?> type, URL url) {
041 super(provider, type, url);
042
043 // Find identifiers.
044 List<JavaFieldProperty> tmpIdentifiers = new ArrayList<JavaFieldProperty>();
045 List<JavaProperty> tmpLazyProperties = new ArrayList<JavaProperty>();
046 for (JavaProperty property : properties.values()) {
047 if (property instanceof JavaFieldProperty && provider.isId((JavaFieldProperty)property))
048 tmpIdentifiers.add((JavaFieldProperty)property);
049 else if (provider.isVersion(property))
050 version = property;
051 else if (provider.isLazy(property))
052 tmpLazyProperties.add(property);
053 }
054 this.identifiers = (tmpIdentifiers.isEmpty() ? null : Collections.unmodifiableList(tmpIdentifiers));
055 this.lazyProperties = (tmpLazyProperties.isEmpty() ? null : Collections.unmodifiableList(tmpLazyProperties));
056
057 // Find IdClass (if any).
058 this.idClass = (
059 type.isAnnotationPresent(IdClass.class) ?
060 provider.getJavaType(type.getAnnotation(IdClass.class).value()) :
061 null
062 );
063
064 // Collect additional imports.
065 if (idClass != null)
066 addToImports(provider.getJavaImport(idClass.getType()));
067 }
068
069 public boolean hasIdentifiers() {
070 return identifiers != null && !identifiers.isEmpty();
071 }
072 public List<JavaFieldProperty> getIdentifiers() {
073 return identifiers;
074 }
075 public JavaFieldProperty getFirstIdentifier() {
076 return (identifiers != null ? identifiers.get(0) : null);
077 }
078
079 public boolean hasIdClass() {
080 return idClass != null;
081 }
082 public JavaType getIdClass() {
083 return idClass;
084 }
085
086 public boolean hasVersion() {
087 return version != null;
088 }
089 public JavaProperty getVersion() {
090 return version;
091 }
092
093 public boolean isLazy(JavaProperty property) {
094 return lazyProperties != null && lazyProperties.contains(property);
095 }
096 }