Skip to content

Commit

Permalink
[384] Make the child creation descriptions editing context aware
Browse files Browse the repository at this point in the history
Bug: #384
Signed-off-by: Stéphane Bégaudeau <stephane.begaudeau@obeo.fr>
  • Loading branch information
sbegaudeau committed Mar 15, 2021
1 parent 41f6b01 commit 4f433f6
Show file tree
Hide file tree
Showing 22 changed files with 1,225 additions and 369 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2019, 2020 Obeo.
* Copyright (c) 2019, 2021 Obeo.
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
Expand Down Expand Up @@ -35,7 +35,7 @@ public Optional<Object> findClass(String classId) {
}

@Override
public List<ChildCreationDescription> getChildCreationDescriptions(String classId) {
public List<ChildCreationDescription> getChildCreationDescriptions(UUID editingContextId, String classId) {
return new ArrayList<>();
}

Expand All @@ -50,12 +50,12 @@ public void delete(Object object) {
}

@Override
public List<Namespace> getNamespaces() {
public List<Namespace> getNamespaces(UUID editingContextId) {
return new ArrayList<>();
}

@Override
public List<ChildCreationDescription> getRootCreationDescriptions(String namespaceId, boolean suggested) {
public List<ChildCreationDescription> getRootCreationDescriptions(UUID editingContextId, String namespaceId, boolean suggested) {
return new ArrayList<>();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EPackage;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.ecore.impl.EPackageRegistryImpl;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
Expand Down Expand Up @@ -58,15 +59,19 @@
@Service
public class EditService implements IEditService {

private final IEditingContextEPackageService editingContextEPackageService;

private final ComposedAdapterFactory composedAdapterFactory;

private final EPackage.Registry ePackageRegistry;
private final EPackage.Registry globalEPackageRegistry;

private final ISuggestedRootObjectTypesProvider suggestedRootObjectTypesProvider;

public EditService(ComposedAdapterFactory composedAdapterFactory, EPackage.Registry ePackageRegistry, ISuggestedRootObjectTypesProvider suggestedRootObjectsProvider) {
public EditService(IEditingContextEPackageService editingContextEPackageService, ComposedAdapterFactory composedAdapterFactory, EPackage.Registry globalEPackageRegistry,
ISuggestedRootObjectTypesProvider suggestedRootObjectsProvider) {
this.editingContextEPackageService = Objects.requireNonNull(editingContextEPackageService);
this.composedAdapterFactory = Objects.requireNonNull(composedAdapterFactory);
this.ePackageRegistry = Objects.requireNonNull(ePackageRegistry);
this.globalEPackageRegistry = Objects.requireNonNull(globalEPackageRegistry);
this.suggestedRootObjectTypesProvider = Objects.requireNonNull(suggestedRootObjectsProvider);
}

Expand All @@ -77,7 +82,7 @@ public Optional<Object> findClass(String classId) {
String eClassName = classIdService.getEClassName(classId);

// @formatter:off
return classIdService.findEPackage(this.ePackageRegistry, ePackageName)
return classIdService.findEPackage(this.globalEPackageRegistry, ePackageName)
.map(ePackage -> ePackage.getEClassifier(eClassName))
.filter(EClass.class::isInstance)
.map(EClass.class::cast)
Expand All @@ -87,11 +92,15 @@ public Optional<Object> findClass(String classId) {
}

@Override
public List<ChildCreationDescription> getChildCreationDescriptions(String classId) {
public List<ChildCreationDescription> getChildCreationDescriptions(UUID editingContextId, String classId) {
List<ChildCreationDescription> childCreationDescriptions = new ArrayList<>();

EPackageRegistryImpl ePackageRegistry = new EPackageRegistryImpl(this.globalEPackageRegistry);
List<EPackage> additionalEPackages = this.editingContextEPackageService.getEPackages(editingContextId);
additionalEPackages.forEach(ePackage -> ePackageRegistry.put(ePackage.getNsURI(), ePackage));

ResourceSet resourceSet = new ResourceSetImpl();
resourceSet.setPackageRegistry(this.ePackageRegistry);
resourceSet.setPackageRegistry(ePackageRegistry);
AdapterFactoryEditingDomain editingDomain = new AdapterFactoryEditingDomain(this.composedAdapterFactory, new BasicCommandStack(), resourceSet);
Resource resource = new JsonResourceImpl(URI.createURI("inmemory"), Map.of()); //$NON-NLS-1$
resourceSet.getResources().add(resource);
Expand Down Expand Up @@ -202,23 +211,37 @@ public void delete(Object object) {
}

@Override
public List<Namespace> getNamespaces() {
public List<Namespace> getNamespaces(UUID editingContextId) {
List<String> eNsURIs = new ArrayList<>();
eNsURIs.addAll(this.globalEPackageRegistry.keySet());

// @formatter:off
return this.ePackageRegistry.keySet().stream()
this.editingContextEPackageService.getEPackages(editingContextId).stream()
.map(EPackage::getNsURI)
.forEach(eNsURIs::add);

return eNsURIs.stream()
.map(nsUri -> new Namespace(nsUri, nsUri))
.collect(Collectors.toList());
// @formatter:on
}

@Override
public List<ChildCreationDescription> getRootCreationDescriptions(String namespaceId, boolean suggested) {
public List<ChildCreationDescription> getRootCreationDescriptions(UUID editingContextId, String namespaceId, boolean suggested) {
List<ChildCreationDescription> rootObjectCreationDescription = new ArrayList<>();

EPackage ePackage = this.ePackageRegistry.getEPackage(namespaceId);
EPackageRegistryImpl ePackageRegistry = new EPackageRegistryImpl(this.globalEPackageRegistry);
List<EPackage> additionalEPackages = this.editingContextEPackageService.getEPackages(editingContextId);
additionalEPackages.forEach(ePackage -> ePackageRegistry.put(ePackage.getNsURI(), ePackage));

EPackage ePackage = ePackageRegistry.getEPackage(namespaceId);
if (ePackage != null) {
List<EClass> classes = new ArrayList<>();
if (suggested) {
classes = this.suggestedRootObjectTypesProvider.getSuggestedRootObjectTypes(ePackage);
if (classes.isEmpty()) {
classes = this.getConcreteClasses(ePackage);
}
} else {
classes = this.getConcreteClasses(ePackage);
}
Expand All @@ -243,7 +266,7 @@ private List<EClass> getConcreteClasses(EPackage ePackage) {
public Optional<Object> createRootObject(IEditingContext editingContext, UUID documentId, String namespaceId, String rootObjectCreationDescriptionId) {
Optional<Object> createdObjectOptional = Optional.empty();

var optionalEClass = this.getMatchingEClass(namespaceId, rootObjectCreationDescriptionId);
var optionalEClass = this.getMatchingEClass(editingContext.getId(), namespaceId, rootObjectCreationDescriptionId);

// @formatter:off
var optionalEditingDomain = Optional.of(editingContext)
Expand Down Expand Up @@ -275,9 +298,13 @@ public Optional<Object> createRootObject(IEditingContext editingContext, UUID do
return createdObjectOptional;
}

private Optional<EClass> getMatchingEClass(String namespaceId, String rootObjectCreationDescriptionId) {
private Optional<EClass> getMatchingEClass(UUID editingContextId, String namespaceId, String rootObjectCreationDescriptionId) {
EPackageRegistryImpl ePackageRegistry = new EPackageRegistryImpl(this.globalEPackageRegistry);
List<EPackage> additionalEPackages = this.editingContextEPackageService.getEPackages(editingContextId);
additionalEPackages.forEach(ePackage -> ePackageRegistry.put(ePackage.getNsURI(), ePackage));

// @formatter:off
return Optional.ofNullable(this.ePackageRegistry.getEPackage(namespaceId))
return Optional.ofNullable(ePackageRegistry.getEPackage(namespaceId))
.map(ePackage -> ePackage.getEClassifier(rootObjectCreationDescriptionId))
.filter(EClass.class::isInstance)
.map(EClass.class::cast)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public Optional<Object> findClass(String classId) {
}

@Override
public List<ChildCreationDescription> getChildCreationDescriptions(String classId) {
public List<ChildCreationDescription> getChildCreationDescriptions(UUID editingContextId, String classId) {
return new ArrayList<>();
}

Expand All @@ -50,12 +50,12 @@ public void delete(Object object) {
}

@Override
public List<Namespace> getNamespaces() {
public List<Namespace> getNamespaces(UUID editingContextId) {
return new ArrayList<>();
}

@Override
public List<ChildCreationDescription> getRootCreationDescriptions(String namespaceId, boolean suggested) {
public List<ChildCreationDescription> getRootCreationDescriptions(UUID editingContextId, String namespaceId, boolean suggested) {
return new ArrayList<>();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
*******************************************************************************/
package org.eclipse.sirius.web.graphql.schema;

import static graphql.schema.GraphQLArgument.newArgument;
import static graphql.schema.GraphQLFieldDefinition.newFieldDefinition;
import static graphql.schema.GraphQLList.list;
import static graphql.schema.GraphQLNonNull.nonNull;
import static graphql.schema.GraphQLTypeReference.typeRef;
Expand All @@ -20,8 +22,12 @@
import java.util.Set;

import org.eclipse.sirius.web.graphql.utils.schema.ITypeProvider;
import org.eclipse.sirius.web.services.api.objects.ChildCreationDescription;
import org.eclipse.sirius.web.services.api.objects.Namespace;
import org.springframework.stereotype.Service;

import graphql.Scalars;
import graphql.schema.GraphQLArgument;
import graphql.schema.GraphQLFieldDefinition;
import graphql.schema.GraphQLObjectType;
import graphql.schema.GraphQLType;
Expand All @@ -36,6 +42,9 @@
* type EditingContext {
* id: ID!
* stereotypeDescriptions: [StereotypeDescription!]!
* childCreationDescriptions(classId: ID!): [ChildCreationDescription!]!
* rootObjectCreationDescriptions(namespaceId: ID!, suggested: Boolean!): [ChildCreationDescription!]!
* namespaces: [Namespace!]!
* }
* </pre>
*
Expand All @@ -48,13 +57,28 @@ public class EditingContextTypeProvider implements ITypeProvider {

public static final String STEREOTYPE_DESCRIPTIONS_FIELD = "stereotypeDescriptions"; //$NON-NLS-1$

public static final String CHILD_CREATION_DESCRIPTIONS_FIELD = "childCreationDescriptions"; //$NON-NLS-1$

public static final String CLASS_ID_ARGUMENT = "classId"; //$NON-NLS-1$

public static final String ROOT_OBJECT_CREATION_DESCRIPTIONS_FIELD = "rootObjectCreationDescriptions"; //$NON-NLS-1$

public static final String NAMESPACE_ID_ARGUMENT = "namespaceId"; //$NON-NLS-1$

public static final String SUGGESTED_ARGUMENT = "suggested"; //$NON-NLS-1$

public static final String NAMESPACES_FIELD = "namespaces"; //$NON-NLS-1$

@Override
public Set<GraphQLType> getTypes() {
// @formatter:off
GraphQLObjectType editingContextType = GraphQLObjectType.newObject()
.name(TYPE)
.field(new IdFieldProvider().getField())
.field(this.getStereotypeDescriptionsField())
.field(this.getChildCreationDescriptionsField())
.field(this.getRootObjectCreationDescriptionsField())
.field(this.getNamespaceField())
.build();
// @formatter:on

Expand All @@ -73,4 +97,61 @@ private GraphQLFieldDefinition getStereotypeDescriptionsField() {
// @formatter:on
}

private GraphQLFieldDefinition getChildCreationDescriptionsField() {
// @formatter:off
return newFieldDefinition()
.name(CHILD_CREATION_DESCRIPTIONS_FIELD)
.argument(this.getClassIdArgument())
.type(nonNull(list(nonNull(typeRef(ChildCreationDescription.class.getSimpleName())))))
.build();
// @formatter:on
}

private GraphQLArgument getClassIdArgument() {
// @formatter:off
return newArgument()
.name(CLASS_ID_ARGUMENT)
.type(nonNull(Scalars.GraphQLID))
.build();
// @formatter:on
}

private GraphQLFieldDefinition getRootObjectCreationDescriptionsField() {
// @formatter:off
return newFieldDefinition()
.name(ROOT_OBJECT_CREATION_DESCRIPTIONS_FIELD)
.type(nonNull(list(nonNull(typeRef(ChildCreationDescription.class.getSimpleName())))))
.argument(this.getNamespaceIdArgument())
.argument(this.getSuggestedArgument())
.build();
// @formatter:on
}

private GraphQLArgument getNamespaceIdArgument() {
// @formatter:off
return newArgument()
.name(NAMESPACE_ID_ARGUMENT)
.type(nonNull(Scalars.GraphQLID))
.build();
// @formatter:on
}

private GraphQLArgument getSuggestedArgument() {
// @formatter:off
return newArgument()
.name(SUGGESTED_ARGUMENT)
.type(nonNull(Scalars.GraphQLBoolean))
.build();
// @formatter:on
}

private GraphQLFieldDefinition getNamespaceField() {
// @formatter:off
return newFieldDefinition()
.name(NAMESPACES_FIELD)
.type(nonNull(list(nonNull(typeRef(Namespace.class.getSimpleName())))))
.build();
// @formatter:on
}

}
Loading

0 comments on commit 4f433f6

Please sign in to comment.