mirror of
https://github.com/dalbodeule/snap-admin.git
synced 2025-12-16 21:31:59 +09:00
0.0.4
This commit is contained in:
@@ -1,23 +0,0 @@
|
||||
package tech.ailef.dbadmin;
|
||||
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.ApplicationContextAware;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* Utility class the get the ApplicationContext
|
||||
*/
|
||||
@Component
|
||||
public class ApplicationContextUtils implements ApplicationContextAware {
|
||||
|
||||
private static ApplicationContext ctx;
|
||||
|
||||
@Override
|
||||
public void setApplicationContext(ApplicationContext appContext) {
|
||||
ctx = appContext;
|
||||
}
|
||||
|
||||
public static ApplicationContext getApplicationContext() {
|
||||
return ctx;
|
||||
}
|
||||
}
|
||||
@@ -6,11 +6,13 @@ import java.lang.reflect.ParameterizedType;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.beans.factory.config.BeanDefinition;
|
||||
import org.springframework.boot.autoconfigure.web.ServerProperties;
|
||||
import org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider;
|
||||
import org.springframework.core.type.filter.AnnotationTypeFilter;
|
||||
import org.springframework.stereotype.Component;
|
||||
@@ -25,8 +27,6 @@ import jakarta.persistence.ManyToOne;
|
||||
import jakarta.persistence.OneToMany;
|
||||
import jakarta.persistence.OneToOne;
|
||||
import jakarta.persistence.PersistenceContext;
|
||||
import tech.ailef.dbadmin.annotations.DbAdminAppConfiguration;
|
||||
import tech.ailef.dbadmin.annotations.DbAdminConfiguration;
|
||||
import tech.ailef.dbadmin.annotations.DisplayFormat;
|
||||
import tech.ailef.dbadmin.dbmapping.AdvancedJpaRepository;
|
||||
import tech.ailef.dbadmin.dbmapping.DbField;
|
||||
@@ -46,6 +46,8 @@ import tech.ailef.dbadmin.misc.Utils;
|
||||
*/
|
||||
@Component
|
||||
public class DbAdmin {
|
||||
private static final Logger logger = Logger.getLogger(DbAdmin.class.getName());
|
||||
|
||||
@PersistenceContext
|
||||
private EntityManager entityManager;
|
||||
|
||||
@@ -53,17 +55,8 @@ public class DbAdmin {
|
||||
|
||||
private String modelsPackage;
|
||||
|
||||
public DbAdmin(@Autowired EntityManager entityManager) {
|
||||
Map<String, Object> beansWithAnnotation =
|
||||
ApplicationContextUtils.getApplicationContext().getBeansWithAnnotation(DbAdminConfiguration.class);
|
||||
|
||||
if (beansWithAnnotation.size() != 1) {
|
||||
throw new DbAdminException("Found " + beansWithAnnotation.size() + " beans with annotation @DbAdminConfiguration, but must be unique");
|
||||
}
|
||||
|
||||
DbAdminAppConfiguration applicationClass = (DbAdminAppConfiguration) beansWithAnnotation.values().iterator().next();
|
||||
|
||||
this.modelsPackage = applicationClass.getModelsPackage();
|
||||
public DbAdmin(@Autowired EntityManager entityManager, @Autowired DbAdminProperties properties) {
|
||||
this.modelsPackage = properties.getModelsPackage();
|
||||
this.entityManager = entityManager;
|
||||
|
||||
ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(false);
|
||||
@@ -73,6 +66,9 @@ public class DbAdmin {
|
||||
for (BeanDefinition bd : beanDefs) {
|
||||
schemas.add(processBeanDefinition(bd));
|
||||
}
|
||||
|
||||
logger.info("Spring Boot Database Admin initialized. Loaded " + schemas.size() + " table definitions");
|
||||
logger.info("Spring Boot Database Admin web interface at: http://YOUR_HOST:YOUR_PORT/" + properties.getBaseUrl());
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
package tech.ailef.dbadmin;
|
||||
|
||||
import org.springframework.boot.autoconfigure.AutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
|
||||
@ConditionalOnProperty(name = "dbadmin.enabled", matchIfMissing = true)
|
||||
@ComponentScan
|
||||
@EnableConfigurationProperties(DbAdminProperties.class)
|
||||
@AutoConfiguration
|
||||
public class DbAdminAutoConfiguration {
|
||||
|
||||
}
|
||||
51
src/main/java/tech/ailef/dbadmin/DbAdminProperties.java
Normal file
51
src/main/java/tech/ailef/dbadmin/DbAdminProperties.java
Normal file
@@ -0,0 +1,51 @@
|
||||
package tech.ailef.dbadmin;
|
||||
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
|
||||
/**
|
||||
* The 'dbadmin.*' properties that can be set in the properties file
|
||||
* to configure the behaviour of Spring Boot Admin Panel.
|
||||
*/
|
||||
@ConfigurationProperties("dbadmin")
|
||||
public class DbAdminProperties {
|
||||
/**
|
||||
* Whether Spring Boot Database Admin is enabled.
|
||||
*/
|
||||
public boolean enabled = true;
|
||||
|
||||
/**
|
||||
* The prefix that is prepended to all routes registered by Spring Boot Database Admin.
|
||||
*/
|
||||
private String baseUrl;
|
||||
|
||||
/**
|
||||
* The path of the package that contains your JPA `@Entity` classes to be scanned.
|
||||
*/
|
||||
private String modelsPackage;
|
||||
|
||||
public boolean isEnabled() {
|
||||
return enabled;
|
||||
}
|
||||
|
||||
public void setEnabled(boolean enabled) {
|
||||
this.enabled = enabled;
|
||||
}
|
||||
|
||||
public String getBaseUrl() {
|
||||
return baseUrl;
|
||||
}
|
||||
|
||||
public void setBaseUrl(String baseUrl) {
|
||||
this.baseUrl = baseUrl;
|
||||
}
|
||||
|
||||
public String getModelsPackage() {
|
||||
return modelsPackage;
|
||||
}
|
||||
|
||||
public void setModelsPackage(String modelsPackage) {
|
||||
this.modelsPackage = modelsPackage;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -1,10 +0,0 @@
|
||||
package tech.ailef.dbadmin.annotations;
|
||||
|
||||
/**
|
||||
* An interface that includes all the configuration methods that
|
||||
* the user has to implement in order to integrate DbAdmin.
|
||||
*
|
||||
*/
|
||||
public interface DbAdminAppConfiguration {
|
||||
public String getModelsPackage();
|
||||
}
|
||||
@@ -1,15 +0,0 @@
|
||||
package tech.ailef.dbadmin.annotations;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
/**
|
||||
* Marks the class that holds the DbAdmin configuration.
|
||||
*
|
||||
*/
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.TYPE)
|
||||
public @interface DbAdminConfiguration {
|
||||
}
|
||||
@@ -28,6 +28,7 @@ import org.springframework.web.servlet.mvc.support.RedirectAttributes;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import tech.ailef.dbadmin.DbAdmin;
|
||||
import tech.ailef.dbadmin.DbAdminProperties;
|
||||
import tech.ailef.dbadmin.dbmapping.DbAdminRepository;
|
||||
import tech.ailef.dbadmin.dbmapping.DbObject;
|
||||
import tech.ailef.dbadmin.dbmapping.DbObjectSchema;
|
||||
@@ -41,8 +42,11 @@ import tech.ailef.dbadmin.misc.Utils;
|
||||
* The main DbAdmin controller that register most of the routes of the web interface.
|
||||
*/
|
||||
@Controller
|
||||
@RequestMapping("/dbadmin")
|
||||
@RequestMapping(value= {"/${dbadmin.baseUrl}", "/${dbadmin.baseUrl}/"})
|
||||
public class DefaultDbAdminController {
|
||||
@Autowired
|
||||
private DbAdminProperties properties;
|
||||
|
||||
@Autowired
|
||||
private DbAdminRepository repository;
|
||||
|
||||
@@ -163,7 +167,7 @@ public class DefaultDbAdminController {
|
||||
return "model/list";
|
||||
|
||||
} catch (InvalidPageException e) {
|
||||
return "redirect:/dbadmin/model/" + className;
|
||||
return "redirect:/" + properties.getBaseUrl() + "/model/" + className;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -260,7 +264,7 @@ public class DefaultDbAdminController {
|
||||
attr.addFlashAttribute("error", e.getMessage());
|
||||
}
|
||||
|
||||
return "redirect:/dbadmin/model/" + className;
|
||||
return "redirect:/" + properties.getBaseUrl() + "/model/" + className;
|
||||
}
|
||||
|
||||
@PostMapping(value="/model/{className}/delete")
|
||||
@@ -287,7 +291,7 @@ public class DefaultDbAdminController {
|
||||
if (countDeleted > 0)
|
||||
attr.addFlashAttribute("message", "Deleted " + countDeleted + " of " + ids.length + " items");
|
||||
|
||||
return "redirect:/dbadmin/model/" + className;
|
||||
return "redirect:/" + properties.getBaseUrl() + "/model/" + className;
|
||||
}
|
||||
|
||||
@PostMapping(value="/model/{className}/create")
|
||||
@@ -397,11 +401,11 @@ public class DefaultDbAdminController {
|
||||
|
||||
if (attr.getFlashAttributes().containsKey("error")) {
|
||||
if (create)
|
||||
return "redirect:/dbadmin/model/" + schema.getClassName() + "/create";
|
||||
return "redirect:/" + properties.getBaseUrl() + "/model/" + schema.getClassName() + "/create";
|
||||
else
|
||||
return "redirect:/dbadmin/model/" + schema.getClassName() + "/edit/" + pkValue;
|
||||
return "redirect:/" + properties.getBaseUrl() + "/model/" + schema.getClassName() + "/edit/" + pkValue;
|
||||
} else {
|
||||
return "redirect:/dbadmin/model/" + schema.getClassName() + "/show/" + pkValue;
|
||||
return "redirect:/" + properties.getBaseUrl() + "/model/" + schema.getClassName() + "/show/" + pkValue;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -28,7 +28,7 @@ import tech.ailef.dbadmin.exceptions.DbAdminException;
|
||||
* Controller to serve file or images (`@DisplayImage`)
|
||||
*/
|
||||
@Controller
|
||||
@RequestMapping("/dbadmin/download")
|
||||
@RequestMapping(value = {"/${dbadmin.baseUrl}/download", "/${dbadmin.baseUrl}/download/"})
|
||||
public class DownloadController {
|
||||
@Autowired
|
||||
private DbAdminRepository repository;
|
||||
|
||||
@@ -2,10 +2,12 @@ package tech.ailef.dbadmin.controller;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.ControllerAdvice;
|
||||
import org.springframework.web.bind.annotation.ModelAttribute;
|
||||
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import tech.ailef.dbadmin.DbAdminProperties;
|
||||
|
||||
/**
|
||||
* This class registers some ModelAttribute objects that are
|
||||
@@ -14,6 +16,9 @@ import jakarta.servlet.http.HttpServletRequest;
|
||||
@ControllerAdvice
|
||||
public class GlobalController {
|
||||
|
||||
@Autowired
|
||||
private DbAdminProperties props;
|
||||
|
||||
/**
|
||||
* A multi valued map containing the query parameters. It is used primarily
|
||||
* in building complex URL when performing faceted search with multiple filters.
|
||||
@@ -24,4 +29,14 @@ public class GlobalController {
|
||||
public Map<String, String[]> getQueryParams(HttpServletRequest request) {
|
||||
return request.getParameterMap();
|
||||
}
|
||||
|
||||
/**
|
||||
* The baseUrl as specified in the properties file by the user
|
||||
* @param request
|
||||
* @return
|
||||
*/
|
||||
@ModelAttribute("baseUrl")
|
||||
public String getBaseUrl(HttpServletRequest request) {
|
||||
return props.getBaseUrl();
|
||||
}
|
||||
}
|
||||
@@ -20,7 +20,7 @@ import tech.ailef.dbadmin.dto.AutocompleteSearchResult;
|
||||
* API controller for autocomplete results
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/dbadmin/api/autocomplete")
|
||||
@RequestMapping(value= {"/${dbadmin.baseUrl}/api/autocomplete", "/${dbadmin.baseUrl}/api/autocomplete/"})
|
||||
public class AutocompleteController {
|
||||
@Autowired
|
||||
private DbAdmin dbAdmin;
|
||||
|
||||
@@ -19,7 +19,7 @@ import tech.ailef.dbadmin.dto.PaginatedResult;
|
||||
import tech.ailef.dbadmin.exceptions.DbAdminException;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/dbadmin/api")
|
||||
@RequestMapping(value = {"/${dbadmin.baseUrl}/api", "/${dbadmin.baseUrl}/api/"})
|
||||
public class DefaultDbAdminRestController {
|
||||
@Autowired
|
||||
public DbAdmin dbAdmin;
|
||||
|
||||
@@ -36,7 +36,6 @@ public class DbAdminRepository {
|
||||
|
||||
public DbAdminRepository(JdbcTemplate jdbcTemplate) {
|
||||
this.jdbcTemplate = jdbcTemplate;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -119,7 +118,7 @@ public class DbAdminRepository {
|
||||
|
||||
|
||||
return new PaginatedResult(
|
||||
new PaginationInfo(page, maxPage, pageSize, maxElement, null, sortKey, sortOrder, new HashSet<>()),
|
||||
new PaginationInfo(page, maxPage, pageSize, maxElement, null, new HashSet<>()),
|
||||
results
|
||||
);
|
||||
}
|
||||
@@ -229,7 +228,7 @@ public class DbAdminRepository {
|
||||
}
|
||||
|
||||
return new PaginatedResult(
|
||||
new PaginationInfo(page, maxPage, pageSize, maxElement, query, sortKey, sortOrder, queryFilters),
|
||||
new PaginationInfo(page, maxPage, pageSize, maxElement, query, queryFilters),
|
||||
jpaRepository.search(query, page, pageSize, sortKey, sortOrder, queryFilters).stream()
|
||||
.map(o -> new DbObject(o, schema))
|
||||
.toList()
|
||||
|
||||
@@ -41,20 +41,13 @@ public class PaginationInfo {
|
||||
|
||||
private String query;
|
||||
|
||||
private String sortKey;
|
||||
|
||||
private String sortOrder;
|
||||
|
||||
public PaginationInfo(int currentPage, int maxPage, int pageSize, long maxElement, String query,
|
||||
String sortKey, String sortOrder, Set<QueryFilter> queryFilters) {
|
||||
public PaginationInfo(int currentPage, int maxPage, int pageSize, long maxElement, String query, Set<QueryFilter> queryFilters) {
|
||||
this.currentPage = currentPage;
|
||||
this.maxPage = maxPage;
|
||||
this.pageSize = pageSize;
|
||||
this.query = query;
|
||||
this.maxElement = maxElement;
|
||||
this.queryFilters = queryFilters;
|
||||
this.sortKey = sortKey;
|
||||
this.sortOrder = sortOrder;
|
||||
}
|
||||
|
||||
public int getCurrentPage() {
|
||||
@@ -122,7 +115,14 @@ public class PaginationInfo {
|
||||
public List<Integer> getAfterPages() {
|
||||
return IntStream.range(currentPage + 1, Math.min(currentPage + PAGE_RANGE, maxPage + 1)).boxed().collect(Collectors.toList());
|
||||
}
|
||||
|
||||
//
|
||||
// public String getSortKey() {
|
||||
// return sortKey;
|
||||
// }
|
||||
//
|
||||
// public String getSortOrder() {
|
||||
// return sortOrder;
|
||||
// }
|
||||
|
||||
public boolean isLastPage() {
|
||||
return currentPage == maxPage;
|
||||
|
||||
Reference in New Issue
Block a user