📄 mtable.java
字号:
else if (m_inserting
|| (m_rowData[col] == null && rowData[col] != null)
|| (m_rowData[col] != null && rowData[col] == null)
|| !m_rowData[col].equals (rowData[col])) // changed
{
// Original == DB
if (m_inserting || !m_compareDB
|| (m_rowData[col] == null && rowDataDB[col] == null)
|| (m_rowData[col] != null && m_rowData[col].equals (rowDataDB[col])))
{
if (CLogMgt.isLevelFinest())
log.fine(columnName + "=" + rowData[col]
+ " " + (rowData[col]==null ? "" : rowData[col].getClass().getName()));
//
boolean encrypted = field.isEncryptedColumn();
//
String type = "String";
if (rowData[col] == null)
{
if (manualUpdate)
createUpdateSql (columnName, "NULL");
else
rs.updateNull (colRs); // ***
}
// ID - int
else if (DisplayType.isID (field.getDisplayType())
|| field.getDisplayType() == DisplayType.Integer)
{
try
{
Object dd = rowData[col];
Integer iii = null;
if (dd instanceof Integer)
iii = (Integer)dd;
else
iii = new Integer(dd.toString());
if (encrypted)
iii = (Integer)encrypt(iii);
if (manualUpdate)
createUpdateSql (columnName, String.valueOf (iii));
else
rs.updateInt (colRs, iii.intValue()); // ***
}
catch (Exception e) // could also be a String (AD_Language, AD_Message)
{
if (manualUpdate)
createUpdateSql (columnName, DB.TO_STRING (rowData[col].toString ()));
else
rs.updateString (colRs, rowData[col].toString ()); // ***
}
type = "Int";
}
// Numeric - BigDecimal
else if (DisplayType.isNumeric (field.getDisplayType ()))
{
BigDecimal bd = (BigDecimal)rowData[col];
if (encrypted)
bd = (BigDecimal)encrypt(bd);
if (manualUpdate)
createUpdateSql (columnName, bd.toString ());
else
rs.updateBigDecimal (colRs, bd); // ***
type = "Number";
}
// Date - Timestamp
else if (DisplayType.isDate (field.getDisplayType ()))
{
Timestamp ts = (Timestamp)rowData[col];
if (encrypted)
ts = (Timestamp)encrypt(ts);
if (manualUpdate)
createUpdateSql (columnName, DB.TO_DATE (ts, false));
else
rs.updateTimestamp (colRs, ts); // ***
type = "Date";
}
// LOB
else if (field.getDisplayType() == DisplayType.TextLong)
{
PO_LOB lob = new PO_LOB (getTableName(), columnName,
null, field.getDisplayType(), rowData[col]);
lobAdd(lob);
type = "CLOB";
}
// Boolean
else if (field.getDisplayType() == DisplayType.YesNo)
{
String yn = null;
if (rowData[col] instanceof Boolean)
{
Boolean bb = (Boolean)rowData[col];
yn = bb.booleanValue() ? "Y" : "N";
}
else
yn = "Y".equals(rowData[col]) ? "Y" : "N";
if (encrypted)
yn = (String)yn;
if (manualUpdate)
createUpdateSql (columnName, DB.TO_STRING (yn));
else
rs.updateString (colRs, yn); // ***
}
// String and others
else
{
String str = rowData[col].toString ();
if (encrypted)
str = (String)encrypt(str);
if (manualUpdate)
createUpdateSql (columnName, DB.TO_STRING (str));
else
rs.updateString (colRs, str); // ***
}
//
is = INFO + columnName + "= " + m_rowData[col]
+ " -> " + rowData[col] + " (" + type + ")";
if (encrypted)
is += " encrypted";
log.fine(is);
}
// Original != DB
else
{
error = true;
is = ERROR + field.getColumnName () + "= " + m_rowData[col]
+ " != DB: " + rowDataDB[col] + " -> " + rowData[col];
log.fine(is);
}
} // Data changed
// Single Key - retrieval sql
if (field.isKey() && !m_inserting)
{
if (rowData[col] == null)
throw new RuntimeException("Key is NULL - " + columnName);
if (columnName.endsWith ("_ID"))
singleRowWHERE.append (columnName).append ("=").append (rowData[col]);
else
{
singleRowWHERE = new StringBuffer(); // overwrite
singleRowWHERE.append (columnName).append ("=").append (DB.TO_STRING(rowData[col].toString()));
}
}
// MultiKey Inserting - retrieval sql
if (field.isParentColumn())
{
if (rowData[col] == null)
throw new RuntimeException("MultiKey Parent is NULL - " + columnName);
if (multiRowWHERE.length() != 0)
multiRowWHERE.append(" AND ");
if (columnName.endsWith ("_ID"))
multiRowWHERE.append (columnName).append ("=").append (rowData[col]);
else
multiRowWHERE.append (columnName).append ("=").append (DB.TO_STRING(rowData[col].toString()));
}
//
colRs++;
} // for every column
if (error)
{
if (manualUpdate)
createUpdateSqlReset();
else
rs.cancelRowUpdates();
rs.close();
pstmt.close();
fireDataStatusEEvent("SaveErrorDataChanged", "", true);
dataRefresh(m_rowChanged);
return SAVE_ERROR;
}
/**
* Save to Database
*/
//
String whereClause = singleRowWHERE.toString();
if (whereClause.length() == 0)
whereClause = multiRowWHERE.toString();
if (m_inserting)
{
log.fine("Inserting ...");
if (manualUpdate)
{
String sql = createUpdateSql(true, null);
int no = DB.executeUpdateEx (sql, null); // no Trx
if (no != 1)
log.log(Level.SEVERE, "Insert #=" + no + " - " + sql);
}
else
rs.insertRow();
}
else
{
log.fine("Updating ... " + whereClause);
if (manualUpdate)
{
String sql = createUpdateSql(false, whereClause);
int no = DB.executeUpdateEx (sql, null); // no Trx
if (no != 1)
log.log(Level.SEVERE, "Update #=" + no + " - " + sql);
}
else
rs.updateRow();
}
log.fine("Committing ...");
DB.commit(true, null); // no Trx
//
lobSave(whereClause);
rs.close();
pstmt.close();
// Need to re-read row to get ROWID, Key, DocumentNo, Trigger, virtual columns
log.fine("Reading ... " + whereClause);
StringBuffer refreshSQL = new StringBuffer(m_SQL_Select)
.append(" WHERE ").append(whereClause);
pstmt = DB.prepareStatement(refreshSQL.toString(), null);
rs = pstmt.executeQuery();
if (rs.next())
{
rowDataDB = readData(rs);
// update buffer
m_buffer.set(sort.index, rowDataDB);
fireTableRowsUpdated(m_rowChanged, m_rowChanged);
}
else
log.log(Level.SEVERE, "Inserted row not found");
//
rs.close();
pstmt.close();
pstmt = null;
}
catch (SQLException e)
{
try
{
if (pstmt != null)
pstmt.close ();
pstmt = null;
}
catch (Exception ex)
{
}
String msg = "SaveError";
if (e.getErrorCode() == 1) // Unique Constraint
{
log.log(Level.SEVERE, "Key Not Unique", e);
msg = "SaveErrorNotUnique";
}
else
log.log(Level.SEVERE, select.toString(), e);
fireDataStatusEEvent(msg, e.getLocalizedMessage(), true);
return SAVE_ERROR;
}
// everything ok
m_rowData = null;
m_changed = false;
m_compareDB = true;
m_rowChanged = -1;
m_newRow = -1;
m_inserting = false;
fireDataStatusIEvent("Saved", "");
//
log.info("fini");
return SAVE_OK;
} // dataSave
/**
* Save via PO
* @param Record_ID
* @return SAVE_ERROR or SAVE_OK
*/
private char dataSavePO (int Record_ID) throws Exception
{
log.fine("ID=" + Record_ID);
//
MSort sort = (MSort)m_sort.get(m_rowChanged);
Object[] rowData = (Object[])m_buffer.get(sort.index);
//
M_Table table = M_Table.get (m_ctx, m_AD_Table_ID);
PO po = null;
if (Record_ID != -1)
po = table.getPO(Record_ID, null);
else // Multi - Key
po = table.getPO(getWhereClause(rowData), null);
// No Persistent Object
if (po == null)
throw new ClassNotFoundException ("No Persistent Object");
int size = m_fields.size();
for (int col = 0; col < size; col++)
{
MField field = (MField)m_fields.get (col);
if (field.isVirtualColumn())
continue;
String columnName = field.getColumnName ();
Object value = rowData[col];
Object oldValue = m_rowData[col];
// RowID
if (field.getDisplayType () == DisplayType.RowID)
; // ignore
// Nothing changed & null
else if (oldValue == null && value == null)
; // ignore
// *** Data changed ***
else if (m_inserting
|| (oldValue == null && value != null)
|| (oldValue != null && value == null)
|| !oldValue.equals (value)) // changed
{
// Check existence
int poIndex = po.get_ColumnIndex(columnName);
if (poIndex < 0)
{
// Custom Fields not in PO
po.set_CustomColumn(columnName, value);
// log.log(Level.SEVERE, "Column not found: " + columnName);
continue;
}
Object dbValue = po.get_Value(poIndex);
if (m_inserting
|| !m_compareDB
// Original == DB
|| (oldValue == null && dbValue == null)
|| (oldValue != null && oldValue.equals (dbValue))
// Target == DB (changed by trigger to new value already)
|| (value == null && dbValue == null)
|| (value != null && value.equals (dbValue)) )
{
po.set_ValueNoCheck (columnName, value);
}
// Original != DB
else
{
String msg = columnName
+ "= " + oldValue
+ (oldValue==null ? "" : "(" + oldValue.getClass().getName() + ")")
+ " != DB: " + dbValue
+ (dbValue==null ? "" : "(" + dbValue.getClass().getName() + ")")
+ " -> New: " + value
+ (value==null ? "" : "(" + value.getClass().getName() + ")");
// CLogMgt.setLevel(Level.FINEST);
// po.dump();
fireDataStatusEEvent("SaveErrorDataChanged", msg, true);
dataRefresh(m_rowChanged);
return SAVE_ERROR;
}
} // Data changed
} // for every column
if (!po.save())
{
String msg = "SaveError";
String info = "";
ValueNamePair pp = CLogger.retrieveError();
if (pp != null)
{
msg = pp.getValue();
info = pp.getName();
// Unique Constraint
Exception ex = CLogger.retrieveException();
if (ex != null
&& ex instanceof SQLException
&& ((SQLException)ex).getErrorCode() == 1)
msg = "SaveErrorNotUnique";
}
fireDataStatusEEvent(msg, info, true);
return SAVE_ERROR;
}
// Refresh - update buffer
String whereClause = po.get_WhereClause(true);
log.fine("Reading ... " + whereClause);
StringBuffer refreshSQL = new StringBuffer(m_SQL_Select)
.append(" WHERE ").append(whereClause);
PreparedStatement pstmt = DB.prepareStatement(refreshSQL.toString(), null);
try
{
ResultSet rs = pstmt.executeQuery();
if (rs.next())
{
Object[] rowDataDB = readData(rs);
// update buffer
m_buffer.set(sort.index, rowDataDB);
fireTableRowsUpdated(m_rowChanged, m_rowChanged);
}
rs.close();
pstmt.close();
pstmt = null;
}
catch (SQLException e)
{
try
{
if (pstmt != null)
pstmt.close ();
pstmt = null;
}
catch (Exception ex)
{
}
String msg = "SaveError";
log.log(Level.SEVERE, refreshSQL.toString(), e);
fireDataStatusEEvent(msg, e.getLocalizedMessage(), true);
return SAVE_ERROR;
}
// everything ok
m_rowData = null;
m_changed = false;
m_compareDB = true;
m_rowChanged = -1;
m_newRow = -1;
m_inserting = false;
//
ValueNamePair pp = CLogger.retrieveWarning();
if (pp != null)
{
String msg = pp.getValue();
String info = pp.getName();
fireDataStatusEEvent(msg, info, false);
}
else
{
pp = CLogger.retrieveInfo();
String msg = "Saved";
String info = "";
if (pp != null)
{
msg = pp.getValue();
info = pp.getName();
}
fireDataStatusIEvent(msg, info);
}
//
log.config("fini");
return SAVE_OK;
} // dataSavePO
/**
* Get Record Where Clause from data (single key or multi-parent)
* @param rowData data
* @return where clause or null
*/
private String getWhereClause (Object[] rowData)
{
int size = m_fields.size();
StringBuffer singleRowWHERE = null;
StringBuffer multiRowWHERE = null;
for (int col = 0; col < size; col++)
{
MField field = (MField)m_fields.get (col);
if (field.isKey())
{
String columnName = field.getColumnName();
Object value = rowData[col];
if (value == null)
{
log.log(Level.WARNING, "PK data is null - " + columnName);
return null;
}
if (columnName.endsWith ("_ID"))
singleRowWHERE = new StringBuffer(columnName)
.append ("=").append (value);
else
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -