Skip to main content
summaryrefslogtreecommitdiffstats
blob: 4c2bda0e12a9d71a7c0178b3644370989869a30a (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
/**
 * <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
 * </copyright>
 *
 * $Id: MappingUtil.java,v 1.7 2007/02/01 12:36:36 mtaal Exp $
 */

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

import java.util.ArrayList;
import java.util.Iterator;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.eclipse.emf.ecore.EAttribute;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EReference;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.teneo.ERuntime;
import org.eclipse.emf.teneo.annotations.pamodel.PAnnotatedEAttribute;
import org.eclipse.emf.teneo.annotations.pannotation.FetchType;
import org.eclipse.emf.teneo.jpox.elist.GenericFeatureMapEntry;
import org.eclipse.emf.teneo.jpox.mapping.AnyTypeEObject;
import org.eclipse.emf.teneo.jpox.mapping.AnyTypeObject;
import org.eclipse.emf.teneo.simpledom.Element;
import org.eclipse.emf.teneo.util.StoreUtil;

/**
 * Generates a jpox mapping file based on the pamodel.
 * 
 * @author <a href="mailto:mtaal@elver.org">Martin Taal</a>
 * @version $Revision: 1.7 $
 */

public class MappingUtil {
	/** The logger for all these exceptions */
	protected static final Log log = LogFactory.getLog(MappingUtil.class);

	/** Returns the impl class name of an eclass */
	public static String getImplNameOfEClass(String eClassURI, MappingContext mappingContext) {
		if (mappingContext == null || mappingContext.getEClassNameStrategy() == null) {
			System.err.println("TEST");
		}
		final EClass eclass = mappingContext.getEClassNameStrategy().toEClass(eClassURI, mappingContext.getEpackages());
		if (eclass == null) {
			throw new JPOXMappingException("Uri: " + eClassURI + " does not translate to an eclass");
		}
		final Class clazz = getImplClassOfEClass(eclass);
		if (clazz == null) {
			throw new JPOXMappingException("Uri: " + eClassURI + " does not translate to an instance class");
		}
		return clazz.getName();
	}

	/** Returns the impl class name of an eclass, if the eclass is an interface
	 * then the interface class is returned */
	public static Class getImplClassOfEClass(EClass eClass) {
		if (eClass.isInterface()) {
			return eClass.getInstanceClass();
		}
		return ERuntime.INSTANCE.getInstanceClass(eClass);
	}

	/** Adds an anytype embedded element definition to the passed element */
	public static void addAnytypeMapping(Element field) {
		Element embeddedElement = field.addElement("element").addElement("embedded");
		Element fieldElement = embeddedElement.addElement("field");
		fieldElement.addAttribute("name", "classifier");
		fieldElement.addElement("column").addAttribute("name", "CLASSIFIER");
		fieldElement = embeddedElement.addElement("field");
		fieldElement.addAttribute("name", "value");
		fieldElement.addElement("column").addAttribute("name", "VALUE");
	}

	/** Adds a feature map entry mapping to the passed element */
	public static void addFeatureMapEntryMapping(Element field) {
		log.debug("Any feature entry, adding embedded tags");
		Element embeddedElement = field.addElement("element").addElement("embedded");
		Element fieldElement = embeddedElement.addElement("field");
		fieldElement.addAttribute("name", "featurePath");
		fieldElement.addElement("column").addAttribute("name", "FEATUREPATH");
		fieldElement = embeddedElement.addElement("field");
		fieldElement.addAttribute("name", "anyValue");
		fieldElement.addElement("column").addAttribute("name", "ANYVALUE");
	}

	/** Adds a generic feature map entry to the passed element */
	public static void addGenericFeatureMapEntryMapping(Element field, PAnnotatedEAttribute aAttribute, FetchType ft) {
		EAttribute eAttribute = aAttribute.getAnnotatedEAttribute();

		log.debug("Any feature entry, adding embedded tags");

		Element collection = field.addElement("collection").addAttribute("element-type", GenericFeatureMapEntry.class.getName());

		field.addElement("join");

		Element embeddedElement = field.addElement("element").addElement("embedded");
		Element fieldElement = embeddedElement.addElement("field").addAttribute("name", "featurePath");

		fieldElement = embeddedElement.addElement("field").addAttribute("name", "localAnyValue").addAttribute("embedded", "true");
		addExtensionImplementationClasses(fieldElement, false, false, eAttribute);

		fieldElement = embeddedElement.addElement("field").addAttribute("name", "localReferenceValue");
		if (!addExtensionImplementationClasses(fieldElement, true, false, eAttribute)) {
			fieldElement.addAttribute("embedded", "true");
		}
		fieldElement.addElement("foreign-key").addAttribute("delete-action", "restrict").addAttribute("update-action", "cascade");

		fieldElement = embeddedElement.addElement("field");
		fieldElement.addAttribute("name", "localContainmentReferenceValue").addAttribute("dependent", "true");
		fieldElement.addElement("foreign-key").addAttribute("delete-action", "cascade").addAttribute("update-action", "cascade");

		if (!addExtensionImplementationClasses(fieldElement, true, true, eAttribute)) {
			fieldElement.addAttribute("embedded", "true");
		}

		addEagerLazyLoading(collection, ft);
	}

	/** Adds the correct jpox extension based on the fetch type */
	public static void addEagerLazyLoading(Element field, FetchType ft) {
		if (ft.equals(FetchType.EAGER_LITERAL)) {
			field.addElement("extension").addAttribute("vendor-name", "jpox").addAttribute("key", "cache-lazy-loading").addAttribute(
					"value", "false");
		} else {
			field.addElement("extension").addAttribute("vendor-name", "jpox").addAttribute("key", "cache-lazy-loading").addAttribute(
					"value", "true");
		}
	}

	/** Returns true if the feature is a wildcard feature */
	public static boolean isWildcard(EStructuralFeature eStructuralFeature) {
		return StoreUtil.isWildCard(eStructuralFeature);
	}

	/** Returns true if the feature is a wildcard feature */
	public static boolean isGroup(EStructuralFeature eStructuralFeature) {
		return StoreUtil.isGroupFeature(eStructuralFeature);
	}

	/**
	 * Returns all the implementation classes which can be stored in a certain featuremap entry. Returns false if no impl. classes
	 * could be found
	 */
	private static boolean addExtensionImplementationClasses(Element element, boolean onlyEObject, boolean containment,
			EAttribute eattr) {
		ArrayList result = new ArrayList();
		ArrayList featureResult = new ArrayList();
		for (Iterator it = eattr.getEContainingClass().getEStructuralFeatures().iterator(); it.hasNext();) {
			EStructuralFeature efeature = (EStructuralFeature) it.next();

			if (containment && efeature instanceof EReference && !((EReference) efeature).isContainment()) {
				continue;
			}

			if (!containment && onlyEObject && efeature instanceof EReference && ((EReference) efeature).isContainment()) {
				continue;
			}

			if (StoreUtil.isElementOfGroup(efeature, eattr)) {
				Class instanceClass = efeature.getEType().getInstanceClass();
				if (!onlyEObject && !EObject.class.isAssignableFrom(instanceClass) && !String.class.isAssignableFrom(instanceClass)
						&& efeature instanceof EAttribute) {
					if (!result.contains(instanceClass.getName())) {
						String name = instanceClass.getName();
						if (name.indexOf('.') == -1) { // assume
							log.warn("Primitive type " + name + " prepending java.lang.");
							name = "java.lang." + name.substring(0, 1).toUpperCase() + name.substring(1);
						}
						result.add(name);
						featureResult.add(efeature);
					}
				} else if (onlyEObject && efeature instanceof EReference) {
					Class implClass = ERuntime.INSTANCE.getInstanceClass(((EReference) efeature).getEReferenceType());
					if (implClass != null && !result.contains(implClass.getName())) {
						result.add(implClass.getName());
						featureResult.add(efeature);
					}
				}
			}
		}

		// add extension to fieldelement
		String[] implClasses = (String[]) result.toArray(new String[result.size()]);
		EStructuralFeature[] features = (EStructuralFeature[]) featureResult.toArray(new EStructuralFeature[featureResult.size()]);
		StringBuffer resultStr = new StringBuffer();
		StringBuffer fResultStr = new StringBuffer();
		boolean foundImpl = implClasses.length > 0;
		if (implClasses.length > 0) {
			for (int i = 0; i < implClasses.length; i++) {
				if (resultStr.length() > 0) {
					resultStr.append(",");
					fResultStr.append(",");
				}
				resultStr.append(implClasses[i]);
				fResultStr.append(features[i].getName());
			}
		} else {
			if (onlyEObject) {
				resultStr.append(AnyTypeEObject.class.getName());
			} else {
				resultStr.append(AnyTypeObject.class.getName());
			}
		}

		element.addElement("extension").addAttribute("vendor-name", "jpox").addAttribute("key", "implementation-classes")
				.addAttribute("value", resultStr.toString());

		element.addElement("extension").addAttribute("vendor-name", "teneo").addAttribute("key", "estructuralfeatures")
				.addAttribute("value", fResultStr.toString());
		return foundImpl;
	}
}

Back to the top