From 2c9adb4973e89a4fc151d228fbef1b2c79feffda Mon Sep 17 00:00:00 2001
From: buczekp <buczekp>
Date: Wed, 14 Jul 2010 09:26:07 +0000
Subject: [PATCH] refactorization of SampleAccessPE

SVN: 17021
---
 .../SampleTechIdCollectionPredicate.java      | 21 ++---
 .../generic/shared/dto/SampleAccessPE.java    | 78 +++++++++++--------
 2 files changed, 56 insertions(+), 43 deletions(-)

diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/generic/shared/authorization/predicate/SampleTechIdCollectionPredicate.java b/openbis/source/java/ch/systemsx/cisd/openbis/generic/shared/authorization/predicate/SampleTechIdCollectionPredicate.java
index 3852ff440af..d1a08175e8c 100644
--- a/openbis/source/java/ch/systemsx/cisd/openbis/generic/shared/authorization/predicate/SampleTechIdCollectionPredicate.java
+++ b/openbis/source/java/ch/systemsx/cisd/openbis/generic/shared/authorization/predicate/SampleTechIdCollectionPredicate.java
@@ -52,16 +52,17 @@ public class SampleTechIdCollectionPredicate extends
 
         for (SampleAccessPE accessDatum : accessData)
         {
-            String groupCode = accessDatum.getGroupCode();
-            String dbInstanceCode = accessDatum.getDatabaseInstanceCode();
-            if (groupCode != null)
+            String ownerCode = accessDatum.getOwnerCode();
+            switch (accessDatum.getOwnerType())
             {
-                ownerIds.add(new SampleOwnerIdentifier(new SpaceIdentifier(
-                        DatabaseInstanceIdentifier.createHome(), groupCode)));
-            } else
-            {
-                ownerIds.add(new SampleOwnerIdentifier(new DatabaseInstanceIdentifier(
-                        dbInstanceCode)));
+                case SPACE:
+                    ownerIds.add(new SampleOwnerIdentifier(new SpaceIdentifier(
+                            DatabaseInstanceIdentifier.createHome(), ownerCode)));
+                    break;
+                case DATABASE_INSTANCE:
+                    ownerIds.add(new SampleOwnerIdentifier(
+                            new DatabaseInstanceIdentifier(ownerCode)));
+                    break;
             }
         }
 
@@ -71,7 +72,7 @@ public class SampleTechIdCollectionPredicate extends
     @Override
     public final String getCandidateDescription()
     {
-        return "sample technical ids"; 
+        return "sample technical ids";
     }
 
 }
diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/generic/shared/dto/SampleAccessPE.java b/openbis/source/java/ch/systemsx/cisd/openbis/generic/shared/dto/SampleAccessPE.java
index 8ca1195f0fd..2feca3dba41 100644
--- a/openbis/source/java/ch/systemsx/cisd/openbis/generic/shared/dto/SampleAccessPE.java
+++ b/openbis/source/java/ch/systemsx/cisd/openbis/generic/shared/dto/SampleAccessPE.java
@@ -26,6 +26,7 @@ import javax.persistence.SqlResultSetMappings;
 
 import org.apache.commons.lang.builder.EqualsBuilder;
 import org.apache.commons.lang.builder.HashCodeBuilder;
