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.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 JavaFieldProperty version;
038        
039        public JavaEntityBean(JavaTypeFactory provider, Class<?> type, URL url) {
040            super(provider, type, url);
041    
042            // Find identifiers.
043            List<JavaFieldProperty> tmpIdentifiers = new ArrayList<JavaFieldProperty>();
044            for (JavaProperty property : properties.values()) {
045                if (property instanceof JavaFieldProperty) {
046                    JavaFieldProperty fieldProperty = (JavaFieldProperty)property;
047                    if (provider.isId(fieldProperty))
048                        tmpIdentifiers.add(fieldProperty);
049                    else if (provider.isVersion(fieldProperty))
050                            version = fieldProperty;
051                }
052            }
053            this.identifiers = (tmpIdentifiers.isEmpty() ? null : Collections.unmodifiableList(tmpIdentifiers));
054    
055            // Find IdClass (if any).
056            this.idClass = (
057                type.isAnnotationPresent(IdClass.class) ?
058                provider.getJavaType(type.getAnnotation(IdClass.class).value()) :
059                null
060            );
061    
062            // Collect additional imports.
063            if (idClass != null)
064                addToImports(provider.getJavaImport(idClass.getType()));
065        }
066    
067        public boolean hasIdentifiers() {
068            return identifiers != null && !identifiers.isEmpty();
069        }
070        public List<JavaFieldProperty> getIdentifiers() {
071            return identifiers;
072        }
073        public JavaFieldProperty getFirstIdentifier() {
074            return (identifiers != null ? identifiers.get(0) : null);
075        }
076    
077        public boolean hasIdClass() {
078            return idClass != null;
079        }
080        public JavaType getIdClass() {
081            return idClass;
082        }
083        
084        public boolean hasVersion() {
085            return version != null;
086        }
087        public JavaFieldProperty getVersion() {
088            return version;
089        }
090    }