From 6fd0d425ee445f943325196451e6194c4acceac8 Mon Sep 17 00:00:00 2001 From: Jan Vlug Date: Wed, 21 Nov 2018 14:48:09 +0100 Subject: [PATCH 1/3] Refactoring: split up code in separate methods. --- .../popup/actions/AutoApplyProfileAction.java | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/com.example.odata.profile.utils/src/som/odata/profile/utils/popup/actions/AutoApplyProfileAction.java b/com.example.odata.profile.utils/src/som/odata/profile/utils/popup/actions/AutoApplyProfileAction.java index 312bb22..d9c0122 100644 --- a/com.example.odata.profile.utils/src/som/odata/profile/utils/popup/actions/AutoApplyProfileAction.java +++ b/com.example.odata.profile.utils/src/som/odata/profile/utils/popup/actions/AutoApplyProfileAction.java @@ -56,11 +56,21 @@ public void run(IAction action) { ResourceSet resourceSet = new ResourceSetImpl(); Resource resource = resourceSet.getResource(URI.createPlatformResourceURI(file.getFullPath().toString(), true), true); List packages = new ArrayList(); + populatePackageList(resource, packages); + applyODataProfile(resourceSet, packages); + resolveBaseType(packages); + save(resource); + } + + private void populatePackageList(Resource resource, List packages) { for (EObject eObject : resource.getContents()) { if (eObject instanceof Package) { packages.add((Package) eObject); } } + } + + private void applyODataProfile(ResourceSet resourceSet, List packages) { for (Package pkg : packages) { // pathmap://ODA_PROFILES/odata.profile.uml#_p6kjUO-pEeaLcvwqpORGRg Resource profileResource = resourceSet.getResource(URI.createURI("pathmap://ODA_PROFILES/odata.profile.uml"), true); @@ -96,6 +106,9 @@ public void run(IAction action) { } } + } + + private void resolveBaseType(List packages) { //resolve basetype for (Package pkg : packages) { for (Iterator it = pkg.eAllContents(); it.hasNext();) { @@ -107,6 +120,9 @@ public void run(IAction action) { } } } + } + + private void save(Resource resource) { try { resource.save(Collections.emptyMap()); } catch (IOException e) { From 54dc7fe04cf35d87f72389ab59df0e2b520d1a64 Mon Sep 17 00:00:00 2001 From: Jan Vlug Date: Wed, 21 Nov 2018 15:18:02 +0100 Subject: [PATCH 2/3] Refactoring: Introduce a method that just works on a model resource and a profile resource. --- .../popup/actions/AutoApplyProfileAction.java | 25 +++++++++++++------ 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/com.example.odata.profile.utils/src/som/odata/profile/utils/popup/actions/AutoApplyProfileAction.java b/com.example.odata.profile.utils/src/som/odata/profile/utils/popup/actions/AutoApplyProfileAction.java index d9c0122..8b144d5 100644 --- a/com.example.odata.profile.utils/src/som/odata/profile/utils/popup/actions/AutoApplyProfileAction.java +++ b/com.example.odata.profile.utils/src/som/odata/profile/utils/popup/actions/AutoApplyProfileAction.java @@ -12,6 +12,7 @@ import org.eclipse.emf.ecore.resource.Resource; import org.eclipse.emf.ecore.resource.ResourceSet; import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl; +import org.eclipse.emf.ecore.util.EcoreUtil; import org.eclipse.jface.action.IAction; import org.eclipse.jface.viewers.ISelection; import org.eclipse.jface.viewers.IStructuredSelection; @@ -26,6 +27,7 @@ import org.eclipse.uml2.uml.PrimitiveType; import org.eclipse.uml2.uml.Profile; import org.eclipse.uml2.uml.Property; +import org.eclipse.uml2.uml.UMLPackage; import som.odata.profile.utils.ODataDefaultProfileUtils; @@ -54,12 +56,18 @@ public void setActivePart(IAction action, IWorkbenchPart targetPart) { */ public void run(IAction action) { ResourceSet resourceSet = new ResourceSetImpl(); - Resource resource = resourceSet.getResource(URI.createPlatformResourceURI(file.getFullPath().toString(), true), true); + Resource modelResource = resourceSet.getResource(URI.createPlatformResourceURI(file.getFullPath().toString(), true), true); + // pathmap://ODA_PROFILES/odata.profile.uml#_p6kjUO-pEeaLcvwqpORGRg + Resource profileResource = resourceSet.getResource(URI.createURI("pathmap://ODA_PROFILES/odata.profile.uml").appendFragment("_pWtvsO-mEeaLcvwqpORGRg"), true); + applyODataProfile(modelResource, profileResource); + save(modelResource); + } + + private void applyODataProfile(Resource modelResource, Resource profileResource) { List packages = new ArrayList(); - populatePackageList(resource, packages); - applyODataProfile(resourceSet, packages); + populatePackageList(modelResource, packages); + applyODataStereotypes(profileResource, packages); resolveBaseType(packages); - save(resource); } private void populatePackageList(Resource resource, List packages) { @@ -70,11 +78,12 @@ private void populatePackageList(Resource resource, List packages) { } } - private void applyODataProfile(ResourceSet resourceSet, List packages) { + private void applyODataStereotypes(Resource profileResource, List packages) { for (Package pkg : packages) { - // pathmap://ODA_PROFILES/odata.profile.uml#_p6kjUO-pEeaLcvwqpORGRg - Resource profileResource = resourceSet.getResource(URI.createURI("pathmap://ODA_PROFILES/odata.profile.uml"), true); - Profile profile = (Profile) profileResource.getEObject("_pWtvsO-mEeaLcvwqpORGRg"); + + Profile profile = (Profile) EcoreUtil + .getObjectByType(profileResource.getContents(), + UMLPackage.Literals.PROFILE); pkg.applyProfile(profile); ODataDefaultProfileUtils.applyODServiceStereotype(pkg); From d67bb0f02a94915bbca4eeeff5f2919828df82f3 Mon Sep 17 00:00:00 2001 From: Jan Vlug Date: Wed, 21 Nov 2018 15:28:13 +0100 Subject: [PATCH 3/3] Refactoring: Move all methods related to applying the OData profile to the ODataDefaultProfileUtils class. --- .../utils/ODataDefaultProfileUtils.java | 80 ++++++++++++++++++ .../popup/actions/AutoApplyProfileAction.java | 81 +------------------ 2 files changed, 81 insertions(+), 80 deletions(-) diff --git a/com.example.odata.profile.utils/src/som/odata/profile/utils/ODataDefaultProfileUtils.java b/com.example.odata.profile.utils/src/som/odata/profile/utils/ODataDefaultProfileUtils.java index cb31618..b048e62 100644 --- a/com.example.odata.profile.utils/src/som/odata/profile/utils/ODataDefaultProfileUtils.java +++ b/com.example.odata.profile.utils/src/som/odata/profile/utils/ODataDefaultProfileUtils.java @@ -1,15 +1,95 @@ 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 packages = new ArrayList(); + populatePackageList(modelResource, packages); + applyODataStereotypes(profileResource, packages); + resolveBaseType(packages); + } + + private static void populatePackageList(Resource resource, List packages) { + for (EObject eObject : resource.getContents()) { + if (eObject instanceof Package) { + packages.add((Package) eObject); + } + } + } + + private static void applyODataStereotypes(Resource profileResource, List packages) { + for (Package pkg : packages) { + + Profile profile = (Profile) EcoreUtil + .getObjectByType(profileResource.getContents(), + UMLPackage.Literals.PROFILE); + + pkg.applyProfile(profile); + ODataDefaultProfileUtils.applyODServiceStereotype(pkg); + for (Iterator 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 packages) { + //resolve basetype + for (Package pkg : packages) { + for (Iterator 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"); diff --git a/com.example.odata.profile.utils/src/som/odata/profile/utils/popup/actions/AutoApplyProfileAction.java b/com.example.odata.profile.utils/src/som/odata/profile/utils/popup/actions/AutoApplyProfileAction.java index 8b144d5..2d6840e 100644 --- a/com.example.odata.profile.utils/src/som/odata/profile/utils/popup/actions/AutoApplyProfileAction.java +++ b/com.example.odata.profile.utils/src/som/odata/profile/utils/popup/actions/AutoApplyProfileAction.java @@ -1,18 +1,13 @@ package som.odata.profile.utils.popup.actions; import java.io.IOException; -import java.util.ArrayList; import java.util.Collections; -import java.util.Iterator; -import java.util.List; import org.eclipse.core.resources.IFile; import org.eclipse.emf.common.util.URI; -import org.eclipse.emf.ecore.EObject; import org.eclipse.emf.ecore.resource.Resource; import org.eclipse.emf.ecore.resource.ResourceSet; import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl; -import org.eclipse.emf.ecore.util.EcoreUtil; import org.eclipse.jface.action.IAction; import org.eclipse.jface.viewers.ISelection; import org.eclipse.jface.viewers.IStructuredSelection; @@ -20,14 +15,6 @@ import org.eclipse.ui.IActionDelegate; import org.eclipse.ui.IObjectActionDelegate; import org.eclipse.ui.IWorkbenchPart; -import org.eclipse.uml2.uml.Class; -import org.eclipse.uml2.uml.DataType; -import org.eclipse.uml2.uml.Enumeration; -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.UMLPackage; import som.odata.profile.utils.ODataDefaultProfileUtils; @@ -59,77 +46,11 @@ public void run(IAction action) { Resource modelResource = resourceSet.getResource(URI.createPlatformResourceURI(file.getFullPath().toString(), true), true); // pathmap://ODA_PROFILES/odata.profile.uml#_p6kjUO-pEeaLcvwqpORGRg Resource profileResource = resourceSet.getResource(URI.createURI("pathmap://ODA_PROFILES/odata.profile.uml").appendFragment("_pWtvsO-mEeaLcvwqpORGRg"), true); - applyODataProfile(modelResource, profileResource); + ODataDefaultProfileUtils.applyODataProfile(modelResource, profileResource); save(modelResource); } - private void applyODataProfile(Resource modelResource, Resource profileResource) { - List packages = new ArrayList(); - populatePackageList(modelResource, packages); - applyODataStereotypes(profileResource, packages); - resolveBaseType(packages); - } - - private void populatePackageList(Resource resource, List packages) { - for (EObject eObject : resource.getContents()) { - if (eObject instanceof Package) { - packages.add((Package) eObject); - } - } - } - private void applyODataStereotypes(Resource profileResource, List packages) { - for (Package pkg : packages) { - - Profile profile = (Profile) EcoreUtil - .getObjectByType(profileResource.getContents(), - UMLPackage.Literals.PROFILE); - - pkg.applyProfile(profile); - ODataDefaultProfileUtils.applyODServiceStereotype(pkg); - for (Iterator 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 void resolveBaseType(List packages) { - //resolve basetype - for (Package pkg : packages) { - for (Iterator it = pkg.eAllContents(); it.hasNext();) { - EObject child = it.next(); - if (child instanceof Class ) { - Class clazz = (Class) child; - ODataDefaultProfileUtils.resolveBaseType(clazz); - - } - } - } - } private void save(Resource resource) { try {