details: https://code.openbravo.com/erp/devel/pi/rev/ec509bcc31cc changeset: 35805:ec509bcc31cc user: Carlos Aristu <carlos.aristu <at> openbravo.com> date: Mon May 20 09:20:09 2019 +0200 summary: fixes bug 40758: Skip cross org check for image properties
The OBInterceptor has a mechanism (cross org check) that verifies whether the organization of the main entity is in the same natural tree that the organization of the entities referenced by the properties of the main entity. This check is performed every time an entity is update/saved through DAL and it is done before the execution of any event observer. In the case of this issue, this mechanism was not allowing to save the record because when changing the organization of an entity, if this entity had a property referencing to an AD_Image, the organization of the AD_Image was not being updated accordingly, which could cause the cross org check fail. To fix this, we are now skipping the cross org check in the OBInterceptor for those properties referencing to an AD_Image record and with the RemoveImagesEventHandler observer, if the org in the main entity is changed, we put exactly the same org in every AD_Image referenced by that entity. diffstat: modules/org.openbravo.client.application/src/org/openbravo/client/application/event/RemoveImagesEventHandler.java | 36 ++++++--- src/org/openbravo/base/model/Property.java | 15 +++- src/org/openbravo/dal/core/OBInterceptor.java | 4 +- 3 files changed, 42 insertions(+), 13 deletions(-) diffs (110 lines): diff -r 1596542c21af -r ec509bcc31cc modules/org.openbravo.client.application/src/org/openbravo/client/application/event/RemoveImagesEventHandler.java --- a/modules/org.openbravo.client.application/src/org/openbravo/client/application/event/RemoveImagesEventHandler.java Fri May 17 11:14:19 2019 +0200 +++ b/modules/org.openbravo.client.application/src/org/openbravo/client/application/event/RemoveImagesEventHandler.java Mon May 20 09:20:09 2019 +0200 @@ -36,8 +36,15 @@ import org.openbravo.dal.service.OBCriteria; import org.openbravo.dal.service.OBDal; import org.openbravo.model.ad.utility.Image; +import org.openbravo.model.common.enterprise.Organization; import org.openbravo.model.common.plm.Product; +/** + * This observer takes care of deleting an image detected as no longer needed after removing the + * last record referencing it. Besides, in case the organization of an entity having image + * properties is updated, this observer will update the referenced images by setting the same + * organization. + */ class RemoveImagesEventHandler extends EntityPersistenceEventObserver { private static Entity[] entities = getImageEntities(); @@ -125,27 +132,34 @@ return; } + Property orgProperty = event.getTargetInstance().getEntity().getProperty("organization"); + // Iterate image properties of the entity for (String property : getImageProperties(event.getTargetInstance().getEntity())) { Property imageProperty = event.getTargetInstance().getEntity().getProperty(property); + Image bob = (Image) event.getPreviousState(imageProperty); + if (bob == null) { + continue; + } // If the old image is different than the new one remove the old image if exists if (event.getPreviousState(imageProperty) != null && event.getCurrentState(imageProperty) != event.getPreviousState(imageProperty)) { - - Image bob = (Image) event.getPreviousState(imageProperty); - if (bob != null) { - String selectedProduct = event.getId(); - if (!checkImageUtilization(selectedProduct, bob)) { - OBContext.setAdminMode(true); - try { - OBDal.getInstance().remove(bob); - } finally { - OBContext.restorePreviousMode(); - } + String selectedProduct = event.getId(); + if (!checkImageUtilization(selectedProduct, bob)) { + OBContext.setAdminMode(true); + try { + OBDal.getInstance().remove(bob); + } finally { + OBContext.restorePreviousMode(); } } + } else if (event.getPreviousState(orgProperty) != null + && event.getCurrentState(orgProperty) != event.getPreviousState(orgProperty)) { + // The ad_org_id of the parent entity has changed, update the ad_org_id of the referenced + // image to be the same + bob.setOrganization((Organization) event.getCurrentState(orgProperty)); } } } diff -r 1596542c21af -r ec509bcc31cc src/org/openbravo/base/model/Property.java --- a/src/org/openbravo/base/model/Property.java Fri May 17 11:14:19 2019 +0200 +++ b/src/org/openbravo/base/model/Property.java Mon May 20 09:20:09 2019 +0200 @@ -11,7 +11,7 @@ * under the License. * The Original Code is Openbravo ERP. * The Initial Developer of the Original Code is Openbravo SLU - * All portions are Copyright (C) 2008-2018 Openbravo SLU + * All portions are Copyright (C) 2008-2019 Openbravo SLU * All Rights Reserved. * Contributor(s): ______________________________________. ************************************************************************ @@ -1349,4 +1349,17 @@ public void setAllowedCrossOrgReference(boolean allowedCrossOrgReference) { this.allowedCrossOrgReference = allowedCrossOrgReference; } + + /** + * This method is used to determine if this is an image property. + * + * @return {@code true} if this property references an AD_Image_ID record. Otherwise, this method + * returns {@code false}. + */ + public boolean isImage() { + if (referencedProperty == null) { + return false; + } + return "ADImage".equals(referencedProperty.getEntity().getName()); + } } diff -r 1596542c21af -r ec509bcc31cc src/org/openbravo/dal/core/OBInterceptor.java --- a/src/org/openbravo/dal/core/OBInterceptor.java Fri May 17 11:14:19 2019 +0200 +++ b/src/org/openbravo/dal/core/OBInterceptor.java Mon May 20 09:20:09 2019 +0200 @@ -301,8 +301,10 @@ } Property property = bob.getEntity().getProperty(propertyNames[i]); + boolean skipCrossOrgCheck = (obContext.isInCrossOrgAdministratorMode() - && property.isAllowedCrossOrgReference()) || property.isAuditInfo(); + && property.isAllowedCrossOrgReference()) || property.isAuditInfo() + || property.isImage(); if (!skipCrossOrgCheck && !obObject.getEntity().isVirtualEntity() && !obContext.getOrganizationStructureProvider(o1.getClient().getId()) _______________________________________________ Openbravo-commits mailing list Openbravo-commits@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/openbravo-commits