package org.hibernate.jpa.criteria.path;
import java.io.Serializable;
import java.util.Map;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.JoinType;
import javax.persistence.criteria.Path;
import javax.persistence.criteria.Predicate;
import javax.persistence.metamodel.MapAttribute;
import org.hibernate.jpa.criteria.CriteriaBuilderImpl;
import org.hibernate.jpa.criteria.CriteriaSubqueryImpl;
import org.hibernate.jpa.criteria.FromImplementor;
import org.hibernate.jpa.criteria.MapJoinImplementor;
import org.hibernate.jpa.criteria.PathImplementor;
import org.hibernate.jpa.criteria.PathSource;
import org.hibernate.jpa.criteria.compile.RenderingContext;
import org.hibernate.jpa.criteria.expression.MapEntryExpression;
public class MapAttributeJoin<O,K,V>
extends PluralAttributeJoinSupport<O, Map<K,V>, V>
implements MapJoinImplementor<O,K,V>, Serializable {
public MapAttributeJoin(
CriteriaBuilderImpl criteriaBuilder,
Class<V> javaType,
PathSource<O> pathSource,
MapAttribute<? super O, K, V> joinAttribute,
JoinType joinType) {
super( criteriaBuilder, javaType, pathSource, joinAttribute, joinType );
}
@Override
public MapAttribute<? super O, K, V> getAttribute() {
return (MapAttribute<? super O, K, V>) super.getAttribute();
}
@Override
public MapAttribute<? super O, K, V> getModel() {
return getAttribute();
}
@Override
public final MapAttributeJoin<O,K,V> correlateTo(CriteriaSubqueryImpl subquery) {
return (MapAttributeJoin<O,K,V>) super.correlateTo( subquery );
}
@Override
protected FromImplementor<O, V> createCorrelationDelegate() {
return new MapAttributeJoin<O,K,V>(
criteriaBuilder(),
getJavaType(),
(PathImplementor<O>) getParentPath(),
getAttribute(),
getJoinType()
);
}
@Override
public Path<V> value() {
return this;
}
@Override
@SuppressWarnings({ "unchecked" })
public Expression<Map.Entry<K, V>> entry() {
return new MapEntryExpression( criteriaBuilder(), Map.Entry.class, this, getAttribute() );
}
@Override
@SuppressWarnings({ "unchecked" })
public Path<K> key() {
final MapKeyHelpers.MapKeySource<K,V> mapKeySource = new MapKeyHelpers.MapKeySource<K,V>(
criteriaBuilder(),
getAttribute().getJavaType(),
this,
getAttribute()
);
final MapKeyHelpers.MapKeyAttribute mapKeyAttribute = new MapKeyHelpers.MapKeyAttribute( criteriaBuilder(), getAttribute() );
return new MapKeyHelpers.MapKeyPath( criteriaBuilder(), mapKeySource, mapKeyAttribute );
}
@Override
public MapJoinImplementor<O, K, V> on(Predicate... restrictions) {
return (MapJoinImplementor<O, K, V>) super.on( restrictions );
}
@Override
public MapJoinImplementor<O, K, V> on(Expression<Boolean> restriction) {
return (MapJoinImplementor<O, K, V>) super.on( restriction );
}
@Override
public <T extends V> MapAttributeJoin<O, K, T> treatAs(Class<T> treatAsType) {
return new TreatedMapAttributeJoin<O,K,T>( this, treatAsType );
}
public static class TreatedMapAttributeJoin<O, K, T> extends MapAttributeJoin<O, K, T> {
private final MapAttributeJoin<O, K, ? super T> original;
protected final Class<T> treatAsType;
@SuppressWarnings("unchecked")
public TreatedMapAttributeJoin(MapAttributeJoin<O, K, ? super T> original, Class<T> treatAsType) {
super(
original.criteriaBuilder(),
treatAsType,
original.getPathSource(),
(MapAttribute<? super O, K, T>) original.getAttribute(),
original.getJoinType()
);
this.original = original;
this.treatAsType = treatAsType;
}
@Override
public String getAlias() {
return original.getAlias();
}
@Override
public void prepareAlias(RenderingContext renderingContext) {
}
@Override
public String render(RenderingContext renderingContext) {
return "treat(" + original.render( renderingContext ) + " as " + treatAsType.getName() + ")";
}
@Override
public String getPathIdentifier() {
return "treat(" + getAlias() + " as " + treatAsType.getName() + ")";
}
@Override
protected PathSource getPathSourceForSubPaths() {
return this;
}
}
}