package org.springframework.data.jpa.mapping;
import java.lang.annotation.Annotation;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.*;
import javax.persistence.metamodel.Metamodel;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.data.annotation.AccessType.Type;
import org.springframework.data.jpa.util.JpaMetamodel;
import org.springframework.data.mapping.Association;
import org.springframework.data.mapping.PersistentEntity;
import org.springframework.data.mapping.model.AnnotationBasedPersistentProperty;
import org.springframework.data.mapping.model.Property;
import org.springframework.data.mapping.model.SimpleTypeHolder;
import org.springframework.data.util.ClassTypeInformation;
import org.springframework.data.util.Lazy;
import org.springframework.data.util.TypeInformation;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
class JpaPersistentPropertyImpl extends AnnotationBasedPersistentProperty<JpaPersistentProperty>
implements JpaPersistentProperty {
private static final Collection<Class<? extends Annotation>> ASSOCIATION_ANNOTATIONS;
private static final Collection<Class<? extends Annotation>> ID_ANNOTATIONS;
private static final Collection<Class<? extends Annotation>> UPDATEABLE_ANNOTATIONS;
static {
Set<Class<? extends Annotation>> annotations = new HashSet<Class<? extends Annotation>>();
annotations.add(OneToMany.class);
annotations.add(OneToOne.class);
annotations.add(ManyToMany.class);
annotations.add(ManyToOne.class);
ASSOCIATION_ANNOTATIONS = Collections.unmodifiableSet(annotations);
annotations = new HashSet<Class<? extends Annotation>>();
annotations.add(Id.class);
annotations.add(EmbeddedId.class);
ID_ANNOTATIONS = Collections.unmodifiableSet(annotations);
annotations = new HashSet<Class<? extends Annotation>>();
annotations.add(Column.class);
annotations.add(OrderColumn.class);
UPDATEABLE_ANNOTATIONS = Collections.unmodifiableSet(annotations);
}
private final @Nullable Boolean usePropertyAccess;
private final @Nullable TypeInformation<?> associationTargetType;
private final boolean updateable;
private final Lazy<Boolean> isIdProperty;
private final Lazy<Boolean> isAssociation;
private final Lazy<Boolean> isEntity;
public JpaPersistentPropertyImpl(JpaMetamodel metamodel, Property property,
PersistentEntity<?, JpaPersistentProperty> owner, SimpleTypeHolder simpleTypeHolder) {
super(property, owner, simpleTypeHolder);
Assert.notNull(metamodel, "Metamodel must not be null!");
this.isAssociation = Lazy.of(() -> ASSOCIATION_ANNOTATIONS.stream().anyMatch(this::isAnnotationPresent));
this.usePropertyAccess = detectPropertyAccess();
this.associationTargetType = detectAssociationTargetType();
this.updateable = detectUpdatability();
this.isIdProperty = Lazy.of(() -> ID_ANNOTATIONS.stream().anyMatch(it -> isAnnotationPresent(it))
|| metamodel.isSingleIdAttribute(getOwner().getType(), getName(), getType()));
this.isEntity = Lazy.of(() -> metamodel.isJpaManaged(getActualType()));
}
@Override
public Class<?> getActualType() {
return associationTargetType != null ? associationTargetType.getType() : super.getActualType();
}
@Override
public Iterable<? extends TypeInformation<?>> getPersistentEntityTypes() {
return associationTargetType != null
? Collections.singleton(associationTargetType)
: super.getPersistentEntityTypes();
}
@Override
public boolean isIdProperty() {
return isIdProperty.get();
}
@Override
public boolean isEntity() {
return isEntity.get();
}
@Override
public boolean isAssociation() {
return isAssociation.get();
}
@Override
public boolean isTransient() {
return isAnnotationPresent(Transient.class) || super.isTransient();
}
@Override
protected Association<JpaPersistentProperty> createAssociation() {
return new Association<JpaPersistentProperty>(this, null);
}
@Override
public boolean usePropertyAccess() {
return usePropertyAccess != null ? usePropertyAccess : super.usePropertyAccess();
}
@Override
public boolean isVersionProperty() {
return isAnnotationPresent(Version.class);
}
@Override
public boolean isWritable() {
return updateable && super.isWritable();
}
@Override
public boolean isEmbeddable() {
return isAnnotationPresent(Embedded.class) || hasActualTypeAnnotation(Embeddable.class);
}
@Nullable
private Boolean detectPropertyAccess() {
org.springframework.data.annotation.AccessType accessType = findAnnotation(
org.springframework.data.annotation.AccessType.class);
if (accessType != null) {
return Type.PROPERTY.equals(accessType.value());
}
Access access = findAnnotation(Access.class);
if (access != null) {
return AccessType.PROPERTY.equals(access.value());
}
accessType = findPropertyOrOwnerAnnotation(org.springframework.data.annotation.AccessType.class);
if (accessType != null) {
return Type.PROPERTY.equals(accessType.value());
}
access = findPropertyOrOwnerAnnotation(Access.class);
if (access != null) {
return AccessType.PROPERTY.equals(access.value());
}
return null;
}
@Nullable
private TypeInformation<?> detectAssociationTargetType() {
if (!isAssociation()) {
return null;
}
for (Class<? extends Annotation> annotationType : ASSOCIATION_ANNOTATIONS) {
Annotation annotation = findAnnotation(annotationType);
if (annotation == null) {
continue;
}
Object entityValue = AnnotationUtils.getValue(annotation, "targetEntity");
if (entityValue == null || entityValue.equals(void.class)) {
continue;
}
return ClassTypeInformation.from((Class<?>) entityValue);
}
return null;
}
private boolean detectUpdatability() {
for (Class<? extends Annotation> annotationType : UPDATEABLE_ANNOTATIONS) {
Annotation annotation = findAnnotation(annotationType);
if (annotation == null) {
continue;
}
return (boolean) AnnotationUtils.getValue(annotation, "updatable");
}
return true;
}
}