This commit is contained in:
Francesco 2023-09-27 10:45:59 +02:00
parent 63470ff412
commit 87d345b679
5 changed files with 10 additions and 123 deletions

View File

@ -7,8 +7,9 @@ import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
import java.util.logging.Logger;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider; import org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider;
@ -44,9 +45,8 @@ import tech.ailef.dbadmin.external.misc.Utils;
*/ */
@Component @Component
public class DbAdmin { public class DbAdmin {
private static final Logger logger = Logger.getLogger(DbAdmin.class.getName()); private static final Logger logger = LoggerFactory.getLogger(DbAdmin.class.getName());
// @PersistenceContext
private EntityManager entityManager; private EntityManager entityManager;
private List<DbObjectSchema> schemas = new ArrayList<>(); private List<DbObjectSchema> schemas = new ArrayList<>();
@ -129,22 +129,20 @@ public class DbAdmin {
CustomJpaRepository simpleJpaRepository = new CustomJpaRepository(schema, entityManager); CustomJpaRepository simpleJpaRepository = new CustomJpaRepository(schema, entityManager);
schema.setJpaRepository(simpleJpaRepository); schema.setJpaRepository(simpleJpaRepository);
System.out.println("\n\n******************************************************"); logger.debug("Processing class: " + klass + " - Table: " + schema.getTableName());
System.out.println("* Class: " + klass + " - Table: " + schema.getTableName());
System.out.println("******************************************************");
Field[] fields = klass.getDeclaredFields(); Field[] fields = klass.getDeclaredFields();
for (Field f : fields) { for (Field f : fields) {
System.out.println(" - Mapping field " + f);
DbField field = mapField(f, schema); DbField field = mapField(f, schema);
if (field == null) { if (field == null) {
throw new DbAdminException("Impossible to map field: " + f); throw new DbAdminException("Impossible to map field: " + f);
} }
field.setSchema(schema); field.setSchema(schema);
schema.addField(field); schema.addField(field);
} }
logger.debug("Processed " + klass + ", extracted " + schema.getSortedFields().size() + " fields");
return schema; return schema;
} catch (ClassNotFoundException | } catch (ClassNotFoundException |
IllegalArgumentException | SecurityException e) { IllegalArgumentException | SecurityException e) {

View File

@ -1,106 +0,0 @@
package tech.ailef.dbadmin.external.controller.rest;
import java.util.List;
import java.util.stream.Collectors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import tech.ailef.dbadmin.external.DbAdmin;
import tech.ailef.dbadmin.external.DbAdminProperties;
import tech.ailef.dbadmin.external.dbmapping.DbAdminRepository;
import tech.ailef.dbadmin.external.dbmapping.DbObjectSchema;
import tech.ailef.dbadmin.external.dto.PaginatedResult;
import tech.ailef.dbadmin.external.exceptions.DbAdminException;
@RestController
@RequestMapping(value = {"/${dbadmin.baseUrl}/api", "/${dbadmin.baseUrl}/api/"})
public class DefaultDbAdminRestController {
@Autowired
public DbAdmin dbAdmin;
@Autowired
private DbAdminProperties properties;
@Autowired
private JdbcTemplate jdbcTemplate;
// @Autowired
// @Qualifier("internalJdbc")
// private JdbcTemplate internalJdbc;
// @GetMapping("/configuration")
// public ResponseEntity<?> conf() {
// return ResponseEntity.ok(properties.toMap());
// }
@GetMapping
public ResponseEntity<?> index(@RequestParam(required = false) String query) {
checkInit();
List<DbObjectSchema> schemas = dbAdmin.getSchemas();
if (query != null && !query.isBlank()) {
schemas = schemas.stream().filter(s -> {
return s.getClassName().toLowerCase().contains(query.toLowerCase())
|| s.getTableName().toLowerCase().contains(query.toLowerCase());
}).collect(Collectors.toList());
}
return ResponseEntity.ok(schemas);
}
@GetMapping("/model/{className}")
public ResponseEntity<?> list(@PathVariable String className,
@RequestParam(required=false) Integer page, @RequestParam(required=false) Integer pageSize,
@RequestParam(required=false) String sortKey, @RequestParam(required=false) String sortOrder) {
checkInit();
DbAdminRepository repository = new DbAdminRepository(jdbcTemplate);
if (page == null) page = 1;
if (pageSize == null) pageSize = 50;
DbObjectSchema schema = dbAdmin.findSchemaByClassName(className);
PaginatedResult result = repository.findAll(schema, page, pageSize, sortKey, sortOrder);
return ResponseEntity.ok(result);
}
@GetMapping("/model/{className}/schema")
public ResponseEntity<?> schema(@PathVariable String className) {
checkInit();
DbObjectSchema schema = dbAdmin.findSchemaByClassName(className);
return ResponseEntity.ok(schema);
}
// @GetMapping("/model/{className}/show/{id}")
// public ResponseEntity<?> show(@PathVariable String className, @PathVariable String id,
// @RequestParam(required = false) Boolean expand) {
// checkInit();
// DbAdminRepository repository = new DbAdminRepository(jdbcTemplate);
// if (expand == null) expand = true;
//
// DbObjectSchema schema = dbAdmin.findSchemaByClassName(className);
//
// DbObject object = repository.findById(schema, id).orElseThrow(() -> {
// return new ResponseStatusException(
// HttpStatus.NOT_FOUND, "Object " + className + " with id " + id + " not found"
// );
// });
//
// return ResponseEntity.ok(new DbObjectDTO(object, expand));
// }
private void checkInit() {
if (dbAdmin == null)
throw new DbAdminException("Not initialized correctly: DB_ADMIN object is null.");
}
}

View File

@ -81,7 +81,7 @@ public class DbAdminRepository {
* @return * @return
*/ */
@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")
public PaginatedResult findAll(DbObjectSchema schema, int page, int pageSize, String sortKey, String sortOrder) { public PaginatedResult<DbObject> findAll(DbObjectSchema schema, int page, int pageSize, String sortKey, String sortOrder) {
SimpleJpaRepository repository = schema.getJpaRepository(); SimpleJpaRepository repository = schema.getJpaRepository();
long maxElement = count(schema); long maxElement = count(schema);

View File

@ -1,7 +1,6 @@
package tech.ailef.dbadmin.external.dbmapping; package tech.ailef.dbadmin.external.dbmapping;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Objects; import java.util.Objects;
import java.util.Set; import java.util.Set;

View File

@ -187,12 +187,8 @@ public class DbObjectSchema {
} }
public List<DbObject> findAll() { public List<DbObject> findAll() {
List r = jpaRepository.findAll(); List<?> r = jpaRepository.findAll();
List<DbObject> results = new ArrayList<>(); return r.stream().map(o -> new DbObject(o, this)).toList();
for (Object o : r) {
results.add(new DbObject(o, this));
}
return results;
} }
@Override @Override