Skip to main content
summaryrefslogtreecommitdiffstats
blob: 52d7b0ddc558bc1ec5baf985d6c1b137d080b8fa (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
/***********************************************************************
 * Copyright (c) 2008 by SAP AG, Walldorf. 
 * 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:
 *     SAP AG - initial API and implementation
 *     Dimiter Dimitrov, d.dimitrov@sap.com - initial API and implementation
 ***********************************************************************/
package org.eclipse.jpt.ui.internal.wizards.entity.data.model;

import java.util.Arrays;
import java.util.List;

public class EntityRow {

	private static final String DOT = ".";
	private boolean key = false;
	private String name = "";
	private String type = "";
	private String fqnTypeName = "";
	private boolean isSimpleType = false;

	private final static String[] PK_TYPES = {"int", "long", "short", "char", "boolean", "byte", "double", "float", 
		"java.lang.String", "java.sql.Date", "java.util.Date", "java.lang.Integer", "java.lang.Long", "java.lang.Short",
		"java.lang.Character", "java.lang.Boolean", "java.lang.Byte", "java.lang.Double", "java.lang.Float"};
	
	private final static List<String> VALID_PK_TYPES = Arrays.asList(PK_TYPES);
	
	
	/**
	 * Constructs <code>EntityColumn</code>.
	 */
	public EntityRow() {
		super();
	}

	
	/**
	 * 
	 * Constructs <code>EntityColumn</code> with the following parameters
	 * 
	 * @param fqnTypeName - fully qualified name of the entity field type
	 * @param name - name of the entity field
	 * @param isKey - flag which indicates whether the entity field is primary key or part of composite primary key
	 */
	public EntityRow(String fqnTypeName, String name, boolean isKey) {
		super();
		this.fqnTypeName = type;
		this.name = name;
		this.key = isKey;
		if (fqnTypeName.indexOf(DOT) == -1) {
			type = fqnTypeName;
			isSimpleType = true;
		} else {
			type = getSimpleName(fqnTypeName);
		}
	}
	
	
	/**
	 * @return whether the presented entity field is primary key or part of composite primary key
	 */
	public boolean isKey() {
		return key;
	}

	/**
	 * Sets the presented entity field to be primary key (or part of composite primari key)
	 * 
	 * @param key 
	 */
	public void setKey(boolean key) {
		this.key = key;
	}
		
	/**
	 * Check whether the type of the entity is allowed to be primary key.
	 * The limitation in the current implementation is that Embedded PK are not checked
	 * @return whether the type of field could be used as primary key
	 */
	public boolean couldBeKey() {
		boolean result = false;
		result = VALID_PK_TYPES.contains(getFqnTypeName());		
		return result;
	}

	/**
	 * @return the name of the entity field
	 */
	public String getName() {
		return name;
	}

	/**
	 * Sets the name of the presented entity field
	 * @param name
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * @return the type (as a simple name) of the entity field
	 */
	public String getType() {
		return type;
	}

	/**
	 * Sets the type (as a simple name) of the entity field
	 * 
	 * @param type
	 */
	public void setType(String type) {
		this.type = type;
	}

	/**
	 * @return the type (as fully qualified name) of the entity field
	 */
	public String getFqnTypeName() {
		return fqnTypeName;
	}

	/**
	 * Sets the fully qualified type name of the entity field
	 * 
	 * @param fqnTypeName
	 */
	public void setFqnTypeName(String fqnTypeName) {
		this.fqnTypeName = fqnTypeName;
		if (fqnTypeName.indexOf(DOT) == -1) {
			setType(fqnTypeName);
			setSimpleType(true);
		} else {
			setType(getSimpleName(fqnTypeName));
		}
		
	}

	/**
	 * @return is the type of the entity field is primitive type
	 */
	public boolean isSimpleType() {
		return isSimpleType;
	}

	/**
	 * Sets the flag which indicate the type of the entity field as primitive type
	 * 
	 * @param isSimpleType
	 */
	public void setSimpleType(boolean isSimpleType) {
		this.isSimpleType = isSimpleType;
	}

	/**
	 * @return whether the type of the entity field is boolean. The information could be used 
	 * when the name of getter should be constructed
	 */
	public boolean isBoolean() {
		return "boolean".equals(getType());
	}
	
	/**
	 * For internal purpose only
	 * Convert fully qualified name to the simple one
	 * @param fullyName
	 * @return the simple name form the fully qualified name parameter(last segment)
	 */
	private String getSimpleName(String fullyName) {
		return fullyName.substring(fullyName.lastIndexOf(DOT) + 1);

	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((fqnTypeName == null) ? 0 : fqnTypeName.hashCode());
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		return result;
	}

	/*
	 * Implement equals, depending from name of the entity field and its type.
	 * The type is presented from the fully qualified name
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		final EntityRow other = (EntityRow) obj;
		if (fqnTypeName == null) {
			if (other.fqnTypeName != null)
				return false;
		} else if (!fqnTypeName.equals(other.fqnTypeName))
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		return true;
	}
	
	
}

Back to the top