mirror of
https://github.com/dalbodeule/snap-admin.git
synced 2025-06-09 05:48:20 +00:00
@ReadOnly fields (#4)
This commit is contained in:
parent
76a99be813
commit
aeac9c10b6
35
src/main/java/tech/ailef/dbadmin/external/annotations/ReadOnly.java
vendored
Normal file
35
src/main/java/tech/ailef/dbadmin/external/annotations/ReadOnly.java
vendored
Normal 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 {
|
||||||
|
}
|
@ -110,6 +110,7 @@ public class CustomJpaRepository extends SimpleJpaRepository {
|
|||||||
|
|
||||||
for (DbField field : schema.getSortedFields()) {
|
for (DbField field : schema.getSortedFields()) {
|
||||||
if (field.isPrimaryKey()) continue;
|
if (field.isPrimaryKey()) continue;
|
||||||
|
if (field.isReadOnly()) continue;
|
||||||
|
|
||||||
boolean keepValue = params.getOrDefault("__keep_" + field.getName(), "off").equals("on");
|
boolean keepValue = params.getOrDefault("__keep_" + field.getName(), "off").equals("on");
|
||||||
if (keepValue) continue;
|
if (keepValue) continue;
|
||||||
|
@ -30,6 +30,7 @@ import com.fasterxml.jackson.annotation.JsonIgnore;
|
|||||||
import tech.ailef.dbadmin.external.annotations.DisplayImage;
|
import tech.ailef.dbadmin.external.annotations.DisplayImage;
|
||||||
import tech.ailef.dbadmin.external.annotations.Filterable;
|
import tech.ailef.dbadmin.external.annotations.Filterable;
|
||||||
import tech.ailef.dbadmin.external.annotations.FilterableType;
|
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.
|
* 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;
|
return filterable != null && filterable.type() == FilterableType.CATEGORICAL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean isReadOnly() {
|
||||||
|
return getPrimitiveField().getAnnotation(ReadOnly.class) != null;
|
||||||
|
}
|
||||||
|
|
||||||
public Set<DbFieldValue> getAllValues() {
|
public Set<DbFieldValue> getAllValues() {
|
||||||
List<?> findAll = schema.getJpaRepository().findAll();
|
List<?> findAll = schema.getJpaRepository().findAll();
|
||||||
return findAll.stream()
|
return findAll.stream()
|
||||||
|
@ -56,6 +56,7 @@
|
|||||||
class="form-control" th:id="|__id_${field.getName()}|"
|
class="form-control" th:id="|__id_${field.getName()}|"
|
||||||
th:required="${!field.isNullable() && !field.isPrimaryKey()}"
|
th:required="${!field.isNullable() && !field.isPrimaryKey()}"
|
||||||
rows="5"
|
rows="5"
|
||||||
|
th:classAppend="${field.isReadOnly() && !create ? 'disable' : ''}"
|
||||||
></textarea>
|
></textarea>
|
||||||
</th:block>
|
</th:block>
|
||||||
<th:block th:if="${!field.isText()}">
|
<th:block th:if="${!field.isText()}">
|
||||||
@ -65,7 +66,8 @@
|
|||||||
${create ? (params != null ? params.getOrDefault(field.getName(), '') : '')
|
${create ? (params != null ? params.getOrDefault(field.getName(), '') : '')
|
||||||
: (object != null ? object.get(field).getValue() : '' )}
|
: (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:classAppend="${field.isPrimaryKey() && object != null ? 'disable' : ''}"
|
||||||
th:required="${!field.isNullable() && !field.isPrimaryKey()}"
|
th:required="${!field.isNullable() && !field.isPrimaryKey()}"
|
||||||
step="any"
|
step="any"
|
||||||
@ -81,17 +83,21 @@
|
|||||||
th:data-fieldname="${field.getName()}"
|
th:data-fieldname="${field.getName()}"
|
||||||
th:id="|__keep_${field.getName()}|"
|
th:id="|__keep_${field.getName()}|"
|
||||||
checked
|
checked
|
||||||
|
th:classAppend="${field.isReadOnly() && !create ? 'disable' : ''}"
|
||||||
th:name="|__keep_${field.getName()}|">
|
th:name="|__keep_${field.getName()}|">
|
||||||
<span>Keep current data</span>
|
<span>Keep current data</span>
|
||||||
<div th:if="${field.isImage()}" class="mb-2">
|
<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|">
|
th:src="|/${baseUrl}/download/${schema.getClassName()}/${field.getJavaName()}/${object.getPrimaryKeyValue()}/image|">
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<!--/*--> File input <!--*/-->
|
<!--/*--> 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()}"
|
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:required="${!field.isNullable()}"
|
||||||
>
|
>
|
||||||
</th:block>
|
</th:block>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user