Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 6d3726e8342d62115bda1ebef0c84e9558294cf5 (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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/**
 * <copyright>
 *
 * Copyright (c) 2005, 2006, 2007, 2008 Springsite BV (The Netherlands) and others
 * 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:
 *   Martin Taal
 * </copyright>
 *
 * $Id: OneToOneReferenceAnnotator.java,v 1.11 2009/03/30 07:53:04 mtaal Exp $
 */

package org.eclipse.emf.teneo.annotations.mapper;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.eclipse.emf.ecore.EReference;
import org.eclipse.emf.teneo.annotations.pamodel.PAnnotatedEReference;
import org.eclipse.emf.teneo.annotations.pannotation.OneToOne;
import org.eclipse.emf.teneo.extension.ExtensionPoint;

/**
 * Annotates an ereference.
 * 
 * @author <a href="mailto:mtaal@elver.org">Martin Taal</a>
 * @version $Revision: 1.11 $
 */

public class OneToOneReferenceAnnotator extends BaseEFeatureAnnotator implements ExtensionPoint {

	// The logger
	protected static final Log log = LogFactory.getLog(OneToOneReferenceAnnotator.class);

	/** Annotate it */
	public void annotate(PAnnotatedEReference aReference) {
		final String logStr = aReference.getModelEReference().getName() + "/"
				+ aReference.getModelEReference().getEContainingClass().getName();

		if (aReference.getOneToMany() != null || aReference.getManyToMany() != null
				|| aReference.getManyToOne() != null) {
			throw new StoreMappingException("The feature/eclass " + logStr + " should be a OneToOne but "
					+ "it already has a OneToMany, ManyToMany or ManyToOne annotation");
		}

		final EReference eReference = (EReference) aReference.getModelElement();

		OneToOne oto = aReference.getOneToOne();
		if (oto == null) {
			if (log.isDebugEnabled()) {
				log.debug("EReference + " + logStr + " does not have a onetoone annotation, adding one");
			}
			oto = getFactory().createOneToOne();
			aReference.setOneToOne(oto);
			// removed unsettable because it is not used to define optional, it
			// is used
			// to allow distinction between the default value set or a feature
			// which has not been
			// set, this is used in validation
			// oto.setOptional(!eReference.isRequired() ||
			// eReference.isUnsettable());
			oto.setOptional(!eReference.isRequired());
			oto.setEModelElement(eReference);
		} else if (log.isDebugEnabled()) {
			log.debug("EReference + " + logStr
					+ " has an onetoone annotation setting defaults if required");
		}

		if (!oto.isSetFetch()) {
			oto.setFetch(getFetch(aReference.getAReferenceType()));
		}

		// determine where to put the mapped-by
		if (oto.getMappedBy() == null && setMappedBy(eReference)) {
			oto.setMappedBy(eReference.getEOpposite().getName());
		}

		if (oto.getCascade().isEmpty() && eReference.isContainment()
				&& getPersistenceOptions().isSetCascadeAllOnContainment()) {
			oto.setOrphanRemoval(true);
		}

		if (getPersistenceOptions().isSetForeignKeyNames() && aReference.getForeignKey() == null) {
			// See bugzilla 211798: handle a specific case when this is a
			// bidirectional
			// association. In that case the foreign key name has to be
			// the same on both sides and is set on the many-side. So use the
			// annotated reference from the other side to ensure that the same
			// foreign key name
			// is used.
			if (eReference.getEOpposite() != null && !eReference.getEOpposite().isTransient()) {
				final PAnnotatedEReference aOpposite = aReference.getPaModel().getPAnnotated(
						eReference.getEOpposite());
				if (aOpposite != null && aOpposite.getTransient() == null) {
					// don't do anything as otherwise hibernate will create two
					// fk's with the same name

					// if (aOpposite.getForeignKey() != null) {
					// final ForeignKey fk = getFactory().createForeignKey();
					// fk.setName(aOpposite.getForeignKey().getName());
					// aReference.setForeignKey(fk);
					// } else {
					// aReference.setForeignKey(createFK(aOpposite));
					// }
				} else {
					aReference.setForeignKey(createFK(aReference));
				}
			} else {
				aReference.setForeignKey(createFK(aReference));
			}
		}

		setCascade(oto.getCascade(), eReference.isContainment());

		if (getPersistenceOptions().isMapEmbeddableAsEmbedded()
				&& aReference.getAReferenceType().getEmbeddable() != null) {
			aReference.setEmbedded(getFactory().createEmbedded());
		}

		// Note: Sometimes EMF generated getters/setters have a
		// very generic type (EObject), if the type can be derived here then
		// this should
		// be added here
		if (oto.getTargetEntity() == null) {
			if (aReference.getAReferenceType() != null
					&& aReference.getAReferenceType().getEntity() != null
					&& aReference.getAReferenceType().getEntity().getName() != null) {
				oto.setTargetEntity(aReference.getAReferenceType().getEntity().getName());
			} else {
				oto.setTargetEntity(getEntityName(eReference.getEReferenceType()));
			}
		}
	}
}

Back to the top