This commit is contained in:
Francesco 2023-10-03 11:16:05 +02:00
parent 76a99be813
commit e1a591b2d5
4 changed files with 51 additions and 4 deletions

View File

@ -0,0 +1,35 @@
/*
* Spring Boot Database Admin - An automatically generated CRUD admin UI for Spring Boot apps
* Copyright (C) 2023 Ailef (http://ailef.tech)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package tech.ailef.dbadmin.external.annotations;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* Marks a field as read-only. The field can be filled at creation time, but
* it will be shown as disabled during edits, making it impossible to change its
* value after creation.
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface ReadOnly {
}

View File

@ -110,6 +110,7 @@ public class CustomJpaRepository extends SimpleJpaRepository {
for (DbField field : schema.getSortedFields()) {
if (field.isPrimaryKey()) continue;
if (field.isReadOnly()) continue;
boolean keepValue = params.getOrDefault("__keep_" + field.getName(), "off").equals("on");
if (keepValue) continue;

View File

@ -30,6 +30,7 @@ import com.fasterxml.jackson.annotation.JsonIgnore;
import tech.ailef.dbadmin.external.annotations.DisplayImage;
import tech.ailef.dbadmin.external.annotations.Filterable;
import tech.ailef.dbadmin.external.annotations.FilterableType;
import tech.ailef.dbadmin.external.annotations.ReadOnly;
/**
* Represent a field on the database, generated from an Entity class instance variable.
@ -190,6 +191,10 @@ public class DbField {
return filterable != null && filterable.type() == FilterableType.CATEGORICAL;
}
public boolean isReadOnly() {
return getPrimitiveField().getAnnotation(ReadOnly.class) != null;
}
public Set<DbFieldValue> getAllValues() {
List<?> findAll = schema.getJpaRepository().findAll();
return findAll.stream()

View File

@ -56,6 +56,7 @@
class="form-control" th:id="|__id_${field.getName()}|"
th:required="${!field.isNullable() && !field.isPrimaryKey()}"
rows="5"
th:classAppend="${field.isReadOnly() && !create ? 'disable' : ''}"
></textarea>
</th:block>
<th:block th:if="${!field.isText()}">
@ -65,7 +66,8 @@
${create ? (params != null ? params.getOrDefault(field.getName(), '') : '')
: (object != null ? object.get(field).getValue() : '' )}
"
class="form-control" th:id="|__id_${field.getName()}|"
th:class="|form-control ${field.isReadOnly() && !create ? 'disable' : ''}|"
th:id="|__id_${field.getName()}|"
th:classAppend="${field.isPrimaryKey() && object != null ? 'disable' : ''}"
th:required="${!field.isNullable() && !field.isPrimaryKey()}"
step="any"
@ -81,17 +83,21 @@
th:data-fieldname="${field.getName()}"
th:id="|__keep_${field.getName()}|"
checked
th:classAppend="${field.isReadOnly() && !create ? 'disable' : ''}"
th:name="|__keep_${field.getName()}|">
<span>Keep current data</span>
<div th:if="${field.isImage()}" class="mb-2">
<img class="thumb-image" th:id="|__thumb_${field.getName()}|"
<img class="thumb-image"
th:id="|__thumb_${field.getName()}|"
th:src="|/${baseUrl}/download/${schema.getClassName()}/${field.getJavaName()}/${object.getPrimaryKeyValue()}/image|">
</div>
</div>
<!--/*--> File input <!--*/-->
<input th:if="${field.isBinary()}" placeholder="NULL" th:type="${field.getType().getHTMLName()}"
<input
th:if="${field.isBinary()}" placeholder="NULL" th:type="${field.getType().getHTMLName()}"
th:name="${field.getName()}"
class="form-control mt-2" th:id="|__id_${field.getName()}|"
th:class="|form-control mt-2 ${field.isReadOnly() && !create ? 'disable' : ''}|"
th:id="|__id_${field.getName()}|"
th:required="${!field.isNullable()}"
>
</th:block>