From 51fb541434ea959810144c5312d256b57f03ffcf Mon Sep 17 00:00:00 2001
From: brinn <brinn>
Date: Tue, 24 Jul 2012 19:24:50 +0000
Subject: [PATCH] Add method SpaceCodeHelper.tryGetSpaceCode().

SVN: 26179
---
 .../generic/shared/util/SpaceCodeHelper.java  | 39 ++++++++++++++-----
 1 file changed, 29 insertions(+), 10 deletions(-)

diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/generic/shared/util/SpaceCodeHelper.java b/openbis/source/java/ch/systemsx/cisd/openbis/generic/shared/util/SpaceCodeHelper.java
index 16e7d4a9a53..35086c17f3e 100644
--- a/openbis/source/java/ch/systemsx/cisd/openbis/generic/shared/util/SpaceCodeHelper.java
+++ b/openbis/source/java/ch/systemsx/cisd/openbis/generic/shared/util/SpaceCodeHelper.java
@@ -16,8 +16,8 @@
 
 package ch.systemsx.cisd.openbis.generic.shared.util;
 
-import ch.systemsx.cisd.openbis.generic.shared.dto.SpacePE;
 import ch.systemsx.cisd.openbis.generic.shared.dto.PersonPE;
+import ch.systemsx.cisd.openbis.generic.shared.dto.SpacePE;
 import ch.systemsx.cisd.openbis.generic.shared.dto.exception.UndefinedSpaceException;
 import ch.systemsx.cisd.openbis.generic.shared.dto.identifier.SpaceIdentifier;
 
@@ -70,29 +70,48 @@ public class SpaceCodeHelper
     /**
      * Tries to find out the space.
      * <p>
-     * If given <var>spaceCode</var> is a home space, the real space must be specified as home space
-     * in given {@link PersonPE}.
+     * If given <var>spaceCode</var> specifies the home space, the home space of the
+     * {@link PersonPE} is returned. May return <code>null</code>, if no home space is defined for
+     * the user.
      * </p>
-     * 
-     * @throws UndefinedSpaceException if no space could be found.
      */
-    public final static String getSpaceCode(final PersonPE person, final String spaceCode)
+    public final static String tryGetSpaceCode(final PersonPE person, final String spaceCode)
             throws UndefinedSpaceException
     {
         if (isHomeSpace(spaceCode))
         {
-            final SpacePE homeGroup = person.getHomeSpace();
-            if (homeGroup == null)
+            final SpacePE homeSpace = person.getHomeSpace();
+            if (homeSpace == null)
             {
-                throw new UndefinedSpaceException();
+                return null;
             }
-            return homeGroup.getCode();
+            return homeSpace.getCode();
         } else
         {
             return spaceCode;
         }
     }
 
+    /**
+     * Tries to find out the space.
+     * <p>
+     * If given <var>spaceCode</var> is a home space, the real space must be specified as home space
+     * in given {@link PersonPE}.
+     * </p>
+     * 
+     * @throws UndefinedSpaceException if no space could be found.
+     */
+    public final static String getSpaceCode(final PersonPE person, final String spaceCode)
+            throws UndefinedSpaceException
+    {
+        final String spaceCodeOrNull = tryGetSpaceCode(person, spaceCode);
+        if (spaceCodeOrNull == null)
+        {
+            throw new UndefinedSpaceException();
+        }
+        return spaceCodeOrNull;
+    }
+
     /**
      * Tries to find out the space.
      * <p>
-- 
GitLab