blob: 97b9f47aac047e2fef013207baf44ad012cef207 [file] [log] [blame]
kmoorec9c9e2b2011-02-06 02:07:28 +00001/*******************************************************************************
2 * Copyright (c) 2009, 2010 Oracle. All rights reserved.
3 * This program and the accompanying materials are made available under the
4 * terms of the Eclipse Public License v1.0, which accompanies this distribution
5 * and is available at http://www.eclipse.org/legal/epl-v10.html.
6 *
7 * Contributors:
8 * Oracle - initial API and implementation
9 ******************************************************************************/
10package org.eclipse.jpt.jpa.core.internal.context.orm;
11
12import java.util.ArrayList;
13
14import org.eclipse.jpt.common.utility.internal.Tools;
15import org.eclipse.jpt.jpa.core.context.orm.OrmAttributeMappingDefinition;
16import org.eclipse.jpt.jpa.core.context.orm.OrmTypeMappingDefinition;
17import org.eclipse.jpt.jpa.core.context.orm.OrmXmlContextNodeFactory;
18import org.eclipse.jpt.jpa.core.context.orm.OrmXmlDefinition;
19import org.eclipse.jpt.jpa.core.context.orm.UnsupportedOrmAttributeMappingDefinition;
20
21/**
22 * All the state in the definition should be "static"
23 * (i.e. unchanging once it is initialized).
24 */
25public abstract class AbstractOrmXmlDefinition
26 implements OrmXmlDefinition
27{
28 protected final OrmXmlContextNodeFactory factory;
29
30 protected ArrayList<OrmTypeMappingDefinition> typeMappingDefinitions;
31
32 protected ArrayList<OrmAttributeMappingDefinition> attributeMappingDefinitions;
33
34
35 /**
36 * zero-argument constructor
37 */
38 protected AbstractOrmXmlDefinition() {
39 super();
40 this.factory = this.buildContextNodeFactory();
41 }
42
43
44 // ********** factory **********
45
46 protected abstract OrmXmlContextNodeFactory buildContextNodeFactory();
47
48 public OrmXmlContextNodeFactory getContextNodeFactory() {
49 return this.factory;
50 }
51
52
53 // ********** type mapping definitions **********
54
55 public OrmTypeMappingDefinition getTypeMappingDefinition(String mappingKey) {
56 for (OrmTypeMappingDefinition definition : this.getTypeMappingDefinitions()) {
57 if (Tools.valuesAreEqual(definition.getKey(), mappingKey)) {
58 return definition;
59 }
60 }
61 throw new IllegalArgumentException("Illegal type mapping key: " + mappingKey); //$NON-NLS-1$
62 }
63
64 /**
65 * Return a list of mapping definitions to use for types in
66 * <code>orm.xml</code> mapping files.
67 * The order is unimportant.
68 */
69 protected synchronized ArrayList<OrmTypeMappingDefinition> getTypeMappingDefinitions() {
70 if (this.typeMappingDefinitions == null) {
71 this.typeMappingDefinitions = this.buildTypeMappingDefinitions();
72 }
73 return this.typeMappingDefinitions;
74 }
75
76 protected ArrayList<OrmTypeMappingDefinition> buildTypeMappingDefinitions() {
77 ArrayList<OrmTypeMappingDefinition> definitions = new ArrayList<OrmTypeMappingDefinition>();
78 this.addTypeMappingDefinitionsTo(definitions);
79 return definitions;
80 }
81
82 protected abstract void addTypeMappingDefinitionsTo(ArrayList<OrmTypeMappingDefinition> definitions);
83
84
85 // ********** attribute mapping definitions **********
86
87 public OrmAttributeMappingDefinition getAttributeMappingDefinition(String mappingKey) {
88 for (OrmAttributeMappingDefinition definition : this.getAttributeMappingDefinitions()) {
89 if (Tools.valuesAreEqual(definition.getKey(), mappingKey)) {
90 return definition;
91 }
92 }
93 return UnsupportedOrmAttributeMappingDefinition.instance();
94 }
95
96 /**
97 * Return a list of mapping definitions to use for attributes in
98 * <code>orm.xml</code> mapping files.
99 * The order is unimportant.
100 */
101 protected synchronized ArrayList<OrmAttributeMappingDefinition> getAttributeMappingDefinitions() {
102 if (this.attributeMappingDefinitions == null) {
103 this.attributeMappingDefinitions = this.buildAttributeMappingDefinitions();
104 }
105 return this.attributeMappingDefinitions;
106 }
107
108 protected ArrayList<OrmAttributeMappingDefinition> buildAttributeMappingDefinitions() {
109 ArrayList<OrmAttributeMappingDefinition> definitions = new ArrayList<OrmAttributeMappingDefinition>();
110 this.addAttributeMappingDefinitionsTo(definitions);
111 return definitions;
112 }
113
114 protected abstract void addAttributeMappingDefinitionsTo(ArrayList<OrmAttributeMappingDefinition> definitions);
115}