Skip to content

Commit

Permalink
Merge pull request #1 from ContextMapper/feature/discovery-aggregates…
Browse files Browse the repository at this point in the history
…-by-spring-resources

Discover Aggregates and Entities in Spring Boot discovery strategy
  • Loading branch information
stefan-ka authored Nov 1, 2019
2 parents 77fce2c + aec9829 commit dd4ca62
Show file tree
Hide file tree
Showing 32 changed files with 1,797 additions and 20 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ if (!project.hasProperty('signing.secretKeyRingFile')) {
dependencies {
implementation 'org.reflections:reflections:0.9.11'
implementation 'org.springframework.boot:spring-boot-autoconfigure:2.2.0.RELEASE'
implementation 'org.springframework:spring-web:5.2.0.RELEASE'
implementation 'org.apache.commons:commons-lang3:3.9'
implementation 'commons-io:commons-io:2.6'
implementation 'org.yaml:snakeyaml:1.25'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

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;
Expand All @@ -29,6 +30,7 @@
public class ContextMapToCMLConverter {

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

public ContextMappingModel convert(org.contextmapper.discovery.model.ContextMap inputMap) {
ContextMappingModel model = ContextMappingDSLFactory.eINSTANCE.createContextMappingModel();
Expand All @@ -50,22 +52,65 @@ public ContextMappingModel convert(org.contextmapper.discovery.model.ContextMap
contextMap.getRelationships().add(convert(relationship));
}

updateEntityAttributesAndReferences();

return model;
}

private BoundedContext convert(org.contextmapper.discovery.model.BoundedContext inputContext) {
BoundedContext bc = ContextMappingDSLFactory.eINSTANCE.createBoundedContext();
bc.setName(inputContext.getName());
bc.setImplementationTechnology(inputContext.getTechnology());
for (org.contextmapper.discovery.model.Aggregate aggregate : inputContext.getAggregates()) {
bc.getAggregates().add(convert(aggregate));
}
this.boundedContextMap.put(inputContext.getName(), bc);
return bc;
}

private Aggregate convert(org.contextmapper.discovery.model.Aggregate inputAggregate) {
Aggregate aggregate = ContextMappingDSLFactory.eINSTANCE.createAggregate();
aggregate.setName(inputAggregate.getName());
for (org.contextmapper.discovery.model.Entity entity : inputAggregate.getEntities()) {
aggregate.getDomainObjects().add(convert(entity));
}
return aggregate;
}

private Entity convert(org.contextmapper.discovery.model.Entity inputEntity) {
Entity entity = TacticdslFactory.eINSTANCE.createEntity();
entity.setName(inputEntity.getName());
entityLookupMap.put(inputEntity, entity);
return entity;
}

private UpstreamDownstreamRelationship convert(Relationship relationship) {
UpstreamDownstreamRelationship upstreamDownstreamRelationship = ContextMappingDSLFactory.eINSTANCE.createUpstreamDownstreamRelationship();
upstreamDownstreamRelationship.setUpstream(this.boundedContextMap.get(relationship.getUpstream().getName()));
upstreamDownstreamRelationship.setDownstream(this.boundedContextMap.get(relationship.getDownstream().getName()));
return upstreamDownstreamRelationship;
}

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

private void updateEntity(org.contextmapper.discovery.model.Entity inputEntity, Entity entity) {
for (org.contextmapper.discovery.model.Attribute inputAttribute : inputEntity.getAttributes()) {
Attribute attribute = TacticdslFactory.eINSTANCE.createAttribute();
attribute.setName(inputAttribute.getName());
attribute.setType(inputAttribute.getType());
attribute.setCollectionType(CollectionType.get(inputAttribute.getCollectionType()));
entity.getAttributes().add(attribute);
}
for (org.contextmapper.discovery.model.Reference inputReference : inputEntity.getReferences()) {
Reference reference = TacticdslFactory.eINSTANCE.createReference();
reference.setName(inputReference.getName());
reference.setDomainObjectType(this.entityLookupMap.get(inputReference.getType()));
reference.setCollectionType(CollectionType.get(inputReference.getCollectionType()));
entity.getReferences().add(reference);
}
}
}
104 changes: 104 additions & 0 deletions src/main/java/org/contextmapper/discovery/model/Aggregate.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/*
* Copyright 2019 The Context Mapper Project Team
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.contextmapper.discovery.model;

import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;

import java.util.HashSet;
import java.util.Set;

/**
* Represents a discovered Aggregate.
*
* @author Stefan Kapferer
*/
public class Aggregate {

private String name;
private Set<Entity> entities;

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

/**
* Sets the name of this Aggregate.
*
* @param name the name of the Aggregate.
*/
public void setName(String name) {
if (name == null || "".equals(name))
throw new IllegalArgumentException("The name of an Aggregate must not be null or empty.");
this.name = name;
}

/**
* Gets the name of this Aggregate.
*
* @return the name of the Aggregate as String
*/
public String getName() {
return name;
}

/**
* Adds an entity to the Aggregate.
*
* @param entity the entity to be added to the Aggregate
*/
public void addEntity(Entity entity) {
this.entities.add(entity);
}

/**
* Adds all entities in the given set to the Aggregate.
*
* @param entities the set of entities to be added to the Aggregate
*/
public void addEntities(Set<Entity> entities) {
this.entities.addAll(entities);
}

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

@Override
public boolean equals(Object object) {
if (!(object instanceof Aggregate))
return false;

Aggregate bc = (Aggregate) object;

return new EqualsBuilder()
.append(name, bc.name)
.isEquals();
}

@Override
public int hashCode() {
return new HashCodeBuilder()
.append(name)
.hashCode();
}
}
122 changes: 122 additions & 0 deletions src/main/java/org/contextmapper/discovery/model/Attribute.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
/*
* Copyright 2019 The Context Mapper Project Team
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.contextmapper.discovery.model;

import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;

/**
* Represents an entity attribute (primitive types).
* For references to other non-primitive types use {@link Reference}.
*
* @author Stefan Kapferer
*/
public class Attribute {

private Entity parent;
private String name;
private String type;
private String collectionType;

public Attribute(String type, String name) {
this.type = type;
this.name = name;
}

/**
* Gets the name of the attribute.
*
* @return the name of the attribute
*/
public String getName() {
return name;
}

/**
* Gets the type of the attribute.
*
* @return the type of the attribute
*/
public String getType() {
return type;
}

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

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

/**
* Sets the collection type of the reference attribute.
*
* @param collectionType the collection type of the reference attribute
*/
public void setCollectionType(String collectionType) {
this.collectionType = collectionType;
}

/**
* Gets the collection type of the reference attribute.
*
* @return the collection type of the reference attribute
*/
public String getCollectionType() {
return collectionType;
}

/**
* Returns whether the reference attribute is a collection of references or not.
*
* @return true, if the reference attribute is a collection of references. false otherwise.
*/
public boolean isCollection() {
return collectionType == null || "".equals(collectionType);
}

@Override
public boolean equals(Object object) {
if (!(object instanceof Attribute))
return false;

Attribute attribute = (Attribute) object;

return new EqualsBuilder()
.append(parent, attribute.parent)
.append(name, attribute.name)
.isEquals();
}

@Override
public int hashCode() {
return new HashCodeBuilder()
.append(parent)
.append(name)
.hashCode();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;

import java.util.HashSet;
import java.util.Set;

/**
* Represents a discovered Bounded Context.
*
Expand All @@ -27,9 +30,11 @@ public class BoundedContext {

private String name;
private String technology;
private Set<Aggregate> aggregates;

public BoundedContext(String name) {
setName(name);
this.aggregates = new HashSet<>();
}

/**
Expand Down Expand Up @@ -70,6 +75,24 @@ public String getTechnology() {
return technology;
}

/**
* Adds Aggregates to the Bounded Context.
*
* @param aggregates the set of Aggregates to be added
*/
public void addAggregates(Set<Aggregate> aggregates) {
this.aggregates.addAll(aggregates);
}

/**
* Gets the Aggregates of the Bounded Context.
*
* @return the set of Aggregates of the Bounded Context
*/
public Set<Aggregate> getAggregates() {
return new HashSet<>(aggregates);
}

@Override
public boolean equals(Object object) {
if (!(object instanceof BoundedContext))
Expand Down
Loading

0 comments on commit dd4ca62

Please sign in to comment.