From aedd412f44444657d88e24de0600ee15b5267da0 Mon Sep 17 00:00:00 2001
From: buczekp <buczekp>
Date: Wed, 25 Nov 2009 08:25:55 +0000
Subject: [PATCH] [LMS-1264] loading permlinks of dependent samples for entity
 tracking

SVN: 13522
---
 .../bo/samplelister/SampleListingWorker.java  | 37 ++++++++-----------
 .../openbis/generic/OpenbisClientTest.java    |  2 +
 2 files changed, 18 insertions(+), 21 deletions(-)

diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/business/bo/samplelister/SampleListingWorker.java b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/business/bo/samplelister/SampleListingWorker.java
index 4b01e61ff21..4e7d2777482 100644
--- a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/business/bo/samplelister/SampleListingWorker.java
+++ b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/business/bo/samplelister/SampleListingWorker.java
@@ -145,6 +145,8 @@ final class SampleListingWorker
 
     private boolean singleSampleTypeMode;
 
+    private boolean enrichDependentSamples;
+
     private int maxSampleParentResolutionDepth;
 
     private int maxSampleContainerResolutionDepth;
@@ -185,6 +187,7 @@ final class SampleListingWorker
         this.databaseInstance = databaseInstance;
         this.query = query;
         this.samplePropertiesEnricherOrNull = samplePropertiesEnricherOrNull;
+        this.enrichDependentSamples = criteria.isEnrichDependentSamplesWithProperties();
         this.referencedEntityDAO = referencedEntityDAO;
     }
 
@@ -216,8 +219,7 @@ final class SampleListingWorker
             watch.reset();
             watch.start();
         }
-
-        if (criteria.isEnrichDependentSamplesWithProperties() == false)
+        if (enrichDependentSamples == false)
         {
             // only 'primary' samples were retrieved up to this point
             enrichRetrievedSamplesWithProperties(watch);
@@ -225,7 +227,7 @@ final class SampleListingWorker
         retrieveDependentSamplesRecursively();
         resolveParents();
         resolveContainers();
-        if (criteria.isEnrichDependentSamplesWithProperties())
+        if (enrichDependentSamples)
         {
             // dependent samples will also be enriched
             enrichRetrievedSamplesWithProperties(watch);
@@ -240,12 +242,6 @@ final class SampleListingWorker
 
     private void enrichRetrievedSamplesWithProperties(StopWatch watch)
     {
-        // Initialize property collections of all samples retrieved up to this point
-        // (without this enricher will not work properly).
-        for (Sample sample : sampleMap.values())
-        {
-            sample.setProperties(new ArrayList<IEntityProperty>());
-        }
         if (samplePropertiesEnricherOrNull != null)
         {
             samplePropertiesEnricherOrNull.enrich(sampleMap.keySet(),
@@ -411,9 +407,6 @@ final class SampleListingWorker
         {
             return null;
         }
-        // uncomment if we want all dependent samples to be loaded
-        // maxSampleContainerResolutionDepth = Integer.MAX_VALUE;
-        // maxSampleParentResolutionDepth = Integer.MAX_VALUE;
         Long sampleTypeId =
                 referencedEntityDAO.getSampleTypeIdForSampleTypeCode(criteria.getSampleTypeCode());
         return query.getNewSamplesForSampleType(sampleTypeId, criteria.getLastSeenSampleId());
@@ -423,16 +416,16 @@ final class SampleListingWorker
     {
         assert sampleList != null;
 
-        retrieveBasicSamples(sampleIteratorOrNull, baseIndexURL, sampleList);
+        retrieveBasicSamples(sampleIteratorOrNull, sampleList);
     }
 
     private void retrieveDependentBasicSamples(final Iterable<SampleRecord> sampleIteratorOrNull)
     {
-        retrieveBasicSamples(sampleIteratorOrNull, null, null);
+        retrieveBasicSamples(sampleIteratorOrNull, null);
     }
 
     private void retrieveBasicSamples(final Iterable<SampleRecord> sampleIteratorOrNull,
-            final String baseIndexURLOrNull, final List<Sample> sampleListOrNull)
+            final List<Sample> sampleListOrNull)
     {
         if (sampleIteratorOrNull == null)
         {
@@ -441,7 +434,7 @@ final class SampleListingWorker
         final boolean primarySample = (sampleListOrNull != null);
         for (SampleRecord row : sampleIteratorOrNull)
         {
-            final Sample sampleOrNull = tryCreateSample(row, baseIndexURLOrNull, primarySample);
+            final Sample sampleOrNull = tryCreateSample(row, primarySample);
             if (sampleOrNull != null) // null == different db instance
             {
                 sampleMap.put(sampleOrNull.getId(), sampleOrNull);
@@ -453,8 +446,7 @@ final class SampleListingWorker
         }
     }
 
-    private Sample tryCreateSample(SampleRecord row, final String baseIndexURLOrNull,
-            final boolean primarySample)
+    private Sample tryCreateSample(SampleRecord row, final boolean primarySample)
     {
         final Sample sample = new Sample();
         sample.setId(row.id);
@@ -485,11 +477,14 @@ final class SampleListingWorker
             }
         }
         // set properties needed for primary samples
-        if (primarySample)
+        // (dependent samples too if they need to be enriched e.g. for entity tracking)
+        if (primarySample || enrichDependentSamples)
         {
+            // initializing property collection - without this enricher will not work properly
+            sample.setProperties(new ArrayList<IEntityProperty>());
             sample.setPermId(StringEscapeUtils.escapeHtml(row.perm_id));
-            sample.setPermlink(PermlinkUtilities.createPermlinkURL(baseIndexURLOrNull,
-                    EntityKind.SAMPLE, row.perm_id));
+            sample.setPermlink(PermlinkUtilities.createPermlinkURL(baseIndexURL, EntityKind.SAMPLE,
+                    row.perm_id));
             sample.setRegistrationDate(row.registration_timestamp);
             if (row.inva_id != null)
             {
diff --git a/openbis/sourceTest/java/ch/systemsx/cisd/openbis/generic/OpenbisClientTest.java b/openbis/sourceTest/java/ch/systemsx/cisd/openbis/generic/OpenbisClientTest.java
index 5fbe34ee64d..3392cc61ec8 100644
--- a/openbis/sourceTest/java/ch/systemsx/cisd/openbis/generic/OpenbisClientTest.java
+++ b/openbis/sourceTest/java/ch/systemsx/cisd/openbis/generic/OpenbisClientTest.java
@@ -176,6 +176,7 @@ public class OpenbisClientTest
             builder.append(" code", sample.getCode());
             builder.append(" identifier", sample.getIdentifier());
             builder.append(" type", sample.getSampleType());
+            builder.append(" permlink", sample.getPermlink());
             builder.append(" properties", toString(sample.getProperties()));
             sb.append(builder.toString());
             final String newIndent = indent + INDENT;
@@ -200,6 +201,7 @@ public class OpenbisClientTest
             builder.append("id", dataSet.getId());
             builder.append(" code", dataSet.getCode());
             builder.append(" type", dataSet.getDataSetType());
+            builder.append(" permlink", dataSet.getPermlink());
             builder.append(" properties", toString(dataSet.getProperties()));
             sb.append(builder.toString());
             if (dataSet.getSample() != null)
-- 
GitLab