1 package org.codehaus.modello.model;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 import org.codehaus.modello.metadata.FieldMetadata;
26
27
28
29
30
31 public class ModelField extends BaseElement {
32 private String type;
33
34 private String defaultValue;
35
36 private String typeValidator;
37
38 private boolean required;
39
40 private boolean identifier;
41
42 private String alias;
43
44 private transient ModelClass modelClass;
45
46 private static final String[] PRIMITIVE_TYPES = {
47 "boolean",
48 "Boolean",
49 "char",
50 "Character",
51 "byte",
52 "Byte",
53 "short",
54 "Short",
55 "int",
56 "Integer",
57 "long",
58 "Long",
59 "float",
60 "Float",
61 "double",
62 "Double",
63 "String",
64 "Date",
65 "DOM"
66 };
67
68 public ModelField() {
69 super(true);
70 }
71
72 public ModelField(ModelClass modelClass, String name) {
73 super(true, name);
74
75 this.modelClass = modelClass;
76 }
77
78
79
80
81
82 public String getType() {
83 return type;
84 }
85
86 public void setType(String type) {
87 this.type = type;
88 }
89
90 public String getDefaultValue() {
91 return defaultValue;
92 }
93
94 public void setDefaultValue(String defaultValue) {
95 this.defaultValue = defaultValue;
96 }
97
98 public String getTypeValidator() {
99 return typeValidator;
100 }
101
102 public void setTypeValidator(String typeValidator) {
103 this.typeValidator = typeValidator;
104 }
105
106 public boolean isRequired() {
107 return required;
108 }
109
110 public void setRequired(boolean required) {
111 this.required = required;
112 }
113
114 public boolean isIdentifier() {
115 return identifier;
116 }
117
118 public void setIdentifier(boolean identifier) {
119 this.identifier = identifier;
120 }
121
122 public String getAlias() {
123 return alias;
124 }
125
126 public void setAlias(String alias) {
127 this.alias = alias;
128 }
129
130
131
132
133 public ModelClass getModelClass() {
134 return modelClass;
135 }
136
137 public FieldMetadata getMetadata(String key) {
138 return getMetadata(FieldMetadata.class, key);
139 }
140
141 public boolean isPrimitive() {
142 String type = getType();
143
144
145 if (type == null) {
146 return false;
147 }
148
149 for (String validType : PRIMITIVE_TYPES) {
150 if (type.equals(validType)) {
151 return true;
152 }
153 }
154
155 return false;
156 }
157
158 public boolean isArray() {
159 return getType().endsWith("[]");
160 }
161
162 public boolean isPrimitiveArray() {
163 String type = getType();
164
165 for (String PRIMITIVE_TYPE : PRIMITIVE_TYPES) {
166 String validType = PRIMITIVE_TYPE + "[]";
167
168 if (validType.equals(type)) {
169 return true;
170 }
171 }
172
173 return false;
174 }
175
176
177
178
179
180 public void initialize(ModelClass modelClass) {
181 this.modelClass = modelClass;
182
183 if (defaultValue == null) {
184 if ("boolean".equals(type)) {
185 defaultValue = "false";
186 } else if ("float".equals(type) || "double".equals(type)) {
187 defaultValue = "0.0";
188 } else if ("int".equals(type) || "long".equals(type) || "short".equals(type) || "byte".equals(type)) {
189 defaultValue = "0";
190 } else if ("char".equals(type)) {
191 defaultValue = "\0";
192 }
193 }
194 }
195
196 public void validateElement() throws ModelValidationException {
197 validateFieldNotEmpty("field", "name", getName());
198
199 validateFieldNotEmpty("field '" + getName() + "'", "type", type);
200
201
202
203 if (!isPrimitive() && !isPrimitiveArray()) {
204 throw new ModelValidationException("Field '" + getName() + "': Illegal type: '" + type + "'.");
205 }
206 }
207
208
209
210
211
212 public String toString() {
213 return "[Field: name=" + getName() + ", alias: " + alias + ", type: " + type + ", " + "version: "
214 + getVersionRange() + "]";
215 }
216
217 public boolean isModelVersionField() {
218 Model model = modelClass.getModel();
219 VersionDefinition versionDefinition = model.getVersionDefinition();
220
221 return (versionDefinition != null)
222 && versionDefinition.isFieldType()
223 && (versionDefinition.getValue().equals(getName())
224 || versionDefinition.getValue().equals(alias));
225 }
226 }