details:   https://code.openbravo.com/erp/devel/pi/rev/e75e4dc96dca
changeset: 27322:e75e4dc96dca
user:      Augusto Mauch <augusto.mauch <at> openbravo.com>
date:      Thu Aug 13 13:31:57 2015 +0200
summary:   Fixes issue 30571: Rows with mandatory image columns can be removed

When a row contains columns that use the ad_image reference, those images are 
removed when the row is removed in the RemoveImagesEventHandler. This is an 
event handler that is executed before the row is removed, so removing the image 
results in a query like this:

update my_table set my_image_field = null

This is a problem is the image column is mandatory.

There were two ways to fix this:
- Give support to event handlers that are executed after the row is removed.
- Set a dummy image to the deleted row, to avoid breaking the not null 
constraint.

The first option is very complex, so the first one has been chosen.

diffstat:

 
modules/org.openbravo.client.application/src/org/openbravo/client/application/event/RemoveImagesEventHandler.java
 |  37 +++++++++-
 1 files changed, 36 insertions(+), 1 deletions(-)

diffs (75 lines):

diff -r 86f2c21db489 -r e75e4dc96dca 
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
 Thu Aug 13 13:07:02 2015 +0200
+++ 
b/modules/org.openbravo.client.application/src/org/openbravo/client/application/event/RemoveImagesEventHandler.java
 Thu Aug 13 13:31:57 2015 +0200
@@ -28,6 +28,7 @@
 import org.openbravo.base.model.Entity;
 import org.openbravo.base.model.ModelProvider;
 import org.openbravo.base.model.Property;
+import org.openbravo.base.provider.OBProvider;
 import org.openbravo.client.kernel.event.EntityDeleteEvent;
 import org.openbravo.client.kernel.event.EntityPersistenceEventObserver;
 import org.openbravo.client.kernel.event.EntityUpdateEvent;
@@ -40,6 +41,7 @@
 public class RemoveImagesEventHandler extends EntityPersistenceEventObserver {
 
   private static Entity[] entities = getImageEntities();
+  private static final String DUMMY_IMAGE_NAME = "DummyImageForDeletedRows";
 
   @Override
   protected Entity[] getObservedEntities() {
@@ -50,7 +52,7 @@
     if (!isValidEvent(event)) {
       return;
     }
-
+    Image dummyImage = getDummyImage();
     // Iterate image properties of the entity
     for (String property : 
getImageProperties(event.getTargetInstance().getEntity())) {
 
@@ -60,6 +62,9 @@
       if (event.getCurrentState(imageProperty) != null) {
 
         Image bob = (Image) event.getCurrentState(imageProperty);
+        // Replace the current image with a dummy one, just in case the image 
column is mandatory
+        // See issue https://issues.openbravo.com/view.php?id=30571
+        event.setCurrentState(imageProperty, dummyImage);
         if (bob != null) {
           String selectedProduct = event.getId();
           if (!checkImageUtilization(selectedProduct, bob)) {
@@ -75,6 +80,36 @@
     }
   }
 
+  /**
+   * Returns a dummy image (AD_Image instance) that will be named 
DUMMY_IMAGE_NAME and will not have
+   * binary data
+   * 
+   * @return a dummy image
+   */
+  private Image getDummyImage() {
+    OBCriteria<Image> dummyImageCriteria = 
OBDal.getInstance().createCriteria(Image.class);
+    dummyImageCriteria.add(Restrictions.eq(Image.PROPERTY_NAME, 
DUMMY_IMAGE_NAME));
+    dummyImageCriteria.add(Restrictions.isNull(Image.PROPERTY_BINDARYDATA));
+    Image dummyImage = (Image) dummyImageCriteria.uniqueResult();
+    // If it is not already created, do it
+    if (dummyImage == null) {
+      dummyImage = createDummyImage();
+    }
+    return dummyImage;
+  }
+
+  /**
+   * Creates a dummy image, that will be called DUMMY_IMAGE_NAME and will not 
have binary data
+   * 
+   * @return the dummy image
+   */
+  private Image createDummyImage() {
+    Image dummyImage = OBProvider.getInstance().get(Image.class);
+    dummyImage.setName(DUMMY_IMAGE_NAME);
+    OBDal.getInstance().save(dummyImage);
+    return dummyImage;
+  }
+
   public void onUpdate(@Observes EntityUpdateEvent event) {
     if (!isValidEvent(event)) {
       return;

------------------------------------------------------------------------------
_______________________________________________
Openbravo-commits mailing list
Openbravo-commits@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/openbravo-commits

Reply via email to