JBStrap form file handling

When it comes to saving and deleting Form data while using JpaDao API, not only visible Form data are saved/deleted. Similarly to data in the input fields, Form files must be handled, too. Use the FileItem editor component to upload files on a Form. The DefaultFileHandler class is responsible for file handling in the JBStrap framework. The DefaultFileHandler class extends the FileHandler class. If you don't want to use the default file handler class (DefaultFileHandler), you can also create your custom class by extending the FileHandler and specifying it as the default one by using the JBStrapParamType.FILE_HANDLER parameter. If you decide to extend the FileHandler class to create your custom file handler, you can specify what happens if users save/delete records and what happens to files upon loading a Form.

Default file handler

If you want to use the default file handler provided by the framework, you need to add a text column to the data table where the file is uploaded, in which the file handler stores the metadata for files. The column size should be sufficient for metadata. Thus, a column with a smaller size is sufficient if a single file is to be uploaded, but a column with a larger size is needed if the user is allowed to upload multiple files. Accordingly, the default file handler will store the metadata for uploaded files in each record, but the physical file will not be saved in a database but the specified file handler and retrieved from there. If the metadata stored by the file handler is corrupted, the file will still be readable by the file system, however, automatic retrieval may not function properly.

Custom file handler

To create your own file manager, you need to create a class that extends the FileHandler class. This class is an abstract class that requires the implementation of three methods. The following methods are used for file management:

  • processFilesBeforePersist()
  • processFilesAfterPersist()
  • getFiles()

The below example shows how to handle Form files in a database.

Custom file handler class

First, you have to extend the FileHandler class to create your custom file handler.

Creating a custom file handler

File handler parameter

You have to specify your custom file handler class in a JBStrap parameter, so that the framework won't use the DefaultFileHandler as the default file handler.

Setting your custom file handler class

File handling before saving/deleting form data

You can handle form files before saving/deleting a record. The processFilesBeforePersist() method is executed before the Form records are persisted to the database. The following example demonstrates how to save the files to a FileStore and the related metadata to the entity named FileEntity. The FileEntity in the example was only created for demonstrational purposes and does not exist in the framework.

Saving files and associated metadata

File handling after saving/deleting form data

It is possible to handle files after saving or deleting Form records. The processFilesAfterPersist() method is execute after persisting a Form record to the database. The following example demonstrates how to delete metadata from the entity called FileEntity and how the file itself is deleted from the FileStore. The entity named FileEntity was created only for demonstrational purposes and does not exist in the framework.

Deleting files and associated metadata

Getting files upon loading form data

You can pass records to a Form by using the editDataRecord(DataRecord record) method. Once the record values are displayed on the Form, the files in the record are processed as well. The way files are retrieved can be specified using the getFiles() method. In the following example, JBFile is created and set using metadata from the FileEntity table.

Setting files in a Form record