Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: adc95b71d15f29c8c8a73263e0da09d54440c948 (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
/*******************************************************************************
 * Copyright (c) 2006, 2007 Oracle. 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: Oracle. - initial API and implementation
 *******************************************************************************/
package org.eclipse.jpt.core.internal.platform;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.eclipse.jpt.core.internal.IMappingKeys;
import org.eclipse.jpt.core.internal.IPersistentType;
import org.eclipse.jpt.core.internal.content.java.IJavaTypeMapping;
import org.eclipse.jpt.core.internal.content.orm.XmlEmbeddable;
import org.eclipse.jpt.core.internal.content.orm.XmlEntity;
import org.eclipse.jpt.core.internal.content.orm.XmlMappedSuperclass;
import org.eclipse.jpt.core.internal.content.orm.XmlRootContentNode;
import org.eclipse.jpt.core.internal.content.orm.XmlTypeMapping;
import org.eclipse.jpt.core.internal.mappings.ISequenceGenerator;
import org.eclipse.jpt.core.internal.mappings.ITableGenerator;
import org.eclipse.wst.validation.internal.provisional.core.IMessage;

public class MappingFileContext extends BaseContext
{
	private XmlRootContentNode ormRoot;
	
	private List<XmlTypeContext> xmlTypeContexts;
	
	
	public MappingFileContext(
			PersistenceUnitContext parentContext, XmlRootContentNode xmlRootContentNode) {
		super(parentContext);
		this.ormRoot = xmlRootContentNode;
		this.xmlTypeContexts = buildXmlTypeContexts();
	}
	
	protected List<XmlTypeContext> buildXmlTypeContexts() {
		List<XmlTypeContext> contexts = new ArrayList<XmlTypeContext>();
		for (XmlTypeMapping typeMapping : this.ormRoot.getEntityMappings().getTypeMappings()) {
			XmlTypeContext xmlTypeContext = buildContext(typeMapping);
			contexts.add(xmlTypeContext);
		}
		return contexts;
	}
	
	protected void populateGeneratorRepository(GeneratorRepository generatorRepository) {
		for (ISequenceGenerator generator : this.ormRoot.getEntityMappings().getSequenceGenerators()) {
			generatorRepository.addGenerator(generator);
		}
		for (ITableGenerator generator : this.ormRoot.getEntityMappings().getTableGenerators()) {
			generatorRepository.addGenerator(generator);
		}
		for (XmlTypeContext context : this.xmlTypeContexts) {
			context.populateGeneratorRepository(generatorRepository);
		}
	}
	
	@Override
	protected void initialize() {
		// nothing yet ...
	}
	
	protected XmlTypeContext buildContext(XmlTypeMapping typeMapping) {
		String key = typeMapping.getKey();
		if (key == IMappingKeys.ENTITY_TYPE_MAPPING_KEY) {
			return new XmlEntityContext(this, (XmlEntity) typeMapping);
		}
		else if (key == IMappingKeys.EMBEDDABLE_TYPE_MAPPING_KEY) {
			return new XmlEmbeddableContext(this, (XmlEmbeddable) typeMapping);
		}
		else if (key == IMappingKeys.MAPPED_SUPERCLASS_TYPE_MAPPING_KEY) {
			return new XmlMappedSuperclassContext(this, (XmlMappedSuperclass) typeMapping);
		}
		else {
			throw new IllegalArgumentException(typeMapping.toString());
		}
	}
		
	protected XmlTypeContext xmlTypeMappingContextFor(IJavaTypeMapping javaTypeMapping) {
		for (XmlTypeContext context : this.xmlTypeContexts) {
			if (javaTypeMapping == context.getJavaTypeMapping()) {
				return context;
			}
		}
		return null;
	}
	
	public void refreshDefaults(DefaultsContext parentDefaults) {
		super.refreshDefaults(parentDefaults);
		ormRoot.getEntityMappings().refreshDefaults(parentDefaults);
		DefaultsContext wrappedDefaultsContext = wrapDefaultsContext(parentDefaults);
		for (XmlTypeContext context : this.xmlTypeContexts) {
			if (!context.isRefreshed()) {
				context.refreshDefaults(wrappedDefaultsContext);
			}
		}
	}
	
	private DefaultsContext wrapDefaultsContext(final DefaultsContext defaultsContext) {
		return new DefaultsContext() {
			public Object getDefault(String key) {
				if (key.equals(BaseJpaPlatform.DEFAULT_TABLE_SCHEMA_KEY)
					||  key.equals(BaseJpaPlatform.DEFAULT_TABLE_GENERATOR_SCHEMA_KEY)) {
					String schema = ormRoot.getEntityMappings().getSchema();
					if (schema != null) {
						return schema;
					}
				}
				if (key.equals(BaseJpaPlatform.DEFAULT_TABLE_CATALOG_KEY)) {
					String catalog =  ormRoot.getEntityMappings().getCatalog();
					if (catalog != null) {
						return catalog;
					}
				}
				
				return defaultsContext.getDefault(key);
			}
			
			public IPersistentType persistentType(String fullyQualifiedTypeName) {
				return defaultsContext.persistentType(fullyQualifiedTypeName);
			}
		};
	}
	
	public XmlRootContentNode getXmlRootContentNode() {
		return ormRoot;
	}
	
	public PersistenceUnitContext getPersistenceUnitContext() {
		return (PersistenceUnitContext) getParentContext();
	}
	
	public Iterator<XmlTypeContext> typeContexts() {
		return  this.xmlTypeContexts.iterator();
	}
	
	public boolean contains(IPersistentType persistentType) {
		for (XmlTypeContext context : this.xmlTypeContexts) {
			if (persistentType == context.getPersistentType()) {
				return true;
			}
			if (persistentType == context.javaPersistentType()) {
				return true;
			}
		}
		return false;	
	}
	
	@Override
	public void addToMessages(List<IMessage> messages) {
		for (XmlTypeContext typeMappingContext : this.xmlTypeContexts) {
			typeMappingContext.addToMessages(messages);
		}
	}
}

Back to the top