You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 2 Next »

 Exporting an app programmatically can be done by invoking the function appService.exportApp() manually in the beanshell script.  We will be using a BeanShell tool inside a process that we can trigger as needed.

Steps Example

1: Create Process

Create a new process in the process builder and build it in such a way as seen in Figure 1. Paste the provided script into the tool

Figure 1: The process

2: Create UI element

Drag a Run Process UI element in the UI Builder and have it point to the process created in Step 1.

Figure 2: Run Process Element that points to Process 1

Script

** Do note that the variable "path" in the code is contextual to your preference/environment and needs to be changed to suit your requirements.

** You will also need to either/or

  • provide sufficient permission level to Tomcat (or your Java container used to run Joget)
  • create a location/folder that allows Tomcat to read/write files


import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.sql.DataSource;
import org.joget.apps.app.service.AppUtil;
import org.joget.apps.app.service.AppService;
import org.joget.apps.app.model.AppDefinition;
import org.joget.commons.util.LogUtil;
import java.text.SimpleDateFormat;
import java.io.File;
import org.joget.commons.util.FileManager;

// prepare the connection & appservice instance
Connection con = null;
AppService appService = (AppService) AppUtil.getApplicationContext().getBean("appService");

// prepare the filename common
String path = "wflow/jwa_backups/";
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
String timestamp = sdf.format(new Date());

try {
    // retrieve connection from the default datasource
    DataSource ds = (DataSource)AppUtil.getApplicationContext().getBean("setupDataSource");
    con = ds.getConnection();
  
    if(!con.isClosed()) {
        // query the currently published app from db
        PreparedStatement stmt = con.prepareStatement("select appId as id, appVersion as version from app_app where published = 1");
        ResultSet rs = stmt.executeQuery();

        // loop through each eligible apps
        while (rs.next()) {
            AppDefinition appDef = appService.getAppDefinition(rs.getObject("id"), Long.toString(rs.getObject("version")));
            // complete the filename for each app
            String filename = "APP_" + appDef.getId() + "-" + appDef.getVersion() + "-" + timestamp + ".jwa";
            
            // create the file
            File test = new File(path+filename);
            
            // move the data to the created file above
            FileOutputStream fos = null;
            fos = new FileOutputStream(test); 
            appService.exportApp(appDef.getId(), appDef.getVersion().toString(), fos);
            
        }
    }
} catch(Exception e) {
    LogUtil.error("error", e, "test");
} finally {
    //always close the connection after used
    try {
        if(con != null) {
            con.close();
        }
    } catch(SQLException e) {/* ignored */}
}

Result

For this example, all app files will be saved into /wflow/jwa_backup folder

Example Execution

Figure 3: Example Execution

Before

Figure 4: Result Before


After

Figure 5: Result After

Sample App
APP_exportAppSample.jwa

  • No labels