-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathODataDefaultProfileUtils.java
More file actions
238 lines (200 loc) · 8.53 KB
/
Copy pathODataDefaultProfileUtils.java
File metadata and controls
238 lines (200 loc) · 8.53 KB
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
package som.odata.profile.utils;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.uml2.uml.AggregationKind;
import org.eclipse.uml2.uml.Class;
import org.eclipse.uml2.uml.Classifier;
import org.eclipse.uml2.uml.DataType;
import org.eclipse.uml2.uml.Enumeration;
import org.eclipse.uml2.uml.Generalization;
import org.eclipse.uml2.uml.Package;
import org.eclipse.uml2.uml.PrimitiveType;
import org.eclipse.uml2.uml.Profile;
import org.eclipse.uml2.uml.Property;
import org.eclipse.uml2.uml.Stereotype;
import org.eclipse.uml2.uml.UMLPackage;
import org.eclipse.uml2.uml.util.UMLUtil;
public class ODataDefaultProfileUtils {
public static void applyODataProfile(Resource modelResource, Resource profileResource) {
List<Package> packages = new ArrayList<Package>();
populatePackageList(modelResource, packages);
applyODataStereotypes(profileResource, packages);
resolveBaseType(packages);
}
private static void populatePackageList(Resource resource, List<Package> packages) {
for (EObject eObject : resource.getContents()) {
if (eObject instanceof Package) {
packages.add((Package) eObject);
}
}
}
private static void applyODataStereotypes(Resource profileResource, List<Package> packages) {
for (Package pkg : packages) {
Profile profile = (Profile) EcoreUtil
.getObjectByType(profileResource.getContents(),
UMLPackage.Literals.PROFILE);
pkg.applyProfile(profile);
ODataDefaultProfileUtils.applyODServiceStereotype(pkg);
for (Iterator<EObject> it = pkg.eAllContents(); it.hasNext();) {
EObject child = it.next();
if (child instanceof Class) {
Class clazz = (Class) child;
ODataDefaultProfileUtils.applyODEntityType(clazz);
ODataDefaultProfileUtils.applyODEntitySet(clazz);
}
if (child instanceof Property) {
Property property = (Property) child;
ODataDefaultProfileUtils.applyODProperty(property);
ODataDefaultProfileUtils.applyODataNavigationProperty(property);
ODataDefaultProfileUtils.applyODataNavigationPropertyBinding(property);
}
if(child instanceof DataType){
DataType dataType = (DataType) child;
if(child instanceof PrimitiveType)
ODataDefaultProfileUtils.applyODPrimitiveType(dataType);
else
if (child instanceof Enumeration)
ODataDefaultProfileUtils.applyODEnumType(dataType);
else
ODataDefaultProfileUtils.applyODComplexType(dataType);
}
}
}
}
private static void resolveBaseType(List<Package> packages) {
//resolve basetype
for (Package pkg : packages) {
for (Iterator<EObject> it = pkg.eAllContents(); it.hasNext();) {
EObject child = it.next();
if (child instanceof Class ) {
Class clazz = (Class) child;
ODataDefaultProfileUtils.resolveBaseType(clazz);
}
}
}
}
public static void applyODServiceStereotype(Package pkg) {
Stereotype odService = pkg.getApplicableStereotype("ODataProfile::ODService");
if (!pkg.isStereotypeApplied(odService)) {
pkg.applyStereotype(odService);
UMLUtil.setTaggedValue(pkg, odService, "version", "4.0");
UMLUtil.setTaggedValue(pkg, odService, "entityContainerName", pkg.getName() + "Service");
UMLUtil.setTaggedValue(pkg, odService, "schemaNamespace", "com.exmple." + pkg.getName());
UMLUtil.setTaggedValue(pkg, odService, "schemaAlias", pkg.getName());
}
}
public static void applyODEntityType(org.eclipse.uml2.uml.Class clazz) {
Stereotype stereotype = clazz.getApplicableStereotype("ODataProfile::ODEntityType");
if (!clazz.isStereotypeApplied(stereotype)) {
clazz.applyStereotype(stereotype);
UMLUtil.setTaggedValue(clazz, stereotype, "name", clazz.getName());
if (clazz.isAbstract()) {
UMLUtil.setTaggedValue(clazz, stereotype, "abstract", true);
}
}
}
public static void applyODComplexType(DataType dataType) {
Stereotype stereotype = dataType.getApplicableStereotype("ODataProfile::ODComplexType");
if (!dataType.isStereotypeApplied(stereotype)) {
dataType.applyStereotype(stereotype);
UMLUtil.setTaggedValue(dataType, stereotype, "name", dataType.getName());
if (dataType.isAbstract()) {
UMLUtil.setTaggedValue(dataType, stereotype, "abstract", true);
}
}
}
public static void applyODEnumType(DataType dataType) {
Stereotype stereotype = dataType.getApplicableStereotype("ODataProfile::ODEnumType");
if (!dataType.isStereotypeApplied(stereotype)) {
dataType.applyStereotype(stereotype);
UMLUtil.setTaggedValue(dataType, stereotype, "name", dataType.getName());
}
}
public static void applyODPrimitiveType(DataType dataType) {
Stereotype stereotype = dataType.getApplicableStereotype("ODataProfile::ODPrimitiveType");
if (!dataType.isStereotypeApplied(stereotype)) {
dataType.applyStereotype(stereotype);
}
}
public static void applyODEntitySet(org.eclipse.uml2.uml.Class clazz) {
Stereotype stereotype = clazz.getApplicableStereotype("ODataProfile::ODEntitySet");
if (!clazz.isStereotypeApplied(stereotype)) {
clazz.applyStereotype(stereotype);
UMLUtil.setTaggedValue(clazz, stereotype, "name", getPlural(clazz.getName()));
}
}
public static void applyODProperty(Property property) {
if (property.getAssociation() == null && !property.isID()) {
Stereotype stereotype = property.getApplicableStereotype("ODataProfile::ODProperty");
if (!property.isStereotypeApplied(stereotype)) {
property.applyStereotype(stereotype);
UMLUtil.setTaggedValue(property, stereotype, "name", property.getName());
if (property.getLower() == 1)
UMLUtil.setTaggedValue(property, stereotype, "nullable", false);
else
UMLUtil.setTaggedValue(property, stereotype, "nullable", true);
if (property.getDefault() != null)
UMLUtil.setTaggedValue(property, stereotype, "defaultValue", property.getDefault());
}
}
}
public static void applyODKey(Property property) {
if(property.getAssociation() == null && property.isID()){
Stereotype stereotype = property.getApplicableStereotype("ODataProfile::ODKey");
if (!property.isStereotypeApplied(stereotype)) {
property.applyStereotype(stereotype);
UMLUtil.setTaggedValue(property, stereotype, "name", property.getName());
}
}
}
public static void applyODataNavigationProperty(Property property) {
if (property.getAssociation() != null) {
Stereotype stereotype = property.getApplicableStereotype("ODataProfile::ODNavigationProperty");
if (!property.isStereotypeApplied(stereotype)) {
property.applyStereotype(stereotype);
UMLUtil.setTaggedValue(property, stereotype, "name", property.getName());
if (property.getLower() == 1)
UMLUtil.setTaggedValue(property, stereotype, "nullable", false);
else
UMLUtil.setTaggedValue(property, stereotype, "nullable", true);
if(property.getAggregation().equals(AggregationKind.COMPOSITE_LITERAL))
UMLUtil.setTaggedValue(property, stereotype, "containsTarget", true);
}
}
}
public static void applyODataNavigationPropertyBinding(Property property) {
if (property.getAssociation() != null) {
Stereotype stereotype = property.getApplicableStereotype("ODataProfile::ODNavigationPropertyBinding");
if (!property.isStereotypeApplied(stereotype)) {
property.applyStereotype(stereotype);
UMLUtil.setTaggedValue(property, stereotype, "path", property.getName());
}
}
}
public static void resolveBaseType(org.eclipse.uml2.uml.Class clazz) {
if (!clazz.getGeneralizations().isEmpty()) {
Generalization generalization = clazz.getGeneralizations().get(0);
Classifier general = generalization.getGeneral();
if (general instanceof org.eclipse.uml2.uml.Class) {
Stereotype stereotype = clazz.getApplicableStereotype("ODataProfile::ODEntityType");
if (((org.eclipse.uml2.uml.Class) general).isStereotypeApplied(stereotype)) {
Stereotype base = ((org.eclipse.uml2.uml.Class) general)
.getAppliedStereotype("ODataProfile::ODEntityType");
UMLUtil.setTaggedValue(clazz, stereotype, "basetype",
((org.eclipse.uml2.uml.Class) general).getStereotypeApplication(base));
}
}
}
}
public static String getPlural(String word){
if(word.endsWith("y")){
return word.substring(0, word.length()- 1)+"ies";
}
else
return word+"s";
}
}