Skip to content

Commit

Permalink
Merge pull request #123 from Tcharl/functionalProgramming
Browse files Browse the repository at this point in the history
Functional programming
  • Loading branch information
Tcharl authored Jan 14, 2025
2 parents 823b65e + b99c31c commit 5f2ec5d
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@ public class DataMigratorConfiguration {

/**
* Graph datasource configuration.
*
* @return
* {@return the graph datasource}
*/
public GraphDatasource getGraphDatasource() {
return graphDatasource;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@ public GraphTraversalSourceProvider(DataMigratorConfiguration dataMigratorConfig
* @return the graph traversal source.
*/
public GraphTraversalSource getGraph() {
if (dataMigratorConfiguration.getGraphDatasource().getType() == GraphDatasourceType.EMBEDDED) {
if (dataMigratorConfiguration.getGraphDatasource().getType().equals(GraphDatasourceType.EMBEDDED)) {
Graph graph = TinkerGraph.open();
return traversal().withEmbedded(graph);
} else if (dataMigratorConfiguration.getGraphDatasource().getType() == GraphDatasourceType.REMOTE) {
} else if (dataMigratorConfiguration.getGraphDatasource().getType().equals(GraphDatasourceType.REMOTE)) {
IoRegistry registry = TinkerIoRegistryV3.instance();
TypeSerializerRegistry typeSerializerRegistry = TypeSerializerRegistry.build().addRegistry(registry)
.create();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,9 @@ private void processEntitiesWithoutCycles(GraphTraversalSource modelGraph, Graph
log.debug("persisting related elements");
Stream stream = modelGraph.V().filter(not(is(P.within(processedVertices)))).toStream();
Collection set = (Collection) stream.collect(Collectors.toSet());
for (var o : set)
for (var o : set) {
persistTraversal(entityMetamodelGraph, processedVertices, Optional.of(o).stream());
}
}
}
}
Expand All @@ -127,8 +128,7 @@ private void persistTraversal(Graph<MetamodelVertex, FieldEdge<MetamodelVertex>>
return id.isPresent() && id.get() != null &&
((id.get() instanceof Long && 0L != (Long) id.get()) ||
(id.get() instanceof String && !((String) id.get()).isEmpty()) ||
id.get().getClass().getAnnotation(jakarta.persistence.Embeddable.class) != null)
;
id.get().getClass().getAnnotation(jakarta.persistence.Embeddable.class) != null);
}
);
TransactionTemplate tpl = new TransactionTemplate(sinkPlatformTxManager);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Arrays;
import java.util.Optional;
import java.util.stream.Stream;

Expand All @@ -48,22 +49,28 @@ public JpaMetamodelGraphBuilder(MetamodelVertexFactory<JpaMetamodelVertex> metam
this.metamodelVertexFactory = metamodelVertexFactory;
}

private record VertexMetamodelClassAndEntityClass(FieldAndTargetType fieldAndTargetType, Class<?> entityClass, Graph<JpaMetamodelVertex, FieldEdge<JpaMetamodelVertex>> graph){};
/**
* {@inheritDoc}
*/
@Override
protected Stream<OutboundEdge<JpaMetamodelVertex>> computeOutboundEdges(JpaMetamodelVertex sourceVertex, Graph<JpaMetamodelVertex, FieldEdge<JpaMetamodelVertex>> graph) {
return Stream.of(sourceVertex.metamodelClass().getDeclaredFields())
.flatMap(f -> targetTypeOfMetamodelField(f)
.map(targetType -> new FieldAndTargetType(f, targetType)).stream())
.flatMap(t ->
graph.vertexSet().stream().filter(candidateVertex -> candidateVertex.entityClass().equals(t.targetType()))
.filter(targetMetamodelVertex -> !relationshipProcessor.isFkIgnored(sourceVertex.entityClass(), t.field().getName()))
.filter(targetMetamodelVertex -> !relationshipProcessor.isDerived(sourceVertex.entityClass(), t.field().getName()))
.map(targetMetamodelVertex ->
metamodelVertexFactory.createOutboundEdge(metamodelVertexFactory.createFieldEdge(t.field()), targetMetamodelVertex)
)
);
record VertexMetamodelClassDeclaredFieldsAndEntityClass(Field[] fields, Class<?> entityClass, Graph<JpaMetamodelVertex, FieldEdge<JpaMetamodelVertex>> graph){};
record VertexMetamodelClassDeclaredFieldAndEntityClass(Field field, Class<?> entityClass, Graph<JpaMetamodelVertex, FieldEdge<JpaMetamodelVertex>> graph){};
record VertexMetamodelClassAndEntityClass(Class<?> metamodelClass, Class<?> entityClass, Graph<JpaMetamodelVertex, FieldEdge<JpaMetamodelVertex>> graph){};

VertexMetamodelClassAndEntityClass streamInput = new VertexMetamodelClassAndEntityClass(sourceVertex.metamodelClass(), sourceVertex.entityClass(), graph);
return Stream
.of(streamInput)
.map((VertexMetamodelClassAndEntityClass r) -> new VertexMetamodelClassDeclaredFieldsAndEntityClass(r.metamodelClass().getDeclaredFields(), r.entityClass(), r.graph()))
.flatMap((VertexMetamodelClassDeclaredFieldsAndEntityClass r) -> Arrays.stream(r.fields()).map(f -> new VertexMetamodelClassDeclaredFieldAndEntityClass(f, r.entityClass(), r.graph())))
.flatMap(f -> targetTypeOfMetamodelField(f.field(), f.entityClass(), f.graph()).stream())
.flatMap(t -> t.graph().vertexSet().stream().filter(candidateVertex -> candidateVertex.entityClass().equals(t.fieldAndTargetType().targetType()))
.filter(targetMetamodelVertex -> !relationshipProcessor.isFkIgnored(t.entityClass(), t.fieldAndTargetType().field().getName()))
.filter(targetMetamodelVertex -> !relationshipProcessor.isDerived(t.entityClass(), t.fieldAndTargetType().field().getName()))
.map(targetMetamodelVertex ->
metamodelVertexFactory.createOutboundEdge(metamodelVertexFactory.createFieldEdge(t.fieldAndTargetType().field()), targetMetamodelVertex)
));
}

@Override
Expand All @@ -74,12 +81,12 @@ protected boolean isEntity(JpaMetamodelVertex metamodelVertex) {
/**
* Get the Java Type of the target of a field.
*/
private Optional<Type> targetTypeOfMetamodelField(Field f) {
private Optional<VertexMetamodelClassAndEntityClass> targetTypeOfMetamodelField(Field f, Class<?> entityClass, Graph<JpaMetamodelVertex, FieldEdge<JpaMetamodelVertex>> graph) {
Type t = f.getGenericType();
if (t instanceof ParameterizedType pt) {
Type[] types = pt.getActualTypeArguments();
if (types.length == 2) {
return Optional.of(types[1]);
return Optional.of(new VertexMetamodelClassAndEntityClass(new FieldAndTargetType(f, types[1]), entityClass, graph));
}
}
return Optional.empty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,15 +124,17 @@ public Optional<Object> getId(MetamodelVertex metamodelVertex, Object entity) {

private Optional<Object> internalGetRawId(Class entityClass, Object entity) {
// Cannot use getRawFieldValue due to cycle and the @Transactional aspect
// log.debug("getting id for entity of class {} and entity {}", entityClass.getSimpleName(), entity);
return getPrimaryKeyGetterMethod(entityClass).map(// TODO investigate why entityClass doesn't work as expected
log.debug("getting Id of entity with class {}, entity {}", entityClass, entity);
return getPrimaryKeyGetterMethod(entityClass).map(
primaryKeyGetterMethod -> {
try {
return primaryKeyGetterMethod.invoke(entity);
} catch (Exception e) {
log.error("Exception thrown while getting the id of entity {}, with entityclass {}", entity, entityClass.getSimpleName());
if (null != entity) {
return primaryKeyGetterMethod.invoke(entity);
}
} catch (IllegalAccessException | InvocationTargetException e) {
throw new ErrorCallingRawElementMethodException(e);
}
return Optional.empty();
}
);
}
Expand Down
2 changes: 1 addition & 1 deletion report-aggregate/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>net.osgiliath.datamigrator</groupId>
<artifactId>data-migrator</artifactId>
<version>1.85-SNAPSHOT</version>
<version>1.86-SNAPSHOT</version>
</parent>
<artifactId>report-aggregate</artifactId>
<properties>
Expand Down
2 changes: 1 addition & 1 deletion sample-mono/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>net.osgiliath.datamigrator</groupId>
<artifactId>data-migrator</artifactId>
<version>1.85-SNAPSHOT</version>
<version>1.86-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<groupId>net.osgiliath.datamigrator.sample</groupId>
Expand Down

0 comments on commit 5f2ec5d

Please sign in to comment.