From 05c9daa38ea210a7e99fbffbbf916f94fe3df204 Mon Sep 17 00:00:00 2001 From: Martin Wittlinger Date: Sun, 16 Jul 2023 17:37:30 +0200 Subject: [PATCH] refactor(database): Refactor database migration to constructor CDI (#856) --- .../persistence/DataBaseMigration.java | 31 +++++++++++-------- .../spoonutils/matcher/Matchers.java | 23 ++++++++++++++ 2 files changed, 41 insertions(+), 13 deletions(-) diff --git a/github-bot/src/main/java/io/github/martinwitt/laughing_train/persistence/DataBaseMigration.java b/github-bot/src/main/java/io/github/martinwitt/laughing_train/persistence/DataBaseMigration.java index 21d7a0d56..4d7590408 100644 --- a/github-bot/src/main/java/io/github/martinwitt/laughing_train/persistence/DataBaseMigration.java +++ b/github-bot/src/main/java/io/github/martinwitt/laughing_train/persistence/DataBaseMigration.java @@ -30,23 +30,29 @@ public class DataBaseMigration { private static final FluentLogger logger = FluentLogger.forEnclosingClass(); - @Inject ProjectConfigRepository projectConfigRepository; - - @Inject ProjectRepository projectRepository; - - @Inject BadSmellRepository badSmellRepository; // we use this for faster mongodb access - @Inject MongoBadSmellRepository badSmellRepositoryImpl; - - @Inject MongoProjectRepository projectRepositoryImpl; + Vertx vertx; @Inject - Vertx vertx; + public DataBaseMigration( + ProjectConfigRepository projectConfigRepository, + ProjectRepository projectRepository, + BadSmellRepository badSmellRepository, + MongoBadSmellRepository badSmellRepositoryImpl, + MongoProjectRepository projectRepositoryImpl, + Vertx vertx) { + this.projectConfigRepository = projectConfigRepository; + this.projectRepository = projectRepository; + this.badSmellRepository = badSmellRepository; + this.badSmellRepositoryImpl = badSmellRepositoryImpl; + this.projectRepositoryImpl = projectRepositoryImpl; + this.vertx = vertx; + } /** * This method is called by the quarkus framework to migrate the database. @@ -56,10 +62,9 @@ public void onStart(@Observes StartupEvent event) { } public void checkPeriodic() { - vertx.setPeriodic( - TimeUnit.MINUTES.toMillis(2), - TimeUnit.MINUTES.toMillis(60), - id -> vertx.executeBlocking(promise -> migrateDataBase(promise))); + vertx.setPeriodic(TimeUnit.MINUTES.toMillis(2), TimeUnit.MINUTES.toMillis(60), id -> vertx.executeBlocking( + promise -> migrateDataBase(promise)) + .onFailure(v -> logger.atSevere().withCause(v).log("Error while migrating database"))); } private void migrateDataBase(Promise promise) { diff --git a/matcher/src/main/java/io/github/martinwitt/laughing_train/spoonutils/matcher/Matchers.java b/matcher/src/main/java/io/github/martinwitt/laughing_train/spoonutils/matcher/Matchers.java index c7a9bc88c..eb00190af 100644 --- a/matcher/src/main/java/io/github/martinwitt/laughing_train/spoonutils/matcher/Matchers.java +++ b/matcher/src/main/java/io/github/martinwitt/laughing_train/spoonutils/matcher/Matchers.java @@ -4,7 +4,9 @@ import spoon.reflect.code.CtLiteral; import spoon.reflect.declaration.CtModifiable; import spoon.reflect.declaration.CtType; +import spoon.reflect.declaration.CtTypedElement; import spoon.reflect.declaration.ModifierKind; +import spoon.reflect.factory.Factory; /** * A utility class for creating matchers for Spoon elements. @@ -68,6 +70,27 @@ public static Matcher isStatic() { return v -> v.isStatic(); } + /** + * Returns a matcher that matches elements that are subtypes of the given class. + * @param fqClassname fully qualified classname of the class to match e.g. java.lang.String + * @param factory spoon factory to create a reference to the class + * @return a matcher that matches elements that are subtypes of the given class + */ + public static Matcher> isSubtypeOf(String fqClassname, Factory factory) { + return v -> + v.getType() != null && v.getType().isSubtypeOf(factory.Type().createReference(fqClassname)); + } + + /** + * Returns a matcher that matches elements that are the type. + * @param fqClassname fully qualified classname of the class to match e.g. java.lang.String + * @param factory spoon factory to create a reference to the class + * @return a matcher that matches elements that are the same type as the matched element + */ + public static Matcher> isType(String fqClassname, Factory factory) { + return v -> v.getType() != null && v.getType().getQualifiedName().equals(fqClassname); + } + /** * Returns a matcher that matches elements that match all of the given matchers. *