📄 mworkflow.java
字号:
/******************************************************************************
* The contents of this file are subject to the Compiere License Version 1.1
* ("License"); You may not use this file except in compliance with the License
* You may obtain a copy of the License at http://www.compiere.org/license.html
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
* The Original Code is Compiere ERP & CRM Smart Business Solution. The Initial
* Developer of the Original Code is Jorg Janke. Portions created by Jorg Janke
* are Copyright (C) 1999-2005 Jorg Janke.
* All parts are Copyright (C) 1999-2005 ComPiere, Inc. All Rights Reserved.
* Contributor(s): ______________________________________.
*****************************************************************************/
package org.compiere.wf;
import java.sql.*;
import java.util.*;
import java.util.logging.*;
import org.compiere.model.*;
import org.compiere.process.*;
import org.compiere.util.*;
/**
* WorkFlow Model
*
* @author Jorg Janke
* @version $Id: MWorkflow.java,v 1.32 2005/11/02 15:05:23 jjanke Exp $
*/
public class MWorkflow extends X_AD_Workflow
{
/**
* Get Workflow from Cache
* @param ctx context
* @param AD_Workflow_ID id
* @return workflow
*/
public static MWorkflow get (Properties ctx, int AD_Workflow_ID)
{
Integer key = new Integer (AD_Workflow_ID);
MWorkflow retValue = (MWorkflow)s_cache.get(key);
if (retValue != null)
return retValue;
retValue = new MWorkflow (ctx, AD_Workflow_ID, null);
if (retValue.get_ID() != 0)
s_cache.put(key, retValue);
return retValue;
} // get
/**
* Get Doc Value Workflow
* @param ctx context
* @param AD_Client_ID client
* @param AD_Table_ID table
* @return document value workflow array or null
*/
public static MWorkflow[] getDocValue (Properties ctx, int AD_Client_ID, int AD_Table_ID)
{
String key = "C" + AD_Client_ID + "T" + AD_Table_ID;
// Reload
if (s_cacheDocValue.isReset())
{
String sql = "SELECT * FROM AD_Workflow "
+ "WHERE WorkflowType='V' AND IsActive='Y' AND IsValid='Y' "
+ "ORDER BY AD_Client_ID, AD_Table_ID";
ArrayList<MWorkflow> list = new ArrayList<MWorkflow>();
String oldKey = "";
String newKey = null;
PreparedStatement pstmt = null;
try
{
pstmt = DB.prepareStatement (sql, null);
ResultSet rs = pstmt.executeQuery ();
while (rs.next ())
{
MWorkflow wf = new MWorkflow (ctx, rs, null);
newKey = "C" + wf.getAD_Client_ID() + "T" + wf.getAD_Table_ID();
if (!newKey.equals(oldKey) && list.size() > 0)
{
MWorkflow[] wfs = new MWorkflow[list.size()];
list.toArray(wfs);
s_cacheDocValue.put (oldKey, wfs);
list = new ArrayList<MWorkflow>();
}
oldKey = newKey;
list.add(wf);
}
rs.close ();
pstmt.close ();
pstmt = null;
}
catch (Exception e)
{
s_log.log(Level.SEVERE, sql, e);
}
try
{
if (pstmt != null)
pstmt.close ();
pstmt = null;
}
catch (Exception e)
{
pstmt = null;
}
// Last one
if (list.size() > 0)
{
MWorkflow[] wfs = new MWorkflow[list.size()];
list.toArray(wfs);
s_cacheDocValue.put (oldKey, wfs);
}
s_log.config("#" + s_cacheDocValue.size());
}
// Look for Entry
MWorkflow[] retValue = (MWorkflow[])s_cacheDocValue.get(key);
return retValue;
} // getDocValue
/** Single Cache */
private static CCache<Integer,MWorkflow> s_cache = new CCache<Integer,MWorkflow>("AD_Workflow", 20);
/** Document Value Cache */
private static CCache<String,MWorkflow[]> s_cacheDocValue = new CCache<String,MWorkflow[]> ("AD_Workflow", 5);
/** Static Logger */
private static CLogger s_log = CLogger.getCLogger (MWorkflow.class);
/**************************************************************************
* Create/Load Workflow
* @param ctx Context
* @param AD_Workflow_ID ID
*/
public MWorkflow (Properties ctx, int AD_Workflow_ID, String trxName)
{
super (ctx, AD_Workflow_ID, trxName);
if (AD_Workflow_ID == 0)
{
// setAD_Workflow_ID (0);
// setValue (null);
// setName (null);
setAccessLevel (ACCESSLEVEL_Organization);
setAuthor ("ComPiere, Inc.");
setDurationUnit(DURATIONUNIT_Day);
setDuration (1);
setEntityType (ENTITYTYPE_UserMaintained); // U
setIsDefault (false);
setPublishStatus (PUBLISHSTATUS_UnderRevision); // U
setVersion (0);
setCost (0);
setWaitingTime (0);
setWorkingTime (0);
}
loadTrl();
loadNodes();
} // MWorkflow
/**
* Load Constructor
* @param ctx context
* @param rs result set
*/
public MWorkflow (Properties ctx, ResultSet rs, String trxName)
{
super(ctx, rs, trxName);
loadTrl();
loadNodes();
} // Workflow
/** WF Nodes */
private ArrayList<MWFNode> m_nodes = new ArrayList<MWFNode>();
/** Translated Name */
private String m_name_trl = null;
/** Translated Description */
private String m_description_trl = null;
/** Translated Help */
private String m_help_trl = null;
/** Translation Flag */
private boolean m_translated = false;
/**
* Load Translation
*/
private void loadTrl()
{
if (Env.isBaseLanguage(getCtx(), "AD_Workflow") || get_ID() == 0)
return;
String sql = "SELECT Name, Description, Help FROM AD_Workflow_Trl WHERE AD_Workflow_ID=? AND AD_Language=?";
try
{
PreparedStatement pstmt = DB.prepareStatement(sql, null);
pstmt.setInt(1, get_ID());
pstmt.setString(2, Env.getAD_Language(getCtx()));
ResultSet rs = pstmt.executeQuery();
if (rs.next())
{
m_name_trl = rs.getString(1);
m_description_trl = rs.getString(2);
m_help_trl = rs.getString(3);
m_translated = true;
}
rs.close();
pstmt.close();
}
catch (SQLException e)
{
log.log(Level.SEVERE, sql, e);
}
log.fine("Translated=" + m_translated);
} // loadTrl
/**
* Load All Nodes
*/
private void loadNodes()
{
String sql = "SELECT * FROM AD_WF_Node WHERE AD_WorkFlow_ID=? AND IsActive='Y'";
try
{
PreparedStatement pstmt = DB.prepareStatement(sql, get_TrxName());
pstmt.setInt(1, get_ID());
ResultSet rs = pstmt.executeQuery();
while (rs.next())
m_nodes.add (new MWFNode (getCtx(), rs, get_TrxName()));
rs.close();
pstmt.close();
}
catch (SQLException e)
{
log.log(Level.SEVERE, sql, e);
}
log.fine("#" + m_nodes.size());
} // loadNodes
/**************************************************************************
* Get Number of Nodes
* @return number of nodes
*/
public int getNodeCount()
{
return m_nodes.size();
} // getNextNodeCount
/**
* Get the nodes
* @param ordered ordered array
* @param AD_Client_ID for client
* @return array of nodes
*/
public MWFNode[] getNodes(boolean ordered, int AD_Client_ID)
{
if (ordered)
return getNodesInOrder(AD_Client_ID);
//
ArrayList<MWFNode> list = new ArrayList<MWFNode>();
for (int i = 0; i < m_nodes.size(); i++)
{
MWFNode node = m_nodes.get(i);
if (node.getAD_Client_ID() == 0 || node.getAD_Client_ID() == AD_Client_ID)
list.add(node);
}
MWFNode[] retValue = new MWFNode [list.size()];
list.toArray(retValue);
return retValue;
} // getNodes
/**
* Get the first node
* @return array of next nodes
*/
public MWFNode getFirstNode()
{
return getNode (getAD_WF_Node_ID());
} // getFirstNode
/**
* Get Node with ID in Workflow
* @param AD_WF_Node_ID ID
* @return node or null
*/
protected MWFNode getNode (int AD_WF_Node_ID)
{
for (int i = 0; i < m_nodes.size(); i++)
{
MWFNode node = (MWFNode)m_nodes.get(i);
if (node.getAD_WF_Node_ID() == AD_WF_Node_ID)
return node;
}
return null;
} // getNode
/**
* Get the next nodes
* @param AD_WF_Node_ID ID
* @param AD_Client_ID for client
* @return array of next nodes or null
*/
public MWFNode[] getNextNodes (int AD_WF_Node_ID, int AD_Client_ID)
{
MWFNode node = getNode(AD_WF_Node_ID);
if (node == null || node.getNextNodeCount() == 0)
return null;
//
MWFNodeNext[] nexts = node.getTransitions(AD_Client_ID);
ArrayList<MWFNode> list = new ArrayList<MWFNode>();
for (int i = 0; i < nexts.length; i++)
{
MWFNode next = getNode (nexts[i].getAD_WF_Next_ID());
if (next != null)
list.add(next);
}
// Return Nodes
MWFNode[] retValue = new MWFNode [list.size()];
list.toArray(retValue);
return retValue;
} // getNextNodes
/**
* Get The Nodes in Sequence Order
* @param AD_Client_ID client
* @return Nodes in sequence
*/
private MWFNode[] getNodesInOrder(int AD_Client_ID)
{
ArrayList<MWFNode> list = new ArrayList<MWFNode>();
addNodesSF (list, getAD_WF_Node_ID(), AD_Client_ID); // start with first
// Remaining Nodes
if (m_nodes.size() != list.size())
{
// Add Stand alone
for (int n = 0; n < m_nodes.size(); n++)
{
MWFNode node = (MWFNode)m_nodes.get(n);
if (node.getAD_Client_ID() == 0 || node.getAD_Client_ID() == AD_Client_ID)
{
boolean found = false;
for (int i = 0; i < list.size(); i++)
{
MWFNode existing = (MWFNode)list.get(i);
if (existing.getAD_WF_Node_ID() == node.getAD_WF_Node_ID())
{
found = true;
break;
}
}
if (!found)
{
log.log(Level.WARNING, "Added Node w/o transition: " + node);
list.add(node);
}
}
}
}
//
MWFNode[] nodeArray = new MWFNode [list.size()];
list.toArray(nodeArray);
return nodeArray;
} // getNodesInOrder
/**
* Add Nodes recursively (depth first) to Ordered List
* @param list list to add to
* @param AD_WF_Node_ID start node id
* @param AD_Client_ID for client
*/
private void addNodesDF (ArrayList<MWFNode> list, int AD_WF_Node_ID, int AD_Client_ID)
{
MWFNode node = getNode (AD_WF_Node_ID);
if (node != null && !list.contains(node))
{
list.add(node);
// Get Dependent
MWFNodeNext[] nexts = node.getTransitions(AD_Client_ID);
for (int i = 0; i < nexts.length; i++)
addNodesDF (list, nexts[i].getAD_WF_Next_ID(), AD_Client_ID);
}
} // addNodesDF
/**
* Add Nodes recursively (sibling first) to Ordered List
* @param list list to add to
* @param AD_WF_Node_ID start node id
* @param AD_Client_ID for client
*/
private void addNodesSF (ArrayList<MWFNode> list, int AD_WF_Node_ID, int AD_Client_ID)
{
MWFNode node = getNode (AD_WF_Node_ID);
if (node != null
&& (node.getAD_Client_ID() == 0 || node.getAD_Client_ID() == AD_Client_ID))
{
if (!list.contains(node))
list.add(node);
MWFNodeNext[] nexts = node.getTransitions(AD_Client_ID);
for (int i = 0; i < nexts.length; i++)
{
MWFNode child = getNode (nexts[i].getAD_WF_Next_ID());
if (child.getAD_Client_ID() == 0
|| child.getAD_Client_ID() == AD_Client_ID)
{
if (!list.contains(child))
list.add(child);
}
}
for (int i = 0; i < nexts.length; i++)
addNodesSF (list, nexts[i].getAD_WF_Next_ID(), AD_Client_ID);
}
} // addNodesSF
/**************************************************************************
* Get first transition (Next Node) of ID
* @param AD_WF_Node_ID id
* @param AD_Client_ID for client
* @return next AD_WF_Node_ID or 0
*/
public int getNext (int AD_WF_Node_ID, int AD_Client_ID)
{
MWFNode[] nodes = getNodesInOrder(AD_Client_ID);
for (int i = 0; i < nodes.length; i++)
{
if (nodes[i].getAD_WF_Node_ID() == AD_WF_Node_ID)
{
MWFNodeNext[] nexts = nodes[i].getTransitions(AD_Client_ID);
if (nexts.length > 0)
return nexts[0].getAD_WF_Next_ID();
return 0;
}
}
return 0;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -