From ec4dc6a0fe48595a397b18c2143a5f77e647d0af Mon Sep 17 00:00:00 2001
From: felmer <felmer>
Date: Wed, 11 Jan 2012 14:26:13 +0000
Subject: [PATCH] LMS-2483 Following bug fixed: In case of deleted container
 data sets EventDAO thrown an exception because number of locations where
 different from number of identifiers. Also archiving tasks are modified to
 delete only physical data sets.

SVN: 24150
---
 .../DeleteFromArchiveMaintenanceTask.java     |  9 +++--
 .../ArchivingPostRegistrationTask.java        |  2 +-
 .../server/dataaccess/db/EventDAO.java        | 37 ++++++++++---------
 .../shared/basic/dto/DeletedDataSet.java      | 11 +++---
 4 files changed, 33 insertions(+), 26 deletions(-)

diff --git a/datastore_server/source/java/ch/systemsx/cisd/etlserver/plugins/DeleteFromArchiveMaintenanceTask.java b/datastore_server/source/java/ch/systemsx/cisd/etlserver/plugins/DeleteFromArchiveMaintenanceTask.java
index 63abb4c4c15..776ae4463a3 100644
--- a/datastore_server/source/java/ch/systemsx/cisd/etlserver/plugins/DeleteFromArchiveMaintenanceTask.java
+++ b/datastore_server/source/java/ch/systemsx/cisd/etlserver/plugins/DeleteFromArchiveMaintenanceTask.java
@@ -125,7 +125,7 @@ public class DeleteFromArchiveMaintenanceTask extends
         List<DeletedDataSet> result = new ArrayList<DeletedDataSet>();
         for (DeletedDataSet dataset : datasets) {
             if (dataset.getIdentifier() != null
-                    && false == dataset.getIdentifier().equals(dataset.getLocation()))
+                    && false == dataset.getIdentifier().equals(dataset.getLocationOrNull()))
             {
                 result.add(dataset);
             }
@@ -138,7 +138,10 @@ public class DeleteFromArchiveMaintenanceTask extends
         ArrayList<DatasetLocation> result = new ArrayList<DatasetLocation>(datasets.size());
         for (DeletedDataSet deletedDS : datasets)
         {
-            result.add(toDataSetLocations(deletedDS));
+            if (deletedDS.getLocationOrNull() != null)
+            {
+                result.add(toDataSetLocations(deletedDS));
+            }
         }
         return result;
     }
@@ -147,7 +150,7 @@ public class DeleteFromArchiveMaintenanceTask extends
     {
         DatasetLocation dsLocation = new DatasetLocation();
         dsLocation.setDatasetCode(deletedDS.getIdentifier());
-        dsLocation.setDataSetLocation(deletedDS.getLocation());
+        dsLocation.setDataSetLocation(deletedDS.getLocationOrNull());
         return dsLocation;
     }
 }
diff --git a/datastore_server/source/java/ch/systemsx/cisd/etlserver/postregistration/ArchivingPostRegistrationTask.java b/datastore_server/source/java/ch/systemsx/cisd/etlserver/postregistration/ArchivingPostRegistrationTask.java
index fc33bdd1721..503a04b7045 100644
--- a/datastore_server/source/java/ch/systemsx/cisd/etlserver/postregistration/ArchivingPostRegistrationTask.java
+++ b/datastore_server/source/java/ch/systemsx/cisd/etlserver/postregistration/ArchivingPostRegistrationTask.java
@@ -209,7 +209,7 @@ public class ArchivingPostRegistrationTask extends AbstractPostRegistrationTask
             
             IArchiverPlugin archiver = ServiceProvider.getDataStoreService().getArchiverPlugin();
             DatasetDescription dataSet = tryGetDatasetWithLocation(dataSetCode, openBISService);
