Skip to main content
summaryrefslogtreecommitdiffstats
blob: 9a3ab9d256366e997b1b648f3bd5d56aba3d3e3f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/*******************************************************************************
 * Copyright (c) 2009, 2011 Oracle. All rights reserved.
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0, which accompanies this distribution
 * and is available at http://www.eclipse.org/legal/epl-v10.html.
 * 
 * Contributors:
 *     Oracle - initial API and implementation
 ******************************************************************************/
package org.eclipse.jpt.jpa.eclipselink.core.internal.context.java;

import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jpt.common.core.resource.java.JavaResourceField;
import org.eclipse.jpt.common.core.resource.java.JavaResourceMethod;
import org.eclipse.jpt.common.utility.internal.Tools;
import org.eclipse.jpt.jpa.core.MappingKeys;
import org.eclipse.jpt.jpa.core.context.PersistentType;
import org.eclipse.jpt.jpa.core.context.java.Accessor;
import org.eclipse.jpt.jpa.core.context.java.JavaAttributeMappingDefinition;
import org.eclipse.jpt.jpa.core.internal.context.JptValidator;
import org.eclipse.jpt.jpa.core.internal.context.java.AbstractJavaPersistentAttribute;
import org.eclipse.jpt.jpa.eclipselink.core.internal.context.EclipseLinkPersistentAttributeValidator;

/**
 * EclipseLink Java persistent attribute
 */
public class JavaEclipseLinkPersistentAttribute
	extends AbstractJavaPersistentAttribute
{

	public JavaEclipseLinkPersistentAttribute(PersistentType parent, JavaResourceField resourceField) {
		super(parent,resourceField);
	}

	public JavaEclipseLinkPersistentAttribute(PersistentType parent, JavaResourceMethod resourceGetter, JavaResourceMethod resourceSetter) {
		super(parent, resourceGetter, resourceSetter);
	}

	public JavaEclipseLinkPersistentAttribute(PersistentType parent, Accessor accessor) {
		super(parent, accessor);
	}

	// ********** mapping **********

	@Override
	protected JavaAttributeMappingDefinition getSpecifiedMappingDefinition(String key) {
		if (this.specifiedKeyIsInvalid(key)) {
			throw new IllegalArgumentException("invalid mapping: " + key); //$NON-NLS-1$
		}
		return super.getSpecifiedMappingDefinition(key);
	}

	/**
	 * EclipseLink does not support setting an attribute with a default 1:1
	 * mapping to a specified mapping of ID; because the resulting ID annotation
	 * indicates a primary key derived from the 1:1 mapping, <em>not</em> an ID
	 * mapping.
	 */
	protected boolean specifiedKeyIsInvalid(String key) {
		return Tools.valuesAreEqual(key, MappingKeys.ID_ATTRIBUTE_MAPPING_KEY) &&
			Tools.valuesAreEqual(this.getDefaultMappingKey(), MappingKeys.ONE_TO_ONE_ATTRIBUTE_MAPPING_KEY);
	}


	// ********** type **********

	/**
	 * Return whether the attribute's type is a subclass of
	 * <code>java.util.Date</code> or <code>java.util.Calendar</code>.
	 */
	public boolean typeIsDateOrCalendar() {
		return this.getResourceAttribute().typeIsSubTypeOf(DATE_TYPE_NAME)
				|| this.getResourceAttribute().typeIsSubTypeOf(CALENDAR_TYPE_NAME);
	}
	protected static final String DATE_TYPE_NAME = java.util.Date.class.getName();
	protected static final String CALENDAR_TYPE_NAME = java.util.Calendar.class.getName();

	public boolean typeIsSerializable() {
		return this.getResourceAttribute().typeIsVariablePrimitive()
				|| this.getResourceAttribute().typeIsSubTypeOf(SERIALIZABLE_TYPE_NAME);
	}

	public boolean typeIsValidForVariableOneToOne() {
		return this.getResourceAttribute().typeIsInterface()
				&& this.interfaceIsValidForVariableOneToOne(getTypeName());
	}

	protected boolean interfaceIsValidForVariableOneToOne(String interfaceName) {
		return ! this.interfaceIsInvalidForVariableOneToOne(interfaceName);
	}

	// TODO we could probably add more interfaces to this list...
	protected boolean interfaceIsInvalidForVariableOneToOne(String interfaceName) {
		return (interfaceName == null) ||
				this.typeIsContainer(interfaceName) ||
				interfaceName.equals("org.eclipse.persistence.indirection.ValueHolderInterface"); //$NON-NLS-1$
	}


	// ********** validation **********

	@Override
	protected JptValidator buildAttributeValidator(CompilationUnit astRoot) {
		return new EclipseLinkPersistentAttributeValidator(this, buildTextRangeResolver(astRoot));
	}
}

Back to the top