/**********************************************************************
Copyright (c) 2009 Andy Jefferson and others. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Contributors:
...
**********************************************************************/
package org.datanucleus.api.jpa.metamodel;
import java.lang.reflect.Member;
import java.util.Collection;
import javax.persistence.metamodel.Attribute;
import javax.persistence.metamodel.ManagedType;
import javax.persistence.metamodel.Type;
import org.datanucleus.ClassLoaderResolver;
import org.datanucleus.metadata.AbstractMemberMetaData;
import org.datanucleus.metadata.Relation;
/**
* Implementation of JPA2 Metamodel "Attribute".
* Provides a wrapper to AbstractMemberMetaData.
*
* @param <X> The type containing the represented attribute
* @param <Y> The type of the represented attribute
*/
public class AttributeImpl<X, Y> implements Attribute<X, Y>
{
AbstractMemberMetaData mmd;
ManagedTypeImpl<X> owner;
public AttributeImpl(AbstractMemberMetaData mmd, ManagedTypeImpl<X> owner)
{
this.mmd = mmd;
this.owner = owner;
}
/**
* Convenience accessor for the metadata underlying this member.
* @return The metadata
*/
public AbstractMemberMetaData getMetadata()
{
return mmd;
}
/* (non-Javadoc)
* @see javax.persistence.metamodel.Attribute#getDeclaringType()
*/
public ManagedType<X> getDeclaringType()
{
Class ownerCls = owner.getModel().getClassLoaderResolver().classForName(mmd.getClassName(true));
return owner.getModel().managedType(ownerCls);
}
/* (non-Javadoc)
* @see javax.persistence.metamodel.Attribute#getJavaMember()
*/
public Member getJavaMember()
{
return mmd.getMemberRepresented();
}
/* (non-Javadoc)
* @see javax.persistence.metamodel.Attribute#getJavaType()
*/
public Class<Y> getJavaType()
{
return mmd.getType();
}
/* (non-Javadoc)
* @see javax.persistence.metamodel.Attribute#getName()
*/
public String getName()
{
return mmd.getName();
}
/* (non-Javadoc)
* @see javax.persistence.metamodel.Attribute#getPersistentAttributeType()
*/
public javax.persistence.metamodel.Attribute.PersistentAttributeType getPersistentAttributeType()
{
int relationType = mmd.getRelationType(owner.getModel().getClassLoaderResolver());
switch (relationType)
{
case Relation.MANY_TO_ONE_BI :
return PersistentAttributeType.MANY_TO_ONE;
case Relation.ONE_TO_ONE_BI :
case Relation.ONE_TO_ONE_UNI :
return PersistentAttributeType.ONE_TO_ONE;
case Relation.ONE_TO_MANY_BI :
case Relation.ONE_TO_MANY_UNI :
return PersistentAttributeType.ONE_TO_MANY;
case Relation.MANY_TO_MANY_BI :
return PersistentAttributeType.MANY_TO_MANY;
default :
if (Collection.class.isAssignableFrom(mmd.getType()))
{
return PersistentAttributeType.ELEMENT_COLLECTION;
}
// TODO Detect embedded
return PersistentAttributeType.BASIC;
}
}
/* (non-Javadoc)
* @see javax.persistence.metamodel.Attribute#isAssociation()
*/
public boolean isAssociation()
{
int relationType = mmd.getRelationType(owner.getModel().getClassLoaderResolver());
return relationType != Relation.NONE;
}
/* (non-Javadoc)
* @see javax.persistence.metamodel.Attribute#isCollection()
*/
public boolean isCollection()
{
return Collection.class.isAssignableFrom(mmd.getType());
}
/**
* Return the type of the attribute.
* If the type is simple then returns that java type, otherwise if a collection then returns the
* element type.
* @return The type of attribute
*/
public final Type<Y> getType()
{
ClassLoaderResolver clr = owner.getModel().getClassLoaderResolver();
if (Collection.class.isAssignableFrom(mmd.getType()))
{
String elementTypeName = mmd.getCollection().getElementType();
return owner.model.getType(clr.classForName(elementTypeName));
}
else if (mmd.getType().isArray())
{
return owner.model.managedType(mmd.getType().getComponentType());
}
else
{
return owner.model.getType(mmd.getType());
}
}
}