-            if (archiver != null && dataSet != null)
+            if (archiver != null && dataSet != null && dataSet.getDataSetLocation() != null)
             {
                 DatasetLocation dataset = new DatasetLocation();
                 dataset.setDatasetCode(dataSetCode);
diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/db/EventDAO.java b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/db/EventDAO.java
index 091e696ac18..462fc87e0ef 100644
--- a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/db/EventDAO.java
+++ b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/db/EventDAO.java
@@ -17,9 +17,10 @@
 package ch.systemsx.cisd.openbis.generic.server.dataaccess.db;
 
 import java.util.ArrayList;
-import java.util.Arrays;
 import java.util.Date;
+import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
 
 import org.apache.log4j.Logger;
 import org.hibernate.Criteria;
@@ -29,7 +30,6 @@ import org.hibernate.criterion.MatchMode;
 import org.hibernate.criterion.Restrictions;
 import org.springframework.jdbc.support.JdbcAccessor;
 
-import ch.systemsx.cisd.common.exceptions.EnvironmentFailureException;
 import ch.systemsx.cisd.common.logging.LogCategory;
 import ch.systemsx.cisd.common.logging.LogFactory;
 import ch.systemsx.cisd.common.utilities.MethodUtils;
@@ -53,8 +53,8 @@ public class EventDAO extends AbstractGenericEntityDAO<EventPE> implements IEven
      * This logger does not output any SQL statement. If you want to do so, you had better set an
      * appropriate debugging level for class {@link JdbcAccessor}. </p>
      */
-    private static final Logger operationLog =
-            LogFactory.getLogger(LogCategory.OPERATION, EventPE.class);
+    private static final Logger operationLog = LogFactory.getLogger(LogCategory.OPERATION,
+            EventPE.class);
 
     public EventDAO(SessionFactory sessionFactory, DatabaseInstancePE databaseInstance)
     {
@@ -108,21 +108,11 @@ public class EventDAO extends AbstractGenericEntityDAO<EventPE> implements IEven
         ArrayList<DeletedDataSet> result = new ArrayList<DeletedDataSet>();
         for (EventPE event : list)
         {
+            Map<String, String> map = createIdentifierToLocationMap(event.getDescription());
             List<String> identifiers = event.getIdentifiers();
-            List<String> locations =
-                    Arrays.asList(event.getDescription().split(EventPE.IDENTIFIER_SEPARATOR));
-            if (identifiers.size() != locations.size())
+            for (String dataSetCode : identifiers)
             {
-                throw EnvironmentFailureException.fromTemplate(
-                        "Number of deleted dataset codes %s does not match "
-                                + "the number of deleted data set locations %s in eventId='%s'",
-                                identifiers.size(), locations.size(), event.getId());
-            }
-            int pos = 0;
-            for (pos = 0; pos < identifiers.size(); pos++)
-            {
-                String dataSetCode = identifiers.get(pos);
-                String dataSetLocation = locations.get(pos);
+                String dataSetLocation = map.get(dataSetCode);
                 DeletedDataSet deletedDataSet =
                         new DeletedDataSet(dataSetCode, dataSetLocation, event.getId());
                 result.add(deletedDataSet);
@@ -130,4 +120,17 @@ public class EventDAO extends AbstractGenericEntityDAO<EventPE> implements IEven
         }
         return result;
     }
+
+    private Map<String, String> createIdentifierToLocationMap(String locations)
+    {
+        Map<String, String> result = new HashMap<String, String>();
+        for (String location : locations.split(EventPE.IDENTIFIER_SEPARATOR))
+        {
+            int lastIndexOfSlash = location.lastIndexOf('/');
+            String identifier =
+                    lastIndexOfSlash < 0 ? location : location.substring(lastIndexOfSlash + 1);
+            result.put(identifier, location);
+        }
+        return result;
+    }
 }
diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/generic/shared/basic/dto/DeletedDataSet.java b/openbis/source/java/ch/systemsx/cisd/openbis/generic/shared/basic/dto/DeletedDataSet.java
index 24a97d495c4..10041d38bc9 100644
--- a/openbis/source/java/ch/systemsx/cisd/openbis/generic/shared/basic/dto/DeletedDataSet.java
+++ b/openbis/source/java/ch/systemsx/cisd/openbis/generic/shared/basic/dto/DeletedDataSet.java
@@ -34,18 +34,19 @@ public class DeletedDataSet implements Serializable, ICodeHolder
 
     private final long eventId;
 
-    private final String location; // the location where the data set existed before deletion
+    private final String locationOrNull; // the location where the data set existed before deletion
+                                         // or null if it was a container data set
 
-    public DeletedDataSet(String identifier, String location, long eventId)
+    public DeletedDataSet(String identifier, String locationOrNull, long eventId)
     {
         this.eventId = eventId;
         this.identifier = identifier;
-        this.location = location;
+        this.locationOrNull = locationOrNull;
     }
 
-    public String getLocation()
+    public String getLocationOrNull()
     {
-        return location;
+        return locationOrNull;
     }
 
     public String getCode()
-- 
GitLab