Skip to content

Commit 7ef077e

Browse files
committed
initialize labelNoExtension in PostLoad
1 parent c3bf547 commit 7ef077e

1 file changed

Lines changed: 10 additions & 3 deletions

File tree

src/main/java/edu/harvard/iq/dataverse/FileMetadata.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import jakarta.persistence.NamedNativeQuery;
4040
import jakarta.persistence.OneToMany;
4141
import jakarta.persistence.OrderBy;
42+
import jakarta.persistence.PostLoad;
4243
import jakarta.persistence.SqlResultSetMapping;
4344
import jakarta.persistence.Table;
4445
import jakarta.persistence.Transient;
@@ -153,10 +154,19 @@ public class FileMetadata implements Serializable {
153154
@OneToMany (mappedBy="fileMetadata", cascade={ CascadeType.REMOVE, CascadeType.MERGE,CascadeType.PERSIST})
154155
private Collection<VariableMetadata> variableMetadatas;
155156

157+
// A transient field is needed for JSF UI - validation errors on label do not get routed directly to the input for labelNoExtension, causing rollback.
158+
// With a separate transient field kept in sync with label, the validation can be done on the labelNoExtension field, which avoids the issue and allows proper validation.
156159
@Transient
157160
@ValidateDataFileLabel(message = "{filename.illegalCharacters}")
158161
@NotBlank(message = "{filename.blank}")
159162
String labelNoExtension;
163+
164+
// Initialize the labelNoExtension from label after loading the entity
165+
@PostLoad
166+
public void postLoad() {
167+
getLabelNoExtension();
168+
}
169+
160170
/**
161171
* Creates a copy of {@code this}, with identical business logic fields, making the bi-drectional connections to the specified version.
162172
*
@@ -219,9 +229,6 @@ public String getLabelForOriginal() {
219229
public FileMetadata() {
220230
variableMetadatas = new ArrayList<VariableMetadata>();
221231
varGroups = new ArrayList<VarGroup>();
222-
int last = label.lastIndexOf(".");
223-
labelNoExtension = (last == -1) ? label : label.substring(0, last);
224-
225232
}
226233

227234
public String getDirectoryLabel() {

0 commit comments

Comments
 (0)