IDEMPIERE-1132 Improve AD_Index; AD_View; foreign keys management - Enhance 2pack to make use of AD_TableIndex and AD_ViewComponent

This commit is contained in:
Elaine Tan 2013-07-16 19:11:32 +08:00
parent a8731ef4a5
commit 7de49de4ef
8 changed files with 699 additions and 14 deletions

View File

@ -20,6 +20,7 @@ import java.math.BigDecimal;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Properties;
import org.adempiere.exceptions.AdempiereException;
import org.compiere.model.MTable;
@ -47,7 +48,12 @@ public class DatabaseViewValidate extends SvrProcess {
if (!table.isView() || !table.isActive())
throw new AdempiereException(Msg.getMsg(getCtx(), "NotActiveDatabaseView"));
Trx trx = Trx.get(get_TrxName(), true);
return validateDatabaseView(getCtx(), table, get_TrxName(), getProcessInfo());
}
public static String validateDatabaseView(Properties ctx, MTable table, String trxName, ProcessInfo pi) throws Exception
{
Trx trx = Trx.get(trxName, true);
DatabaseMetaData md = trx.getConnection().getMetaData();
String tableName = table.getTableName();
if (md.storesUpperCaseIdentifiers())
@ -85,7 +91,7 @@ public class DatabaseViewValidate extends SvrProcess {
//
MViewComponent[] m_vcs = table.getViewComponent(true);
if (m_vcs == null || m_vcs.length == 0)
throw new AdempiereException(Msg.getMsg(getCtx(), "NoViewComponentsSpecified"));
throw new AdempiereException(Msg.getMsg(ctx, "NoViewComponentsSpecified"));
boolean modified = false;
MViewColumn[] vCols = null;
@ -98,7 +104,7 @@ public class DatabaseViewValidate extends SvrProcess {
{
vCols = vc.getColumns(true);
if (vCols == null || vCols.length == 0)
throw new AdempiereException(Msg.getMsg(getCtx(), "NoViewColumnsSpecified"));
throw new AdempiereException(Msg.getMsg(ctx, "NoViewColumnsSpecified"));
if (viewColumnNames.size() > vCols.length)
modified = true;
@ -138,15 +144,17 @@ public class DatabaseViewValidate extends SvrProcess {
if (found && modified)
{
String sql = "DROP VIEW " + table.getTableName();
int rvalue = DB.executeUpdate(sql, (Object[]) null, true, get_TrxName());
addLog(0, null, new BigDecimal(rvalue), sql);
int rvalue = DB.executeUpdate(sql, (Object[]) null, true, trxName);
if (pi != null)
pi.addLog(0, null, new BigDecimal(rvalue), sql);
}
String sql = sb.toString();
int rvalue = DB.executeUpdate(sql, (Object[]) null, true, get_TrxName());
addLog(0, null, new BigDecimal(rvalue), sql);
int rvalue = DB.executeUpdate(sql, (Object[]) null, true, trxName);
if (pi != null)
pi.addLog(0, null, new BigDecimal(rvalue), sql);
if(rvalue == -1)
throw new AdempiereException(Msg.getMsg(getCtx(), "FailedCreateOrReplaceView"));
throw new AdempiereException(Msg.getMsg(ctx, "FailedCreateOrReplaceView"));
else
return Msg.getMsg(getCtx(), "CreatedOrReplacedViewSuccess");
return Msg.getMsg(ctx, "CreatedOrReplacedViewSuccess");
}
}

View File

@ -97,6 +97,7 @@ public class TableIndexValidate extends SvrProcess {
{
String sql = index.getDDL();
int rvalue = DB.executeUpdate(sql, (Object[]) null, true, trxName);
if (pi != null)
pi.addLog(0, null, new BigDecimal(rvalue), sql);
if (rvalue == -1)
throw new AdempiereException(Msg.getMsg(ctx, "Failed to create index"));
@ -160,10 +161,12 @@ public class TableIndexValidate extends SvrProcess {
{
String sql = "DROP INDEX " + index.getName();
int rvalue = DB.executeUpdate(sql, (Object[]) null, true, trxName);
if (pi != null)
pi.addLog(0, null, new BigDecimal(rvalue), sql);
sql = index.getDDL();
rvalue = DB.executeUpdate(sql, (Object[]) null, true, trxName);
if (pi != null)
pi.addLog(0, null, new BigDecimal(rvalue), sql);
if(rvalue == -1)
throw new AdempiereException(Msg.getMsg(ctx, "FailedModifyIndex"));

View File

@ -188,6 +188,22 @@
class="org.adempiere.pipo2.handler.ModificationElementHandler"
id="AD_Modification">
</handler>
<handler
class="org.adempiere.pipo2.handler.TableIndexElementHandler"
id="AD_TableIndex">
</handler>
<handler
class="org.adempiere.pipo2.handler.IndexColumnElementHandler"
id="AD_IndexColumn">
</handler>
<handler
class="org.adempiere.pipo2.handler.ViewComponentElementHandler"
id="AD_ViewComponent">
</handler>
<handler
class="org.adempiere.pipo2.handler.ViewColumnElementHandler"
id="AD_ViewColumn">
</handler>
</extension>
</plugin>

View File

@ -0,0 +1,136 @@
package org.adempiere.pipo2.handler;
import java.util.List;
import javax.xml.transform.sax.TransformerHandler;
import org.adempiere.pipo2.AbstractElementHandler;
import org.adempiere.pipo2.Element;
import org.adempiere.pipo2.PIPOContext;
import org.adempiere.pipo2.PackOut;
import org.adempiere.pipo2.PoExporter;
import org.adempiere.pipo2.PoFiller;
import org.adempiere.pipo2.ReferenceUtils;
import org.adempiere.pipo2.exception.POSaveFailedException;
import org.compiere.model.MIndexColumn;
import org.compiere.model.MTableIndex;
import org.compiere.model.X_AD_Package_Imp_Detail;
import org.compiere.util.Env;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.AttributesImpl;
public class IndexColumnElementHandler extends AbstractElementHandler {
@Override
public void startElement(PIPOContext ctx, Element element) throws SAXException {
String entitytype = getStringValue(element, "EntityType");
if (isProcessElement(ctx.ctx, entitytype)) {
if (isParentDefer(element, MTableIndex.Table_Name)) {
element.defer = true;
return;
}
List<String> excludes = defaultExcludeList(MIndexColumn.Table_Name);
MIndexColumn mIndexColumn = findPO(ctx, element);
if (mIndexColumn == null) {
int parentId = 0;
if (getParentId(element, MTableIndex.Table_Name) > 0) {
parentId = getParentId(element, MTableIndex.Table_Name);
} else {
Element pfElement = element.properties.get(MIndexColumn.COLUMNNAME_AD_TableIndex_ID);
parentId = ReferenceUtils.resolveReference(ctx.ctx, pfElement, getTrxName(ctx));
}
if (parentId <= 0) {
element.defer = true;
element.unresolved = "AD_TableIndex_ID";
return;
}
mIndexColumn = new MIndexColumn(ctx.ctx, 0, getTrxName(ctx));
mIndexColumn.setAD_TableIndex_ID(parentId);
excludes.add(MIndexColumn.COLUMNNAME_AD_TableIndex_ID);
}
PoFiller filler = new PoFiller(ctx, mIndexColumn, element, this);
if (mIndexColumn.getAD_IndexColumn_ID() == 0 && isOfficialId(element, "AD_IndexColumn_ID"))
filler.setInteger("AD_IndexColumn_ID");
excludes.add("AD_Column_ID");
int columnId = 0;
Element columnElement = element.properties.get("AD_Column_ID");
if (ReferenceUtils.isIDLookup(columnElement) || ReferenceUtils.isUUIDLookup(columnElement)) {
columnId = ReferenceUtils.resolveReference(ctx.ctx, columnElement, getTrxName(ctx));
}
if (columnId > 0)
mIndexColumn.setAD_Column_ID(columnId);
List<String> notfounds = filler.autoFill(excludes);
if (notfounds.size() > 0) {
element.defer = true;
element.unresolved = notfounds.toString();
return;
}
if (mIndexColumn.is_new() || mIndexColumn.is_Changed()) {
X_AD_Package_Imp_Detail impDetail = createImportDetail(ctx, element.qName, MIndexColumn.Table_Name, MIndexColumn.Table_ID);
String action = null;
if (!mIndexColumn.is_new()) {
backupRecord(ctx, impDetail.getAD_Package_Imp_Detail_ID(), MIndexColumn.Table_Name, mIndexColumn);
action = "Update";
} else {
action = "New";
}
if (mIndexColumn.save(getTrxName(ctx)) == true) {
logImportDetail(ctx, impDetail, 1, mIndexColumn.toString(), mIndexColumn.get_ID(), action);
} else {
logImportDetail(ctx, impDetail, 0, mIndexColumn.toString(), mIndexColumn.get_ID(), action);
throw new POSaveFailedException("Failed to save AD_IndexColumn " + mIndexColumn.toString());
}
}
} else {
element.skip = true;
}
}
@Override
public void endElement(PIPOContext ctx, Element element) throws SAXException {
}
public void create(PIPOContext ctx, TransformerHandler document) throws SAXException {
int AD_IndexColumn_ID = Env.getContextAsInt(ctx.ctx, MIndexColumn.COLUMNNAME_AD_IndexColumn_ID);
MIndexColumn m_IndexColumn = new MIndexColumn(ctx.ctx, AD_IndexColumn_ID, getTrxName(ctx));
if (ctx.packOut.getFromDate() != null) {
if (m_IndexColumn.getUpdated().compareTo(ctx.packOut.getFromDate()) < 0) {
return;
}
}
AttributesImpl atts = new AttributesImpl();
addTypeName(atts, "table");
document.startElement("", "", MIndexColumn.Table_Name, atts);
createIndexColumnBinding(ctx, document, m_IndexColumn);
document.endElement("", "", MIndexColumn.Table_Name);
}
private void createIndexColumnBinding(PIPOContext ctx, TransformerHandler document, MIndexColumn m_IndexColumn) {
PoExporter filler = new PoExporter(ctx, document, m_IndexColumn);
List<String>excludes = defaultExcludeList(MIndexColumn.Table_Name);
if (m_IndexColumn.getAD_IndexColumn_ID() <= PackOut.MAX_OFFICIAL_ID)
filler.add("AD_IndexColumn_ID", new AttributesImpl());
filler.export(excludes);
}
@Override
public void packOut(PackOut packout, TransformerHandler packoutHandler, TransformerHandler docHandler, int recordId) throws Exception {
Env.setContext(packout.getCtx().ctx, MIndexColumn.COLUMNNAME_AD_IndexColumn_ID, recordId);
create(packout.getCtx(), packoutHandler);
packout.getCtx().ctx.remove(MIndexColumn.COLUMNNAME_AD_IndexColumn_ID);
}
}

View File

@ -20,32 +20,40 @@ import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import javax.xml.transform.sax.TransformerHandler;
import org.adempiere.exceptions.AdempiereException;
import org.adempiere.pipo2.AbstractElementHandler;
import org.adempiere.pipo2.Element;
import org.adempiere.pipo2.ElementHandler;
import org.adempiere.pipo2.PIPOContext;
import org.adempiere.pipo2.PoExporter;
import org.adempiere.pipo2.Element;
import org.adempiere.pipo2.PackIn;
import org.adempiere.pipo2.PackOut;
import org.adempiere.pipo2.PoExporter;
import org.adempiere.pipo2.PoFiller;
import org.adempiere.pipo2.exception.DatabaseAccessException;
import org.adempiere.pipo2.exception.POSaveFailedException;
import org.compiere.model.I_AD_Table;
import org.compiere.model.MTable;
import org.compiere.model.MTableIndex;
import org.compiere.model.MViewComponent;
import org.compiere.model.X_AD_Column;
import org.compiere.model.X_AD_Package_Exp_Detail;
import org.compiere.model.X_AD_Package_Imp_Detail;
import org.compiere.model.X_AD_Table;
import org.compiere.process.DatabaseViewValidate;
import org.compiere.util.DB;
import org.compiere.util.Env;
import org.compiere.util.Trx;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.AttributesImpl;
public class TableElementHandler extends AbstractElementHandler {
private ColumnElementHandler columnHandler = new ColumnElementHandler();
private TableIndexElementHandler tableIndexHandler = new TableIndexElementHandler();
private ViewComponentElementHandler viewComponentHandler = new ViewComponentElementHandler();
private List<Integer>tables = new ArrayList<Integer>();
@ -117,6 +125,43 @@ public class TableElementHandler extends AbstractElementHandler {
}
public void endElement(PIPOContext ctx, Element element) throws SAXException {
MTable mTable = findPO(ctx, element);
boolean isValidateView = false;
MViewComponent[] m_vcs = mTable.getViewComponent(true);
if (m_vcs != null && m_vcs.length > 0)
isValidateView = true;
if (mTable.isView() && isValidateView) {
int success = validateDatabaseView(ctx, mTable);
X_AD_Package_Imp_Detail dbDetail = createImportDetail(ctx, "dbView", MTable.Table_Name, MTable.Table_ID);
if (success == 1) {
logImportDetail(ctx, dbDetail, 1, mTable.getName(), mTable.get_ID(), "Validate");
} else {
logImportDetail(ctx, dbDetail, 0, mTable.getName(), mTable.get_ID(), "Validate");
throw new DatabaseAccessException("Failed to validate view for " + mTable.getName());
}
}
}
private int validateDatabaseView(PIPOContext ctx, MTable table)
{
Trx trx = Trx.get(getTrxName(ctx), true);
if (!trx.commit())
return 0;
try {
DatabaseViewValidate.validateDatabaseView(ctx.ctx, table, trx.getTrxName(), null);
trx.commit(true);
} catch (Exception e) {
log.log(Level.SEVERE, e.getLocalizedMessage(), e);
trx.rollback();
return 0;
} finally {
if (trx != null)
trx.close();
}
return 1;
}
public void create(PIPOContext ctx, TransformerHandler document)
@ -188,6 +233,40 @@ public class TableElementHandler extends AbstractElementHandler {
DB.close(rs, pstmt);
}
sql = "SELECT * FROM AD_TableIndex WHERE AD_Table_ID = ? ORDER BY AD_TableIndex_ID";
pstmt = null;
rs = null;
try {
pstmt = DB.prepareStatement (sql, getTrxName(ctx));
pstmt.setInt(1, AD_Table_ID);
rs = pstmt.executeQuery();
while (rs.next()) {
createTableIndex(ctx, document, rs.getInt(MTableIndex.COLUMNNAME_AD_TableIndex_ID));
}
} catch (Exception e) {
throw new AdempiereException(e);
} finally {
DB.close(rs, pstmt);
}
sql = "SELECT * FROM AD_ViewComponent WHERE AD_Table_ID = ? ORDER BY SeqNo, AD_ViewComponent_ID";
pstmt = null;
rs = null;
try {
pstmt = DB.prepareStatement (sql, getTrxName(ctx));
pstmt.setInt(1, AD_Table_ID);
rs = pstmt.executeQuery();
while (rs.next()) {
createViewComponent(ctx, document, rs.getInt(MViewComponent.COLUMNNAME_AD_ViewComponent_ID));
}
} catch (Exception e) {
throw new AdempiereException(e);
} finally {
DB.close(rs, pstmt);
}
if (createElement) {
document.endElement("","",X_AD_Table.Table_Name);
}
@ -201,6 +280,18 @@ public class TableElementHandler extends AbstractElementHandler {
ctx.ctx.remove(X_AD_Column.COLUMNNAME_AD_Column_ID);
}
private void createTableIndex(PIPOContext ctx, TransformerHandler document, int AD_TableIndex_ID) throws SAXException {
Env.setContext(ctx.ctx, MTableIndex.COLUMNNAME_AD_TableIndex_ID, AD_TableIndex_ID);
tableIndexHandler.create(ctx, document);
ctx.ctx.remove(MTableIndex.COLUMNNAME_AD_TableIndex_ID);
}
private void createViewComponent(PIPOContext ctx, TransformerHandler document, int AD_ViewComponent_ID) throws SAXException {
Env.setContext(ctx.ctx, MViewComponent.COLUMNNAME_AD_ViewComponent_ID, AD_ViewComponent_ID);
viewComponentHandler.create(ctx, document);
ctx.ctx.remove(MViewComponent.COLUMNNAME_AD_ViewComponent_ID);
}
private boolean isTableProcess(PIPOContext ctx, int AD_Table_ID) {
if (tables.contains(AD_Table_ID))
return true;

View File

@ -0,0 +1,169 @@
package org.adempiere.pipo2.handler;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import javax.xml.transform.sax.TransformerHandler;
import org.adempiere.pipo2.AbstractElementHandler;
import org.adempiere.pipo2.Element;
import org.adempiere.pipo2.PIPOContext;
import org.adempiere.pipo2.PackOut;
import org.adempiere.pipo2.PoExporter;
import org.adempiere.pipo2.PoFiller;
import org.adempiere.pipo2.exception.DatabaseAccessException;
import org.adempiere.pipo2.exception.POSaveFailedException;
import org.compiere.model.MIndexColumn;
import org.compiere.model.MTableIndex;
import org.compiere.model.X_AD_Package_Imp_Detail;
import org.compiere.process.TableIndexValidate;
import org.compiere.util.Env;
import org.compiere.util.Trx;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.AttributesImpl;
public class TableIndexElementHandler extends AbstractElementHandler {
private List<Integer> tableIndexes = new ArrayList<Integer>();
@Override
public void startElement(PIPOContext ctx, Element element) throws SAXException {
String entitytype = getStringValue(element, "EntityType");
if (isProcessElement(ctx.ctx, entitytype)) {
MTableIndex mTableIndex = findPO(ctx, element);
if (mTableIndex == null) {
int id = 0;
if (!hasUUIDKey(ctx, element)) {
String name = getStringValue(element, "Name");
id = findIdByColumn(ctx, "AD_TableIndex_ID", "Name", name, true);
}
mTableIndex = new MTableIndex(ctx.ctx, id > 0 ? id : 0, getTrxName(ctx));
}
PoFiller filler = new PoFiller(ctx, mTableIndex, element, this);
List<String> excludes = defaultExcludeList(MTableIndex.Table_Name);
if (mTableIndex.getAD_TableIndex_ID() == 0 && isOfficialId(element, "AD_TableIndex_ID"))
mTableIndex.setAD_TableIndex_ID(getIntValue(element, "AD_TableIndex_ID"));
List<String> notfounds = filler.autoFill(excludes);
if (notfounds.size() > 0) {
element.defer = true;
element.unresolved = notfounds.toString();
return;
}
if (mTableIndex.is_new() || mTableIndex.is_Changed()) {
X_AD_Package_Imp_Detail impDetail = createImportDetail(ctx, element.qName, MTableIndex.Table_Name, MTableIndex.Table_ID);
String action = null;
if (!mTableIndex.is_new()) {
backupRecord(ctx, impDetail.getAD_Package_Imp_Detail_ID(), MTableIndex.Table_Name, mTableIndex);
action = "Update";
} else {
action = "New";
}
if (mTableIndex.save(getTrxName(ctx)) == true) {
logImportDetail(ctx, impDetail, 1, mTableIndex.getName(), mTableIndex.get_ID(), action);
element.recordId = mTableIndex.getAD_TableIndex_ID();
} else {
logImportDetail(ctx, impDetail, 0, mTableIndex.getName(), mTableIndex.get_ID(), action);
throw new POSaveFailedException("Failed to save AD_TableIndex " + mTableIndex.getName());
}
}
} else {
element.skip = true;
}
}
@Override
public void endElement(PIPOContext ctx, Element element) throws SAXException {
MTableIndex mTableIndex = findPO(ctx, element);
int success = validateTableIndex(ctx, mTableIndex);
X_AD_Package_Imp_Detail dbDetail = createImportDetail(ctx, "dbIndex", MTableIndex.Table_Name, MTableIndex.Table_ID);
if (success == 1) {
logImportDetail(ctx, dbDetail, 1, mTableIndex.getName(), mTableIndex.get_ID(), "Validate");
} else {
logImportDetail(ctx, dbDetail, 0, mTableIndex.getName(), mTableIndex.get_ID(), "Validate");
throw new DatabaseAccessException("Failed to validate AD_TableIndex for " + mTableIndex.getName());
}
}
private int validateTableIndex(PIPOContext ctx, MTableIndex tableIndex)
{
Trx trx = Trx.get(getTrxName(ctx), true);
if (!trx.commit())
return 0;
try {
TableIndexValidate.validateTableIndex(ctx.ctx, tableIndex, trx.getTrxName(), null);
trx.commit(true);
} catch (Exception e) {
log.log(Level.SEVERE, e.getLocalizedMessage(), e);
trx.rollback();
return 0;
} finally {
if (trx != null)
trx.close();
}
return 1;
}
public void create(PIPOContext ctx, TransformerHandler document) throws SAXException {
int AD_TableIndex_ID = Env.getContextAsInt(ctx.ctx, MTableIndex.COLUMNNAME_AD_TableIndex_ID);
if (tableIndexes.contains(AD_TableIndex_ID))
return;
tableIndexes.add(AD_TableIndex_ID);
AttributesImpl atts = new AttributesImpl();
MTableIndex m_TableIndex = new MTableIndex(ctx.ctx, AD_TableIndex_ID, null);
boolean createElement = true;
if (ctx.packOut.getFromDate() != null) {
if (m_TableIndex.getUpdated().compareTo(ctx.packOut.getFromDate()) < 0) {
createElement = false;
}
}
if (createElement) {
addTypeName(atts, "table");
document.startElement("", "", MTableIndex.Table_Name, atts);
createTableIndexBinding(ctx, document, m_TableIndex);
}
MIndexColumn[] indexColumns = m_TableIndex.getColumns(true);
for (MIndexColumn indexColumn : indexColumns) {
createIndexColumn(ctx, document, indexColumn.getAD_IndexColumn_ID());
}
if (createElement) {
document.endElement("", "", MTableIndex.Table_Name);
}
}
private void createIndexColumn(PIPOContext ctx, TransformerHandler document, int AD_IndexColumn_ID) throws SAXException {
try {
ctx.packOut.getHandler(MIndexColumn.Table_Name).packOut(ctx.packOut, document, ctx.logDocument, AD_IndexColumn_ID);
} catch (Exception e) {
throw new SAXException(e);
}
}
private void createTableIndexBinding(PIPOContext ctx, TransformerHandler document, MTableIndex m_TableIndex) {
PoExporter filler = new PoExporter(ctx, document, m_TableIndex);
List<String>excludes = defaultExcludeList(MTableIndex.Table_Name);
if (m_TableIndex.getAD_TableIndex_ID() <= PackOut.MAX_OFFICIAL_ID)
filler.add("AD_TableIndex_ID", new AttributesImpl());
filler.export(excludes);
}
@Override
public void packOut(PackOut packout, TransformerHandler packoutHandler, TransformerHandler docHandler, int recordId) throws Exception {
Env.setContext(packout.getCtx().ctx, MTableIndex.COLUMNNAME_AD_TableIndex_ID, recordId);
this.create(packout.getCtx(), packoutHandler);
packout.getCtx().ctx.remove(MTableIndex.COLUMNNAME_AD_TableIndex_ID);
}
}

View File

@ -0,0 +1,127 @@
package org.adempiere.pipo2.handler;
import java.util.List;
import javax.xml.transform.sax.TransformerHandler;
import org.adempiere.pipo2.AbstractElementHandler;
import org.adempiere.pipo2.Element;
import org.adempiere.pipo2.PIPOContext;
import org.adempiere.pipo2.PackOut;
import org.adempiere.pipo2.PoExporter;
import org.adempiere.pipo2.PoFiller;
import org.adempiere.pipo2.ReferenceUtils;
import org.adempiere.pipo2.exception.POSaveFailedException;
import org.compiere.model.MViewColumn;
import org.compiere.model.MViewComponent;
import org.compiere.model.X_AD_Package_Imp_Detail;
import org.compiere.util.Env;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.AttributesImpl;
public class ViewColumnElementHandler extends AbstractElementHandler {
@Override
public void startElement(PIPOContext ctx, Element element) throws SAXException {
String entitytype = getStringValue(element, "EntityType");
if (isProcessElement(ctx.ctx, entitytype)) {
if (isParentDefer(element, MViewComponent.Table_Name)) {
element.defer = true;
return;
}
List<String> excludes = defaultExcludeList(MViewColumn.Table_Name);
MViewColumn mViewColumn = findPO(ctx, element);
if (mViewColumn == null) {
int parentId = 0;
if (getParentId(element, MViewComponent.Table_Name) > 0) {
parentId = getParentId(element, MViewComponent.Table_Name);
} else {
Element pfElement = element.properties.get(MViewColumn.COLUMNNAME_AD_ViewComponent_ID);
parentId = ReferenceUtils.resolveReference(ctx.ctx, pfElement, getTrxName(ctx));
}
if (parentId <= 0) {
element.defer = true;
element.unresolved = "AD_ViewComponent_ID";
return;
}
mViewColumn = new MViewColumn(ctx.ctx, 0, getTrxName(ctx));
mViewColumn.setAD_ViewComponent_ID(parentId);
excludes.add(MViewColumn.COLUMNNAME_AD_ViewComponent_ID);
}
PoFiller filler = new PoFiller(ctx, mViewColumn, element, this);
if (mViewColumn.getAD_ViewColumn_ID() == 0 && isOfficialId(element, "AD_ViewColumn_ID"))
filler.setInteger("AD_ViewColumn_ID");
List<String> notfounds = filler.autoFill(excludes);
if (notfounds.size() > 0) {
element.defer = true;
element.unresolved = notfounds.toString();
return;
}
if (mViewColumn.is_new() || mViewColumn.is_Changed()) {
X_AD_Package_Imp_Detail impDetail = createImportDetail(ctx, element.qName, MViewColumn.Table_Name, MViewColumn.Table_ID);
String action = null;
if (!mViewColumn.is_new()) {
backupRecord(ctx, impDetail.getAD_Package_Imp_Detail_ID(), MViewColumn.Table_Name, mViewColumn);
action = "Update";
} else {
action = "New";
}
if (mViewColumn.save(getTrxName(ctx)) == true) {
logImportDetail(ctx, impDetail, 1, mViewColumn.toString(), mViewColumn.get_ID(), action);
} else {
logImportDetail(ctx, impDetail, 0, mViewColumn.toString(), mViewColumn.get_ID(), action);
throw new POSaveFailedException("Failed to save AD_ViewColumn " + mViewColumn.toString());
}
}
} else {
element.skip = true;
}
}
@Override
public void endElement(PIPOContext ctx, Element element) throws SAXException {
}
public void create(PIPOContext ctx, TransformerHandler document) throws SAXException {
int AD_ViewColumn_ID = Env.getContextAsInt(ctx.ctx, MViewColumn.COLUMNNAME_AD_ViewColumn_ID);
MViewColumn m_ViewColumn = new MViewColumn(ctx.ctx, AD_ViewColumn_ID, getTrxName(ctx));
if (ctx.packOut.getFromDate() != null) {
if (m_ViewColumn.getUpdated().compareTo(ctx.packOut.getFromDate()) < 0) {
return;
}
}
AttributesImpl atts = new AttributesImpl();
addTypeName(atts, "table");
document.startElement("", "", MViewColumn.Table_Name, atts);
createViewColumnBinding(ctx, document, m_ViewColumn);
document.endElement("", "", MViewColumn.Table_Name);
}
private void createViewColumnBinding(PIPOContext ctx, TransformerHandler document, MViewColumn m_ViewColumn) {
PoExporter filler = new PoExporter(ctx, document, m_ViewColumn);
List<String>excludes = defaultExcludeList(MViewColumn.Table_Name);
if (m_ViewColumn.getAD_ViewColumn_ID() <= PackOut.MAX_OFFICIAL_ID)
filler.add("AD_ViewColumn_ID", new AttributesImpl());
filler.export(excludes);
}
@Override
public void packOut(PackOut packout, TransformerHandler packoutHandler, TransformerHandler docHandler, int recordId) throws Exception {
Env.setContext(packout.getCtx().ctx, MViewColumn.COLUMNNAME_AD_ViewColumn_ID, recordId);
create(packout.getCtx(), packoutHandler);
packout.getCtx().ctx.remove(MViewColumn.COLUMNNAME_AD_ViewColumn_ID);
}
}

View File

@ -0,0 +1,135 @@
package org.adempiere.pipo2.handler;
import java.util.ArrayList;
import java.util.List;
import javax.xml.transform.sax.TransformerHandler;
import org.adempiere.pipo2.AbstractElementHandler;
import org.adempiere.pipo2.Element;
import org.adempiere.pipo2.PIPOContext;
import org.adempiere.pipo2.PackOut;
import org.adempiere.pipo2.PoExporter;
import org.adempiere.pipo2.PoFiller;
import org.adempiere.pipo2.exception.POSaveFailedException;
import org.compiere.model.MViewColumn;
import org.compiere.model.MViewComponent;
import org.compiere.model.X_AD_Package_Imp_Detail;
import org.compiere.util.Env;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.AttributesImpl;
public class ViewComponentElementHandler extends AbstractElementHandler {
private List<Integer> viewComponents = new ArrayList<Integer>();
@Override
public void startElement(PIPOContext ctx, Element element) throws SAXException {
String entitytype = getStringValue(element, "EntityType");
if (isProcessElement(ctx.ctx, entitytype)) {
MViewComponent mViewComponent = findPO(ctx, element);
if (mViewComponent == null) {
int id = 0;
if (!hasUUIDKey(ctx, element)) {
String name = getStringValue(element, "Name");
id = findIdByColumn(ctx, "AD_ViewComponent_ID", "Name", name, true);
}
mViewComponent = new MViewComponent(ctx.ctx, id > 0 ? id : 0, getTrxName(ctx));
}
PoFiller filler = new PoFiller(ctx, mViewComponent, element, this);
List<String> excludes = defaultExcludeList(MViewComponent.Table_Name);
if (mViewComponent.getAD_ViewComponent_ID() == 0 && isOfficialId(element, "AD_ViewComponent_ID"))
mViewComponent.setAD_ViewComponent_ID(getIntValue(element, "AD_ViewComponent_ID"));
List<String> notfounds = filler.autoFill(excludes);
if (notfounds.size() > 0) {
element.defer = true;
element.unresolved = notfounds.toString();
return;
}
if (mViewComponent.is_new() || mViewComponent.is_Changed()) {
X_AD_Package_Imp_Detail impDetail = createImportDetail(ctx, element.qName, MViewComponent.Table_Name, MViewComponent.Table_ID);
String action = null;
if (!mViewComponent.is_new()) {
backupRecord(ctx, impDetail.getAD_Package_Imp_Detail_ID(), MViewComponent.Table_Name, mViewComponent);
action = "Update";
} else {
action = "New";
}
if (mViewComponent.save(getTrxName(ctx)) == true) {
logImportDetail(ctx, impDetail, 1, mViewComponent.getName(), mViewComponent.get_ID(), action);
element.recordId = mViewComponent.getAD_ViewComponent_ID();
} else {
logImportDetail(ctx, impDetail, 0, mViewComponent.getName(), mViewComponent.get_ID(), action);
throw new POSaveFailedException("Failed to save AD_ViewComponent " + mViewComponent.getName());
}
}
} else {
element.skip = true;
}
}
@Override
public void endElement(PIPOContext ctx, Element element) throws SAXException {
}
public void create(PIPOContext ctx, TransformerHandler document) throws SAXException {
int AD_ViewComponent_ID = Env.getContextAsInt(ctx.ctx, MViewComponent.COLUMNNAME_AD_ViewComponent_ID);
if (viewComponents.contains(AD_ViewComponent_ID))
return;
viewComponents.add(AD_ViewComponent_ID);
AttributesImpl atts = new AttributesImpl();
MViewComponent m_ViewComponent = new MViewComponent(ctx.ctx, AD_ViewComponent_ID, null);
boolean createElement = true;
if (ctx.packOut.getFromDate() != null) {
if (m_ViewComponent.getUpdated().compareTo(ctx.packOut.getFromDate()) < 0) {
createElement = false;
}
}
if (createElement) {
addTypeName(atts, "table");
document.startElement("", "", MViewComponent.Table_Name, atts);
createViewComponentBinding(ctx, document, m_ViewComponent);
}
MViewColumn[] viewColumns = m_ViewComponent.getColumns(true);
for (MViewColumn viewColumn : viewColumns) {
createViewColumn(ctx, document, viewColumn.getAD_ViewColumn_ID());
}
if (createElement) {
document.endElement("", "", MViewComponent.Table_Name);
}
}
private void createViewColumn(PIPOContext ctx, TransformerHandler document, int AD_ViewColumn_ID) throws SAXException {
try {
ctx.packOut.getHandler(MViewColumn.Table_Name).packOut(ctx.packOut, document, ctx.logDocument, AD_ViewColumn_ID);
} catch (Exception e) {
throw new SAXException(e);
}
}
private void createViewComponentBinding(PIPOContext ctx, TransformerHandler document, MViewComponent m_ViewComponent) {
PoExporter filler = new PoExporter(ctx, document, m_ViewComponent);
List<String>excludes = defaultExcludeList(MViewComponent.Table_Name);
if (m_ViewComponent.getAD_ViewComponent_ID() <= PackOut.MAX_OFFICIAL_ID)
filler.add("AD_ViewComponent_ID", new AttributesImpl());
filler.export(excludes);
}
@Override
public void packOut(PackOut packout, TransformerHandler packoutHandler, TransformerHandler docHandler, int recordId) throws Exception {
Env.setContext(packout.getCtx().ctx, MViewComponent.COLUMNNAME_AD_ViewComponent_ID, recordId);
this.create(packout.getCtx(), packoutHandler);
packout.getCtx().ctx.remove(MViewComponent.COLUMNNAME_AD_ViewComponent_ID);
}
}