📄 sqlworkitemcoder.java
字号:
/* * Copyright (c) 2005, John Mettraux, OpenWFE.org * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * . Redistributions of source code must retain the above copyright notice, this * list of conditions and the following disclaimer. * * . Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * . Neither the name of the "OpenWFE" nor the names of its contributors may be * used to endorse or promote products derived from this software without * specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. * * $Id: SqlWorkItemCoder.java,v 1.31 2005/06/15 08:49:08 jmettraux Exp $ *///// SqlWorkItemCoder.java//// john.mettraux@openwfe.org//// generated with// jtmpl 1.1.01 2004/05/19 (john.mettraux@openwfe.org)//package openwfe.org.worklist.impl.swis;import openwfe.org.ServiceException;import openwfe.org.ApplicationContext;import openwfe.org.sql.SqlUtils;import openwfe.org.sql.ds.OwfeDataSource;import openwfe.org.misc.UniqueIdGenerator;import openwfe.org.engine.listen.reply.ListenerReplyCoder;import openwfe.org.engine.workitem.WorkItem;import openwfe.org.engine.workitem.CodingException;import openwfe.org.engine.workitem.Attribute;import openwfe.org.engine.workitem.AttributeCoder;import openwfe.org.engine.workitem.AttributeCoder;import openwfe.org.engine.workitem.HistoryItem;import openwfe.org.engine.workitem.InFlowWorkItem;import openwfe.org.engine.workitem.StringMapAttribute;import openwfe.org.engine.expressions.FlowExpressionId;import openwfe.org.engine.participants.Filter;import openwfe.org.engine.participants.FilterEntry;import openwfe.org.engine.impl.workitem.AbstractWorkItemCoder;import openwfe.org.MapUtils;/** * This implementation details how the 'swis' stores workitem in a RDBMS * (it outputs vanilla SQL) * * <p><font size=2>CVS Info : * <br>$Author: jmettraux $ * <br>$Id: SqlWorkItemCoder.java,v 1.31 2005/06/15 08:49:08 jmettraux Exp $ </font> * * @author john.mettraux@openwfe.org * @author jay.lawrence@openwfe.org */public class SqlWorkItemCoder extends AbstractWorkItemCoder{ private final static org.apache.log4j.Logger log = org.apache.log4j.Logger .getLogger(SqlWorkItemCoder.class.getName()); // // CONSTANTS & co protected final static String WID = "workitem_id"; protected final static String WFID = "wf_instance_id"; protected final static String WORKITEM_TABLE = "workitem"; protected final static String[] WORKITEM_COLS = new String[] { WID, "participant_name", "dispatch_time", "last_modified" }; protected final static String FEI_TABLE = "flow_expression_id"; protected final static String[] FEI_COLS = new String[] { WID, "stack_index", "engine_id", "initial_engine_id", "wfd_url", "wfd_name", "wfd_revision", WFID, "expression_name", "expression_id" }; protected final static String HISTORY_TABLE = "history_item"; protected final static String[] HISTORY_COLS = new String[] { WID, "id", "hi_date", "author", "host", "hi_text", "wfd_name", "wfd_revision", WFID }; protected final static String FILTER_TABLE = "filter"; protected final static String[] FILTER_COLS = new String[] { WID, "name", "f_type", "add_allowed", "remove_allowed" }; protected final static String FILTER_ENTRY_TABLE = "filter_entry"; protected final static String[] FILTER_ENTRY_COLS = new String[] { WID, "regex", "permissions", "attributeType" }; protected final static String ATTRIBUTE_TABLE = "attribute"; protected final static String[] ATTRIBUTE_COLS = new String[] { SqlWorkItemCoder.WID, "id", "parent_id", "a_type", "a_value" }; // // FIELDS protected java.util.Map perRepresentationMap = null; protected static UniqueIdGenerator uidGenerator = new UniqueIdGenerator(); // // CONSTRUCTORS public void init (final String name, final java.util.List attributeCoders, final ListenerReplyCoder replyCoder) { super.init(name, attributeCoders, replyCoder); this.perRepresentationMap = new java.util.HashMap(attributeCoders.size()); java.util.Iterator it = attributeCoders.iterator(); while (it.hasNext()) { AbstractSqlAttributeCoder coder = (AbstractSqlAttributeCoder)it.next(); this.perRepresentationMap.put (coder.getRepresentationName(), coder); log.debug ("init() put '"+coder.getClass().getName()+ "' for '"+coder.getRepresentationName()+"'"); } } // // METHODS from WorkItemCoder public Object encode (final WorkItem wi, final ApplicationContext context, final java.util.Map serviceParams) throws CodingException { log.debug("encode()"); if ( ! (wi instanceof InFlowWorkItem)) { throw new CodingException ("This workItemCoder doesn't support encoding something else "+ "than an InFlowWorkItem"); } final InFlowWorkItem item = (InFlowWorkItem)wi; log.debug("encode() "+item.getLastExpressionId()); // // lookup data source final OwfeDataSource ds = lookupDataSource(context, serviceParams); // // create batch statement java.sql.Statement st = null; try { st = ds.getConnection().createStatement(); encode(st, item); st.executeBatch(); } catch (java.sql.SQLException se) { ds.logSQLException( "encode", log, se ); log.error("encode (insert) failure", se); throw new CodingException ("encode (insert) failure", se); } finally { SqlUtils.closeStatement(st); ds.releaseConnection(); } return null; } public WorkItem decode (final Object o, final ApplicationContext context, final java.util.Map serviceParams) throws CodingException { long workitemId = -1; FlowExpressionId fei = null; if (o instanceof FlowExpressionId) fei = (FlowExpressionId)o; else workitemId = ((Long)o).longValue(); OwfeDataSource ds = null; try { ds = lookupDataSource(context, serviceParams); if (workitemId < 0) workitemId = determineWorkitemId(ds.getConnection(), fei); else fei = determineFlowExpressionId(ds.getConnection(), workitemId); return decode(ds, fei, workitemId); } catch (java.sql.SQLException se) { throw new CodingException ("Failed to decode workitem with fei "+fei, se); } finally { if (ds != null) ds.releaseConnection(); } } // // METHODS public AbstractSqlAttributeCoder getAttributeCoder (final String representationName) { /* log.debug ("getAttributeCoder() this.perRepresentationMap is "+ this.perRepresentationMap); log.debug ("getAttributeCoder() repr to find is '"+representationName+"'"); */ return (AbstractSqlAttributeCoder)this.perRepresentationMap .get(representationName); } // // encoding // protected void encode (final java.sql.Statement st, final InFlowWorkItem wi) throws java.sql.SQLException, CodingException { // // generate id for the workitem final Long workitemId = (Long)uidGenerator.generateUniqueId(); log.debug("Building SWIS insert batch"); st.addBatch(buildWorkItemSql(workitemId, wi)); // // generate batch statements for payload and routing info // of workitem encode(st, workitemId, -1, wi.getLastExpressionId()); // stackIndex is set to -1 for the 'last expression id' // when >= 0, it means it's a flow stack entry //encode(st, workitemId, wi.getFlowStack(), wi); encode(st, workitemId, wi.getAttributes()); if (wi.getFilter() != null) encode(st, workitemId, wi.getFilter()); encode(st, workitemId, wi.getHistory(), wi); log.debug("Finished SWIS insert batch"); } protected String buildWorkItemSql (final Long workitemId, final InFlowWorkItem wi) { // // generate batch statements for inserting core workitem data final java.util.List values = new java.util.ArrayList(WORKITEM_COLS.length); values.add(workitemId.toString()); values.add(wi.getParticipantName()); values.add(wi.getDispatchTime()); values.add(wi.getLastModified()); final String sInsert = SqlUtils.buildInsertString (WORKITEM_TABLE, WORKITEM_COLS, values); log.debug("buildWorkItemSql() "+sInsert ); return sInsert; } protected void encode (final java.sql.Statement st, final Long workitemId, final int stackIndex, final FlowExpressionId fei) //final InFlowWorkItem wi) throws java.sql.SQLException { final java.util.List values = new java.util.ArrayList(FEI_COLS.length); values.add(workitemId.toString()); values.add(new Integer(stackIndex)); values.add(fei.getEngineId()); values.add(fei.getInitialEngineId()); values.add(fei.getWorkflowDefinitionUrl()); values.add(fei.getWorkflowDefinitionName()); values.add(fei.getWorkflowDefinitionRevision()); values.add(fei.getWorkflowInstanceId()); values.add(fei.getExpressionName()); values.add(fei.getExpressionId()); final String sInsert = SqlUtils.buildInsertString (FEI_TABLE, FEI_COLS, values); log.debug("addBatch() "+sInsert ); st.addBatch(sInsert); } protected void encode (final java.sql.Statement st, final Long workitemId, final int id, final HistoryItem hi, final InFlowWorkItem wi) throws java.sql.SQLException { final java.util.List values = new java.util.ArrayList(HISTORY_COLS.length); values.add(workitemId.toString()); values.add(new Integer(id)); values.add(hi.getDate()); values.add(hi.getAuthor()); values.add(hi.getHost()); values.add(hi.getText()); values.add(hi.getWorkflowDefinitionName()); values.add(hi.getWorkflowDefinitionRevision()); values.add(hi.getWorkflowInstanceId()); final String sInsert = SqlUtils.buildInsertString (HISTORY_TABLE, HISTORY_COLS, values); log.debug("addBatch() "+sInsert ); st.addBatch(sInsert); } protected void encode (final java.sql.Statement st, final Long workitemId, final java.util.List list, final InFlowWorkItem wi) throws java.sql.SQLException { if (list.size() < 1) return; Object o = list.get(0); if (o instanceof HistoryItem) { for (int i=0; i<list.size(); i++) encode(st, workitemId, i, (HistoryItem)list.get(i), wi); } else if (o instanceof FlowExpressionId) { for (int i=0; i<list.size(); i++) encode(st, workitemId, i, (FlowExpressionId)list.get(i)); } else {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -