mirror of
https://github.com/dalbodeule/snap-admin.git
synced 2025-06-08 21:38:21 +00:00
WIP
This commit is contained in:
parent
154bb1fcb8
commit
3d9de0246e
@ -1,23 +1,29 @@
|
|||||||
package tech.ailef.dbadmin.dbmapping;
|
package tech.ailef.dbadmin.dbmapping;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
import java.time.LocalDate;
|
import java.time.LocalDate;
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
import org.springframework.data.jpa.repository.support.SimpleJpaRepository;
|
import org.springframework.data.jpa.repository.support.SimpleJpaRepository;
|
||||||
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
import jakarta.persistence.EntityManager;
|
import jakarta.persistence.EntityManager;
|
||||||
|
import jakarta.persistence.Query;
|
||||||
import jakarta.persistence.criteria.CriteriaBuilder;
|
import jakarta.persistence.criteria.CriteriaBuilder;
|
||||||
import jakarta.persistence.criteria.CriteriaQuery;
|
import jakarta.persistence.criteria.CriteriaQuery;
|
||||||
|
import jakarta.persistence.criteria.CriteriaUpdate;
|
||||||
import jakarta.persistence.criteria.Path;
|
import jakarta.persistence.criteria.Path;
|
||||||
import jakarta.persistence.criteria.Predicate;
|
import jakarta.persistence.criteria.Predicate;
|
||||||
import jakarta.persistence.criteria.Root;
|
import jakarta.persistence.criteria.Root;
|
||||||
import tech.ailef.dbadmin.dto.CompareOperator;
|
import tech.ailef.dbadmin.dto.CompareOperator;
|
||||||
import tech.ailef.dbadmin.dto.QueryFilter;
|
import tech.ailef.dbadmin.dto.QueryFilter;
|
||||||
|
import tech.ailef.dbadmin.exceptions.DbAdminException;
|
||||||
|
|
||||||
@SuppressWarnings("rawtypes")
|
@SuppressWarnings("rawtypes")
|
||||||
public class AdvancedJpaRepository extends SimpleJpaRepository {
|
public class AdvancedJpaRepository extends SimpleJpaRepository {
|
||||||
@ -145,22 +151,39 @@ public class AdvancedJpaRepository extends SimpleJpaRepository {
|
|||||||
}
|
}
|
||||||
return finalPredicates;
|
return finalPredicates;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void update(DbObjectSchema schema, Map<String, String> params, Map<String, MultipartFile> files) {
|
||||||
// @SuppressWarnings("unchecked")
|
CriteriaBuilder cb = entityManager.getCriteriaBuilder();
|
||||||
// public List<Object> distinctFieldValues(DbField field) {
|
|
||||||
// CriteriaBuilder cb = entityManager.getCriteriaBuilder();
|
CriteriaUpdate update = cb.createCriteriaUpdate(schema.getJavaClass());
|
||||||
//
|
|
||||||
// Class<?> outputType = field.getType().getJavaClass();
|
Root employee = update.from(schema.getJavaClass());
|
||||||
// if (field.getConnectedType() != null) {
|
|
||||||
// outputType = field.getConnectedSchema().getPrimaryKey().getType().getJavaClass();
|
for (DbField field : schema.getSortedFields()) {
|
||||||
// }
|
if (field.isPrimaryKey()) continue;
|
||||||
//
|
|
||||||
// CriteriaQuery query = cb.createQuery(outputType);
|
String stringValue = params.get(field.getName());
|
||||||
// Root root = query.from(schema.getJavaClass());
|
Object value = null;
|
||||||
//
|
if (stringValue != null && stringValue.isBlank()) stringValue = null;
|
||||||
// query.select(root.get(field.getJavaName()).as(outputType)).distinct(true);
|
if (stringValue != null) {
|
||||||
//
|
value = field.getType().parseValue(stringValue);
|
||||||
// return entityManager.createQuery(query).getResultList();
|
} else {
|
||||||
// }
|
try {
|
||||||
|
MultipartFile file = files.get(field.getJavaName());
|
||||||
|
if (file != null)
|
||||||
|
value = file.getBytes();
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new DbAdminException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
update.set(employee.get(field.getJavaName()), value);
|
||||||
|
}
|
||||||
|
String pkName = schema.getPrimaryKey().getJavaName();
|
||||||
|
update.where(cb.equal(employee.get(pkName), params.get(schema.getPrimaryKey().getName())));
|
||||||
|
|
||||||
|
Query query = entityManager.createQuery(update);
|
||||||
|
int rowCount = query.executeUpdate();
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -129,14 +129,18 @@ public class DbAdminRepository {
|
|||||||
* @param schema
|
* @param schema
|
||||||
* @param params
|
* @param params
|
||||||
*/
|
*/
|
||||||
|
@Transactional
|
||||||
public void update(DbObjectSchema schema, Map<String, String> params, Map<String, MultipartFile> files) {
|
public void update(DbObjectSchema schema, Map<String, String> params, Map<String, MultipartFile> files) {
|
||||||
Object[] updateArray = schema.getUpdateArray(params, files);
|
// Object[] updateArray = schema.getUpdateArray(params, files);
|
||||||
|
//
|
||||||
|
// String updateFields =
|
||||||
|
// schema.getSortedFields().stream().map(f -> "`" + f.getName() + "` = ?").collect(Collectors.joining(", "));
|
||||||
|
//
|
||||||
|
// String query = "UPDATE `" + schema.getTableName() + "` SET " + updateFields + " WHERE `" + schema.getPrimaryKey().getName() + "` = ?";
|
||||||
|
// jdbcTemplate.update(query, updateArray);
|
||||||
|
|
||||||
String updateFields =
|
schema.getJpaRepository().update(schema, params, files);
|
||||||
schema.getSortedFields().stream().map(f -> "`" + f.getName() + "` = ?").collect(Collectors.joining(", "));
|
|
||||||
|
|
||||||
String query = "UPDATE `" + schema.getTableName() + "` SET " + updateFields + " WHERE `" + schema.getPrimaryKey().getName() + "` = ?";
|
|
||||||
jdbcTemplate.update(query, updateArray);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
@ -206,17 +210,6 @@ public class DbAdminRepository {
|
|||||||
insert.execute(allValues);
|
insert.execute(allValues);
|
||||||
return primaryKey;
|
return primaryKey;
|
||||||
}
|
}
|
||||||
// String fieldsString =
|
|
||||||
// schema.getSortedFields().stream().skip(primaryKey == null ? 1 : 0).map(f -> "`" + f.getName() + "`").collect(Collectors.joining(", "));
|
|
||||||
//
|
|
||||||
// String placeholdersString =
|
|
||||||
// schema.getSortedFields().stream().skip(primaryKey == null ? 1 : 0).map(f -> "?").collect(Collectors.joining(", "));
|
|
||||||
// Object[] array = schema.getInsertArray(values, files);
|
|
||||||
//
|
|
||||||
// String query = "INSERT INTO " + schema.getTableName() + " (" + fieldsString + ") VALUES (" + placeholdersString + ");";
|
|
||||||
// jdbcTemplate.update(query, array);
|
|
||||||
|
|
||||||
// return primaryKey;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -17,8 +17,13 @@ public class DbFieldValue {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public String getFormattedValue() {
|
public String getFormattedValue() {
|
||||||
if (field.getFormat() == null) return value == null ? "NULL" : value.toString();
|
if (value == null) return null;
|
||||||
return String.format(field.getFormat(), value);
|
|
||||||
|
if (field.getFormat() == null) {
|
||||||
|
return value.toString();
|
||||||
|
} else {
|
||||||
|
return String.format(field.getFormat(), value);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public DbField getField() {
|
public DbField getField() {
|
||||||
|
@ -10,8 +10,6 @@ import java.util.Map;
|
|||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
|
||||||
|
|
||||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||||
|
|
||||||
import jakarta.persistence.ManyToMany;
|
import jakarta.persistence.ManyToMany;
|
||||||
@ -181,101 +179,6 @@ public class DbObjectSchema {
|
|||||||
&& f.getPrimitiveField().getAnnotation(Filterable.class) != null;
|
&& f.getPrimitiveField().getAnnotation(Filterable.class) != null;
|
||||||
}).toList();
|
}).toList();
|
||||||
}
|
}
|
||||||
|
|
||||||
// public List<Object> getFieldValues(DbField field) {
|
|
||||||
// return jpaRepository.distinctFieldValues(field);
|
|
||||||
// }
|
|
||||||
|
|
||||||
public Object[] getInsertArray(Map<String, String> params, Map<String, MultipartFile> files) {
|
|
||||||
int currentIndex = 0;
|
|
||||||
|
|
||||||
String pkValue = params.get(getPrimaryKey().getName());
|
|
||||||
if (pkValue == null || pkValue.isBlank())
|
|
||||||
pkValue = null;
|
|
||||||
|
|
||||||
Object[] row;
|
|
||||||
if (pkValue == null) {
|
|
||||||
row = new Object[getSortedFields().size() - 1];
|
|
||||||
} else {
|
|
||||||
row = new Object[getSortedFields().size()];
|
|
||||||
}
|
|
||||||
|
|
||||||
for (DbField field : getSortedFields()) {
|
|
||||||
// Skip the primary key if the value is null
|
|
||||||
// If it is autogenerated, it will be filled by the database
|
|
||||||
// otherwise it will throw an error
|
|
||||||
if (field.isPrimaryKey() && pkValue == null) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
String name = field.getName();
|
|
||||||
|
|
||||||
String stringValue = params.get(name);
|
|
||||||
Object value = null;
|
|
||||||
if (stringValue != null && stringValue.isBlank()) stringValue = null;
|
|
||||||
if (stringValue != null) {
|
|
||||||
value = stringValue;
|
|
||||||
} else {
|
|
||||||
value = files.get(name);
|
|
||||||
}
|
|
||||||
|
|
||||||
// String type = params.get("__dbadmin_" + name + "_type");
|
|
||||||
|
|
||||||
// if (type == null)
|
|
||||||
// throw new RuntimeException("Missing type hidden field for: " + name);
|
|
||||||
|
|
||||||
try {
|
|
||||||
if (value == null)
|
|
||||||
row[currentIndex++] = null;
|
|
||||||
else
|
|
||||||
row[currentIndex++] = value; //DbFieldType.valueOf(type).parseValue(value);
|
|
||||||
} catch (IllegalArgumentException | SecurityException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return row;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Object[] getUpdateArray(Map<String, String> params, Map<String, MultipartFile> files) {
|
|
||||||
Object[] row = new Object[getSortedFields().size() + 1];
|
|
||||||
|
|
||||||
int currentIndex = 0;
|
|
||||||
DbField primaryKey = getPrimaryKey();
|
|
||||||
String pkValue = params.get(primaryKey.getName());
|
|
||||||
|
|
||||||
for (DbField field : getSortedFields()) {
|
|
||||||
String name = field.getName();
|
|
||||||
|
|
||||||
String stringValue = params.get(name);
|
|
||||||
Object value = null;
|
|
||||||
if (stringValue != null && stringValue.isBlank()) stringValue = null;
|
|
||||||
if (stringValue != null) {
|
|
||||||
value = stringValue;
|
|
||||||
} else {
|
|
||||||
value = files.get(name);
|
|
||||||
}
|
|
||||||
|
|
||||||
// String type = params.get("__dbadmin_" + name + "_type");
|
|
||||||
|
|
||||||
// if (type == null)
|
|
||||||
// throw new RuntimeException("Missing type hidden field for: " + name);
|
|
||||||
|
|
||||||
try {
|
|
||||||
if (value == null)
|
|
||||||
row[currentIndex++] = null;
|
|
||||||
else
|
|
||||||
row[currentIndex++] = value; //DbFieldType.valueOf(type).parseValue(value);
|
|
||||||
} catch (IllegalArgumentException | SecurityException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
row[currentIndex] = primaryKey.getType().parseValue(pkValue);
|
|
||||||
|
|
||||||
return row;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
|
@ -48,13 +48,24 @@
|
|||||||
</a>
|
</a>
|
||||||
</th:block>
|
</th:block>
|
||||||
<th:block th:if="${!field.isPrimaryKey()}">
|
<th:block th:if="${!field.isPrimaryKey()}">
|
||||||
<span th:text="${object.get(field).getFormattedValue()}" th:if="${!field.isBinary()}">
|
|
||||||
</span>
|
<th:block th:if="${!field.isBinary()}">
|
||||||
|
<span th:if="${object.get(field).getFormattedValue() == null}" class="font-monospace null-label">
|
||||||
|
NULL
|
||||||
|
</span>
|
||||||
|
<span th:unless="${object.get(field).getFormattedValue() == null}"
|
||||||
|
th:text="${object.get(field).getFormattedValue()}">
|
||||||
|
|
||||||
|
</span>
|
||||||
|
|
||||||
|
</th:block>
|
||||||
<span th:unless="${!field.isBinary()}">
|
<span th:unless="${!field.isBinary()}">
|
||||||
<th:block th:if="${object.get(field).getValue()}">
|
<th:block th:if="${object.get(field).getValue()}">
|
||||||
<a class="text-decoration-none null-label"
|
<a class="text-decoration-none null-label"
|
||||||
th:href="|/dbadmin/download/${schema.getJavaClass().getName()}/${field.getName()}/${object.get(schema.getPrimaryKey()).getValue()}|">
|
th:href="|/dbadmin/download/${schema.getJavaClass().getName()}/${field.getName()}/${object.get(schema.getPrimaryKey()).getValue()}|">
|
||||||
<i class="align-middle bi bi-box-arrow-down"></i><span class="align-middle"> Download</span>
|
<i class="align-middle bi bi-box-arrow-down"></i><span class="align-middle"> Download
|
||||||
|
<!--/*--> <span class="text-muted">([[ ${object.get(field).getValue().length} ]] bytes)</span> <!--*/-->
|
||||||
|
</span>
|
||||||
</a>
|
</a>
|
||||||
</th:block>
|
</th:block>
|
||||||
<th:block th:unless="${object.get(field).getValue()}">
|
<th:block th:unless="${object.get(field).getValue()}">
|
||||||
|
@ -162,6 +162,11 @@
|
|||||||
<input type="hidden" th:value="${page.getPagination().getCurrentPage()}" th:name="page">
|
<input type="hidden" th:value="${page.getPagination().getCurrentPage()}" th:name="page">
|
||||||
<input type="hidden" th:value="${query}" th:name="query">
|
<input type="hidden" th:value="${query}" th:name="query">
|
||||||
<input type="hidden" name="pageSize">
|
<input type="hidden" name="pageSize">
|
||||||
|
<th:block th:each="p : ${queryParams.keySet()}">
|
||||||
|
<input th:each="v : ${queryParams.get(p)}"
|
||||||
|
th:name="${p}" th:value="${v}" type="hidden"
|
||||||
|
th:if="${p.startsWith('filter_')}">
|
||||||
|
</th:block>
|
||||||
<select class="form-select page-size">
|
<select class="form-select page-size">
|
||||||
<option disabled>Page size</option>
|
<option disabled>Page size</option>
|
||||||
<option th:selected="${page.getPagination().getPageSize() == 50}">50</option>
|
<option th:selected="${page.getPagination().getPageSize() == 50}">50</option>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user