diff --git a/src/main/java/tech/ailef/dbadmin/external/annotations/HiddenColumn.java b/src/main/java/tech/ailef/dbadmin/external/annotations/HiddenColumn.java new file mode 100644 index 0000000..ba6fbd6 --- /dev/null +++ b/src/main/java/tech/ailef/dbadmin/external/annotations/HiddenColumn.java @@ -0,0 +1,20 @@ +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 column as hidden. This column and its values will not be shown + * in the list and detail view for objects of this type. + * If the column is nullable, it will be hidden in the create and edit + * forms as well (and this will result in the column having being NULL + * when the objects are created/edited). If, instead, it's not a nullable + * column, it will be included in the create and edit forms. + * + */ +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.FIELD) +public @interface HiddenColumn { +} \ No newline at end of file diff --git a/src/main/java/tech/ailef/dbadmin/external/dbmapping/DbObjectSchema.java b/src/main/java/tech/ailef/dbadmin/external/dbmapping/DbObjectSchema.java index 3333b10..5a0fb2a 100644 --- a/src/main/java/tech/ailef/dbadmin/external/dbmapping/DbObjectSchema.java +++ b/src/main/java/tech/ailef/dbadmin/external/dbmapping/DbObjectSchema.java @@ -19,6 +19,7 @@ import jakarta.persistence.OneToOne; import jakarta.persistence.Table; import tech.ailef.dbadmin.external.DbAdmin; import tech.ailef.dbadmin.external.annotations.ComputedColumn; +import tech.ailef.dbadmin.external.annotations.HiddenColumn; import tech.ailef.dbadmin.external.exceptions.DbAdminException; import tech.ailef.dbadmin.external.misc.Utils; @@ -185,15 +186,28 @@ public class DbObjectSchema { } /** - * Returns a sorted list of physical fields (i.e., fields that correspond to - * a column in the table as opposed to fields that are just present as - * instance variables, like relationship fields). Sorted alphabetically - * with priority to the primary key. - * + * See {@link DbObjectSchema#getSortedFields()} * @return */ @JsonIgnore public List getSortedFields() { + return getSortedFields(true); + } + + /** + * Returns a sorted list of physical fields (i.e., fields that correspond to + * a column in the table as opposed to fields that are just present as + * instance variables, like relationship fields). Sorted alphabetically + * with priority the primary key, and non nullable fields. + * + * If readOnly is true, `@HiddenColumn`s are not returned. If instead + * readOnly is false, i.e. how it gets called in the create/edit page, + * hidden columns are included if they are not nullable. + * + * @param readOnly whether we only need to read the fields are create/edit + * @return + */ + public List getSortedFields(boolean readOnly) { return getFields().stream() .filter(f -> { boolean toMany = f.getPrimitiveField().getAnnotation(OneToMany.class) == null @@ -202,7 +216,10 @@ public class DbObjectSchema { OneToOne oneToOne = f.getPrimitiveField().getAnnotation(OneToOne.class); boolean mappedBy = oneToOne != null && !oneToOne.mappedBy().isBlank(); - return toMany && !mappedBy; + boolean hidden = f.getPrimitiveField().getAnnotation(HiddenColumn.class) != null; + + + return toMany && !mappedBy && (!hidden || !readOnly); }) .sorted((a, b) -> { if (a.isPrimaryKey() && !b.isPrimaryKey()) @@ -315,6 +332,4 @@ public class DbObjectSchema { return Objects.equals(tableName, other.tableName); } - - } diff --git a/src/main/resources/templates/model/create.html b/src/main/resources/templates/model/create.html index e368bfb..0878c85 100644 --- a/src/main/resources/templates/model/create.html +++ b/src/main/resources/templates/model/create.html @@ -30,7 +30,7 @@

-
+