+import org.apache.commons.lang.builder.ToStringBuilder;
 
 /**
  * A PE for retrieving only the information necessary to determine if a user/person can access a
@@ -42,14 +43,14 @@ import org.apache.commons.lang.builder.HashCodeBuilder;
     })
 @NamedNativeQueries(value =
     {
-            @NamedNativeQuery(name = "space_sample_access", query = "SELECT DISTINCT g.code as dummyId, g.code as groupCode, null as databaseInstanceCode "
+            @NamedNativeQuery(name = "space_sample_access", query = "SELECT DISTINCT g.code as ownerCode, 'SPACE' as ownerType "
                     + "FROM "
                     + TableNames.SAMPLES_TABLE
                     + " s, "
                     + TableNames.GROUPS_TABLE
                     + " g "
                     + "WHERE s.id in (:ids) and s.grou_id = g.id", resultSetMapping = "implicit1"),
-            @NamedNativeQuery(name = "shared_sample_access", query = "SELECT DISTINCT dbi.code as dummyId, dbi.code as databaseInstanceCode, null as groupCode "
+            @NamedNativeQuery(name = "shared_sample_access", query = "SELECT DISTINCT dbi.code as ownerCode, 'DATABASE_INSTANCE' as ownerType "
                     + "FROM "
                     + TableNames.SAMPLES_TABLE
                     + " s, "
@@ -60,11 +61,6 @@ import org.apache.commons.lang.builder.HashCodeBuilder;
     })
 public class SampleAccessPE
 {
-    private String dummyId;
-
-    private String groupCode;
-
-    private String databaseInstanceCode;
 
     public final static String SPACE_SAMPLE_ACCESS_QUERY_NAME = "space_sample_access";
 
@@ -72,49 +68,58 @@ public class SampleAccessPE
 
     public final static String SAMPLE_IDS_PARAMETER_NAME = "ids";
 
+    public enum SampleOwnerType
+    {
+        SPACE, DATABASE_INSTANCE
+    }
+
+    private String ownerCode;
+
+    private SampleOwnerType ownerType;
+
     /**
      * A factory method that should only be used for testing.
      */
-    public static SampleAccessPE createSampleAccessPEForTest(String dataSetId, String dataSetCode,
-            String groupCode, String databaseInstanceCode)
+    public static SampleAccessPE createSpaceSampleAccessPEForTest(String dataSetId,
+            String dataSetCode, String groupCode)
     {
         SampleAccessPE newMe = new SampleAccessPE();
-        newMe.setGroupCode(groupCode);
-        newMe.setDatabaseInstanceCode(databaseInstanceCode);
+        newMe.setOwnerType(SampleOwnerType.SPACE);
+        newMe.setOwnerCode(groupCode);
         return newMe;
     }
 
-    // WORKAROUND we need a dummy id that is not null
-    // otherwise null will be returned instead of entity when listing
-    @Id
-    String getDummyId()
-    {
-        return dummyId;
-    }
-
-    public String getGroupCode()
+    /**
+     * A factory method that should only be used for testing.
+     */
+    public static SampleAccessPE createSharedSampleAccessPEForTest(String dataSetId,
+            String dataSetCode, String databaseInstanceCode)
     {
-        return groupCode;
+        SampleAccessPE newMe = new SampleAccessPE();
+        newMe.setOwnerType(SampleOwnerType.DATABASE_INSTANCE);
+        newMe.setOwnerCode(databaseInstanceCode);
+        return newMe;
     }
 
-    public String getDatabaseInstanceCode()
+    @Id
+    public String getOwnerCode()
     {
-        return databaseInstanceCode;
+        return ownerCode;
     }
 
-    void setDummyId(String dummyId)
+    public void setOwnerCode(String ownerCode)
     {
-        this.dummyId = dummyId;
+        this.ownerCode = ownerCode;
     }
 
-    void setGroupCode(String groupCode)
+    public SampleOwnerType getOwnerType()
     {
-        this.groupCode = groupCode;
+        return ownerType;
     }
 
-    void setDatabaseInstanceCode(String databaseInstanceCode)
+    public void setOwnerType(SampleOwnerType ownerType)
     {
-        this.databaseInstanceCode = databaseInstanceCode;
+        this.ownerType = ownerType;
     }
 
     //
@@ -134,8 +139,8 @@ public class SampleAccessPE
         }
         final SampleAccessPE that = (SampleAccessPE) obj;
         final EqualsBuilder builder = new EqualsBuilder();
-        builder.append(getGroupCode(), that.getGroupCode());
-        builder.append(getDatabaseInstanceCode(), that.getDatabaseInstanceCode());
+        builder.append(getOwnerType(), that.getOwnerType());
+        builder.append(getOwnerCode(), that.getOwnerCode());
         return builder.isEquals();
     }
 
@@ -143,8 +148,15 @@ public class SampleAccessPE
     public int hashCode()
     {
         final HashCodeBuilder builder = new HashCodeBuilder();
-        builder.append(getGroupCode());
-        builder.append(getDatabaseInstanceCode());
+        builder.append(getOwnerType());
+        builder.append(getOwnerCode());
         return builder.toHashCode();
     }
+
+    @Override
+    public String toString()
+    {
+        return ToStringBuilder.reflectionToString(this);
+    }
+
 }
-- 
GitLab