From f046a5b89d2dba13ed3513987ba22fce7e46bc1c Mon Sep 17 00:00:00 2001
From: brinn <brinn>
Date: Sun, 24 Feb 2013 15:21:54 +0000
Subject: [PATCH] [BIS-278/SP-417] Add API entity identifiers.

SVN: 28419
---
 .../api/v1/dto/ExperimentIdentifier.java      | 227 ++++++++++++++++++
 .../api/v1/dto/IDatabaseIdentifier.java       |  33 +++
 .../api/v1/dto/IPermanentIdentifier.java      |  32 +++
 .../shared/api/v1/dto/ProjectIdentifier.java  | 187 +++++++++++++++
 .../shared/api/v1/dto/SampleIdentifier.java   | 200 +++++++++++++++
 5 files changed, 679 insertions(+)
 create mode 100644 openbis_api/source/java/ch/systemsx/cisd/openbis/generic/shared/api/v1/dto/ExperimentIdentifier.java
 create mode 100644 openbis_api/source/java/ch/systemsx/cisd/openbis/generic/shared/api/v1/dto/IDatabaseIdentifier.java
 create mode 100644 openbis_api/source/java/ch/systemsx/cisd/openbis/generic/shared/api/v1/dto/IPermanentIdentifier.java
 create mode 100644 openbis_api/source/java/ch/systemsx/cisd/openbis/generic/shared/api/v1/dto/ProjectIdentifier.java
 create mode 100644 openbis_api/source/java/ch/systemsx/cisd/openbis/generic/shared/api/v1/dto/SampleIdentifier.java

