Tips & Tricks - Importing files
With "g_dgFile", any files can be imported into an Intrexx application as file attachments. In this example, a time-controlled process that uses Groovy will be used to search through a directory for .png files and then write these to an application as an attachment.
Sample application, process and four PNG image files can be downloaded here. Unzip the downloaded file. Included are the directory image-files-png, in which you will find four PNG files that you can use for testing, and the file help.2.tipps-tricks-files-import-app-process.zip. You can import these as usual.
Here you can find more information about the class "g_dgFile".
The example application possesses a data group that uses a GUID as the data type of the primary key. This data type can be selected when you create a new data group.
Furthermore, the data group has a file data field.
The Overview page has a table that is connected to the data field from the data group.
The example process contains a timer with the corresponding timer event handler and a Groovy action. Double-click to open the Properties dialog of the Groovy script action. Switch to the Script tab and open the Intrexx editor. You will find the following Groovy script there.
import groovy.io.FileType
// Connection to portal data base
def conn = g_dbConnections.systemConnection
// Source direction
def path = 'c:/img/'
// the source directory should be searched in a depth of three subdirectories
// for .png and .PNG
new File(path).traverse(
type:FileType.FILES,
nameFilter:~/.+\.(?i)png$/,
maxDepth:3
){ pngFile ->
// in the Closure, each .PNG file is available as java.io.file
// in the variable pngFile
// for each file a record is created, to which the files are
// will be added later as an attachment
// the GUID is the GUID of the data group from the application
def id = newGuid()
g_dbQuery.executeUpdate(conn, "INSERT INTO DATAGROUP('B78B96ADA0A81927B7ED013946ED7DC0331CCEC2') (STRID) VALUES (?)") {
setString(1, id)
}
// now the file can be attached to the record, where
// the GUID corresponds to the GUID of the file data field
// the source file is moved, i.e. it is no longer located afterwards
// in the source directory
g_dgFile.move(guid: "185C69FFD1D97C10F3C09BC1D0D2C1633BEAE039", id: id, file: pngFile, deleteAlways: false, triggerWorkflow: false)
}
Replace the path "c:/img/" with the name of the directory in which the images you want to import are located. Of course, other types such as PDF files can also be imported. The script then needs to be adjusted accordingly.
The executor of the portal service must have write access to the directory where the files are located. This is particularly important for network drives. If there are not only png files in a directory, the extensions of the files must be checked.
Replace the GUIDs for the data group and data field in the Groovy script with the corresponding GUIDs from your application. You can select your target application in the Application structure area and determine the corresponding GUIDs there. Then close the editor and the properties dialog of the Groovy action by clicking on "OK". Save the process and start the timer via the "Edit" main menu.
Below, you can see the files in the target application.