Skip to content

Commit 849c286

Browse files
committed
ST6RI-682 Refactored Json-EMF transformation code
1 parent 9e2dfed commit 849c286

1 file changed

Lines changed: 37 additions & 22 deletions

File tree

org.omg.sysml/src/org/omg/sysml/util/repository/RepositoryContentFetcher.java

Lines changed: 37 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ public ProjectDelta fetch() {
9999
tracker.forEachTrackedUserElement((id, langElement) -> {
100100
Element element = repositoryProject.getElement(id);
101101
if (element != null && isNotStandardLibraryElement(langElement)) {
102-
tranformCrossReferences(langElement, element);
102+
transformCrossReferences(langElement, element);
103103
transformAttributes(langElement, element);
104104
//set importedMemberships for MembershipImports
105105
setImportedMembership(langElement, element);
@@ -167,7 +167,7 @@ private void transformContainmentReferences(EObject langElement, Element remoteE
167167
}
168168
}
169169

170-
private void tranformCrossReferences(EObject langElement, Element remoteElement) {
170+
private void transformCrossReferences(EObject langElement, Element remoteElement) {
171171
for (EStructuralFeature feature: getStructuralFeatures(langElement))
172172
{
173173
if (!feature.isDerived() && remoteElement.containsKey(feature.getName())) {
@@ -203,28 +203,43 @@ private static Collection<EStructuralFeature> getStructuralFeatures(EObject lang
203203
return eClass.getEAllStructuralFeatures().stream().filter(f -> !allRedefined.contains(f)).collect(Collectors.toList());
204204
}
205205

206-
@SuppressWarnings("unchecked")
206+
207207
private void transformStructuralFeature(EObject langElement, Element remoteElement, EStructuralFeature feature, boolean isReference, boolean isContainment) {
208-
if (!disabledStructuralFeatures.contains(feature.getName())) {
209-
try {
210-
if (feature.isMany()) {
211-
var referenceList = (List<EObject>) langElement.eGet(feature, false);
212-
Object value = remoteElement.get(feature.getName());
213-
if (value instanceof Collection valueCollection) {
214-
for (Object referenceValue: valueCollection) {
215-
resolveReference(referenceValue, isContainment).ifPresent(referenceList::add);
216-
}
217-
}
218-
} else {
219-
Object value = remoteElement.get(feature.getName());
220-
if (value != null) {
221-
langElement.eSet(feature, isReference? resolveReference(value, isContainment).orElse(null) : transformAttributeValue(value, feature));
222-
}
223-
}
208+
209+
if (disabledStructuralFeatures.contains(feature.getName())) {
210+
return;
211+
}
212+
213+
try {
214+
if (feature.isMany()) {
215+
Object value = remoteElement.get(feature.getName());
216+
transformAndAddFeatureValues(langElement, feature, isReference, isContainment, value);
217+
} else {
218+
Object value = remoteElement.get(feature.getName());
219+
transformAndSetFeatureValue(langElement, feature, isReference, isContainment, value);
224220
}
225-
catch (IllegalArgumentException e) {
226-
//handle EObject.eGet, eSet errors
227-
issues.add(new Issue(String.format("Unable to set structural feature %s because %s ha no such feature.", feature.getName(), feature.getEType().getName())));
221+
} catch (IllegalArgumentException e) {
222+
// handle EObject.eGet, eSet errors
223+
issues.add(new Issue(String.format("Unable to set structural feature %s because %s ha no such feature or feature is not changable.",
224+
feature.getName(), feature.getEType().getName())));
225+
}
226+
}
227+
228+
private void transformAndSetFeatureValue(EObject langElement, EStructuralFeature feature, boolean isReference, boolean isContainment, Object valueToTransform) {
229+
if (valueToTransform != null) {
230+
langElement.eSet(feature, isReference ? resolveReference(valueToTransform, isContainment).orElse(null)
231+
: transformAttributeValue(valueToTransform, feature));
232+
}
233+
}
234+
235+
@SuppressWarnings("unchecked")
236+
private void transformAndAddFeatureValues(EObject langElement, EStructuralFeature feature, boolean isReference, boolean isContainment, Object valueToTransform) {
237+
var referenceList = (List<EObject>) langElement.eGet(feature, false);
238+
if (valueToTransform instanceof Collection valueCollection) {
239+
for (Object referenceValue : valueCollection) {
240+
resolveReference(referenceValue, isContainment).ifPresentOrElse(referenceList::add, () -> {
241+
issues.add(new Issue("Could not resolve reference: " + feature.getName()));
242+
});
228243
}
229244
}
230245
}

0 commit comments

Comments
 (0)