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
021package org.granite.generator.as3.reflect;
022
023import java.net.URL;
024import java.util.ArrayList;
025import java.util.Collections;
026import java.util.List;
027
028import javax.persistence.IdClass;
029
030/**
031 * @author Franck WOLFF
032 */
033public class JavaEntityBean extends JavaBean {
034
035    protected final List<JavaFieldProperty> identifiers;
036    protected final JavaType idClass;
037    protected JavaProperty 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 && provider.isId((JavaFieldProperty)property))
046                tmpIdentifiers.add((JavaFieldProperty)property);
047            else if (provider.isVersion(property))
048                version = property;
049        }
050        this.identifiers = (tmpIdentifiers.isEmpty() ? null : Collections.unmodifiableList(tmpIdentifiers));
051
052        // Find IdClass (if any).
053        this.idClass = (
054            type.isAnnotationPresent(IdClass.class) ?
055            provider.getJavaType(type.getAnnotation(IdClass.class).value()) :
056            null
057        );
058
059        // Collect additional imports.
060        if (idClass != null)
061            addToImports(provider.getJavaImport(idClass.getType()));
062    }
063
064    public boolean hasIdentifiers() {
065        return identifiers != null && !identifiers.isEmpty();
066    }
067    public List<JavaFieldProperty> getIdentifiers() {
068        return identifiers;
069    }
070    public JavaFieldProperty getFirstIdentifier() {
071        return (identifiers != null ? identifiers.get(0) : null);
072    }
073
074    public boolean hasIdClass() {
075        return idClass != null;
076    }
077    public JavaType getIdClass() {
078        return idClass;
079    }
080    
081    public boolean hasVersion() {
082        return version != null;
083    }
084    public JavaProperty getVersion() {
085        return version;
086    }
087}