Versions Compared

Key

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

...

Code Block
linenumberstrue
    public FormRowSet store(Element element, FormRowSet rows, FormData formData) {
        Form parentFormfinal Object[] binders = FormUtil.findRootForm(element(Object[]) getProperty("binders");
        Stringif primaryKeyValue(binders != parentForm.getPrimaryKeyValue(formData);
     null && binders.length > 0) {
       
     Thread newThread;  Connection con = null;
      
  PreparedStatement pstmt = null;
       final ResultSetPluginManager rspluginManager = null(PluginManager) AppUtil.getApplicationContext().getBean("pluginManager");
        
    newThread = new  tryPluginThread(new Runnable() {
            DataSource ds = createDataSource();
     public       con = ds.getConnectionvoid run();
  {          
            //check for deletion
            FormRowSet originalRowSet = formData.getLoadBinderData(element);
         for (Object binder if (originalRowSet != null && !originalRowSet.isEmpty()): binders) {
                for (FormRow r : originalRowSet) {
       if (binder != null && binder instanceof Map) {
      if (!rows.contains(r)) {
                        StringMap querybinderMap = getPropertyString("delete_sql");Map) binder;
    
                        pstmt = con.prepareStatement(getQuery(query));
  if (binderMap != null && binderMap.containsKey("className") &&                int i = 1;
 !binderMap.get("className").toString().isEmpty()) {
                       for (String obj : getParams(query, r, primaryKeyValue)) {
                            pstmt.setObject(i, obj);
                            i++;
                        }
                        pstmt.executeUpdate();
                    }
                }
            }
            
            if (!(rows == null || rows.isEmpty())) {
            
                //run query for each row
                for (FormRow row : rows) {
                    //check to use insert query or update query
                    String checkSql = getPropertyString("check_sql");
                    pstmt = con.prepareStatement(getQuery(checkSql));
                    int i = 1;
                    for (String obj : getParams(checkSql, row, primaryKeyValue)) {
                        pstmt.setObject(i, obj);
                        i++;
                    }
                    String query = getPropertyString("insert_sql");
                    rs = pstmt.executeQuery();
                    //record exist, use update query
                    if (rs.next()) {
                        query = getPropertyString("update_sql");
                    }
                    pstmt = con.prepareStatement(getQuery(query));
                    i = 1;
                    for (String obj : getParams(query, row, primaryKeyValue)) {
                        pstmt.setObject(i, obj);
                        i++;
                    }
                    pstmt.executeUpdate();
                }
            }
        } catch (Exception e) {
            LogUtil.error(getClassName(), e, "");
        } finally {
            try {
                if (rs != null) {
                    rs.close();
                }
                if (pstmt != null) {
                    pstmt.close();
                }
                if (con != null) {
                    con.close();
                }
            } catch (Exception e) {
                LogUtil.error(getClassName(), e, "");
            }
        }
        
        return rows;
    }
    
    /**
     * Used to replaces all syntax like {field_id} to question mark
     * @param query
     * @return 
     */
    protected String getQuery(String query) {
        return query.replaceAll("\\{[a-zA-Z0-9_]+\\}", "?");
    }
    
    /**
     * Used to retrieves the value of variables in query 
     * @param query
     * @param row
     * @return 
     */
    protected Collection<String> getParams(String query, FormRow row, String primaryKey) {
        Collection<String> params = new ArrayList<String>();
        
        Pattern pattern = Pattern.compile("\\{([a-zA-Z0-9_]+)\\}");
        Matcher matcher = pattern.matcher(query);
        
      String className while= (matcher.find()) {binderMap.get("className").toString();
            String key = matcher.group(1);
            
         FormStoreBinder p = if (FormUtil.PROPERTY_ID.equals(key)) {(FormStoreBinder) pluginManager.getPlugin(className);
                String value = row.getId();
                   if (valuep !== null || value.isEmpty()) {
                     value  = UuidGenerator.getInstance().getUuid();
                Map properties = new row.setIdHashMap(value);
                      }
                   params.add(value);
properties.putAll((Map) binderMap.get("properties"));                 } else if ("uuid".equals(key)) {
                params.add(UuidGenerator.getInstance().getUuid());
  
          } else if ("foreignKey".equals(key)) {
                params.add(primaryKey);
          if (p }instanceof elsePropertyEditable) {
                         String value = row.getProperty(key);
                params.add((value != null)?value:""PropertyEditable) p).setProperties(properties);
            }
        }
        
        return params;
    }
    
    /**
     * To creates data source based on setting
     * @return
     * @throws Exception 
     */
    protected DataSource createDataSource() throws Exception { p.store(element, rows, formData);
        DataSource ds = null;
        String datasource = getPropertyString("jdbcDatasource");
        if ("default".equals(datasource)) {
    }
        // use current datasource
            ds = (DataSource)AppUtil.getApplicationContext().getBean("setupDataSource");
        }
 else {
            // use custom datasource
           }
 Properties dsProps = new Properties();
            dsProps.put("driverClassName", getPropertyString("jdbcDriver"));
      }
      dsProps.put("url", getPropertyString("jdbcUrl"));
            dsProps.put("username", getPropertyString("jdbcUser")); }
            dsProps.put("password", getPropertyString("jdbcPassword"))    });
            ds   = BasicDataSourceFactorynewThread.createDataSourcestart(dsProps);            
        }
        return dsnull;
    }

c. Manage the dependency libraries of your plugin

...

Code Block
languagexml
<!-- Change plugin specific dependencies here -->
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jsp-api</artifactId>
    <version>2.0</version>
</dependency>
<dependency>
    <groupId>commons-dbcp<<groupId>org.json</groupId>
    <artifactId>commons-dbcp<<artifactId>json</artifactId>
    <version>1.3<<version>20080701</version>
    <scope>provided</scope>
</dependency>
<!-- End change plugin specific dependencies here -->

...

Let build our plugin. Once the building process is done, we will found a "jdbcmulti_store_binder-57.0.0.jar" file is created under the "jdbcmulti_store_binder/target" directory.

Then, let upload the plugin jar to Manage Plugins. After upload, the jar file, double-check the plugin is uploaded and activated correctly.

...