Implemented scanning of multiple models packages (#3)

This commit is contained in:
Francesco 2023-10-02 09:16:20 +02:00
parent 52a2939b35
commit 1d66169a43
4 changed files with 57 additions and 41 deletions

View File

@ -23,6 +23,7 @@ import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.ParameterizedType; import java.lang.reflect.ParameterizedType;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
@ -70,7 +71,7 @@ public class DbAdmin {
private List<DbObjectSchema> schemas = new ArrayList<>(); private List<DbObjectSchema> schemas = new ArrayList<>();
private String modelsPackage; private List<String> modelsPackage;
/** /**
* Builds the DbAdmin instance by scanning the `@Entity` beans and loading * Builds the DbAdmin instance by scanning the `@Entity` beans and loading
@ -79,18 +80,21 @@ public class DbAdmin {
* @param properties the configuration properties * @param properties the configuration properties
*/ */
public DbAdmin(@Autowired EntityManager entityManager, @Autowired DbAdminProperties properties) { public DbAdmin(@Autowired EntityManager entityManager, @Autowired DbAdminProperties properties) {
this.modelsPackage = properties.getModelsPackage(); this.modelsPackage = Arrays.stream(properties.getModelsPackage().split(",")).map(String::trim).toList();
this.entityManager = entityManager; this.entityManager = entityManager;
ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(false); ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(false);
provider.addIncludeFilter(new AnnotationTypeFilter(Entity.class)); provider.addIncludeFilter(new AnnotationTypeFilter(Entity.class));
Set<BeanDefinition> beanDefs = provider.findCandidateComponents(modelsPackage); for (String currentPackage : modelsPackage) {
for (BeanDefinition bd : beanDefs) { Set<BeanDefinition> beanDefs = provider.findCandidateComponents(currentPackage);
schemas.add(processBeanDefinition(bd)); for (BeanDefinition bd : beanDefs) {
schemas.add(processBeanDefinition(bd));
}
logger.info("Scanned package '" + currentPackage + "'. Loaded " + beanDefs.size() + " schemas.");
} }
logger.info("Spring Boot Database Admin initialized. Loaded " + schemas.size() + " table definitions"); logger.info("Spring Boot Database Admin initialized. Loaded " + schemas.size() + " schemas from " + modelsPackage.size() + " packages");
logger.info("Spring Boot Database Admin web interface at: http://YOUR_HOST:YOUR_PORT/" + properties.getBaseUrl()); logger.info("Spring Boot Database Admin web interface at: http://YOUR_HOST:YOUR_PORT/" + properties.getBaseUrl());
} }
@ -154,6 +158,7 @@ public class DbAdmin {
CustomJpaRepository simpleJpaRepository = new CustomJpaRepository(schema, entityManager); CustomJpaRepository simpleJpaRepository = new CustomJpaRepository(schema, entityManager);
schema.setJpaRepository(simpleJpaRepository); schema.setJpaRepository(simpleJpaRepository);
logger.debug("Processing class: " + klass + " - Table: " + schema.getTableName()); logger.debug("Processing class: " + klass + " - Table: " + schema.getTableName());
Field[] fields = klass.getDeclaredFields(); Field[] fields = klass.getDeclaredFields();

View File

@ -103,10 +103,13 @@ public class DefaultDbAdminController {
}).collect(Collectors.toList()); }).collect(Collectors.toList());
} }
Map<String, List<DbObjectSchema>> groupedBy =
schemas.stream().collect(Collectors.groupingBy(s -> s.getBasePackage()));
Map<String, Long> counts = Map<String, Long> counts =
schemas.stream().collect(Collectors.toMap(s -> s.getClassName(), s -> repository.count(s))); schemas.stream().collect(Collectors.toMap(s -> s.getClassName(), s -> repository.count(s)));
model.addAttribute("schemas", schemas); model.addAttribute("schemas", groupedBy);
model.addAttribute("query", query); model.addAttribute("query", query);
model.addAttribute("counts", counts); model.addAttribute("counts", counts);
model.addAttribute("activePage", "entities"); model.addAttribute("activePage", "entities");

View File

@ -115,6 +115,10 @@ public class DbObjectSchema {
} }
} }
public String getBasePackage() {
return entityClass.getPackageName();
}
/** /**
* Returns the DbAdmin instance * Returns the DbAdmin instance
* @return the DbAdmin instance * @return the DbAdmin instance

View File

@ -21,40 +21,44 @@
<div class="col"> <div class="col">
<div class="box"> <div class="box">
<table class="table table-striped mt-4" th:if="${!schemas.isEmpty()}"> <th:block th:each="package : ${schemas.keySet()}">
<tr> <h4 class="fw-bold" th:text="${package}"></h4>
<th>Table</th> <table class="table table-striped mt-4" th:if="${!schemas.isEmpty()}">
<th>Rows</th> <tr>
<th>Java class</th> <th>Table</th>
<th></th> <th>Rows</th>
</tr> <th>Java class</th>
<tr th:each="schema : ${schemas}"> <th></th>
<td> </tr>
<a th:text="${schema.getTableName()}" <tr th:each="schema : ${schemas.get(package)}">
th:href="|/${baseUrl}/model/${schema.getClassName()}|"></a> <td>
</td> <a th:text="${schema.getTableName()}"
<td> th:href="|/${baseUrl}/model/${schema.getClassName()}|"></a>
<span th:text="${counts.get(schema.getClassName())}"></span> </td>
</td> <td>
<td> <span th:text="${counts.get(schema.getClassName())}"></span>
<span th:text="${schema.getClassName()}"></span> </td>
</td> <td>
<td class="text-end row-icons"> <span th:text="${schema.getClassName()}"></span>
<a title="List all" th:href="|/${baseUrl}/model/${schema.getClassName()}|"><i class="bi bi-list"></i></i></a> </td>
<a title="Create new" th:href="|/${baseUrl}/model/${schema.getClassName()}/create|"><i class="bi bi-plus-square"></i></a> <td class="text-end row-icons">
</td> <a title="List all" th:href="|/${baseUrl}/model/${schema.getClassName()}|"><i class="bi bi-list"></i></i></a>
</tr> <a title="Create new" th:href="|/${baseUrl}/model/${schema.getClassName()}/create|"><i class="bi bi-plus-square"></i></a>
</table> </td>
<div class="p-0 m-0" th:if="${schemas.isEmpty()}"> </tr>
No entities have been loaded from Java classes. </table>
<ul class="mt-3"> <div class="p-0 m-0" th:if="${schemas.isEmpty()}">
<li>Make sure you are initializing Spring Boot DB Admin Panel correctly and double check No entities have been loaded from Java classes.
that the package you have set in the CommandLineRunner is the correct one.</li>
<li>Check that the Java classes in the package have been correctly marked with <ul class="mt-3">
the <code>@Entity</code> annotation.</li> <li>Make sure you are initializing Spring Boot DB Admin Panel correctly and double check
</ul> that the package you have set in the CommandLineRunner is the correct one.</li>
</div> <li>Check that the Java classes in the package have been correctly marked with
the <code>@Entity</code> annotation.</li>
</ul>
</div>
</th:block>
</div> </div>
</div> </div>
</div> </div>