Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring: moved all code for appying profile to ODataDefaultProfileUtils class #2

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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<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");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
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;
Expand All @@ -19,13 +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 som.odata.profile.utils.ODataDefaultProfileUtils;

Expand Down Expand Up @@ -54,59 +43,16 @@ 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);
List<Package> packages = new ArrayList<Package>();
for (EObject eObject : resource.getContents()) {
if (eObject instanceof Package) {
packages.add((Package) eObject);
}
}
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");

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);
}

}
}
//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);

}
}
}
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);
ODataDefaultProfileUtils.applyODataProfile(modelResource, profileResource);
save(modelResource);
}



private void save(Resource resource) {
try {
resource.save(Collections.emptyMap());
} catch (IOException e) {
Expand Down