Added new method IsGenerateUUIDSupported to DB class. Modify UUIDGenerator to process all tables and expose the method that update existing record with uuid as public static method. Added uuid support for insertTranslations, insertAccounting and insertTree in PO.java

This commit is contained in:
Heng Sin Low 2011-01-11 17:37:58 +08:00
parent 1c374d3bdc
commit 4ed732b50c
3 changed files with 91 additions and 18 deletions

View File

@ -83,9 +83,7 @@ public class UUIDGenerator extends SvrProcess {
while(rs.next()) { while(rs.next()) {
int AD_Table_ID = rs.getInt(1); int AD_Table_ID = rs.getInt(1);
String cTableName = rs.getString(2); String cTableName = rs.getString(2);
//skip import and translation table
if (cTableName.startsWith("I_") || cTableName.endsWith("_Trl"))
continue;
String columnName = cTableName + "_UU"; String columnName = cTableName + "_UU";
int AD_Column_ID = DB.getSQLValue(null, "SELECT AD_Column_ID FROM AD_Column Where AD_Table_ID = ? AND ColumnName = ?", AD_Table_ID, columnName); int AD_Column_ID = DB.getSQLValue(null, "SELECT AD_Column_ID FROM AD_Column Where AD_Table_ID = ? AND ColumnName = ?", AD_Table_ID, columnName);
if (AD_Column_ID <= 0) { if (AD_Column_ID <= 0) {
@ -125,7 +123,7 @@ public class UUIDGenerator extends SvrProcess {
return count + " table altered"; return count + " table altered";
} }
private void updateUUID(MColumn column) { public static void updateUUID(MColumn column) {
MTable table = (MTable) column.getAD_Table(); MTable table = (MTable) column.getAD_Table();
int AD_Column_ID = DB.getSQLValue(null, "SELECT AD_Column_ID FROM AD_Column WHERE AD_Table_ID=? AND ColumnName=?", table.getAD_Table_ID(), table.getTableName()+"_ID"); int AD_Column_ID = DB.getSQLValue(null, "SELECT AD_Column_ID FROM AD_Column WHERE AD_Table_ID=? AND ColumnName=?", table.getAD_Table_ID(), table.getTableName()+"_ID");
StringBuffer sql = new StringBuffer("SELECT "); StringBuffer sql = new StringBuffer("SELECT ");
@ -138,6 +136,7 @@ public class UUIDGenerator extends SvrProcess {
keyColumn = "ctid"; keyColumn = "ctid";
} }
sql.append(keyColumn).append(" FROM ").append(table.getTableName()); sql.append(keyColumn).append(" FROM ").append(table.getTableName());
sql.append(" WHERE ").append(column.getColumnName()).append(" IS NULL ");
String updateSQL = "UPDATE "+table.getTableName()+" SET "+column.getColumnName()+"=? WHERE "+keyColumn+"="; String updateSQL = "UPDATE "+table.getTableName()+" SET "+column.getColumnName()+"=? WHERE "+keyColumn+"=";
PreparedStatement stmt = null; PreparedStatement stmt = null;
ResultSet rs = null; ResultSet rs = null;

View File

@ -45,6 +45,7 @@ import org.adempiere.base.event.EventManager;
import org.adempiere.base.event.IEventTopics; import org.adempiere.base.event.IEventTopics;
import org.adempiere.exceptions.AdempiereException; import org.adempiere.exceptions.AdempiereException;
import org.adempiere.exceptions.DBException; import org.adempiere.exceptions.DBException;
import org.adempiere.process.UUIDGenerator;
import org.compiere.Adempiere; import org.compiere.Adempiere;
import org.compiere.acct.Doc; import org.compiere.acct.Doc;
import org.compiere.util.CLogMgt; import org.compiere.util.CLogMgt;
@ -3180,22 +3181,42 @@ public abstract class PO
String tableName = p_info.getTableName(); String tableName = p_info.getTableName();
String keyColumn = m_KeyColumns[0]; String keyColumn = m_KeyColumns[0];
//check whether db have working generate_uuid function.
boolean uuidFunction = DB.isGenerateUUIDSupported();
//uuid column
int uuidColumnId = DB.getSQLValue(get_TrxName(), "SELECT col.AD_Column_ID FROM AD_Column as col INNER JOIN AD_Table as tbl ON col.AD_Table_ID = tbl.AD_Table_ID WHERE tbl.TableName=? AND col.ColumnName=?",
tableName+"_Trl", tableName+"_Trl_UU");
StringBuffer sql = new StringBuffer ("INSERT INTO ") StringBuffer sql = new StringBuffer ("INSERT INTO ")
.append(tableName).append("_Trl (AD_Language,") .append(tableName).append("_Trl (AD_Language,")
.append(keyColumn).append(", ") .append(keyColumn).append(", ")
.append(iColumns) .append(iColumns)
.append(" IsTranslated,AD_Client_ID,AD_Org_ID,Created,Createdby,Updated,UpdatedBy) ") .append(" IsTranslated,AD_Client_ID,AD_Org_ID,Created,Createdby,Updated,UpdatedBy");
.append("SELECT l.AD_Language,t.") if (uuidColumnId > 0 && uuidFunction)
sql.append(",").append(tableName).append("_Trl_UU ) ");
else
sql.append(" ) ");
sql.append("SELECT l.AD_Language,t.")
.append(keyColumn).append(", ") .append(keyColumn).append(", ")
.append(sColumns) .append(sColumns)
.append(" 'N',t.AD_Client_ID,t.AD_Org_ID,t.Created,t.Createdby,t.Updated,t.UpdatedBy ") .append(" 'N',t.AD_Client_ID,t.AD_Org_ID,t.Created,t.Createdby,t.Updated,t.UpdatedBy");
.append("FROM AD_Language l, ").append(tableName).append(" t ") if (uuidColumnId > 0 && uuidFunction)
sql.append(",Generate_UUID() ");
else
sql.append(" ");
sql.append("FROM AD_Language l, ").append(tableName).append(" t ")
.append("WHERE l.IsActive='Y' AND l.IsSystemLanguage='Y' AND l.IsBaseLanguage='N' AND t.") .append("WHERE l.IsActive='Y' AND l.IsSystemLanguage='Y' AND l.IsBaseLanguage='N' AND t.")
.append(keyColumn).append("=").append(get_ID()) .append(keyColumn).append("=").append(get_ID())
.append(" AND NOT EXISTS (SELECT * FROM ").append(tableName) .append(" AND NOT EXISTS (SELECT * FROM ").append(tableName)
.append("_Trl tt WHERE tt.AD_Language=l.AD_Language AND tt.") .append("_Trl tt WHERE tt.AD_Language=l.AD_Language AND tt.")
.append(keyColumn).append("=t.").append(keyColumn).append(")"); .append(keyColumn).append("=t.").append(keyColumn).append(")");
int no = DB.executeUpdate(sql.toString(), m_trxName); int no = DB.executeUpdate(sql.toString(), m_trxName);
if (uuidColumnId > 0 && !uuidFunction) {
MColumn column = new MColumn(getCtx(), uuidColumnId, get_TrxName());
UUIDGenerator.updateUUID(column);
}
log.fine("#" + no); log.fine("#" + no);
return no > 0; return no > 0;
} // insertTranslations } // insertTranslations
@ -3340,12 +3361,24 @@ public abstract class PO
.append("_ID, C_AcctSchema_ID, AD_Client_ID,AD_Org_ID,IsActive, Created,CreatedBy,Updated,UpdatedBy "); .append("_ID, C_AcctSchema_ID, AD_Client_ID,AD_Org_ID,IsActive, Created,CreatedBy,Updated,UpdatedBy ");
for (int i = 0; i < s_acctColumns.size(); i++) for (int i = 0; i < s_acctColumns.size(); i++)
sb.append(",").append(s_acctColumns.get(i)); sb.append(",").append(s_acctColumns.get(i));
//check whether db have working generate_uuid function.
boolean uuidFunction = DB.isGenerateUUIDSupported();
//uuid column
int uuidColumnId = DB.getSQLValue(get_TrxName(), "SELECT col.AD_Column_ID FROM AD_Column as col INNER JOIN AD_Table as tbl ON col.AD_Table_ID = tbl.AD_Table_ID WHERE tbl.TableName=? AND col.ColumnName=?",
acctTable, acctTable+"_UU");
if (uuidColumnId > 0 && uuidFunction)
sb.append(",").append(acctTable).append("_UU");
// .. SELECT // .. SELECT
sb.append(") SELECT ").append(get_ID()) sb.append(") SELECT ").append(get_ID())
.append(", p.C_AcctSchema_ID, p.AD_Client_ID,0,'Y', SysDate,") .append(", p.C_AcctSchema_ID, p.AD_Client_ID,0,'Y', SysDate,")
.append(getUpdatedBy()).append(",SysDate,").append(getUpdatedBy()); .append(getUpdatedBy()).append(",SysDate,").append(getUpdatedBy());
for (int i = 0; i < s_acctColumns.size(); i++) for (int i = 0; i < s_acctColumns.size(); i++)
sb.append(",p.").append(s_acctColumns.get(i)); sb.append(",p.").append(s_acctColumns.get(i));
//uuid column
if (uuidColumnId > 0 && uuidFunction)
sb.append(",generate_uuid()");
// .. FROM // .. FROM
sb.append(" FROM ").append(acctBaseTable) sb.append(" FROM ").append(acctBaseTable)
.append(" p WHERE p.AD_Client_ID=").append(getAD_Client_ID()); .append(" p WHERE p.AD_Client_ID=").append(getAD_Client_ID());
@ -3361,6 +3394,12 @@ public abstract class PO
else else
log.warning("#" + no log.warning("#" + no
+ " - Table=" + acctTable + " from " + acctBaseTable); + " - Table=" + acctTable + " from " + acctBaseTable);
//fall back to the slow java client update code
if (uuidColumnId > 0 && !uuidFunction) {
MColumn column = new MColumn(getCtx(), uuidColumnId, get_TrxName());
UUIDGenerator.updateUUID(column);
}
return no > 0; return no > 0;
} // insert_Accounting } // insert_Accounting
@ -3394,13 +3433,30 @@ public abstract class PO
*/ */
protected boolean insert_Tree (String treeType, int C_Element_ID) protected boolean insert_Tree (String treeType, int C_Element_ID)
{ {
String tableName = MTree_Base.getNodeTableName(treeType);
//check whether db have working generate_uuid function.
boolean uuidFunction = DB.isGenerateUUIDSupported();
//uuid column
int uuidColumnId = DB.getSQLValue(get_TrxName(), "SELECT col.AD_Column_ID FROM AD_Column as col INNER JOIN AD_Table as tbl ON col.AD_Table_ID = tbl.AD_Table_ID WHERE tbl.TableName=? AND col.ColumnName=?",
tableName, tableName+"_UU");
StringBuffer sb = new StringBuffer ("INSERT INTO ") StringBuffer sb = new StringBuffer ("INSERT INTO ")
.append(MTree_Base.getNodeTableName(treeType)) .append(tableName)
.append(" (AD_Client_ID,AD_Org_ID, IsActive,Created,CreatedBy,Updated,UpdatedBy, " .append(" (AD_Client_ID,AD_Org_ID, IsActive,Created,CreatedBy,Updated,UpdatedBy, "
+ "AD_Tree_ID, Node_ID, Parent_ID, SeqNo) " + "AD_Tree_ID, Node_ID, Parent_ID, SeqNo");
+ "SELECT t.AD_Client_ID, 0, 'Y', SysDate, "+getUpdatedBy()+", SysDate, "+getUpdatedBy()+"," if (uuidColumnId > 0 && uuidFunction)
+ "t.AD_Tree_ID, ").append(get_ID()).append(", 0, 999 " sb.append(", ").append(tableName).append("_UU) ");
+ "FROM AD_Tree t " else
sb.append(") ");
sb.append("SELECT t.AD_Client_ID, 0, 'Y', SysDate, "+getUpdatedBy()+", SysDate, "+getUpdatedBy()+","
+ "t.AD_Tree_ID, ").append(get_ID()).append(", 0, 999");
if (uuidColumnId > 0 && uuidFunction)
sb.append(", Generate_UUID() ");
else
sb.append(" ");
sb.append("FROM AD_Tree t "
+ "WHERE t.AD_Client_ID=").append(getAD_Client_ID()).append(" AND t.IsActive='Y'"); + "WHERE t.AD_Client_ID=").append(getAD_Client_ID()).append(" AND t.IsActive='Y'");
// Account Element Value handling // Account Element Value handling
if (C_Element_ID != 0) if (C_Element_ID != 0)
@ -3416,6 +3472,12 @@ public abstract class PO
log.fine("#" + no + " - TreeType=" + treeType); log.fine("#" + no + " - TreeType=" + treeType);
else else
log.warning("#" + no + " - TreeType=" + treeType); log.warning("#" + no + " - TreeType=" + treeType);
if (uuidColumnId > 0 && !uuidFunction )
{
MColumn column = new MColumn(getCtx(), uuidColumnId, get_TrxName());
UUIDGenerator.updateUUID(column);
}
return no > 0; return no > 0;
} // insert_Tree } // insert_Tree

View File

@ -2236,6 +2236,18 @@ public final class DB
} }
} }
/***
* @return true if current db have working generate_uuid function. generate_uuid doesn't work on 64 bit postgresql
* on windows yet.
*/
public static boolean isGenerateUUIDSupported() {
String uuidTest = null;
try {
uuidTest = getSQLValueStringEx(null, "SELECT Generate_UUID() FROM Dual");
} catch (Exception e) {}
return uuidTest != null && uuidTest.trim().length() == 36;
}
private static void verifyTrx(String trxName, String sql) { private static void verifyTrx(String trxName, String sql) {
if (trxName != null && Trx.get(trxName, false) == null) { if (trxName != null && Trx.get(trxName, false) == null) {
// Using a trx that was previously closed or never opened // Using a trx that was previously closed or never opened