package org.hibernate.mapping;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class MappedSuperclass {
private final MappedSuperclass superMappedSuperclass;
private final PersistentClass superPersistentClass;
private final List declaredProperties;
private Class mappedClass;
private Property identifierProperty;
private Property version;
private Component identifierMapper;
public MappedSuperclass(MappedSuperclass superMappedSuperclass, PersistentClass superPersistentClass) {
this.superMappedSuperclass = superMappedSuperclass;
this.superPersistentClass = superPersistentClass;
this.declaredProperties = new ArrayList();
}
public MappedSuperclass getSuperMappedSuperclass() {
return superMappedSuperclass;
}
public boolean hasIdentifierProperty() {
return getIdentifierProperty() != null;
}
public boolean isVersioned() {
return getVersion() != null;
}
public PersistentClass getSuperPersistentClass() {
return superPersistentClass;
}
public Iterator getDeclaredPropertyIterator() {
return declaredProperties.iterator();
}
public void addDeclaredProperty(Property p) {
String name = p.getName();
Iterator it = declaredProperties.iterator();
while (it.hasNext()) {
if ( name.equals( ((Property)it.next()).getName() ) ) {
return;
}
}
declaredProperties.add(p);
}
public Class getMappedClass() {
return mappedClass;
}
public void setMappedClass(Class mappedClass) {
this.mappedClass = mappedClass;
}
public Property getIdentifierProperty() {
Property propagatedIdentifierProp = identifierProperty;
if ( propagatedIdentifierProp == null ) {
if ( superMappedSuperclass != null ) {
propagatedIdentifierProp = superMappedSuperclass.getIdentifierProperty();
}
if (propagatedIdentifierProp == null && superPersistentClass != null){
propagatedIdentifierProp = superPersistentClass.getIdentifierProperty();
}
}
return propagatedIdentifierProp;
}
public Property getDeclaredIdentifierProperty() {
return identifierProperty;
}
public void setDeclaredIdentifierProperty(Property prop) {
this.identifierProperty = prop;
}
public Property getVersion() {
Property propagatedVersion = version;
if (propagatedVersion == null) {
if ( superMappedSuperclass != null ) {
propagatedVersion = superMappedSuperclass.getVersion();
}
if (propagatedVersion == null && superPersistentClass != null){
propagatedVersion = superPersistentClass.getVersion();
}
}
return propagatedVersion;
}
public Property getDeclaredVersion() {
return version;
}
public void setDeclaredVersion(Property prop) {
this.version = prop;
}
public Component getIdentifierMapper() {
Component propagatedMapper = identifierMapper;
if ( propagatedMapper == null ) {
if ( superMappedSuperclass != null ) {
propagatedMapper = superMappedSuperclass.getIdentifierMapper();
}
if (propagatedMapper == null && superPersistentClass != null){
propagatedMapper = superPersistentClass.getIdentifierMapper();
}
}
return propagatedMapper;
}
public Component getDeclaredIdentifierMapper() {
return identifierMapper;
}
public void setDeclaredIdentifierMapper(Component identifierMapper) {
this.identifierMapper = identifierMapper;
}
@SuppressWarnings("WeakerAccess")
public boolean hasProperty(String name) {
final Iterator itr = getDeclaredPropertyIterator();
while ( itr.hasNext() ) {
final Property property = (Property) itr.next();
if ( property.getName().equals( name ) ) {
return true;
}
}
return false;
}
@SuppressWarnings({"WeakerAccess", "RedundantIfStatement"})
public boolean isPropertyDefinedInHierarchy(String name) {
if ( hasProperty( name ) ) {
return true;
}
if ( getSuperMappedSuperclass() != null
&& getSuperMappedSuperclass().isPropertyDefinedInHierarchy( name ) ) {
return true;
}
if ( getSuperPersistentClass() != null
&& getSuperPersistentClass().isPropertyDefinedInHierarchy( name ) ) {
return true;
}
return false;
}
}