From 2694d54b7180c8f04af090c8f13c0124b6b0375d Mon Sep 17 00:00:00 2001 From: Carlos Ruiz Date: Wed, 5 Aug 2015 07:11:13 -0500 Subject: [PATCH] IDEMPIERE-2752 Same Attribute Instance on many Products --- .../src/org/compiere/model/MProduct.java | 30 +++++++++++++------ .../webui/window/WPAttributeDialog.java | 4 +-- 2 files changed, 23 insertions(+), 11 deletions(-) diff --git a/org.adempiere.base/src/org/compiere/model/MProduct.java b/org.adempiere.base/src/org/compiere/model/MProduct.java index d6c9ef5fb9..fcd11091af 100644 --- a/org.adempiere.base/src/org/compiere/model/MProduct.java +++ b/org.adempiere.base/src/org/compiere/model/MProduct.java @@ -598,17 +598,29 @@ public class MProduct extends X_M_Product m_precision = null; // AttributeSetInstance reset - if (is_ValueChanged(COLUMNNAME_M_AttributeSet_ID)) + if (getM_AttributeSetInstance_ID() > 0 && is_ValueChanged(COLUMNNAME_M_AttributeSet_ID)) { MAttributeSetInstance asi = new MAttributeSetInstance(getCtx(), getM_AttributeSetInstance_ID(), get_TrxName()); - setM_AttributeSetInstance_ID(0); - // Delete the old m_attributesetinstance - try { - asi.deleteEx(true, get_TrxName()); - } catch (AdempiereException ex) - { - log.saveError("Error", "Error deleting the AttributeSetInstance"); - return false; + if (asi.getM_AttributeSet_ID() != getM_AttributeSet_ID()) + setM_AttributeSetInstance_ID(0); + } + if (!newRecord && is_ValueChanged(COLUMNNAME_M_AttributeSetInstance_ID)) + { + // IDEMPIERE-2752 check if the ASI is referenced in other products before trying to delete it + int oldasiid = get_ValueOldAsInt(COLUMNNAME_M_AttributeSetInstance_ID); + if (oldasiid > 0) { + MAttributeSetInstance oldasi = new MAttributeSetInstance(getCtx(), get_ValueOldAsInt(COLUMNNAME_M_AttributeSetInstance_ID), get_TrxName()); + int cnt = DB.getSQLValueEx(get_TrxName(), "SELECT COUNT(*) FROM M_Product WHERE M_AttributeSetInstance_ID=?", oldasi.getM_AttributeSetInstance_ID()); + if (cnt == 1) { + // Delete the old m_attributesetinstance + try { + oldasi.deleteEx(true, get_TrxName()); + } catch (AdempiereException ex) + { + log.saveError("Error", "Error deleting the AttributeSetInstance"); + return false; + } + } } } diff --git a/org.adempiere.ui.zk/WEB-INF/src/org/adempiere/webui/window/WPAttributeDialog.java b/org.adempiere.ui.zk/WEB-INF/src/org/adempiere/webui/window/WPAttributeDialog.java index 0527c39010..ef021501ae 100644 --- a/org.adempiere.ui.zk/WEB-INF/src/org/adempiere/webui/window/WPAttributeDialog.java +++ b/org.adempiere.ui.zk/WEB-INF/src/org/adempiere/webui/window/WPAttributeDialog.java @@ -253,7 +253,8 @@ public class WPAttributeDialog extends Window implements EventListener MAttributeSet as = null; - if (m_M_Product_ID != 0) + int M_AttributeSet_ID = Env.getContextAsInt(Env.getCtx(), m_WindowNoParent, "M_AttributeSet_ID"); + if (m_M_Product_ID != 0 && M_AttributeSet_ID == 0) { // Get Model m_masi = MAttributeSetInstance.get(Env.getCtx(), m_M_AttributeSetInstance_ID, m_M_Product_ID); @@ -269,7 +270,6 @@ public class WPAttributeDialog extends Window implements EventListener } else { - int M_AttributeSet_ID = Env.getContextAsInt(Env.getCtx(), m_WindowNoParent, "M_AttributeSet_ID"); m_masi = new MAttributeSetInstance (Env.getCtx(), m_M_AttributeSetInstance_ID, M_AttributeSet_ID, null); as = m_masi.getMAttributeSet(); }