1
2
3
4
5
6
7
8
9
10
11 package org.mod4j.businessdomain.generator.helpers;
12
13 import java.util.ArrayList;
14 import java.util.List;
15
16 import BusinessDomainDsl.AbstractType;
17 import BusinessDomainDsl.Association;
18 import BusinessDomainDsl.BoolProperty;
19 import BusinessDomainDsl.BusinessClass;
20 import BusinessDomainDsl.BusinessDomainModel;
21 import BusinessDomainDsl.DateTimeProperty;
22 import BusinessDomainDsl.DecimalProperty;
23 import BusinessDomainDsl.Enumeration;
24 import BusinessDomainDsl.EnumerationProperty;
25 import BusinessDomainDsl.IntegerProperty;
26 import BusinessDomainDsl.Multiplicity;
27 import BusinessDomainDsl.Property;
28 import BusinessDomainDsl.StringProperty;
29
30 public class BusinessClassHelpers {
31
32 public static String getCollectionInterface(Association a) {
33 if (isOne2Many(a) || isMany2Many(a)) {
34 return a.isOrdered() ? "List" : "Set";
35 }
36 return null;
37 }
38
39 public static String getCollectionImplementation(Association a) {
40 if (isOne2Many(a) || isMany2Many(a)) {
41 return a.isOrdered() ? "ArrayList" : "HashSet";
42 }
43 return null;
44 }
45
46 public static boolean isOne2Many(Association a) {
47 Multiplicity srcMult = a.getSourceMultiplicity();
48 Multiplicity tgtMult = a.getTargetMultiplicity();
49 return ((srcMult == Multiplicity.ONE) || (srcMult == Multiplicity.ZERO_ONE))
50 && ((tgtMult == Multiplicity.ONE_MANY) || (tgtMult == Multiplicity.ZERO_MANY));
51 }
52
53 public static boolean isOne2One(Association a) {
54 Multiplicity srcMult = a.getSourceMultiplicity();
55 Multiplicity tgtMult = a.getTargetMultiplicity();
56 return ((srcMult == Multiplicity.ONE) || (srcMult == Multiplicity.ZERO_ONE))
57 && ((tgtMult == Multiplicity.ONE) || (tgtMult == Multiplicity.ZERO_ONE));
58 }
59
60 public static boolean isMany2One(Association a) {
61 Multiplicity srcMult = a.getSourceMultiplicity();
62 Multiplicity tgtMult = a.getTargetMultiplicity();
63 return ((srcMult == Multiplicity.ONE_MANY) || (srcMult == Multiplicity.ZERO_MANY))
64 && ((tgtMult == Multiplicity.ONE) || (tgtMult == Multiplicity.ZERO_ONE));
65 }
66
67 public static boolean isMany2Many(Association a) {
68 Multiplicity srcMult = a.getSourceMultiplicity();
69 Multiplicity tgtMult = a.getTargetMultiplicity();
70 return ((srcMult == Multiplicity.ONE_MANY) || (srcMult == Multiplicity.ZERO_MANY))
71 && ((tgtMult == Multiplicity.ONE_MANY) || (tgtMult == Multiplicity.ZERO_MANY));
72 }
73
74 public static boolean isCompositePart(BusinessClass cls) {
75 for (Association a : cls.getAssociationsFrom()) {
76 if (a.isComposite()
77 && (a.getSourceMultiplicity() == Multiplicity.ONE)) {
78 return true;
79 }
80 }
81 return false;
82 }
83
84 public static String javaType(Property p) {
85 if (p instanceof BoolProperty) {
86 return javaType((BoolProperty) p);
87 } else if (p instanceof StringProperty) {
88 return javaType((StringProperty) p);
89 } else if (p instanceof IntegerProperty) {
90 return javaType((IntegerProperty) p);
91 } else if (p instanceof DecimalProperty) {
92 return javaType((DecimalProperty) p);
93 } else if (p instanceof EnumerationProperty) {
94 return javaType((EnumerationProperty) p);
95 } else if (p instanceof DateTimeProperty) {
96 return javaType((DateTimeProperty) p);
97 }
98 return "Object";
99 }
100
101 public static String javaNullableType(Property p) {
102 if (p instanceof BoolProperty) {
103 return javaNullableType((BoolProperty) p);
104 } else if (p instanceof StringProperty) {
105 return javaNullableType((StringProperty) p);
106 } else if (p instanceof IntegerProperty) {
107 return javaNullableType((IntegerProperty) p);
108 } else if (p instanceof DecimalProperty) {
109 return javaNullableType((DecimalProperty) p);
110 } else if (p instanceof EnumerationProperty) {
111 return javaNullableType((EnumerationProperty) p);
112 } else if (p instanceof DateTimeProperty) {
113 return javaNullableType((DateTimeProperty) p);
114 }
115 return "Object";
116 }
117
118 public static String javaDefaultValue(Property p) {
119 if (p instanceof BoolProperty) {
120 return javaDefaultValue((BoolProperty) p);
121 } else if (p instanceof StringProperty) {
122 return javaDefaultValue((StringProperty) p);
123 } else if (p instanceof IntegerProperty) {
124 return javaDefaultValue((IntegerProperty) p);
125 } else if (p instanceof DecimalProperty) {
126 return javaDefaultValue((DecimalProperty) p);
127 } else if (p instanceof EnumerationProperty) {
128 return javaDefaultValue((EnumerationProperty) p);
129 } else if (p instanceof DateTimeProperty) {
130 return javaDefaultValue((DateTimeProperty) p);
131 }
132 return "null";
133 }
134
135 public static String javaType(BoolProperty p) {
136 return javaNullableType(p);
137 }
138
139 public static String javaNullableType(BoolProperty p) {
140 return "Boolean";
141 }
142
143 public static String javaDefaultValue(BoolProperty p) {
144 return "true";
145 }
146
147 public static String javaType(DateTimeProperty p) {
148 return "DateTime";
149 }
150
151 public static String javaNullableType(DateTimeProperty p) {
152 return javaType(p);
153 }
154
155 public static String javaDefaultValue(DateTimeProperty p) {
156 return "null";
157 }
158
159 public static String javaType(EnumerationProperty p) {
160 Enumeration e = p.getType();
161 if (e == null) {
162 System.err.println("ERROR in javaType() for EnumerationProperty");
163 return "Object";
164 }
165 return p.getType().getName();
166 }
167
168 public static String javaNullableType(EnumerationProperty p) {
169 return javaType(p);
170 }
171
172 public static String javaDefaultValue(EnumerationProperty p) {
173 return "null";
174 }
175
176 public static String javaType(StringProperty p) {
177 return "String";
178 }
179
180 public static String javaNullableType(StringProperty p) {
181 return javaType(p);
182 }
183
184 public static String javaDefaultValue(StringProperty p) {
185 return "null";
186 }
187
188 public static String javaType(IntegerProperty p) {
189 return javaNullableType(p);
190 }
191
192 public static String javaNullableType(IntegerProperty p) {
193 return "Integer";
194 }
195
196 public static String javaDefaultValue(IntegerProperty p) {
197 return p.isNullable() ? "null" : "0";
198 }
199
200 public static String javaType(DecimalProperty p) {
201 return javaNullableType(p);
202 }
203
204 public static String javaNullableType(DecimalProperty p) {
205 return "Float";
206 }
207
208 public static String javaDefaultValue(DecimalProperty p) {
209 return p.isNullable() ? "null" : "0F";
210 }
211
212 public static List<Property> getAllProperties(BusinessClass cls) {
213 List<Property> result = new ArrayList<Property>();
214 if (cls.getSuperclass() != null) {
215 result.addAll(getAllProperties(cls.getSuperclass()));
216 }
217 result.addAll(cls.getProperties());
218 return result;
219 }
220
221 public static List<Association> getAllAssociationsTo(BusinessClass cls) {
222 List<Association> result = new ArrayList<Association>();
223 if (cls.getSuperclass() != null) {
224 result.addAll(getAllAssociationsTo(cls.getSuperclass()));
225 }
226 result.addAll(cls.getAssociationsTo());
227 return result;
228 }
229
230 public static List<Association> getAllAssociationsFrom(BusinessClass cls) {
231 List<Association> result = new ArrayList<Association>();
232 if (cls.getSuperclass() != null) {
233 result.addAll(getAllAssociationsFrom(cls.getSuperclass()));
234 }
235 result.addAll(cls.getAssociationsFrom());
236 return result;
237 }
238
239 public static boolean hasSubclasses(BusinessClass clazz) {
240 BusinessDomainModel model = (BusinessDomainModel) clazz.eContainer();
241 for (AbstractType type : model.getTypes()) {
242 if (type instanceof BusinessClass) {
243 BusinessClass businessClass = (BusinessClass) type;
244 if (businessClass.getSuperclass() != null) {
245 return true;
246 }
247 }
248 }
249 return false;
250 }
251 }