Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
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
// ensure the path and folder declared exists
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 */}
}

...

For this example, all app files will be saved into /wflow/jwa_backup folder  (Make sure the jwa_backup folder has been created)

Example Execution

Figure 3: Example Execution

Before

...


After

Figure 5: Result After

Sample App


View file
name

...

app_kb_dx8_exportAppSample.jwa
height250