use of org.adempiere.exceptions.DBException in project adempiere by adempiere.
the class SetGetUtil method updateColumns.
/**
* Update columns from the result of the given query.
*
* @param models
* @param columnNames
* @param sql
* @param params
* @param trxName
*
* @see #updateColumns(SetGetModel[], String[], ResultSet)
*/
public static void updateColumns(SetGetModel[] models, String[] columnNames, String sql, Object[] params, String trxName) {
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
pstmt = DB.prepareStatement(sql, trxName);
DB.setParameters(pstmt, params);
rs = pstmt.executeQuery();
updateColumns(models, columnNames, rs);
} catch (SQLException e) {
throw new DBException(e, sql);
} finally {
DB.close(rs, pstmt);
rs = null;
pstmt = null;
}
}
use of org.adempiere.exceptions.DBException in project adempiere by adempiere.
the class PO method saveNew.
/**
* Create New Record
* @return true if new record inserted
*/
private boolean saveNew() {
// Set ID for single key - Multi-Key values need explicitly be set previously
if (m_IDs.length == 1 && p_info.hasKeyColumn() && m_KeyColumns[0].endsWith("_ID") && // AD_Language, EntityType
!isDirectLoad) {
int no = saveNew_getID();
if (no <= 0)
no = DB.getNextID(getAD_Client_ID(), p_info.getTableName(), m_trxName);
// the primary key is not overwrite with the local sequence
if (isReplication()) {
if (get_ID() > 0) {
no = get_ID();
}
}
if (no <= 0) {
log.severe("No NextID (" + no + ")");
return saveFinish(true, false);
}
m_IDs[0] = new Integer(no);
set_ValueNoCheck(m_KeyColumns[0], m_IDs[0]);
}
if (m_trxName == null)
log.fine(p_info.getTableName() + " - " + get_WhereClause(true));
else
log.fine("[" + m_trxName + "] - " + p_info.getTableName() + " - " + get_WhereClause(true));
// Set new DocumentNo
String columnName = "DocumentNo";
int index = p_info.getColumnIndex(columnName);
if (index != -1 && p_info.getColumn(index).ColumnSQL == null) {
String value = (String) get_Value(index);
if (value != null && value.startsWith("<") && value.endsWith(">"))
value = null;
if (value == null || value.length() == 0) {
int dt = p_info.getColumnIndex("C_DocTypeTarget_ID");
if (dt == -1)
dt = p_info.getColumnIndex("C_DocType_ID");
if (// get based on Doc Type (might return null)
dt != -1)
value = DB.getDocumentNo(get_ValueAsInt(dt), m_trxName, false, this);
if (// not overwritten by DocType and not manually entered
value == null)
value = DB.getDocumentNo(getAD_Client_ID(), p_info.getTableName(), m_trxName, this);
set_ValueNoCheck(columnName, value);
}
}
// Set empty Value
columnName = "Value";
index = p_info.getColumnIndex(columnName);
if (index != -1) {
String value = (String) get_Value(index);
if (value == null || value.length() == 0) {
value = DB.getDocumentNo(getAD_Client_ID(), p_info.getTableName(), m_trxName, this);
set_ValueNoCheck(columnName, value);
}
}
lobReset();
// Change Log
MSession session = MSession.get(p_ctx, false);
if (session == null)
log.fine("No Session found");
else // log migration
if (Ini.isPropertyBool(Ini.P_LOGMIGRATIONSCRIPT))
session.logMigration(this, p_info, MMigrationStep.ACTION_Insert);
int AD_ChangeLog_ID = 0;
// SQL
StringBuffer sqlInsert = new StringBuffer("INSERT INTO ");
sqlInsert.append(p_info.getTableName()).append(" (");
StringBuffer sqlValues = new StringBuffer(") VALUES (");
int size = get_ColumnCount();
boolean doComma = false;
for (int i = 0; i < size; i++) {
Object value = get_Value(i);
// Don't insert NULL values (allows Database defaults)
if (value == null || p_info.isVirtualColumn(i))
continue;
// Display Type
int dt = p_info.getColumnDisplayType(i);
if (DisplayType.isLOB(dt) || (DisplayType.isText(dt) && p_info.getFieldLength(i) > 4000)) {
lobAdd(value, i, dt);
continue;
}
// ** add column **
if (doComma) {
sqlInsert.append(",");
sqlValues.append(",");
} else
doComma = true;
sqlInsert.append(p_info.getColumnName(i));
//
// Based on class of definition, not class of value
Class<?> c = p_info.getColumnClass(i);
try {
if (// may have need to deal with null values differently
c == Object.class)
sqlValues.append(saveNewSpecial(value, i));
else if (value == null || value.equals(Null.NULL))
sqlValues.append("NULL");
else if (value instanceof Integer || value instanceof BigDecimal)
sqlValues.append(encrypt(i, value));
else if (c == Boolean.class) {
boolean bValue = false;
if (value instanceof Boolean)
bValue = ((Boolean) value).booleanValue();
else
bValue = "Y".equals(value);
sqlValues.append(encrypt(i, bValue ? "'Y'" : "'N'"));
} else if (value instanceof Timestamp)
sqlValues.append(DB.TO_DATE((Timestamp) encrypt(i, value), p_info.getColumnDisplayType(i) == DisplayType.Date));
else if (c == String.class)
sqlValues.append(encrypt(i, DB.TO_STRING((String) value)));
else if (DisplayType.isLOB(dt))
// no db dependent stuff here
sqlValues.append("null");
else
sqlValues.append(saveNewSpecial(value, i));
} catch (Exception e) {
String msg = "";
if (m_trxName != null)
msg = "[" + m_trxName + "] - ";
msg += p_info.toString(i) + " - Value=" + value + "(" + (value == null ? "null" : value.getClass().getName()) + ")";
log.log(Level.SEVERE, msg, e);
// fini
throw new DBException(e);
}
// Change Log - Only
String insertLog = MSysConfig.getValue("SYSTEM_INSERT_CHANGELOG", "Y", getAD_Client_ID());
if (session != null && m_IDs.length == 1 && // logging allowed
p_info.isAllowLogging(i) && // not encrypted
!p_info.isEncrypted(i) && // no virtual column
!p_info.isVirtualColumn(i) && !"Password".equals(columnName) && (insertLog.equalsIgnoreCase("Y") || (insertLog.equalsIgnoreCase("K") && p_info.getColumn(i).IsKey))) {
// change log on new
MChangeLog cLog = session.changeLog(m_trxName, AD_ChangeLog_ID, p_info.getAD_Table_ID(), p_info.getColumn(i).AD_Column_ID, get_ID(), getAD_Client_ID(), getAD_Org_ID(), null, value, MChangeLog.EVENTCHANGELOG_Insert);
if (cLog != null)
AD_ChangeLog_ID = cLog.getAD_ChangeLog_ID();
}
}
// Custom Columns
if (m_custom != null) {
Iterator<String> it = m_custom.keySet().iterator();
while (it.hasNext()) {
String column = (String) it.next();
index = p_info.getColumnIndex(column);
String value = (String) m_custom.get(column);
if (doComma) {
sqlInsert.append(",");
sqlValues.append(",");
} else
doComma = true;
sqlInsert.append(column);
//jz for ad_issue, some value may include ' in a string???
sqlValues.append(encrypt(index, value));
}
m_custom = null;
}
sqlInsert.append(sqlValues).append(")");
//
int no = DB.executeUpdate(sqlInsert.toString(), m_trxName);
boolean ok = no == 1;
if (ok) {
ok = lobSave();
if (// re-read Info
!load(m_trxName)) {
if (m_trxName == null)
log.log(Level.SEVERE, "reloading");
else
log.log(Level.SEVERE, "[" + m_trxName + "] - reloading");
ok = false;
;
}
} else {
String msg = "Not inserted - ";
if (CLogMgt.isLevelFiner())
msg += sqlInsert.toString();
else
msg += get_TableName();
if (m_trxName == null)
log.log(Level.WARNING, msg);
else
log.log(Level.WARNING, "[" + m_trxName + "]" + msg);
}
return saveFinish(true, ok);
}
use of org.adempiere.exceptions.DBException in project adempiere by adempiere.
the class PO method saveFinish.
/**
* Finish Save Process
* @param newRecord new
* @param success success
* @return true if saved
*/
private boolean saveFinish(boolean newRecord, boolean success) {
if (!isDirectLoad) {
// Translations
if (success) {
if (newRecord)
insertTranslations();
else
updateTranslations();
}
//
try {
success = afterSave(newRecord, success);
} catch (Exception e) {
log.log(Level.WARNING, "afterSave", e);
log.saveError("Error", e, false);
success = false;
// throw new DBException(e);
}
}
// Call ModelValidators TYPE_AFTER_NEW/TYPE_AFTER_CHANGE - teo_sarca [ 1675490 ]
if (success && !isDirectLoad) {
String errorMsg = ModelValidationEngine.get().fireModelChange(this, newRecord ? (isReplication() ? ModelValidator.TYPE_AFTER_NEW_REPLICATION : ModelValidator.TYPE_AFTER_NEW) : (isReplication() ? ModelValidator.TYPE_AFTER_CHANGE_REPLICATION : ModelValidator.TYPE_AFTER_CHANGE));
setReplication(false);
if (errorMsg != null) {
log.saveError("Error", errorMsg);
success = false;
}
}
// OK
if (success && !isDirectLoad) {
if (s_docWFMgr == null) {
try {
Class.forName("org.compiere.wf.DocWorkflowManager");
} catch (Exception e) {
}
}
if (s_docWFMgr != null)
s_docWFMgr.process(this, p_info.getAD_Table_ID());
}
if (success) {
// Copy to Old values
int size = p_info.getColumnCount();
for (int i = 0; i < size; i++) {
if (m_newValues[i] != null) {
if (m_newValues[i] == Null.NULL)
m_oldValues[i] = null;
else
m_oldValues[i] = m_newValues[i];
}
}
m_newValues = new Object[size];
}
m_createNew = false;
if (!newRecord)
CacheMgt.get().reset(p_info.getTableName());
return success;
}
use of org.adempiere.exceptions.DBException in project adempiere by adempiere.
the class POResultSet method next.
/**
*
* @return PO or null if reach the end of resultset
* @throws DBException
*/
public T next() throws DBException {
if (currentPO != null) {
T po = currentPO;
currentPO = null;
return po;
}
try {
if (resultSet.next()) {
return (T) table.getPO(resultSet, trxName);
} else {
// close it if there is no more data to read
this.close();
return null;
}
} catch (SQLException e) {
if (this.closeOnError) {
this.close();
}
throw new DBException(e);
}// Catching any RuntimeException, and close the resultset (if closeOnError is set)
catch (RuntimeException e) {
if (this.closeOnError) {
this.close();
}
throw e;
}
}
use of org.adempiere.exceptions.DBException in project adempiere by adempiere.
the class Query method iterate.
/**
* Return an Iterator implementation to fetch one PO at a time. The implementation first retrieve
* all IDS that match the query criteria and issue sql query to fetch the PO when caller want to
* fetch the next PO. This minimize memory usage but it is slower than the list method.
* @return Iterator
* @throws DBException
*/
public <T extends PO> Iterator<T> iterate() throws DBException {
String[] keys = table.getKeyColumns();
StringBuffer sqlBuffer = new StringBuffer(" SELECT ");
for (int i = 0; i < keys.length; i++) {
if (i > 0)
sqlBuffer.append(", ");
sqlBuffer.append(keys[i]);
}
sqlBuffer.append(" FROM ").append(table.getTableName());
String sql = buildSQL(sqlBuffer, true);
PreparedStatement pstmt = null;
ResultSet rs = null;
List<Object[]> idList = new ArrayList<Object[]>();
try {
pstmt = DB.prepareStatement(sql, trxName);
rs = createResultSet(pstmt);
while (rs.next()) {
Object[] ids = new Object[keys.length];
for (int i = 0; i < ids.length; i++) {
ids[i] = rs.getObject(i + 1);
}
idList.add(ids);
}
} catch (SQLException e) {
log.log(Level.SEVERE, sql, e);
throw new DBException(e, sql);
} finally {
DB.close(rs, pstmt);
rs = null;
pstmt = null;
}
return new POIterator<T>(table, idList, trxName);
}
Aggregations