001    /**
002     *   GRANITE DATA SERVICES
003     *   Copyright (C) 2006-2013 GRANITE DATA SERVICES S.A.S.
004     *
005     *   This file is part of the Granite Data Services Platform.
006     *
007     *   Granite Data Services is free software; you can redistribute it and/or
008     *   modify it under the terms of the GNU Lesser General Public
009     *   License as published by the Free Software Foundation; either
010     *   version 2.1 of the License, or (at your option) any later version.
011     *
012     *   Granite Data Services is distributed in the hope that it will be useful,
013     *   but WITHOUT ANY WARRANTY; without even the implied warranty of
014     *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
015     *   General Public License for more details.
016     *
017     *   You should have received a copy of the GNU Lesser General Public
018     *   License along with this library; if not, write to the Free Software
019     *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
020     *   USA, or see <http://www.gnu.org/licenses/>.
021     */
022    
023    package org.granite.generator.as3.reflect;
024    
025    import java.net.URL;
026    import java.util.ArrayList;
027    import java.util.Collections;
028    import java.util.List;
029    
030    import javax.persistence.IdClass;
031    
032    /**
033     * @author Franck WOLFF
034     */
035    public class JavaEntityBean extends JavaBean {
036    
037        protected final List<JavaFieldProperty> identifiers;
038        protected final JavaType idClass;
039        protected JavaProperty version;
040        
041        public JavaEntityBean(JavaTypeFactory provider, Class<?> type, URL url) {
042            super(provider, type, url);
043    
044            // Find identifiers.
045            List<JavaFieldProperty> tmpIdentifiers = new ArrayList<JavaFieldProperty>();
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            }
052            this.identifiers = (tmpIdentifiers.isEmpty() ? null : Collections.unmodifiableList(tmpIdentifiers));
053    
054            // Find IdClass (if any).
055            this.idClass = (
056                type.isAnnotationPresent(IdClass.class) ?
057                provider.getJavaType(type.getAnnotation(IdClass.class).value()) :
058                null
059            );
060    
061            // Collect additional imports.
062            if (idClass != null)
063                addToImports(provider.getJavaImport(idClass.getType()));
064        }
065    
066        public boolean hasIdentifiers() {
067            return identifiers != null && !identifiers.isEmpty();
068        }
069        public List<JavaFieldProperty> getIdentifiers() {
070            return identifiers;
071        }
072        public JavaFieldProperty getFirstIdentifier() {
073            return (identifiers != null ? identifiers.get(0) : null);
074        }
075    
076        public boolean hasIdClass() {
077            return idClass != null;
078        }
079        public JavaType getIdClass() {
080            return idClass;
081        }
082        
083        public boolean hasVersion() {
084            return version != null;
085        }
086        public JavaProperty getVersion() {
087            return version;
088        }
089    }