Skip to content

Commit

Permalink
Update Bounded Context discovery to discover aggregate root entities …
Browse files Browse the repository at this point in the history
…with methods (#6)
  • Loading branch information
stefan-ka authored Nov 21, 2019
1 parent 4fcc016 commit 7917e2a
Show file tree
Hide file tree
Showing 29 changed files with 1,622 additions and 692 deletions.
2 changes: 1 addition & 1 deletion Examples/LakesideMutual/gradle.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# dependency versions
discoveryLibVersion=1.1.0
discoveryLibVersion=1.2.0-SNAPSHOT
lakesideMutualVersion=0.0.1-SNAPSHOT
409 changes: 258 additions & 151 deletions Examples/LakesideMutual/src-gen/lakesidemutual.cml

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ ossReleaseStagingRepository=https://oss.sonatype.org/service/local/staging/deplo

# dependency versions
jUnitVersion=5.5.2
cmlVersion=5.3.2
cmlVersion=5.5.0
reflectionsVersion=0.9.11
springBootVersion=2.2.0.RELEASE
springWebVersion=5.2.0.RELEASE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@
*/
package org.contextmapper.discovery.cml;

import org.contextmapper.discovery.model.Method;
import org.contextmapper.discovery.model.Relationship;
import org.contextmapper.dsl.contextMappingDSL.*;
import org.contextmapper.tactic.dsl.tacticdsl.*;

import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.*;

import static org.contextmapper.discovery.model.DomainObjectType.ENTITY;

/**
* Converts a {@link org.contextmapper.discovery.model.ContextMap} to the CML {@link org.contextmapper.dsl.contextMappingDSL.ContextMap}
Expand All @@ -31,7 +32,7 @@
public class ContextMapToCMLConverter {

private Map<String, BoundedContext> boundedContextMap = new HashMap<>();
private Map<org.contextmapper.discovery.model.Entity, Entity> entityLookupMap = new HashMap<>();
private Map<org.contextmapper.discovery.model.DomainObject, DomainObject> domainObjectLookupMap = new HashMap<>();

public ContextMappingModel convert(org.contextmapper.discovery.model.ContextMap inputMap) {
ContextMappingModel model = ContextMappingDSLFactory.eINSTANCE.createContextMappingModel();
Expand All @@ -45,11 +46,6 @@ public ContextMappingModel convert(org.contextmapper.discovery.model.ContextMap
}

for (Relationship relationship : inputMap.getRelationships()) {
UpstreamDownstreamRelationship upstreamDownstreamRelationship = convert(relationship);
if (!contextMap.getBoundedContexts().contains(upstreamDownstreamRelationship.getUpstream()))
contextMap.getBoundedContexts().add(upstreamDownstreamRelationship.getUpstream());
if (!contextMap.getBoundedContexts().contains(upstreamDownstreamRelationship.getDownstream()))
contextMap.getBoundedContexts().add(upstreamDownstreamRelationship.getDownstream());
contextMap.getRelationships().add(convert(relationship));
}

Expand All @@ -72,21 +68,73 @@ private BoundedContext convert(org.contextmapper.discovery.model.BoundedContext
private Aggregate convert(org.contextmapper.discovery.model.Aggregate inputAggregate) {
Aggregate aggregate = ContextMappingDSLFactory.eINSTANCE.createAggregate();
aggregate.setName(inputAggregate.getName());
aggregate.setComment("// " + inputAggregate.getDiscoveryComment());
for (org.contextmapper.discovery.model.Entity entity : inputAggregate.getEntities()) {
aggregate.getDomainObjects().add(convert(entity));
if (inputAggregate.getDiscoveryComment() != null && !"".equals(inputAggregate.getDiscoveryComment()))
aggregate.setComment("// " + inputAggregate.getDiscoveryComment());
for (org.contextmapper.discovery.model.DomainObject domainObject : inputAggregate.getDomainObjects()) {
aggregate.getDomainObjects().add(convert(domainObject));
}
for (org.contextmapper.discovery.model.DomainObject domainObject : inputAggregate.getDomainObjects()) {
convertDomainObjectMethods(domainObject);
}
Optional<Entity> rootEntity = aggregate.getDomainObjects().stream().filter(o -> o instanceof Entity).map(o -> (Entity) o)
.filter(e -> e.getName().endsWith("_RootEntity")).findFirst();
if (rootEntity.isPresent())
rootEntity.get().setAggregateRoot(true);
return aggregate;
}

private Entity convert(org.contextmapper.discovery.model.Entity inputEntity) {
private DomainObject convert(org.contextmapper.discovery.model.DomainObject inputDomainObject) {
if (ENTITY.equals(inputDomainObject.getType()))
return convertDomainObjectToEntity(inputDomainObject);
return convertDomainObjectToValueObject(inputDomainObject);
}

private Entity convertDomainObjectToEntity(org.contextmapper.discovery.model.DomainObject inputDomainObject) {
Entity entity = TacticdslFactory.eINSTANCE.createEntity();
entity.setName(inputEntity.getName());
entity.setComment("// " + inputEntity.getDiscoveryComment());
entityLookupMap.put(inputEntity, entity);
entity.setName(inputDomainObject.getName());
domainObjectLookupMap.put(inputDomainObject, entity);
return entity;
}

private ValueObject convertDomainObjectToValueObject(org.contextmapper.discovery.model.DomainObject inputDomainObject) {
ValueObject valueObject = TacticdslFactory.eINSTANCE.createValueObject();
valueObject.setName(inputDomainObject.getName());
if (inputDomainObject.getDiscoveryComment() != null && !"".equals(inputDomainObject.getDiscoveryComment()))
valueObject.setComment("// " + inputDomainObject.getDiscoveryComment());
domainObjectLookupMap.put(inputDomainObject, valueObject);
return valueObject;
}

private void convertDomainObjectMethods(org.contextmapper.discovery.model.DomainObject inputDomainObject) {
DomainObject domainObject = this.domainObjectLookupMap.get(inputDomainObject);
for (Method inputMethod : inputDomainObject.getMethods()) {
DomainObjectOperation operation = TacticdslFactory.eINSTANCE.createDomainObjectOperation();
operation.setName(inputMethod.getName());
operation.setReturnType(createComplexType(inputMethod.getReturnType(), inputMethod.getReturnCollectionType()));
operation.getParameters().addAll(createParameters(inputMethod.getParameters()));
domainObject.getOperations().add(operation);
}
}

private Set<Parameter> createParameters(Set<org.contextmapper.discovery.model.Parameter> inputParameters) {
Set<Parameter> parameters = new HashSet<>();
for (org.contextmapper.discovery.model.Parameter inputParameter : inputParameters) {
Parameter parameter = TacticdslFactory.eINSTANCE.createParameter();
parameter.setName(inputParameter.getName());
parameter.setParameterType(createComplexType(inputParameter.getType(), inputParameter.getCollectionType()));
parameters.add(parameter);
}
return parameters;
}

private ComplexType createComplexType(org.contextmapper.discovery.model.DomainObject inputDomainObject, String collectionType) {
ComplexType complexType = TacticdslFactory.eINSTANCE.createComplexType();
complexType.setDomainObjectType(this.domainObjectLookupMap.get(inputDomainObject));
if (collectionType != null)
complexType.setCollectionType(CollectionType.get(collectionType));
return complexType;
}

private UpstreamDownstreamRelationship convert(Relationship relationship) {
UpstreamDownstreamRelationship upstreamDownstreamRelationship = ContextMappingDSLFactory.eINSTANCE.createUpstreamDownstreamRelationship();
upstreamDownstreamRelationship.setUpstream(this.boundedContextMap.get(relationship.getUpstream().getName()));
Expand All @@ -96,30 +144,31 @@ private UpstreamDownstreamRelationship convert(Relationship relationship) {
if (cmlAggregate.isPresent())
upstreamDownstreamRelationship.getUpstreamExposedAggregates().add(cmlAggregate.get());
}
upstreamDownstreamRelationship.setExposedAggregatesComment("// " + relationship.getExposedAggregatesComment());
if (relationship.getExposedAggregatesComment() != null && !"".equals(relationship.getExposedAggregatesComment()))
upstreamDownstreamRelationship.setExposedAggregatesComment("// " + relationship.getExposedAggregatesComment());
return upstreamDownstreamRelationship;
}

private void updateEntityAttributesAndReferences() {
for (Map.Entry<org.contextmapper.discovery.model.Entity, Entity> entry : this.entityLookupMap.entrySet()) {
updateEntity(entry.getKey(), entry.getValue());
for (Map.Entry<org.contextmapper.discovery.model.DomainObject, DomainObject> entry : this.domainObjectLookupMap.entrySet()) {
updateDomainObject(entry.getKey(), entry.getValue());
}
}

private void updateEntity(org.contextmapper.discovery.model.Entity inputEntity, Entity entity) {
for (org.contextmapper.discovery.model.Attribute inputAttribute : inputEntity.getAttributes()) {
private void updateDomainObject(org.contextmapper.discovery.model.DomainObject inputDomainObject, DomainObject domainObject) {
for (org.contextmapper.discovery.model.Attribute inputAttribute : inputDomainObject.getAttributes()) {
Attribute attribute = TacticdslFactory.eINSTANCE.createAttribute();
attribute.setName(inputAttribute.getName());
attribute.setType(inputAttribute.getType());
attribute.setCollectionType(CollectionType.get(inputAttribute.getCollectionType()));
entity.getAttributes().add(attribute);
domainObject.getAttributes().add(attribute);
}
for (org.contextmapper.discovery.model.Reference inputReference : inputEntity.getReferences()) {
for (org.contextmapper.discovery.model.Reference inputReference : inputDomainObject.getReferences()) {
Reference reference = TacticdslFactory.eINSTANCE.createReference();
reference.setName(inputReference.getName());
reference.setDomainObjectType(this.entityLookupMap.get(inputReference.getType()));
reference.setDomainObjectType(this.domainObjectLookupMap.get(inputReference.getType()));
reference.setCollectionType(CollectionType.get(inputReference.getCollectionType()));
entity.getReferences().add(reference);
domainObject.getReferences().add(reference);
}
}
}
31 changes: 17 additions & 14 deletions src/main/java/org/contextmapper/discovery/model/Aggregate.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@
public class Aggregate {

private String name;
private Set<Entity> entities;
private Set<DomainObject> domainObjects;
private String discoveryComment;

public Aggregate(String name) {
setName(name);
this.entities = new HashSet<>();
this.domainObjects = new HashSet<>();
}

/**
Expand All @@ -58,30 +58,33 @@ public String getName() {
}

/**
* Adds an entity to the Aggregate.
* Adds a domain object to the Aggregate.
*
* @param entity the entity to be added to the Aggregate
* @param domainObject the domain object to be added to the Aggregate
*/
public void addEntity(Entity entity) {
this.entities.add(entity);
public void addDomainObject(DomainObject domainObject) {
this.domainObjects.add(domainObject);
domainObject.setParent(this);
}

/**
* Adds all entities in the given set to the Aggregate.
* Adds all domain objects in the given set to the Aggregate.
*
* @param entities the set of entities to be added to the Aggregate
* @param domainObjects the set of domain objects to be added to the Aggregate
*/
public void addEntities(Set<Entity> entities) {
this.entities.addAll(entities);
public void addDomainObjects(Set<DomainObject> domainObjects) {
for (DomainObject domainObject : domainObjects) {
this.addDomainObject(domainObject);
}
}

/**
* Gets the set of entities within the Aggregate.
* Gets the set of domain objects within the Aggregate.
*
* @return the set of entities which are part of the Aggregate
* @return the set of domain objects which are part of the Aggregate
*/
public Set<Entity> getEntities() {
return new HashSet<>(entities);
public Set<DomainObject> getDomainObjects() {
return new HashSet<>(domainObjects);
}

/**
Expand Down
14 changes: 7 additions & 7 deletions src/main/java/org/contextmapper/discovery/model/Attribute.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
*/
public class Attribute {

private Entity parent;
private DomainObject parent;
private String name;
private String type;
private String collectionType;
Expand Down Expand Up @@ -55,20 +55,20 @@ public String getType() {
}

/**
* Gets the parent entity containing this attribute.
* Gets the parent domain object containing this attribute.
*
* @return the parent entity containing this attribute
* @return the parent domain object containing this attribute
*/
public Entity getParent() {
public DomainObject getParent() {
return parent;
}

/**
* Sets the parent entity containing this attribute.
* Sets the parent domain object containing this attribute.
*
* @param parent the parent entity containing this attribute
* @param parent the parent domain object containing this attribute
*/
public void setParent(Entity parent) {
public void setParent(DomainObject parent) {
this.parent = parent;
}

Expand Down
Loading

0 comments on commit 7917e2a

Please sign in to comment.