判断是否存在某应用并赋值给表单字段

应用场景

  • 表单打开时判断系统是否存在某个应用(已发布)
  • 根据判断结果给表单某个字段赋值为"true"或"false"

样例代码

方式一:数据库连接查询app_app

    1. 替换样例代码中的
      • appIdList- 应用ID(多个用逗号分隔)
      • filedIdList- 表单字段(多个用逗号分隔)
        import java.sql.Connection;
        import java.sql.PreparedStatement;
        import java.sql.ResultSet;
        import java.sql.SQLException;
        import java.util.Collection;
        import java.util.HashSet;
        import java.util.Iterator;
        
        import javax.sql.DataSource;
        
        import org.joget.apps.app.model.AppDefinition;
        import org.joget.apps.app.service.AppService;
        import org.joget.apps.app.service.AppUtil;
        import org.joget.apps.form.model.FormRow;
        import org.joget.apps.form.model.FormRowSet;
        import org.joget.commons.util.LogUtil;
        import org.json.JSONArray;
        import org.json.JSONException;
        import org.json.JSONObject;
        /**
         * BeanShell Form Load Binder
         * 根据所提供的应用id判断该应用是否存在并已发布,同时将结果(true/false)赋值给表单字段
         * @author eric
         *
         */
        public class PackageBSH {
            
            String[] appIdList = {"vt_timing"};// 需要查询的应用id列表
            String[] filedIdList = {"vt_timing_exist"};//需要赋值的字段id列表
            
            /**
             * 获取所有发布的应用
             * @return
             */
            protected Collection getPublishedAppsFromApi(){
                AppService appService = (AppService) AppUtil.getApplicationContext().getBean("appService");
                Collection collection = appService.getPublishedApps(null);
                
                return collection;
            }
            protected Collection getPublishedAppsFromDB(){
                Collection resultCol = new HashSet();
                AppDefinition appDef = null;
                
                Connection connection = null;
                DataSource ds = (DataSource)AppUtil.getApplicationContext().getBean("setupDataSource");
                PreparedStatement ps = null;
                ResultSet rs = null;
                
                try {
                    connection = ds.getConnection();
                    if(!connection.isClosed()){
                        String sql = "SELECT appId FROM app_app WHERE published = ?";
                        ps = connection.prepareStatement(sql);
                        ps.setString(1, "1");
                        rs = ps.executeQuery();
                        while(rs.next()){
                            appDef = new AppDefinition();
                            appDef.setAppId(rs.getString(1));
                            resultCol.add(appDef);
                        }
                    }
                } catch (SQLException e1) {
                    e1.printStackTrace();
                }finally{
                    if(ps != null){
                        try {
                            ps.close();
                        } catch (SQLException e) {
                            e.printStackTrace();
                        }
                    }
                    if(connection!=null){
                        try {
                            connection.close();
                        } catch (SQLException e) {
                            e.printStackTrace();
                        }
                    }           
                }
                
                return resultCol;
            }
            protected JSONArray isAppsExist(String[] appIds,Collection publishedApps){
                JSONArray resultArr = new JSONArray();
                JSONObject appObj = null;
                
                AppDefinition appDef = null;
                String appId = null;
                
                boolean isExist = false;
                for (int i = 0; i < appIds.length; i++) {
                    isExist = false;
                    appObj = new JSONObject();
                    try {
                        appObj.put("appId", appIds[i]);
                        Iterator iterator = publishedApps.iterator();
                        while(iterator.hasNext()){
                            appDef = (AppDefinition) iterator.next();
                            appId = appDef.getAppId();
                            if(appId.equals(appIds[i])){
                                isExist = true;
                                break;
                            }
                        }
                        if(isExist){
                            appObj.put("isExist", true);
                        }else {
                            appObj.put("isExist", false);
                        }
                        resultArr.put(appObj);
                    } catch (JSONException e) {
                        // ignore
                    }
                    
                }
                
                return resultArr;
            }
            /**
             * 
             * @param appsArr
             * @return
             */
            protected FormRowSet load(JSONArray appsArr){
                FormRowSet fs = new FormRowSet();
                FormRow fr = new FormRow();
                
                String appId = null;
                boolean isExist = false;
                JSONObject appObj = null;
                for (int i = 0; i < appsArr.length(); i++) {
                    try {
                        appObj = appsArr.getJSONObject(i);
                        appId = appObj.getString("appId");
                        isExist = appObj.getBoolean("isExist");
                        
                        fr.put(filedIdList[i], isExist? "true":"false");
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
                fs.add(fr);
                
                return fs;
            }
            public FormRowSet run(){
        //      Collection publishedAppsCol = getPublishedAppsFromApi();
                Collection publishedAppsCol = getPublishedAppsFromDB();
                JSONArray resultArr = isAppsExist(appIdList, publishedAppsCol);
                LogUtil.info(getClass().getName(), resultArr.toString());
                
                FormRowSet fr = load(resultArr);
                
                return fr;
            }
        }
        new PackageBSH().run();
        

方式二:通过API判断(速度较慢,待优化)

    1. 替换样例代码中的
      • appIdList- 应用ID(多个用逗号分隔)
      • filedIdList- 表单字段(多个用逗号分隔)
        import java.util.Collection;
        import java.util.Iterator;
        
        import org.joget.apps.app.model.AppDefinition;
        import org.joget.apps.app.service.AppService;
        import org.joget.apps.app.service.AppUtil;
        import org.joget.apps.form.model.FormRow;
        import org.joget.apps.form.model.FormRowSet;
        import org.joget.commons.util.LogUtil;
        import org.json.JSONArray;
        import org.json.JSONException;
        import org.json.JSONObject;
        /**
         * BeanShell Form Load Binder
         * 根据所提供的应用id判断该应用是否存在并已发布,同时将结果(true/false)赋值给表单字段
         * @author eric
         *
         */
        public class PackageBSH {
        	
        	String[] appIdList = {};// 需要查询的应用id列表
        	String[] filedIdList = {};//需要赋值的字段id列表
        	
        	/**
        	 * 获取所有发布的应用
        	 * @return
        	 */
        	protected Collection getPublishedApps(){
        		AppService appService = (AppService) AppUtil.getApplicationContext().getBean("appService");
        		Collection collection = appService.getPublishedApps(null);
        		
        		return collection;
        	}
        	protected JSONArray isAppsExist(String[] appIds,Collection publishedApps){
        		JSONArray resultArr = new JSONArray();
        		JSONObject appObj = null;
        		
        		AppDefinition appDef = null;
        		String appId = null;
        		
        		for (int i = 0; i < appIds.length; i++) {
        			appObj = new JSONObject();
        			try {
        				appObj.put("appId", appIds[i]);
        				Iterator iterator = publishedApps.iterator();
        				while(iterator.hasNext()){
        					appDef = (AppDefinition) iterator.next();
        					appId = appDef.getAppId();
        					if(appId.equals(appIds[i])){
        						appObj.put("isExist", true);
        						break;
        					}else {
        						appObj.put("isExist", false);
        					}
        				}
        				resultArr.put(appObj);
        			} catch (JSONException e) {
        				// ignore
        			}
        			
        		}
        		
        		return resultArr;
        	}
        	/**
        	 * 
        	 * @param appsArr
        	 * @return
        	 */
        	protected FormRowSet load(JSONArray appsArr){
        		FormRowSet fs = new FormRowSet();
        		FormRow fr = new FormRow();
        		
        		String appId = null;
        		boolean isExist = false;
        		JSONObject appObj = null;
        		for (int i = 0; i < appsArr.length(); i++) {
        			try {
        				appObj = appsArr.getJSONObject(i);
        				appId = appObj.getString("appId");
        				isExist = appObj.getBoolean("isExist");
        				
        				fr.put(filedIdList[i], isExist? "true":"false");
        			} catch (JSONException e) {
        				e.printStackTrace();
        			}
        		}
        		fs.add(fr);
        		
        		return fs;
        	}
        	public FormRowSet run(){
        		Collection publishedAppsCol = getPublishedApps();
        		JSONArray resultArr = isAppsExist(appIdList, publishedAppsCol);
        		LogUtil.info(getClass().getName(), resultArr.toString());
        		
        		FormRowSet fr = load(resultArr);
        		
        		return fr;
        	}
        }
        new PackageBSH().run();
        
Create by Sean Fung on 2016-06-29 12:34:26.0
Last updated by Sean Fung on 2016-06-29 12:34:26.0