Skip to main content
summaryrefslogtreecommitdiffstats
blob: 3ac320826f084640303f9f6223cf983e7e304e5d (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
/**
 * <copyright>
 *
 * Copyright (c) 2005, 2006, 2007 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
 *   Davide Marchignoli
 * </copyright>
 *
 * $Id: EmbeddedMapper.java,v 1.6 2007/02/01 12:35:55 mtaal Exp $
 */

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

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.teneo.annotations.pamodel.PAnnotatedEClass;
import org.eclipse.emf.teneo.annotations.pamodel.PAnnotatedEReference;
import org.eclipse.emf.teneo.annotations.pannotation.ManyToOne;
import org.eclipse.emf.teneo.annotations.pannotation.OneToOne;
import org.eclipse.emf.teneo.simpledom.Element;

/**
 * Maps Embedded properties.
 * 
 * @author <a href="mailto:marchign at elver.org">Davide Marchignoli</a>
 * @author <a href="mailto:mtaal at elver.org">Martin Taal</a>
 */
class EmbeddedMapper extends AbstractMapper {

	// the logger
	private static final Log log = LogFactory.getLog(EmbeddedMapper.class);

	/** Constructor */
	public EmbeddedMapper(MappingContext mappingContext) {
		super(mappingContext);
	}

	/**
	 * Process Embedded object
	 */
	public void process(PAnnotatedEReference paReference) {
		log.debug("Processing embedded: " + paReference.toString());
		
		// push the current overrides
		getHbmContext().pushOverrideOnStack();
		// and add our own
		getHbmContext().addAttributeOverrides(paReference.getAttributeOverrides());

		// push the feature is used for automatic renaming
		getHbmContext().pushEmbeddingFeature(paReference);
		try {
			// make a difference between a many-to-one component and multi-component
			if (paReference.getManyToOne() != null) {
				final ManyToOne mto = paReference.getManyToOne();
				processSingleEmbedded(paReference, mto.getTargetEntity(), paReference.getAnnotatedEReference()
						.getEReferenceType());
			} else if (paReference.getOneToOne() != null) {
				final OneToOne oto = paReference.getOneToOne();
				processSingleEmbedded(paReference, oto.getTargetEntity(), paReference.getAnnotatedEReference()
						.getEReferenceType());
			} else {
				if (paReference.getManyToMany() != null) {
					throw new MappingException("ManyToMany can not be combined with Embedded " + paReference);
				} else if (paReference.getOneToMany() == null) {
					throw new MappingException("OneToMany must be set for embedded elist type: " + paReference);
				}

				// only one to many
				processMultiEmbedded(paReference);
			}
		} finally {
			// and continue with the previous set of overrides
			getHbmContext().popOverrideStack();
			getHbmContext().popEmbeddingFeature();
		}
	}

	/** Process a many-to-one component */
	private void processSingleEmbedded(PAnnotatedEReference paReference, String targetName, EClass target) {
		log.debug("Processing single embedded: " + paReference.toString());
		
		final EClass refType = (EClass) paReference.getAnnotatedEReference().getEType();

		if (targetName == null || getHbmContext().isEasyEMFGenerated(refType)) {
			targetName = getHbmContext().getEntityName(target);
		}

		final Element componentElement = getHbmContext().getCurrent().addElement("component").addAttribute("name",
				paReference.getAnnotatedEReference().getName()).addAttribute("class", targetName);
		getHbmContext().setCurrent(componentElement);
		try {
			// process the features of the target
			final PAnnotatedEClass componentAClass = paReference.getPaModel().getPAnnotated(
					paReference.getAnnotatedEReference().getEReferenceType());
			getHbmContext().processFeatures(componentAClass.getPaEStructuralFeatures());
		} finally {
			getHbmContext().setCurrent(componentElement.getParent());
		}
	}

	/** Process a list of components */
	private void processMultiEmbedded(PAnnotatedEReference paReference) {
		log.debug("Processing multi embedded: " + paReference.toString());
		
		// let the featureprocessor handle this, the one to many is handled by the OneToManyMapper
		getHbmContext().getFeatureMapper().getOneToManyMapper().process(paReference);
	}
}

Back to the top