diff --git a/openbis_api/source/java/ch/systemsx/cisd/openbis/generic/shared/api/v1/dto/ExperimentIdentifier.java b/openbis_api/source/java/ch/systemsx/cisd/openbis/generic/shared/api/v1/dto/ExperimentIdentifier.java
new file mode 100644
index 00000000000..bc279e14d8a
--- /dev/null
+++ b/openbis_api/source/java/ch/systemsx/cisd/openbis/generic/shared/api/v1/dto/ExperimentIdentifier.java
@@ -0,0 +1,227 @@
+/*
+ * Copyright 2010 ETH Zuerich, CISD
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package ch.systemsx.cisd.openbis.generic.shared.api.v1.dto;
+
+import org.apache.commons.lang.StringUtils;
+import org.apache.commons.lang.builder.EqualsBuilder;
+import org.apache.commons.lang.builder.HashCodeBuilder;
+import org.apache.commons.lang.builder.ToStringBuilder;
+import org.apache.commons.lang.builder.ToStringStyle;
+
+import com.fasterxml.jackson.annotation.JsonIgnore;
+
+import ch.systemsx.cisd.base.annotation.JsonObject;
+import ch.systemsx.cisd.common.reflection.ModifiedShortPrefixToStringStyle;
+
+/**
+ * Unique identifier for an experiment in openBIS.
+ * 
+ * @author Bernd Rinn
+ */
+@SuppressWarnings("unused")
+@JsonObject("ExperimentIdentifier")
+public class ExperimentIdentifier implements IPermanentIdentifier, IDatabaseIdentifier
+{
+    private static final long serialVersionUID = 1L;
+
+    private Long databaseId;
+
+    private String permId;
+
+    private String spaceCode;
+
+    private String projectCode;
+
+    private String code;
+
+    /**
+     * Creates an {@link ExperimentIdentifier} from the given <var>augmentedCode</code>.
+     * 
+     * @param augmentedCode The <var>augmentedCode</code> in the form
+     *            <code>/SPACE/PROJECT/EXPERIMENT</code>
+     * @return An experiment identifer corresponding to <var>augmentedCode</code>. Note that this
+     *         experiment identifier has no perm id or database id set.
+     * @throws IllegalArgumentException If the <var>augmentedCode</code> is not in the form
+     *             <code>/SPACE/PROJECT/EXPERIMENT</code>.
+     */
+    public static ExperimentIdentifier createFromAugmentedCode(String augmentedCode)
+            throws IllegalArgumentException
+    {
+        final String[] splitted = augmentedCode.split("/");
+        if (splitted.length != 4 || splitted[0].length() != 0)
+        {
+            throw new IllegalArgumentException("Augmented code '" + augmentedCode
+                    + "' needs to be either of the form '/SPACE/PROJECT/EXPERIMENT' "
+                    + "or 'PROJECT/EXPERIMENT'.");
+        }
+        if (StringUtils.isBlank(splitted[3]))
+        {
+            throw new IllegalArgumentException("No code given.");
+        }
+        if (StringUtils.isBlank(splitted[2]))
+        {
+            throw new IllegalArgumentException("No project code given.");
+        }
+        if (StringUtils.isBlank(splitted[1]))
+        {
+            throw new IllegalArgumentException("No space code given.");
+        }
+        return new ExperimentIdentifier(null, null, splitted[3], splitted[2], splitted[1]);
+    }
+
+    /**
+     * Creates an {@link ExperimentIdentifier} from the given <var>permId</code>.
+     * 
+     * @param permId The <var>permId</code>
+     * @return An experiment identifier corresponding to <var>permId</code>. Note that this
+     *         experiment identifier has no code, project or space information.
+     */
+    public static ExperimentIdentifier createFromPermId(String permId)
+            throws IllegalArgumentException
+    {
+        if (StringUtils.isBlank(permId))
+        {
+            throw new IllegalArgumentException("No perm id given.");
+        }
+        return new ExperimentIdentifier(null, permId, null, null, null);
+    }
+    
+    /**
+     * Creates an {@link ExperimentIdentifier} from the given <var>entity</code>.
+     * 
+     * @param entity The entity as received from one of the other methods.
+     * @return An experiment identifier corresponding to <var>entity</code>. Note that this
+     *         experiment identifier has no permid, code, project or space information.
+     */
+    public static ExperimentIdentifier createFromEntity(Experiment entity)
+    {
+        return new ExperimentIdentifier(entity.getId(), null, null, null, null);
+    }
+
+    /**
+     * A <code>spaceCode == null</code> is interpreted as the home space.
+     */
+    private ExperimentIdentifier(Long databaseId, String permId, String code,
+            String projectCode, String spaceCode)
+    {
+        this.databaseId = databaseId;
+        this.permId = permId;
+        this.spaceCode = spaceCode;
+        this.projectCode = projectCode;
+        this.code = code;
+    }
+
+    @Override
+    public Long getDatabaseId()
+    {
+        return databaseId;
+    }
+
+    @Override
+    public String getPermId()
+    {
+        return permId;
+    }
+
+    /**
+     * The code of the space of this experiment.
+     */
+    public String getSpaceCode()
+    {
+        return spaceCode;
+    }
+
+    /**
+     * The code of the project of this experiment.
+     */
+    public String getProjectCode()
+    {
+        return projectCode;
+    }
+
+    /**
+     * The experiment code.
+     */
+    public String getCode()
+    {
+        return code;
+    }
+
+    /**
+     * Returns the augmented (full) code of this experiment.
+     */
+    @JsonIgnore
+    public String getAugmentedCode()
+    {
+        if (code == null)
+        {
+            return null;
+        }
+        return "/" + spaceCode + "/" + projectCode + "/" + code;
+    }
+
+    //
+    // JSON-RPC
+    //
+
+    private ExperimentIdentifier()
+    {
+    }
+
+    private void setSpaceCode(String spaceCode)
+    {
+        this.spaceCode = spaceCode;
+    }
+
+    private void setProjectCode(String projectCode)
+    {
+        this.projectCode = projectCode;
+    }
+
+    private void setcode(String code)
+    {
+        this.code = code;
+    }
+
+    private void setPermId(String permId)
+    {
+        this.permId = permId;
+    }
+
+    private void setDatabaseId(Long databaseId)
+    {
+        this.databaseId = databaseId;
+    }
+
+    @Override
+    public final boolean equals(final Object obj)
+    {
+        return EqualsBuilder.reflectionEquals(this, obj);
+    }
+
+    @Override
+    public final int hashCode()
+    {
+        return HashCodeBuilder.reflectionHashCode(this);
+    }
+
+    @Override
+    public String toString()
+    {
+        return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE);
+    }
+}
diff --git a/openbis_api/source/java/ch/systemsx/cisd/openbis/generic/shared/api/v1/dto/IDatabaseIdentifier.java b/openbis_api/source/java/ch/systemsx/cisd/openbis/generic/shared/api/v1/dto/IDatabaseIdentifier.java
new file mode 100644
index 00000000000..d60c1561d40
--- /dev/null
+++ b/openbis_api/source/java/ch/systemsx/cisd/openbis/generic/shared/api/v1/dto/IDatabaseIdentifier.java
@@ -0,0 +1,33 @@
+/*
+ * Copyright 2013 ETH Zuerich, CISD
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package ch.systemsx.cisd.openbis.generic.shared.api.v1.dto;
+
+/**
+ * A database id of an entity.
+ * <i>
+ * Note that this is id not meant to be persisted anywhere outside of the database. Use
+ * {@link IPermanentIdentifier} for that.
+ * 
+ * @author Bernd Rinn
+ */
+public interface IDatabaseIdentifier
+{
+    /**
+     * Returns the database id of the entity.
+     */
+    public Long getDatabaseId();
+}
diff --git a/openbis_api/source/java/ch/systemsx/cisd/openbis/generic/shared/api/v1/dto/IPermanentIdentifier.java b/openbis_api/source/java/ch/systemsx/cisd/openbis/generic/shared/api/v1/dto/IPermanentIdentifier.java
new file mode 100644
index 00000000000..4a55874d282
--- /dev/null
+++ b/openbis_api/source/java/ch/systemsx/cisd/openbis/generic/shared/api/v1/dto/IPermanentIdentifier.java
@@ -0,0 +1,32 @@
+/*
+ * Copyright 2010 ETH Zuerich, CISD
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package ch.systemsx.cisd.openbis.generic.shared.api.v1.dto;
+
+/**
+ * A role that represents a permanent identifier in openBIS.
+ * 
+ * @author Bernd Rinn
+ */
+public interface IPermanentIdentifier
+{
+
+    /**
+     * Returns the permanent identifier.
+     */
+    public String getPermId();
+
+}
\ No newline at end of file
diff --git a/openbis_api/source/java/ch/systemsx/cisd/openbis/generic/shared/api/v1/dto/ProjectIdentifier.java b/openbis_api/source/java/ch/systemsx/cisd/openbis/generic/shared/api/v1/dto/ProjectIdentifier.java
new file mode 100644
index 00000000000..ca2f4c70c4d
--- /dev/null
+++ b/openbis_api/source/java/ch/systemsx/cisd/openbis/generic/shared/api/v1/dto/ProjectIdentifier.java
@@ -0,0 +1,187 @@
+/*
+ * Copyright 2010 ETH Zuerich, CISD
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package ch.systemsx.cisd.openbis.generic.shared.api.v1.dto;
+
+import org.apache.commons.lang.builder.EqualsBuilder;
+import org.apache.commons.lang.builder.HashCodeBuilder;
+import org.apache.commons.lang.builder.ToStringBuilder;
+import org.apache.commons.lang.builder.ToStringStyle;
+
+import com.fasterxml.jackson.annotation.JsonIgnore;
+
+import ch.systemsx.cisd.base.annotation.JsonObject;
+import ch.systemsx.cisd.common.reflection.ModifiedShortPrefixToStringStyle;
+
+/**
+ * Unique identifier for a project in openBIS.
+ * 
+ * @author Bernd Rinn
+ */
+@SuppressWarnings("unused")
+@JsonObject("ProjectIdentifier")
+public class ProjectIdentifier implements IPermanentIdentifier, IDatabaseIdentifier
+{
+    private static final long serialVersionUID = 1L;
+
+    private Long databaseId;
+
+    private String permId;
+
+    private String spaceCode;
+
+    private String code;
+
+    /**
+     * Creates an {@link ProjectIdentifier} from the given <var>augmentedCode</code>.
+     * 
+     * @param augmentedCode The <var>augmentedCode</code> in the form
+     *            <code>/SPACE/PROJECT</code>. 
+     * @return A sample identifier corresponding to <var>augmentedCode</code>. Note that this
+     *         sample identifier has no perm id set.
+     * @throws IllegalArgumentException If the <var>augmentedCode</code> is not in the form
+     *             <code>/SPACE/SAMPLE</code> or <code>/SAMPLE</code>.
+     */
+    public static ProjectIdentifier createFromAugmentedCode(String augmentedCode)
+            throws IllegalArgumentException
+    {
+        final String[] splitted = augmentedCode.split("/");
+        if (splitted.length == 3 && splitted[0].length() == 0)
+        {
+            return new ProjectIdentifier(null, null, splitted[2], splitted[1]);
+        }
+        throw new IllegalArgumentException("Augmented code '" + augmentedCode
+                + "' needs to be of the form '/SPACE/PROJECT'.");
+    }
+
+    /**
+     * Creates an {@link ProjectIdentifier} from the given <var>permId</code>.
+     * 
+     * @param permId The <var>permId</code>
+     * @return An identifier corresponding to <var>permId</code>. Note that this
+     *         identifier has no code, project or space information.
+     */
+    public static ProjectIdentifier createFromPermId(String permId)
+            throws IllegalArgumentException
+    {
+        return new ProjectIdentifier(null, permId, null, null);
+    }
+    
+    /**
+     * Creates an {@link ProjectIdentifier} from the given <var>entity</code>.
+     * 
+     * @param entity The entity as received from one of the other methods.
+     * @return An identifier corresponding to <var>entity</code>. Note that this
+     *         identifier has no perm id, code, project or space information.
+     */
+    public static ProjectIdentifier createFromEntity(Project entity)
+    {
+        return new ProjectIdentifier(entity.getId(), null, null, null);
+    }
+
+    private ProjectIdentifier(Long databaseId, String permId, String sampleCode,
+            String spaceCode)
+    {
+        this.databaseId = databaseId;
+        this.permId = permId;
+        this.spaceCode = spaceCode;
+        this.code = sampleCode;
+    }
+
+    @Override
+    public Long getDatabaseId()
+    {
+        return databaseId;
+    }
+
+    @Override
+    public String getPermId()
+    {
+        return permId;
+    }
+
+    /**
+     * The code of the space of this project.
+     */
+    public String getSpaceCode()
+    {
+        return spaceCode;
+    }
+
+    public String getCode()
+    {
+        return code;
+    }
+
+    /**
+     * Returns the augmented (full) code of this project.
+     */
+    @JsonIgnore
+    public String getAugmentedCode()
+    {
+        if (code == null)
+        {
+            return null;
+        }
+        return "/" + spaceCode + "/" + code;
+    }
+
+    //
+    // JSON-RPC
+    //
+
+    private ProjectIdentifier()
+    {
+    }
+
+    private void setSpaceCode(String spaceCode)
+    {
+        this.spaceCode = spaceCode;
+    }
+
+    private void setCode(String code)
+    {
+        this.code = code;
+    }
+
+    private void setPermId(String permId)
+    {
+        this.permId = permId;
+    }
+
+    private void setDatabaseId(Long databaseId)
+    {
+        this.databaseId = databaseId;
+    }
+
+    @Override
+    public final boolean equals(final Object obj)
+    {
+        return EqualsBuilder.reflectionEquals(this, obj);
+    }
+
+    @Override
+    public final int hashCode()
+    {
+        return HashCodeBuilder.reflectionHashCode(this);
+    }
+
+    @Override
+    public String toString()
+    {
+        return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE);
+    }
+}
diff --git a/openbis_api/source/java/ch/systemsx/cisd/openbis/generic/shared/api/v1/dto/SampleIdentifier.java b/openbis_api/source/java/ch/systemsx/cisd/openbis/generic/shared/api/v1/dto/SampleIdentifier.java
new file mode 100644
index 00000000000..6a0cf8da1ce
--- /dev/null
+++ b/openbis_api/source/java/ch/systemsx/cisd/openbis/generic/shared/api/v1/dto/SampleIdentifier.java
@@ -0,0 +1,200 @@
+/*
+ * Copyright 2010 ETH Zuerich, CISD
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package ch.systemsx.cisd.openbis.generic.shared.api.v1.dto;
+
+import org.apache.commons.lang.builder.EqualsBuilder;
+import org.apache.commons.lang.builder.HashCodeBuilder;
+import org.apache.commons.lang.builder.ToStringBuilder;
+import org.apache.commons.lang.builder.ToStringStyle;
+
+import com.fasterxml.jackson.annotation.JsonIgnore;
+
+import ch.systemsx.cisd.base.annotation.JsonObject;
+import ch.systemsx.cisd.common.reflection.ModifiedShortPrefixToStringStyle;
+
+/**
+ * Unique identifier for a sample in openBIS.
+ * 
+ * @author Bernd Rinn
+ */
+@SuppressWarnings("unused")
+@JsonObject("SampleIdentifier")
+public class SampleIdentifier implements IPermanentIdentifier, IDatabaseIdentifier
+{
+    private static final long serialVersionUID = 1L;
+
+    private Long databaseId;
+
+    private String permId;
+
+    private String spaceCode;
+
+    private String code;
+
+    /**
+     * Creates an {@link SampleIdentifier} from the given <var>augmentedCode</code>.
+     * 
+     * @param augmentedCode The <var>augmentedCode</code> in the form
+     *            <code>/SPACE/SAMPLE</code> or <code>/SAMPLE</code>. 
+     * @return A sample identifier corresponding to <var>augmentedCode</code>. Note that this
+     *         sample identifier has no perm id set.
+     * @throws IllegalArgumentException If the <var>augmentedCode</code> is not in the form
+     *             <code>/SPACE/SAMPLE</code> or <code>/SAMPLE</code>.
+     */
+    public static SampleIdentifier createFromAugmentedCode(String augmentedCode)
+            throws IllegalArgumentException
+    {
+        final String[] splitted = augmentedCode.split("/");
+        if (splitted.length == 3 && splitted[0].length() == 0)
+        {
+            return new SampleIdentifier(null, null, splitted[2], splitted[1]);
+        }
+        if (splitted.length == 2 && splitted[0].length() == 0) // Instance sample
+        {
+            return new SampleIdentifier(null, null, splitted[1], null);
+        }
+        throw new IllegalArgumentException("Augmented code '" + augmentedCode
+                + "' needs to be either of the form '/SPACE/SAMPLE' " + "or '/SAMPLE'.");
+    }
+
+    /**
+     * Creates an {@link SampleIdentifier} from the given <var>permId</code>.
+     * 
+     * @param permId The <var>permId</code>
+     * @return An identifier corresponding to <var>permId</code>. Note that this
+     *         identifier has no code, project or space information.
+     */
+    public static SampleIdentifier createFromPermId(String permId)
+            throws IllegalArgumentException
+    {
+        return new SampleIdentifier(null, permId, null, null);
+    }
+    
+    /**
+     * Creates an {@link SampleIdentifier} from the given <var>entity</code>.
+     * 
+     * @param entity The entity as received from one of the other methods.
+     * @return An identifier corresponding to <var>entity</code>. Note that this
+     *         identifier has no permid, code, project or space information.
+     */
+    public static SampleIdentifier createFromEntity(Sample entity)
+    {
+        return new SampleIdentifier(entity.getId(), null, null, null);
+    }
+
+    /**
+     * A <code>spaceCode == null</code> means: instance sample.
+     */
+    private SampleIdentifier(Long databaseId, String permId, String sampleCode,
+            String spaceCode)
+    {
+        this.databaseId = databaseId;
+        this.permId = permId;
+        this.spaceCode = spaceCode;
+        this.code = sampleCode;
+    }
+
+    @Override
+    public Long getDatabaseId()
+    {
+        return databaseId;
+    }
+
+    @Override
+    public String getPermId()
+    {
+        return permId;
+    }
+
+    /**
+     * The code of the space of this sample.
+     */
+    public String getSpaceCode()
+    {
+        return spaceCode;
+    }
+
+    public String getCode()
+    {
+        return code;
+    }
+
+    /**
+     * Returns the augmented (full) code of this sample.
+     */
+    @JsonIgnore
+    public String getAugmentedCode()
+    {
+        if (code == null)
+        {
+            return null;
+        }
+        if (spaceCode != null)
+        {
+            return "/" + spaceCode + "/" + code;
+        } else
+        {
+            return "/" + code;
+        }
+    }
+
+    //
+    // JSON-RPC
+    //
+
+    private SampleIdentifier()
+    {
+    }
+
+    private void setSpaceCode(String spaceCode)
+    {
+        this.spaceCode = spaceCode;
+    }
+
+    private void setCode(String code)
+    {
+        this.code = code;
+    }
+
+    private void setPermId(String permId)
+    {
+        this.permId = permId;
+    }
+
+    private void setDatabaseId(Long databaseId)
+    {
+        this.databaseId = databaseId;
+    }
+
+    @Override
+    public final boolean equals(final Object obj)
+    {
+        return EqualsBuilder.reflectionEquals(this, obj);
+    }
+
+    @Override
+    public final int hashCode()
+    {
+        return HashCodeBuilder.reflectionHashCode(this);
+    }
+
+    @Override
+    public String toString()
+    {
+        return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE);
+    }
+}
-- 
GitLab