use of org.compiere.model.MTable in project adempiere by adempiere.
the class Export method doIt.
/**
* Process - Generate Export Format
* @return info
*/
protected String doIt() throws Exception {
outDocument = createNewDocument();
MClient client = MClient.get(getCtx(), p_AD_Client_ID);
log.info(client.toString());
// TODO - get proper Export Format!
int EXP_Format_ID = 1000000;
MTable table = MTable.get(getCtx(), AD_Table_ID);
log.info("Table = " + table);
PO po = table.getPO(p_Record_ID, get_TrxName());
if (po.get_KeyColumns().length > 1 || po.get_KeyColumns().length < 1) {
throw new Exception(Msg.getMsg(getCtx(), "ExportMultiColumnNotSupported"));
}
MEXPFormat exportFormat = new MEXPFormat(getCtx(), EXP_Format_ID, get_TrxName());
StringBuffer sql = new StringBuffer("SELECT * ").append("FROM ").append(table.getTableName()).append(" ").append("WHERE ").append(po.get_KeyColumns()[0]).append("=?");
if (exportFormat.getWhereClause() != null & !"".equals(exportFormat.getWhereClause())) {
sql.append(" AND ").append(exportFormat.getWhereClause());
}
ResultSet rs = null;
PreparedStatement pstmt = null;
try {
pstmt = DB.prepareStatement(sql.toString(), get_TrxName());
pstmt.setInt(1, p_Record_ID);
rs = pstmt.executeQuery();
if (rs.next()) {
HashMap<String, Integer> variableMap = new HashMap<String, Integer>();
variableMap.put(TOTAL_SEGMENTS, new Integer(1));
Element rootElement = outDocument.createElement(exportFormat.getValue());
rootElement.appendChild(outDocument.createComment(exportFormat.getDescription()));
outDocument.appendChild(rootElement);
generateExportFormat(rootElement, exportFormat, rs, po, p_Record_ID, variableMap);
}
} finally {
try {
if (rs != null)
rs.close();
if (pstmt != null)
pstmt.close();
} catch (SQLException ex) {
/*ignored*/
}
rs = null;
pstmt = null;
}
/* int C_EDIProcessorType_ID = ediProcessor.getC_EDIProcessorType_ID();
X_C_EDIProcessorType ediProcessType = new X_C_EDIProcessorType(getCtx(), C_EDIProcessorType_ID, get_TrxName() );
String javaClass = ediProcessType.getJavaClass();
try {
Class clazz = Class.forName(javaClass);
IOutbandEdiProcessor outbandProcessor = (IOutbandEdiProcessor)clazz.newInstance();
outbandProcessor.process(getCtx(), ediProcessor, result.toString(), "C_Invoice-"+p_Record_ID+".txt", Trx.get( get_TrxName(), false ));
} catch (Exception e) {
result = new StringBuffer( e.toString() );
}
*/
addLog(0, null, null, Msg.getMsg(getCtx(), "ExportProcessResult") + "\n" + outDocument.toString());
return outDocument.toString();
}
use of org.compiere.model.MTable in project adempiere by adempiere.
the class ChangeLogProcess method setCustomization.
// executeStatement
/**
* Set Customization Flag
* @return summary
*/
private String setCustomization() {
log.info("");
String sql = "UPDATE AD_ChangeLog SET IsCustomization='N' WHERE IsCustomization='Y'";
int resetNo = DB.executeUpdate(sql, get_TrxName());
int updateNo = 0;
// Get Tables
sql = "SELECT * FROM AD_Table t " + // Table with EntityType
"WHERE EXISTS (SELECT * FROM AD_Column c " + "WHERE t.AD_Table_ID=c.AD_Table_ID AND c.ColumnName='EntityType')" + // Changed Tables
" AND EXISTS (SELECT * FROM AD_ChangeLog l " + "WHERE t.AD_Table_ID=l.AD_Table_ID)";
StringBuffer update = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
pstmt = DB.prepareStatement(sql, get_TrxName());
rs = pstmt.executeQuery();
while (rs.next()) {
MTable table = new MTable(getCtx(), rs, get_TrxName());
String tableName = table.getTableName();
String columnName = tableName + "_ID";
if (tableName.equals("AD_Ref_Table"))
columnName = "AD_Reference_ID";
update = new StringBuffer("UPDATE AD_ChangeLog SET IsCustomization='Y' " + "WHERE AD_Table_ID=").append(table.getAD_Table_ID());
update.append(" AND Record_ID IN (SELECT ").append(columnName).append(" FROM ").append(tableName).append(" WHERE EntityType IN ('D','C'))");
int no = DB.executeUpdate(update.toString(), get_TrxName());
log.config(table.getTableName() + " = " + no);
updateNo += no;
}
} catch (Exception e) {
log.log(Level.SEVERE, sql + " --- " + update, e);
} finally {
DB.close(rs, pstmt);
rs = null;
pstmt = null;
}
return "@Reset@: " + resetNo + " - @Updated@: " + updateNo;
}
use of org.compiere.model.MTable in project adempiere by adempiere.
the class ChangeLogProcess method createStatement.
// doIt
/**
* Create Statement
* @param cLog log
* @param trxName trx
*/
private void createStatement(MChangeLog cLog, String trxName) {
// New Table
if (m_table != null) {
if (cLog.getAD_Table_ID() != m_table.getAD_Table_ID()) {
executeStatement();
m_table = null;
}
}
if (m_table == null)
m_table = new MTable(getCtx(), cLog.getAD_Table_ID(), trxName);
// New Record
if (m_sqlUpdate != null && cLog.getRecord_ID() != m_oldRecord_ID)
executeStatement();
// Column Info
m_column = new MColumn(getCtx(), cLog.getAD_Column_ID(), get_TrxName());
// Same Column twice
if (m_columns.contains(m_column.getColumnName()))
executeStatement();
m_columns.add(m_column.getColumnName());
// Create new Statement
if (m_sqlUpdate == null) {
String tableName = m_table.getTableName();
m_keyColumn = m_table.getTableName() + "_ID";
if (tableName.equals("AD_Ref_Table"))
m_keyColumn = "AD_Reference_ID";
//
m_sqlUpdate = new StringBuffer("UPDATE ").append(tableName).append(" SET ");
// Single Key Only
m_sqlUpdateWhere = new StringBuffer(" WHERE ").append(m_keyColumn).append("=").append(cLog.getRecord_ID());
m_oldRecord_ID = cLog.getRecord_ID();
// Insert - new value is null and UnDo only
// m_isInsert = cLog.isNewNull() && p_CheckNewValue != null;
m_isInsert = MChangeLog.EVENTCHANGELOG_Insert.equals(cLog.getEventChangeLog());
if (m_isInsert) {
m_sqlInsert = new StringBuffer("INSERT INTO ").append(tableName).append("(").append(m_keyColumn);
m_sqlInsertValue = new StringBuffer(") VALUES (").append(cLog.getRecord_ID());
if (!m_keyColumn.equals(m_column.getColumnName())) {
m_sqlInsert.append(",").append(m_column.getColumnName());
m_sqlInsertValue.append(",").append(getSQLValue(cLog.getOldValue()));
}
}
m_numberColumns = 1;
} else // Just new Column
{
m_sqlUpdate.append(", ");
// Insert
if (m_isInsert)
m_isInsert = cLog.isNewNull();
if (m_isInsert && !m_keyColumn.equals(m_column.getColumnName())) {
m_sqlInsert.append(",").append(m_column.getColumnName());
m_sqlInsertValue.append(",").append(getSQLValue(cLog.getOldValue()));
}
m_numberColumns++;
}
// Update Set clause -- columnName=value
m_sqlUpdate.append(m_column.getColumnName()).append("=");
// UnDo a <- (b)
if (p_CheckNewValue != null) {
m_sqlUpdate.append(getSQLValue(cLog.getOldValue()));
if (p_CheckNewValue.booleanValue()) {
m_sqlUpdateWhere.append(" AND ").append(m_column.getColumnName());
String newValue = getSQLValue(cLog.getNewValue());
if (newValue == null || "NULL".equals(newValue))
m_sqlUpdateWhere.append(" IS NULL");
else
m_sqlUpdateWhere.append("=").append(newValue);
}
} else // ReDo (a) -> b
if (p_CheckOldValue != null) {
m_sqlUpdate.append(getSQLValue(cLog.getNewValue()));
if (p_CheckOldValue.booleanValue()) {
String newValue = getSQLValue(cLog.getOldValue());
m_sqlUpdateWhere.append(" AND ").append(m_column.getColumnName());
if (newValue == null || "NULL".equals(newValue))
m_sqlUpdateWhere.append(" IS NULL");
else
m_sqlUpdateWhere.append("=").append(newValue);
}
}
}
use of org.compiere.model.MTable in project adempiere by adempiere.
the class CleanUpGW method setPeriods.
private void setPeriods(String tableName, String columnName) {
//update periods according to the new dates
String dateAcctColumn = null;
MTable table = MTable.get(getCtx(), tableName);
MColumn periodColumn = table.getColumn("C_Period_ID");
if (periodColumn != null) {
MColumn dateaAcctColumn = table.getColumn("DateAcct");
MColumn assetDepDateColumn = table.getColumn("AssetDepreciationDate");
if (dateaAcctColumn != null)
dateAcctColumn = dateaAcctColumn.getColumnName();
else if (assetDepDateColumn != null)
dateAcctColumn = assetDepDateColumn.getColumnName();
if (dateAcctColumn != null) {
log.fine("Table: " + tableName + " dateAcctColumn: " + dateAcctColumn);
String updatePeriodSql = "update " + tableName + " set " + " C_Period_ID=(SELECT C_Period_ID from C_Period WHERE " + dateAcctColumn + " BETWEEN StartDate and EndDate and AD_Client_ID=" + gw_client_id + ") WHERE AD_Client_ID=" + gw_client_id;
PreparedStatement pstm = null;
try {
pstm = DB.prepareStatement(updatePeriodSql, get_TrxName());
pstm.executeUpdate();
} catch (SQLException e) {
log.log(Level.SEVERE, e.getLocalizedMessage());
throw new AdempiereException("Problem in updating periods according to new accounting values.", e);
} finally {
DB.close(pstm);
}
}
}
}
use of org.compiere.model.MTable in project adempiere by adempiere.
the class CopyColumnsFromTable method doIt.
// prepare
/**
* Process
* @return info
* @throws Exception
*/
protected String doIt() throws Exception {
if (p_target_AD_Table_ID == 0)
throw new AdempiereSystemError("@NotFound@ @AD_Table_ID@ " + p_target_AD_Table_ID);
if (p_source_AD_Table_ID == 0)
throw new AdempiereSystemError("@NotFound@ @AD_Table_ID@ " + p_source_AD_Table_ID);
log.info("Source AD_Table_ID=" + p_source_AD_Table_ID + ", Target AD_Table_ID=" + p_target_AD_Table_ID);
MTable targetTable = new MTable(getCtx(), p_target_AD_Table_ID, get_TrxName());
MColumn[] targetColumns = targetTable.getColumns(true);
//if (targetColumns.length > 0)
// // TODO: dictionary message
// throw new AdempiereSystemError("Target table must not have columns");
MTable sourceTable = new MTable(getCtx(), p_source_AD_Table_ID, get_TrxName());
MColumn[] sourceColumns = sourceTable.getColumns(true);
for (int i = 0; i < sourceColumns.length; i++) {
//[FR1784588] logic to validate exist columns
boolean foundColumn = false;
MColumn colTarget = new MColumn(targetTable);
for (MColumn col : targetColumns) {
String columnName = null;
if (sourceColumns[i].getColumnName().equals(sourceTable.getTableName() + "_ID")) {
columnName = new String(targetTable.getTableName() + "_ID");
} else {
columnName = sourceColumns[i].getColumnName();
}
if (col.getColumnName().equals(columnName)) {
foundColumn = true;
break;
}
}
if (foundColumn)
continue;
// special case the key -> sourceTable_ID
if (sourceColumns[i].getColumnName().equals(sourceTable.getTableName() + "_ID")) {
String targetColumnName = new String(targetTable.getTableName() + "_ID");
colTarget.setColumnName(targetColumnName);
// if the element don't exist, create it
M_Element element = M_Element.get(getCtx(), targetColumnName);
if (element == null) {
element = new M_Element(getCtx(), targetColumnName, targetTable.getEntityType(), get_TrxName());
if (targetColumnName.equalsIgnoreCase(targetTable.getTableName() + "_ID")) {
element.setColumnName(targetTable.getTableName() + "_ID");
element.setName(targetTable.getName());
element.setPrintName(targetTable.getName());
}
element.save(get_TrxName());
}
colTarget.setAD_Element_ID(element.getAD_Element_ID());
colTarget.setName(targetTable.getName());
colTarget.setDescription(targetTable.getDescription());
colTarget.setHelp(targetTable.getHelp());
} else {
colTarget.setColumnName(sourceColumns[i].getColumnName());
colTarget.setAD_Element_ID(sourceColumns[i].getAD_Element_ID());
colTarget.setName(sourceColumns[i].getName());
colTarget.setDescription(sourceColumns[i].getDescription());
colTarget.setHelp(sourceColumns[i].getHelp());
}
colTarget.setVersion(sourceColumns[i].getVersion());
colTarget.setAD_Val_Rule_ID(sourceColumns[i].getAD_Val_Rule_ID());
colTarget.setDefaultValue(sourceColumns[i].getDefaultValue());
colTarget.setFieldLength(sourceColumns[i].getFieldLength());
colTarget.setIsKey(sourceColumns[i].isKey());
colTarget.setIsParent(sourceColumns[i].isParent());
colTarget.setIsMandatory(sourceColumns[i].isMandatory());
colTarget.setIsTranslated(sourceColumns[i].isTranslated());
colTarget.setIsIdentifier(sourceColumns[i].isIdentifier());
colTarget.setSeqNo(sourceColumns[i].getSeqNo());
colTarget.setIsEncrypted(sourceColumns[i].getIsEncrypted());
colTarget.setAD_Reference_ID(sourceColumns[i].getAD_Reference_ID());
colTarget.setAD_Reference_Value_ID(sourceColumns[i].getAD_Reference_Value_ID());
colTarget.setIsActive(sourceColumns[i].isActive());
colTarget.setVFormat(sourceColumns[i].getVFormat());
colTarget.setCallout(sourceColumns[i].getCallout());
colTarget.setIsUpdateable(sourceColumns[i].isUpdateable());
colTarget.setAD_Process_ID(sourceColumns[i].getAD_Process_ID());
colTarget.setValueMin(sourceColumns[i].getValueMin());
colTarget.setValueMax(sourceColumns[i].getValueMax());
colTarget.setIsSelectionColumn(sourceColumns[i].isSelectionColumn());
colTarget.setReadOnlyLogic(sourceColumns[i].getReadOnlyLogic());
colTarget.setIsSyncDatabase(sourceColumns[i].getIsSyncDatabase());
colTarget.setIsAlwaysUpdateable(sourceColumns[i].isAlwaysUpdateable());
colTarget.setColumnSQL(sourceColumns[i].getColumnSQL());
colTarget.save(get_TrxName());
// TODO: Copy translations
m_count++;
}
//
return "#" + m_count;
}
Aggregations