Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 6d860e4574bb717704cc7f901f52c98bf59780af (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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/**
 * <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: OneToManyAttributeAnnotator.java,v 1.11 2010/03/28 09:20:25 mtaal Exp $
 */

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

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.eclipse.emf.ecore.EAttribute;
import org.eclipse.emf.ecore.EEnum;
import org.eclipse.emf.ecore.util.FeatureMapUtil;
import org.eclipse.emf.teneo.PersistenceOptions;
import org.eclipse.emf.teneo.annotations.pamodel.PAnnotatedEAttribute;
import org.eclipse.emf.teneo.annotations.pannotation.CascadeType;
import org.eclipse.emf.teneo.annotations.pannotation.EnumType;
import org.eclipse.emf.teneo.annotations.pannotation.Enumerated;
import org.eclipse.emf.teneo.annotations.pannotation.FetchType;
import org.eclipse.emf.teneo.annotations.pannotation.JoinTable;
import org.eclipse.emf.teneo.annotations.pannotation.OneToMany;
import org.eclipse.emf.teneo.annotations.pannotation.TemporalType;
import org.eclipse.emf.teneo.extension.ExtensionPoint;
import org.eclipse.emf.teneo.util.StoreUtil;

/**
 * Annotates a one-to-many attribute (an eattribute with ismany=true), an example is a list of
 * primitives (list of ints).
 * 
 * @author <a href="mailto:mtaal@elver.org">Martin Taal</a>
 * @version $Revision: 1.11 $
 */

public class OneToManyAttributeAnnotator extends BaseEFeatureAnnotator implements ExtensionPoint {

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

	private TemporalType optionDefaultTemporal = null;

	/** Process the features of the eclass */
	public void annotate(PAnnotatedEAttribute aAttribute) {
		final String logStr = aAttribute.getModelEAttribute().getName() + "/"
				+ aAttribute.getModelEAttribute().getEContainingClass().getName();

		if (log.isDebugEnabled()) {
			log.debug("EAttribute " + logStr + " needs a onetomany");
		}

		final EAttribute eAttribute = (EAttribute) aAttribute.getModelElement();

		OneToMany otm = aAttribute.getOneToMany();
		final boolean otmWasSet = otm != null; // otm was set manually
		if (otm == null) {
			if (log.isDebugEnabled()) {
				log.debug("One to many not present adding one");
			}
			otm = getFactory().createOneToMany();
			aAttribute.setOneToMany(otm);
			otm.setEModelElement(eAttribute);

			if (getPersistenceOptions().isFetchContainmentEagerly()) {
				otm.setFetch(FetchType.EAGER);
			} else if (getPersistenceOptions().isFetchAssociationExtraLazy()) {
				otm.setFetch(FetchType.EXTRA);
			}
		} else if (log.isDebugEnabled()) {
			log.debug("One to many present adding default information if required");
		}

		if (getPersistenceOptions().isSetForeignKeyNames() && aAttribute.getForeignKey() == null) {
			aAttribute.setForeignKey(createFK(aAttribute));
		}

		// handle list of enums
		if (eAttribute.getEType() instanceof EEnum && aAttribute.getEnumerated() == null) {
			final Enumerated enumerated = getFactory().createEnumerated();
			enumerated.setValue(EnumType.STRING);
			enumerated.setEModelElement(eAttribute);
			aAttribute.setEnumerated(enumerated);
		}

		if (aAttribute.getTemporal() == null) {
			setTemporal(aAttribute, optionDefaultTemporal);
		}

		// set cascade if not set
		if (otm.getCascade().isEmpty()) {
			otm.getCascade().add(CascadeType.ALL);
		}

		if (otm.getTargetEntity() == null) {
			otm.setTargetEntity(getTargetTypeName(aAttribute));
		}

		if (aAttribute.getJoinTable() == null) {
			// note not optional because lists of simple types are embedded
			final JoinTable jt = getFactory().createJoinTable();
			jt.setName(getSqlNameStrategy().getJoinTableName(aAttribute));
			aAttribute.setJoinTable(jt);
			if (StoreUtil.isAuditEntryEClass(aAttribute.getModelEAttribute().getEContainingClass())
					&& getPersistenceOptions().getAuditingDBSchema() != null
					&& getPersistenceOptions().getAuditingDBSchema().length() > 0) {
				jt.setSchema(getPersistenceOptions().getAuditingDBSchema());
			}

		}

		if (aAttribute.getJoinColumns().size() == 0) {
			final List<String> names = getSqlNameStrategy().getOneToManyEAttributeJoinColumns(aAttribute);
			aAttribute.getJoinColumns().addAll(
					getJoinColumns(names, FeatureMapUtil.isFeatureMap(eAttribute), true, otm));
		}

		// set unique and indexed
		if (!otmWasSet) {
			if (log.isDebugEnabled()) {
				log.debug("Setting indexed and unique on otm from eAttribute.isOrdered/isUnique "
						+ "because otm was not set manually");
			}
			otm.setIndexed(eAttribute.isOrdered());
			otm.setUnique(eAttribute.isUnique());

			if (aAttribute.getElementCollection() != null
					&& aAttribute.getElementCollection().getFetch() != otm.getFetch()) {
				otm.setFetch(aAttribute.getElementCollection().getFetch());
			}
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @seeorg.eclipse.emf.teneo.annotations.mapper.AbstractAnnotator#
	 * setPersistenceOptions(org.eclipse.emf.teneo.PersistenceOptions)
	 */
	@Override
	public void setPersistenceOptions(PersistenceOptions persistenceOptions) {
		super.setPersistenceOptions(persistenceOptions);

		optionDefaultTemporal = TemporalType.get(persistenceOptions.getDefaultTemporalValue());
		if (optionDefaultTemporal == null) {
			throw new StoreMappingException("Temporal value not found: "
					+ persistenceOptions.getDefaultTemporalValue());
		}
	}

}

Back to the top