FR [ 2214883 ] Remove SQL code and Replace for Query

https://sourceforge.net/tracker/index.php?func=detail&aid=2214883&group_id=176962&atid=879335
This commit is contained in:
teo_sarca 2009-04-14 09:21:23 +00:00
parent 9b92bc56db
commit 0626cb260f
2 changed files with 68 additions and 80 deletions

View File

@ -23,10 +23,12 @@ import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.sql.Timestamp; import java.sql.Timestamp;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List;
import java.util.Properties; import java.util.Properties;
import java.util.logging.Level;
import org.adempiere.exceptions.DBException;
import org.compiere.model.MColumn; import org.compiere.model.MColumn;
import org.compiere.model.Query;
import org.compiere.model.X_AD_WF_Node; import org.compiere.model.X_AD_WF_Node;
import org.compiere.util.CCache; import org.compiere.util.CCache;
import org.compiere.util.DB; import org.compiere.util.DB;
@ -38,6 +40,9 @@ import org.compiere.util.Msg;
* *
* @author Jorg Janke * @author Jorg Janke
* @version $Id: MWFNode.java,v 1.2 2006/07/30 00:51:05 jjanke Exp $ * @version $Id: MWFNode.java,v 1.2 2006/07/30 00:51:05 jjanke Exp $
*
* @author Teo Sarca, www.arhipac.ro
* <li>FR [ 2214883 ] Remove SQL code and Replace for Query
*/ */
public class MWFNode extends X_AD_WF_Node public class MWFNode extends X_AD_WF_Node
{ {
@ -66,7 +71,7 @@ public class MWFNode extends X_AD_WF_Node
} // get } // get
/** Cache */ /** Cache */
private static CCache<Integer,MWFNode> s_cache = new CCache<Integer,MWFNode> ("AD_WF_Node", 50); private static CCache<Integer,MWFNode> s_cache = new CCache<Integer,MWFNode> (Table_Name, 50);
/************************************************************************** /**************************************************************************
@ -129,13 +134,13 @@ public class MWFNode extends X_AD_WF_Node
loadNext(); loadNext();
loadTrl(); loadTrl();
// Save to Cache // Save to Cache
s_cache.put (new Integer(getAD_WF_Node_ID()), this); s_cache.put (get_ID(), this);
} // MWFNode } // MWFNode
/** Next Modes */ /** Next Modes */
private ArrayList<MWFNodeNext> m_next = new ArrayList<MWFNodeNext>(); private List<MWFNodeNext> m_next = new ArrayList<MWFNodeNext>();
/** Translated Name */ /** Translated Name */
private String m_name_trl = null; private String m_name_trl = null;
/** Translated Description */ /** Translated Description */
@ -166,25 +171,15 @@ public class MWFNode extends X_AD_WF_Node
*/ */
private void loadNext() private void loadNext()
{ {
String sql = "SELECT * FROM AD_WF_NodeNext WHERE AD_WF_Node_ID=? AND IsActive='Y' ORDER BY SeqNo"; m_next = new Query(getCtx(), MWFNodeNext.Table_Name, "AD_WF_Node_ID=?", get_TrxName())
.setParameters(new Object[]{get_ID()})
.setOnlyActiveRecords(true)
.setOrderBy(MWFNodeNext.COLUMNNAME_SeqNo)
.list();
boolean splitAnd = SPLITELEMENT_AND.equals(getSplitElement()); boolean splitAnd = SPLITELEMENT_AND.equals(getSplitElement());
try for (MWFNodeNext next : m_next)
{ {
PreparedStatement pstmt = DB.prepareStatement(sql, get_TrxName()); next.setFromSplitAnd(splitAnd);
pstmt.setInt(1, get_ID());
ResultSet rs = pstmt.executeQuery();
while (rs.next())
{
MWFNodeNext next = new MWFNodeNext (getCtx(), rs, get_TrxName());
next.setFromSplitAnd(splitAnd);
m_next.add(next);
}
rs.close();
pstmt.close();
}
catch (SQLException e)
{
log.log(Level.SEVERE, sql, e);
} }
log.fine("#" + m_next.size()); log.fine("#" + m_next.size());
} // loadNext } // loadNext
@ -196,13 +191,16 @@ public class MWFNode extends X_AD_WF_Node
{ {
if (Env.isBaseLanguage(getCtx(), "AD_Workflow") || get_ID() == 0) if (Env.isBaseLanguage(getCtx(), "AD_Workflow") || get_ID() == 0)
return; return;
String sql = "SELECT Name, Description, Help FROM AD_WF_Node_Trl WHERE AD_WF_Node_ID=? AND AD_Language=?"; final String sql = "SELECT Name, Description, Help FROM AD_WF_Node_Trl"
+" WHERE AD_WF_Node_ID=? AND AD_Language=?";
PreparedStatement pstmt = null;
ResultSet rs = null;
try try
{ {
PreparedStatement pstmt = DB.prepareStatement(sql, get_TrxName()); pstmt = DB.prepareStatement(sql, get_TrxName());
pstmt.setInt(1, get_ID()); pstmt.setInt(1, get_ID());
pstmt.setString(2, Env.getAD_Language(getCtx())); pstmt.setString(2, Env.getAD_Language(getCtx()));
ResultSet rs = pstmt.executeQuery(); rs = pstmt.executeQuery();
if (rs.next()) if (rs.next())
{ {
m_name_trl = rs.getString(1); m_name_trl = rs.getString(1);
@ -210,12 +208,16 @@ public class MWFNode extends X_AD_WF_Node
m_help_trl = rs.getString(3); m_help_trl = rs.getString(3);
m_translated = true; m_translated = true;
} }
rs.close();
pstmt.close();
} }
catch (SQLException e) catch (SQLException e)
{ {
log.log(Level.SEVERE, sql, e); //log.log(Level.SEVERE, sql, e);
throw new DBException(e, sql);
}
finally
{
DB.close(rs, pstmt);
rs = null; pstmt = null;
} }
log.fine("Trl=" + m_translated); log.fine("Trl=" + m_translated);
} // loadTrl } // loadTrl

View File

@ -22,12 +22,15 @@ import java.sql.SQLException;
import java.sql.Timestamp; import java.sql.Timestamp;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Calendar; import java.util.Calendar;
import java.util.List;
import java.util.Properties; import java.util.Properties;
import java.util.logging.Level; import java.util.logging.Level;
import org.adempiere.exceptions.DBException;
import org.compiere.model.MMenu; import org.compiere.model.MMenu;
import org.compiere.model.MProduct; import org.compiere.model.MProduct;
import org.compiere.model.MWindow; import org.compiere.model.MWindow;
import org.compiere.model.Query;
import org.compiere.model.X_AD_WF_Node; import org.compiere.model.X_AD_WF_Node;
import org.compiere.model.X_AD_Workflow; import org.compiere.model.X_AD_Workflow;
import org.compiere.process.ProcessInfo; import org.compiere.process.ProcessInfo;
@ -44,6 +47,9 @@ import org.compiere.util.Trx;
* *
* @author Jorg Janke * @author Jorg Janke
* @version $Id: MWorkflow.java,v 1.4 2006/07/30 00:51:05 jjanke Exp $ * @version $Id: MWorkflow.java,v 1.4 2006/07/30 00:51:05 jjanke Exp $
*
* @author Teo Sarca, www.arhipac.ro
* <li>FR [ 2214883 ] Remove SQL code and Replace for Query
*/ */
public class MWorkflow extends X_AD_Workflow public class MWorkflow extends X_AD_Workflow
{ {
@ -61,13 +67,12 @@ public class MWorkflow extends X_AD_Workflow
*/ */
public static MWorkflow get (Properties ctx, int AD_Workflow_ID) public static MWorkflow get (Properties ctx, int AD_Workflow_ID)
{ {
Integer key = new Integer (AD_Workflow_ID); MWorkflow retValue = (MWorkflow)s_cache.get(AD_Workflow_ID);
MWorkflow retValue = (MWorkflow)s_cache.get(key);
if (retValue != null) if (retValue != null)
return retValue; return retValue;
retValue = new MWorkflow (ctx, AD_Workflow_ID, null); retValue = new MWorkflow (ctx, AD_Workflow_ID, null);
if (retValue.get_ID() != 0) if (retValue.get_ID() != 0)
s_cache.put(key, retValue); s_cache.put(AD_Workflow_ID, retValue);
return retValue; return retValue;
} // get } // get
@ -87,41 +92,27 @@ public class MWorkflow extends X_AD_Workflow
// Reload // Reload
if (s_cacheDocValue.isReset()) if (s_cacheDocValue.isReset())
{ {
String sql = "SELECT * FROM AD_Workflow " final String whereClause = "WorkflowType=? AND IsValid=?";
+ "WHERE WorkflowType='V' AND IsActive='Y' AND IsValid='Y' " List<MWorkflow> workflows = new Query(ctx, Table_Name, whereClause, trxName)
+ "ORDER BY AD_Client_ID, AD_Table_ID"; .setParameters(new Object[]{WORKFLOWTYPE_DocumentValue, true})
.setOnlyActiveRecords(true)
.setOrderBy("AD_Client_ID, AD_Table_ID")
.list();
ArrayList<MWorkflow> list = new ArrayList<MWorkflow>(); ArrayList<MWorkflow> list = new ArrayList<MWorkflow>();
String oldKey = ""; String oldKey = "";
String newKey = null; String newKey = null;
PreparedStatement pstmt = null; for (MWorkflow wf : workflows)
ResultSet rs = null;
try
{ {
pstmt = DB.prepareStatement (sql, trxName); //Bug 1568766 newKey = "C" + wf.getAD_Client_ID() + "T" + wf.getAD_Table_ID();
rs = pstmt.executeQuery (); if (!newKey.equals(oldKey) && list.size() > 0)
while (rs.next ())
{ {
MWorkflow wf = new MWorkflow (ctx, rs, null); MWorkflow[] wfs = new MWorkflow[list.size()];
newKey = "C" + wf.getAD_Client_ID() + "T" + wf.getAD_Table_ID(); list.toArray(new MWorkflow[list.size()]);
if (!newKey.equals(oldKey) && list.size() > 0) s_cacheDocValue.put (oldKey, wfs);
{ list = new ArrayList<MWorkflow>();
MWorkflow[] wfs = new MWorkflow[list.size()];
list.toArray(wfs);
s_cacheDocValue.put (oldKey, wfs);
list = new ArrayList<MWorkflow>();
}
oldKey = newKey;
list.add(wf);
} }
} oldKey = newKey;
catch (Exception e) list.add(wf);
{
s_log.log(Level.SEVERE, sql, e);
}
finally
{
DB.close(rs, pstmt);
rs = null; pstmt = null;
} }
// Last one // Last one
@ -202,7 +193,7 @@ public class MWorkflow extends X_AD_Workflow
} // Workflow } // Workflow
/** WF Nodes */ /** WF Nodes */
private ArrayList<MWFNode> m_nodes = new ArrayList<MWFNode>(); private List<MWFNode> m_nodes = new ArrayList<MWFNode>();
/** Translated Name */ /** Translated Name */
private String m_name_trl = null; private String m_name_trl = null;
@ -221,12 +212,14 @@ public class MWorkflow extends X_AD_Workflow
if (Env.isBaseLanguage(getCtx(), "AD_Workflow") || get_ID() == 0) if (Env.isBaseLanguage(getCtx(), "AD_Workflow") || get_ID() == 0)
return; return;
String sql = "SELECT Name, Description, Help FROM AD_Workflow_Trl WHERE AD_Workflow_ID=? AND AD_Language=?"; String sql = "SELECT Name, Description, Help FROM AD_Workflow_Trl WHERE AD_Workflow_ID=? AND AD_Language=?";
PreparedStatement pstmt = null;
ResultSet rs = null;
try try
{ {
PreparedStatement pstmt = DB.prepareStatement(sql, null); pstmt = DB.prepareStatement(sql, null);
pstmt.setInt(1, get_ID()); pstmt.setInt(1, get_ID());
pstmt.setString(2, Env.getAD_Language(getCtx())); pstmt.setString(2, Env.getAD_Language(getCtx()));
ResultSet rs = pstmt.executeQuery(); rs = pstmt.executeQuery();
if (rs.next()) if (rs.next())
{ {
m_name_trl = rs.getString(1); m_name_trl = rs.getString(1);
@ -234,12 +227,16 @@ public class MWorkflow extends X_AD_Workflow
m_help_trl = rs.getString(3); m_help_trl = rs.getString(3);
m_translated = true; m_translated = true;
} }
rs.close();
pstmt.close();
} }
catch (SQLException e) catch (SQLException e)
{ {
log.log(Level.SEVERE, sql, e); //log.log(Level.SEVERE, sql, e);
throw new DBException(e, sql);
}
finally
{
DB.close(rs, pstmt);
rs = null; pstmt = null;
} }
log.fine("Translated=" + m_translated); log.fine("Translated=" + m_translated);
} // loadTrl } // loadTrl
@ -249,21 +246,10 @@ public class MWorkflow extends X_AD_Workflow
*/ */
private void loadNodes() private void loadNodes()
{ {
String sql = "SELECT * FROM AD_WF_Node WHERE AD_WorkFlow_ID=? AND IsActive='Y'"; m_nodes = new Query(getCtx(), MWFNode.Table_Name, "AD_WorkFlow_ID=?", get_TrxName())
try .setParameters(new Object[]{get_ID()})
{ .setOnlyActiveRecords(true)
PreparedStatement pstmt = DB.prepareStatement(sql, get_TrxName()); .list();
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()); log.fine("#" + m_nodes.size());
} // loadNodes } // loadNodes