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.AssociationMetadata;
26 import org.codehaus.plexus.util.StringUtils;
27
28
29
30
31
32 public class ModelAssociation extends ModelField {
33 public static final String ONE_MULTIPLICITY = "1";
34
35 public static final String MANY_MULTIPLICITY = "*";
36
37
38
39
40
41 private String to;
42
43 private String multiplicity;
44
45 private ModelClass toClass;
46
47
48
49
50
51
52
53
54 public void setTo(String to) {
55 this.to = to;
56 }
57
58
59
60
61 public String getTo() {
62 return to;
63 }
64
65 public String getType() {
66 if (ONE_MULTIPLICITY.equals(getMultiplicity())) {
67 return getTo();
68 } else {
69 return super.getType();
70 }
71 }
72
73
74
75
76 public String getMultiplicity() {
77 return multiplicity;
78 }
79
80
81
82
83 public void setMultiplicity(String multiplicity) {
84 this.multiplicity = multiplicity;
85 }
86
87 public boolean isManyMultiplicity() {
88 return MANY_MULTIPLICITY.equals(multiplicity);
89 }
90
91 public boolean isOneMultiplicity() {
92 return ONE_MULTIPLICITY.equals(multiplicity);
93 }
94
95
96
97
98 public ModelClass getToClass() {
99 return toClass;
100 }
101
102 public AssociationMetadata getAssociationMetadata(String key) {
103 return getMetadata(AssociationMetadata.class, key);
104 }
105
106
107
108
109
110 public void validateElement() throws ModelValidationException {
111 validateFieldNotEmpty("Association", "name", getName());
112
113 validateFieldNotEmpty("Association '" + getName() + "'", "to", to);
114
115 if (isEmpty(to)) {
116 throw new ModelValidationException("You must define the type of association.");
117 }
118
119 if (!"String".equals(to)) {
120 toClass = getModelClass().getModel().getClass(to, getVersionRange());
121
122 if (toClass == null) {
123 throw new ModelValidationException("Association '" + getName() + "': Could not find to class.");
124 }
125 }
126
127 if (isEmpty(multiplicity)) {
128 multiplicity = ONE_MULTIPLICITY;
129 }
130
131 if ("n".equals(multiplicity) || "*".equals(multiplicity)) {
132 multiplicity = MANY_MULTIPLICITY;
133 }
134
135 if (!ONE_MULTIPLICITY.equals(multiplicity) && !MANY_MULTIPLICITY.equals(multiplicity)) {
136 throw new ModelValidationException("Association multiplicity '" + getName() + "' is incorrect: "
137 + "Possible values are '1', '*' or 'n'.");
138 }
139
140 if (isEmpty(getType())) {
141 ModelDefault modelDefault = getModelClass().getModel().getDefault(ModelDefault.LIST);
142
143 setType(modelDefault.getKey());
144
145 setDefaultValue(getDefaultValue(modelDefault));
146 } else {
147 if (isManyMultiplicity()) {
148 if ("Set".equalsIgnoreCase(getType())) {
149 setType(ModelDefault.SET);
150 } else if ("List".equalsIgnoreCase(getType())) {
151 setType(ModelDefault.LIST);
152 } else if ("Map".equalsIgnoreCase(getType())) {
153 setType(ModelDefault.MAP);
154 } else if ("Properties".equalsIgnoreCase(getType())) {
155 setType(ModelDefault.PROPERTIES);
156 } else {
157 throw new ModelValidationException(
158 "The type of element '" + getName() + "' must be List, Map, Properties or Set.");
159 }
160
161 if (isEmpty(getDefaultValue())) {
162 ModelDefault modelDefault = getModelClass().getModel().getDefault(getType());
163
164
165
166 setDefaultValue(getDefaultValue(modelDefault));
167 }
168 }
169 }
170 }
171
172 public boolean isGenericType() {
173 return getType().equals(ModelDefault.LIST) || getType().equals(ModelDefault.SET);
174 }
175
176 private String getDefaultValue(ModelDefault modelDefault) {
177 String value = modelDefault.getValue();
178
179 if (isGenericType()) {
180 value = StringUtils.replace(value, "<?>", "/*<" + getTo() + ">*/");
181 }
182
183 return value;
184 }
185 }