All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jvnet.hyperjaxb3.ejb.strategy.mapping.EmbeddableAttributesMapping Maven / Gradle / Ivy

There is a newer version: 0.6.2
Show newest version
package org.jvnet.hyperjaxb3.ejb.strategy.mapping;

import java.util.Collection;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jvnet.hyperjaxb3.codemodel.util.JTypeUtils;
import org.jvnet.hyperjaxb3.ejb.schemas.customizations.Customizations;
import org.jvnet.jaxb2_commons.util.CustomizationUtils;
import org.jvnet.jaxb2_commons.util.FieldAccessorUtils;

import com.sun.codemodel.JMethod;
import com.sun.codemodel.JType;
import com.sun.java.xml.ns.persistence.orm.Basic;
import com.sun.java.xml.ns.persistence.orm.EmbeddableAttributes;
import com.sun.java.xml.ns.persistence.orm.Transient;
import com.sun.tools.xjc.Options;
import com.sun.tools.xjc.model.CClass;
import com.sun.tools.xjc.model.CEnumLeafInfo;
import com.sun.tools.xjc.model.CPropertyInfo;
import com.sun.tools.xjc.model.CTypeInfo;
import com.sun.tools.xjc.outline.ClassOutline;
import com.sun.tools.xjc.outline.FieldOutline;

public class EmbeddableAttributesMapping implements
		ClassOutlineMapping {

	protected Log logger = LogFactory.getLog(getClass());

	public EmbeddableAttributes process(Mapping context,
			ClassOutline classOutline, Options options) {

		final EmbeddableAttributes attributes = new EmbeddableAttributes();

		final FieldOutline[] fieldOutlines = classOutline.getDeclaredFields();
		for (final FieldOutline fieldOutline : fieldOutlines) {

			final Object attributeMapping = getAttributeMapping(context,
					fieldOutline, options).process(context, fieldOutline,
					options);

			if (attributeMapping instanceof Basic) {
				attributes.getBasic().add((Basic) attributeMapping);
			} else if (attributeMapping instanceof Transient) {
				attributes.getTransient().add((Transient) attributeMapping);
			}
		}
		return attributes;
	}

	public FieldOutlineMapping getAttributeMapping(Mapping context,
			FieldOutline fieldOutline, Options options) {
		if (context.getIgnoring().isFieldOutlineIgnored(fieldOutline)) {
			return context.getTransientMapping();
		} else if (isFieldOutlineId(fieldOutline)) {
			final CPropertyInfo propertyInfo = fieldOutline.getPropertyInfo();
			logger.warn("Field outline  [" + propertyInfo.getName(true)
					+ "] is marked as [id] field. "
					+ "This is not supported in embeddable classes. "
					+ "This field will be made transient.");

			return context.getTransientMapping();
		} else if (isFieldOutlineEmbeddedId(fieldOutline)) {
			final CPropertyInfo propertyInfo = fieldOutline.getPropertyInfo();
			logger.warn("Field outline  [" + propertyInfo.getName(true)
					+ "] is marked as [embedded-id] field. "
					+ "This is not supported in embeddable classes. "
					+ "This field will be made transient.");

			return context.getTransientMapping();
		} else if (isFieldOutlineVersion(fieldOutline)) {
			final CPropertyInfo propertyInfo = fieldOutline.getPropertyInfo();
			logger.warn("Field outline  [" + propertyInfo.getName(true)
					+ "] is marked as [version] field. "
					+ "This is not supported in embeddable classes. "
					+ "This field will be made transient.");
			return context.getTransientMapping();
		} else {

			final CPropertyInfo propertyInfo = fieldOutline.getPropertyInfo();
			if (!propertyInfo.isCollection()) {
				logger.trace("Field outline  [" + propertyInfo.getName(true)
						+ "] is a single field.");

				final Collection types = propertyInfo
						.ref();

				if (types.size() == 1) {
					logger.trace("Field outline  ["
							+ propertyInfo.getName(true)
							+ "] is a homogeneous single field.");

					if (isFieldOutlineBasic(fieldOutline)) {
						return context.getBasicMapping();
					} else

					if (isFieldOutlineComplex(fieldOutline)) {
						logger.warn("Field outline  ["
								+ propertyInfo.getName(true)
								+ "] is a complex field. "
								+ "This is not supported in embeddable classes. "
								+ "This field will be made transient.");

						return context.getTransientMapping();
					} else {
						logger.warn("Field outline  ["
								+ propertyInfo.getName(true)
								+ "] is not a basic field. "
								+ "This is not supported in embeddable classes. "
								+ "This field will be made transient.");

						return context.getTransientMapping();

					}
				} else {
					logger.warn("Field outline  [" + propertyInfo.getName(true)
							+ "] is a heterogeneous field. "
							+ "This is not supported in embeddable classes. "
							+ "This field will be made transient.");

					return context.getTransientMapping();
				}
			} else {
				logger.warn("Field outline  [" + propertyInfo.getName(true)
						+ "] is a collection field. "
						+ "This is not supported in embeddable classes. "
						+ "This field will be made transient.");
				return context.getTransientMapping();
			}
		}
	}

	public boolean isFieldOutlineId(FieldOutline fieldOutline) {
		return CustomizationUtils.containsCustomization(fieldOutline,
				Customizations.ID_ELEMENT_NAME);
	}

	public boolean isFieldOutlineVersion(FieldOutline fieldOutline) {

		return CustomizationUtils.containsCustomization(fieldOutline,
				Customizations.VERSION_ELEMENT_NAME);
	}

	public boolean isFieldOutlineBasic(FieldOutline fieldOutline) {

		return isFieldOutlineCore(fieldOutline)
				|| isFieldOutlineEnumerated(fieldOutline);
	}

	public boolean isFieldOutlineCore(FieldOutline fieldOutline) {
		final JMethod getter = FieldAccessorUtils.getter(fieldOutline);

		final JType type = getter.type();
		return JTypeUtils.isBasicType(type);
	}

	public boolean isFieldOutlineEnumerated(FieldOutline fieldOutline) {
		final CPropertyInfo propertyInfo = fieldOutline.getPropertyInfo();

		final Collection types = propertyInfo.ref();
		if (types.size() == 1) {

			final CTypeInfo type = types.iterator().next();

			return type instanceof CEnumLeafInfo;
		} else {
			return false;
		}
	}

	public boolean isFieldOutlineComplex(FieldOutline fieldOutline) {

		final CPropertyInfo propertyInfo = fieldOutline.getPropertyInfo();

		final Collection types = propertyInfo.ref();

		assert types.size() == 1;

		final CTypeInfo type = types.iterator().next();

		return type instanceof CClass;
	}

	public boolean isFieldOutlineEmbeddedId(FieldOutline fieldOutline) {
		return CustomizationUtils.containsCustomization(fieldOutline,
				Customizations.EMBEDDED_ID_ELEMENT_NAME);
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy