DEE

数据交换


自定义适配器

<p><strong>简要描述:</strong> </p> <ul> <li>根据表单的“字段1”选择内容,查询对应地区的数据库,写入指定数据表中 (说明:上海数据库的“t_adr”存放的记录为“上海”表;香港数据库的“t_adr”表存放的记录为“香港”,需要写入到演示数据库的“t_input”表中)</li> </ul> <p><strong>必要条件:</strong> </p> <ul> <li>开发自定义适配器</li> <li>配置自定义适配</li> <li>配置A8表单 <strong>开发步骤:</strong> <h1>1 开发自定义适配器</h1> <p>package com.seeyon.v3x.dee.adapter.extend;</p></li> </ul> <p>import com.seeyon.v3x.dee.Document; import com.seeyon.v3x.dee.Parameters; import com.seeyon.v3x.dee.TransformException; import com.seeyon.v3x.dee.TransformFactory; import com.seeyon.v3x.dee.adapter.Adapter; import com.seeyon.v3x.dee.resource.DbDataSource; import com.seeyon.v3x.dee.util.DocumentUtil; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.ResultSetMetaData; import java.util.HashMap; import java.util.Iterator; import java.util.Map;</p> <p>/**</p> <ul> <li>Created by dkywolf on 2017-3-25.</li> <li>实现 com.seeyon.v3x.dee.adapter.Adapter 接口</li> <li>execute(Document document)方法</li> <li> <p>方法参数document为上文传入document对象 */ public class TrainAdapter implements Adapter {</p> <p>//传入参数 private String dbType; //执行数据源 private DbDataSource dsId1; //上海数据源 private DbDataSource dsId2; //香港数据源 //............ //执行sql private Map&lt;String, String&gt; sql;</p> <p>//document:上文出入的document对象 @Override public Document execute(Document document) throws TransformException { Connection con = null; Document newDoc = TransformFactory.getInstance().newDocument(&quot;root&quot;); Document.Element root = newDoc.getRootElement(); try { //获取数据库连接 con = getConnection(document.getContext().getParameters()); if (con == null) return document; for (Map.Entry&lt;String, String&gt; entry : sql.entrySet()){ //表名 String tableName = entry.getKey(); //SQL String mySql = entry.getValue(); Document.Element table = root.addChild(tableName); PreparedStatement pstmt = null; ResultSet rs = null; try { pstmt = con.prepareStatement(mySql); rs = pstmt.executeQuery(); //获取列头 Map&lt;String, Integer&gt; cmap = getColumnInfo(rs); //构造document对象 int count = 0; while(rs.next()){ Document.Element row = table.addChild(&quot;row&quot;); Iterator&lt;String&gt; it = cmap.keySet().iterator(); while (it.hasNext()) { String columnName = it.next(); Document.Element column = row.addChild(columnName.trim()); Object o = rs.getObject(columnName); column.setValue(o); } count++; } table.setAttribute(&quot;count&quot;,count); table.setAttribute(&quot;totalCount&quot;,count);</p> <pre><code> } catch (Exception e){ e.printStackTrace(); } finally { if (rs != null) rs.close(); if (pstmt != null) pstmt.close(); } } } catch (Exception e){ e.printStackTrace(); } finally { if (con != null) try { con.close(); } catch (SQLException e) { e.printStackTrace(); } } //合并document(不考虑分页) //newDoc = PageUtil.pageDocument(newDoc, document.getContext().getParameters()); document = DocumentUtil.merge(document, newDoc); return document;</code></pre> <p>}</p> <p>protected Connection getConnection(Parameters param) throws TransformException { try { if (&quot;2&quot;.equals(param.evalString(dbType))){ //香港 if (dsId2 == null) { return null; } return dsId2.getConnection(); } if (dsId1 == null) { return null; } return dsId1.getConnection();</p> <pre><code>} catch (Exception e) { throw new TransformException(e.getLocalizedMessage(), e); }</code></pre> <p>} //获取列头 public Map&lt;String, Integer&gt; getColumnInfo(ResultSet rs) throws SQLException { ResultSetMetaData metaData = rs.getMetaData(); Map&lt;String, Integer&gt; cmap = new HashMap&lt;String, Integer&gt;(); int columnCount = metaData.getColumnCount(); for (int i = 1; i &lt; columnCount + 1; i++) { cmap.put(metaData.getColumnLabel(i), metaData.getColumnType(i)); } return cmap; }</p> <p>public String getDbType() { return dbType; }</p> <p>public void setDbType(String dbType) { this.dbType = dbType; }</p> <p>public DbDataSource getDsId1() { return dsId1; }</p> <p>public void setDsId1(DbDataSource dsId1) { this.dsId1 = dsId1; }</p> <p>public DbDataSource getDsId2() { return dsId2; }</p> <p>public void setDsId2(DbDataSource dsId2) { this.dsId2 = dsId2; }</p> <p>public Map&lt;String, String&gt; getSql() { return sql; }</p> <p>public void setSql(Map&lt;String, String&gt; sql) { this.sql = sql; } }</p> </li> </ul> <h1>2 配置任务</h1> <p>自定义适配器: 在来源配置中创建一适配器,选择自定义配置 <img src="https://www.showdoc.cc/home/common/visitfile/sign/97995f0b8f3f74800aa1566dea16e79a?showdoc=.jpg" alt="" /> 映射配置: <img src="https://www.showdoc.cc/home/common/visitfile/sign/ca3963f2a0b6a8adda3cb2e1ad63c5e4?showdoc=.jpg" alt="" /> 写入配置: <img src="https://www.showdoc.cc/home/common/visitfile/sign/4fc14a0e640f082331db359b13e2ebd4?showdoc=.jpg" alt="" /></p> <h1>3 表单配置</h1> <p>在流程首节点,填写权限的开发高级中阻塞绑定该DEE任务, 参数绑定表单字段field1字段,如图 <img src="https://www.showdoc.cc/home/common/visitfile/sign/4241ab739f998846eba802f15527cba5?showdoc=.jpg" alt="" /></p> <p>发起流程时,任务就会根据field1字段选取的值,去查询对应的数据库 <img src="https://www.showdoc.cc/home/common/visitfile/sign/c7e9ea11e1c679accc8a8c1b5134245c?showdoc=.jpg" alt="" /> 发起后,执行结果: <img src="https://www.showdoc.cc/home/common/visitfile/sign/28083414746e3bf3f93640ad1261d915?showdoc=.jpg" alt="" /></p>

页面列表

ITEM_HTML