Advanced Techniques - Total size of all files in a file field




An application contains a File selection element which allows users to upload files in the browser. We now want to provide additional information about the attached files such as the file size or the number of files.

Requirements




As the file selection was created, the option to allow the uploading of multiple files per dataset was activated in the properties dialog.



Moreover, the data group requires additional data fields to provide more information about the files:

Implementation

A new process is created in the Processes module. This process responds to a dataset being inserted or changed. This means that retrospective changes (files being added or deleted) to the dataset are also intercepted and analyzed. A Groovy script action is attached to the Data group event handler. The Groovy action will query the file information.



The following script is performed in the Groovy script action:
//The FileUCHelper needs to be imported
import de.uplanet.lucy.server.businesslogic.util.FileUCHelper

//Request required information from the data field
def id= g_record["<GUID of the ID field>"].value /* datafield (PK) (S) ID <integer> */

def files = FileUCHelper.getFileValueHolder(g_context, "<GUID of the file field>", id)
def filesIt = files.getValue()

def intSize = 0
def anz = 0
filesIt.each{element ->
	intSize = intSize + element.getFile().length()
	anz++
}

//Write calculated information back to the source dataset
def conn = g_dbConnections.systemConnection

g_dbQuery.executeUpdate(conn, """
	UPDATE 
		DATAGROUP('<GUID of the data group>') 
	SET 
		L_TOTALFILESIZE = ?,
		L_NUMBEROFFILES = ?,
		FLT_FILESIZEINMB = ? 
	WHERE 
		LID = ?""") {
	setInt(1, intSize)
	setInt(2, anz)
	setDouble(3, intSize/1024/1024)
	setInt(4, id)
}

Documentation on http://docs.intrexx.com:

The call of the FileUCHelp with the subsequent getValue() is required to identify the files contained in the file field. The, the variables for calculating the number of files and the file size is initialized and set to null. The loop (each) for every file found follows this. element stands for the single file that is queried in the following line with geFile(). Its size in bytes is identified with .length(). anz++ increments the control variable that represents the number of files. Afterwards, the information is written back, which can of course also be achieved using shareShare (g_sharedState) and a subsequent data group action. With very large files, it could be the case that the total size of the files in bytes is too large for a normal integer data field. The database will output an error message in this case. In our example, the saving in MB is therefore specified as well - this results with the corresponding divisor. Now, when datasets are added or changed in the application, the required data is calculated and written to the data field. This data can be displayed in a view table on an overview page, for example.