Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: f06624c0510d3f141a11008de7e08339ceefff19 (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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/**
 * <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: BaseEFeatureAnnotator.java,v 1.17 2010/08/18 12:56:38 mtaal Exp $
 */

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

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
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.EClassifier;
import org.eclipse.emf.ecore.EDataType;
import org.eclipse.emf.ecore.EReference;
import org.eclipse.emf.ecore.xml.type.XMLTypePackage;
import org.eclipse.emf.teneo.PersistenceOptions;
import org.eclipse.emf.teneo.annotations.pamodel.PAnnotatedEAttribute;
import org.eclipse.emf.teneo.annotations.pamodel.PAnnotatedEClass;
import org.eclipse.emf.teneo.annotations.pamodel.PAnnotatedEReference;
import org.eclipse.emf.teneo.annotations.pamodel.PAnnotatedEStructuralFeature;
import org.eclipse.emf.teneo.annotations.pannotation.CascadeType;
import org.eclipse.emf.teneo.annotations.pannotation.Column;
import org.eclipse.emf.teneo.annotations.pannotation.FetchType;
import org.eclipse.emf.teneo.annotations.pannotation.ForeignKey;
import org.eclipse.emf.teneo.annotations.pannotation.JoinColumn;
import org.eclipse.emf.teneo.annotations.pannotation.PAnnotation;
import org.eclipse.emf.teneo.annotations.pannotation.Temporal;
import org.eclipse.emf.teneo.annotations.pannotation.TemporalType;
import org.eclipse.emf.teneo.util.EcoreDataTypes;

/**
 * Placeholder for several utility methods which are relevant for annotating ereferences and eattributes.
 * 
 * @author <a href="mailto:mtaal@elver.org">Martin Taal</a>
 * @version $Revision: 1.17 $
 */

public abstract class BaseEFeatureAnnotator extends AbstractAnnotator {

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

	private int defaultVarCharLength = -1;

	/** Create a foreign key and set its name */
	protected ForeignKey createFK(PAnnotatedEStructuralFeature aFeature) {
		final ForeignKey fk = getFactory().createForeignKey();
		fk.setGenerated(true);
		fk.setName(getSqlNameStrategy().getForeignKeyName(aFeature));
		return fk;
	}

	protected FetchType getFetch(PAnnotatedEClass aClass) {
		return FetchType.EAGER;
	}

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

		defaultVarCharLength = persistenceOptions.getDefaultVarCharLength();
	}

	/**
	 * Adds the column level constraints on the basis of the xsd extended meta data
	 */
	protected void addColumnConstraints(PAnnotatedEAttribute aAttribute) {

		// disabled because of bugzilla:
		// 317479
//		if (aAttribute.getId() != null) {
//			aAttribute.getModelEAttribute().setLowerBound(1);
//		}

		final EAttribute eAttribute = aAttribute.getModelEAttribute();

		// decide if a column annotation should be added, this is done
		// when the maxLength or length, totalDigits or fractionDigits are set
		// and when no other column has been set
		if (aAttribute.getColumn() == null) {
			String maxLength = getExtendedMetaData(eAttribute, "maxLength");
			if (maxLength == null) {
				maxLength = getExtendedMetaData(eAttribute, "length");
			}
			if (maxLength == null && defaultVarCharLength > 0) {
				maxLength = "" + defaultVarCharLength;
			}
			final String totalDigits = getExtendedMetaData(eAttribute, "totalDigits");
			final String fractionDigits = getExtendedMetaData(eAttribute, "fractionDigits");
			boolean setUnique = false;
			// bugzilla 249246
			if (getPersistenceOptions().isIDFeatureAsPrimaryKey() && eAttribute.isID() && aAttribute.getId() == null) {
				if (aAttribute.getPaEClass().getPaSuperEntity() != null
						&& aAttribute.getPaEClass().getPaSuperEntity().getMappedSuperclass() == null) {
					setUnique = true;
				}
			}
			if (maxLength != null || setUnique || totalDigits != null || fractionDigits != null
					|| defaultVarCharLength > -1) {
				final Column column = getFactory().createColumn();
				// only support this for the string class, the length/maxlength
				// is also
				// used in case of the xsd list/union types but this can not be
				// enforced using a constraint on the
				// columnlength
				if (maxLength != null && eAttribute.getEAttributeType().getInstanceClass() != null
						&& eAttribute.getEAttributeType().getInstanceClass() == String.class) {
					column.setLength(Integer.parseInt(maxLength)); // you'll
					// find
					// parse
					// errors!
				}
				if (totalDigits != null) {
					column.setPrecision(Integer.parseInt(totalDigits));
				}
				if (fractionDigits != null) {
					column.setScale(Integer.parseInt(fractionDigits));
				}
				if (aAttribute.getBasic() != null) {
					column.setNullable(aAttribute.getBasic().isOptional());
				}
				if (setUnique) {
					column.setUnique(true);
				}
				aAttribute.setColumn(column);
			}
		} else if (aAttribute.getBasic() != null && !aAttribute.getColumn().isSetNullable()) {
			// bugzilla 226775
			aAttribute.getColumn().setNullable(aAttribute.getBasic().isOptional());
		}

		final Column c = aAttribute.getColumn();
		if (isStringType(aAttribute.getModelEAttribute()) && c != null && defaultVarCharLength > 0 && !c.isSetLength()) {
			c.setLength(defaultVarCharLength);
		}

		// disable unique constraint as the uniqueness is covered by the primary
		// key
		// constraints. See issue 280169
		if (c != null && aAttribute.getId() != null) {
			c.setUnique(false);
		}
	}

	private boolean isStringType(EAttribute eAttribute) {
		final Class<?> clz = eAttribute.getEAttributeType().getInstanceClass();
		if (clz != null && String.class.isAssignableFrom(clz)) {
			return true;
		}
		if (eAttribute.getEAttributeType() == XMLTypePackage.eINSTANCE.getString()) {
			return true;
		}
		if (eAttribute.getEAttributeType() == XMLTypePackage.eINSTANCE.getName_()) {
			return true;
		}
		if (eAttribute.getEAttributeType() == XMLTypePackage.eINSTANCE.getNCName()) {
			return true;
		}
		if (eAttribute.getEAttributeType() == XMLTypePackage.eINSTANCE.getToken()) {
			return true;
		}
		if (eAttribute.getEAttributeType() == XMLTypePackage.eINSTANCE.getQName()) {
			return true;
		}
		return false;
	}

	/** Return a list of join columns */
	protected List<JoinColumn> getJoinColumns(List<String> names, boolean optional, boolean isUpdateInsertable,
			PAnnotation pAnnotation) {
		final List<JoinColumn> result = new ArrayList<JoinColumn>();
		for (String name : names) {
			JoinColumn jc = getFactory().createJoinColumn();
			jc.setName(name);
			jc.setNullable(optional);
			jc.setUpdatable(isUpdateInsertable);
			jc.setInsertable(isUpdateInsertable);
			result.add(jc);
		}
		return result;
	}

	protected String getTargetTypeName(PAnnotatedEAttribute aAttribute) {
		return EcoreDataTypes.INSTANCE.getTargetTypeName(aAttribute);
	}

	/** Get a specific extended metadate */
	protected String getExtendedMetaData(EAttribute eAttribute, String key) {
		String value = EcoreDataTypes.INSTANCE.getEAnnotationValue(eAttribute,
				"http:///org/eclipse/emf/ecore/util/ExtendedMetaData", key);
		if (value == null) {
			value = EcoreDataTypes.INSTANCE.getEAnnotationValue(eAttribute.getEAttributeType(),
					"http:///org/eclipse/emf/ecore/util/ExtendedMetaData", key);
		}
		return value;
	}

	/** Determines if mapped by should be set */
	protected boolean setMappedBy(EReference eReference) {
		// only set in two way relation
		// if has not been set on the other side (mappedtoFields)
		// if not a containment relation, containment relations are handled
		// differently
		// the other side may neither be containment
		final EReference eOpposite = eReference.getEOpposite();
		if (eOpposite == null) {
			return false;
		}

		final PAnnotatedEReference aOpposite = getAnnotatedModel().getPAnnotated(eOpposite);
		if (aOpposite.getOneToOne() != null && aOpposite.getOneToOne().getMappedBy() != null) {
			return false;
		}

		return compareNames(eReference, eOpposite);
		// &&
		// !eReference.isContainment() && !eOpposite.isContainment();
	}

	/**
	 * Determines where to place a certain annotation/characteristic, this is done by comparing names..
	 */
	protected boolean compareNames(EReference here, EReference there) {
		final String nameHere = here.eClass().getName() + here.getName();
		final String nameThere = there.eClass().getName() + there.getName();
		assert (nameHere.compareTo(nameThere) != 0);
		return nameHere.compareTo(nameThere) > 0;
	}

	/**
	 * Checks if the cascade should be set in the cascade list, is only done if the list is empty
	 */
	protected void setCascade(List<CascadeType> cascadeList, boolean isContainment) {
		if (!cascadeList.isEmpty()) {
			return;
		}

		if (isContainment) {
			if (getPersistenceOptions().isSetCascadeAllOnContainment()) {
				cascadeList.add(CascadeType.ALL);
			} else {
				if (getPersistenceOptions().isSetCascadeRemoveOnContainment()) {
					cascadeList.add(CascadeType.REMOVE);
				}
				if (getPersistenceOptions().isSetCascadeMergeOnContainment()) {
					cascadeList.add(CascadeType.MERGE);
				}
				if (getPersistenceOptions().isSetCascadePersistOnContainment()) {
					cascadeList.add(CascadeType.PERSIST);
				}
				if (getPersistenceOptions().isSetCascadeRefreshOnContainment()) {
					cascadeList.add(CascadeType.REFRESH);
				}
			}
		} else if (getPersistenceOptions().isSetCascadePolicyForNonContainment()) {
			if (getPersistenceOptions().isSetCascadeMergeOnNonContainment()) {
				cascadeList.add(CascadeType.MERGE);
			}
			if (getPersistenceOptions().isSetCascadePersistOnNonContainment()) {
				cascadeList.add(CascadeType.PERSIST);
			}
			if (getPersistenceOptions().isSetCascadeRefreshOnNonContainment()) {
				cascadeList.add(CascadeType.REFRESH);
			}
		} else {
			cascadeList.add(CascadeType.MERGE);
			cascadeList.add(CascadeType.PERSIST);
			cascadeList.add(CascadeType.REFRESH);
		}
	}

	protected void setTemporal(PAnnotatedEAttribute aAttribute, TemporalType defaultTemporal) {
		final EAttribute eAttribute = aAttribute.getModelEAttribute();
		Class<?> clazz = eAttribute.getEAttributeType().getInstanceClass();
		// clazz is hidden somewhere
		if (clazz == null || Object.class.equals(clazz)) {
			ArrayList<EClassifier> eclassifiers = EcoreDataTypes.INSTANCE.getItemTypes((EDataType) eAttribute
					.getEType());
			for (EClassifier eclassifier : eclassifiers) {
				if (eclassifier.getInstanceClass() != null) {
					clazz = eclassifier.getInstanceClass();
					break;
				}
			}
		}

		final EDataType eDataType = aAttribute.getModelEAttribute().getEAttributeType();
		if (clazz != null
				&& (Date.class.isAssignableFrom(clazz) || eDataType == XMLTypePackage.eINSTANCE.getDate() || eDataType == XMLTypePackage.eINSTANCE
						.getDateTime())) {
			final Temporal temporal = getFactory().createTemporal();
			if (eDataType == XMLTypePackage.eINSTANCE.getDate()) {
				temporal.setValue(TemporalType.DATE);
			} else if (eDataType == XMLTypePackage.eINSTANCE.getDateTime()) {
				temporal.setValue(TemporalType.TIMESTAMP);
			} else {
				temporal.setValue(defaultTemporal);
			}
			aAttribute.setTemporal(temporal);
			temporal.setEModelElement(eAttribute);
		} else if (clazz != null
				&& (Calendar.class.isAssignableFrom(clazz) || eDataType == XMLTypePackage.eINSTANCE.getDate() || eDataType == XMLTypePackage.eINSTANCE
						.getDateTime())) {
			final Temporal temporal = getFactory().createTemporal();
			if (eDataType == XMLTypePackage.eINSTANCE.getDate()) {
				temporal.setValue(TemporalType.DATE);
			} else if (eDataType == XMLTypePackage.eINSTANCE.getDateTime()) {
				temporal.setValue(TemporalType.TIMESTAMP);
			} else {
				temporal.setValue(defaultTemporal);
			}
			aAttribute.setTemporal(temporal);
			temporal.setEModelElement(eAttribute);
		}
	}
}

Back to the top