From f1caddfaca03faec67eb500b387900dcf953e1b2 Mon Sep 17 00:00:00 2001
From: ribeaudc <ribeaudc>
Date: Mon, 29 Sep 2008 13:23:40 +0000
Subject: [PATCH] remove: - TODO regarding 'AbstractAttachmentPE' parent field
 (which is in fact an ExperimentPE).

SVN: 8495
---
 .../shared/dto/AbstractAttachmentPE.java      | 24 +++---
 .../generic/shared/dto/ExperimentPE.java      | 78 ++++++++++++++++---
 .../generic/shared/dto/FileAttachmentPE.java  |  2 +-
 .../shared/dto/ProcessingInstructionDTO.java  | 11 ++-
 4 files changed, 88 insertions(+), 27 deletions(-)

diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/generic/shared/dto/AbstractAttachmentPE.java b/openbis/source/java/ch/systemsx/cisd/openbis/generic/shared/dto/AbstractAttachmentPE.java
index d6392da30c8..c7b6c61e4d9 100644
--- a/openbis/source/java/ch/systemsx/cisd/openbis/generic/shared/dto/AbstractAttachmentPE.java
+++ b/openbis/source/java/ch/systemsx/cisd/openbis/generic/shared/dto/AbstractAttachmentPE.java
@@ -19,9 +19,12 @@ package ch.systemsx.cisd.openbis.generic.shared.dto;
 import java.io.Serializable;
 
 import javax.persistence.Column;
+import javax.persistence.FetchType;
 import javax.persistence.GeneratedValue;
 import javax.persistence.GenerationType;
 import javax.persistence.Id;
+import javax.persistence.JoinColumn;
+import javax.persistence.ManyToOne;
 import javax.persistence.MappedSuperclass;
 import javax.persistence.SequenceGenerator;
 
@@ -52,12 +55,10 @@ public class AbstractAttachmentPE extends HibernateAbstractRegistratrationHolder
 
     transient private Long id;
 
-    // TODO 2008-08-21, Christian Ribeaud: Put the experiment here instead of the id once it has
-    // been hibernated.
     /**
-     * Id of the parent (e.g. an experiment) to which this attachment belongs.
+     * Parent (e.g. an experiment) to which this attachment belongs.
      */
-    transient private Long parentId;
+    private ExperimentPE parent;
 
     /**
      * Returns the file name of the property or <code>null</code>.
@@ -100,16 +101,17 @@ public class AbstractAttachmentPE extends HibernateAbstractRegistratrationHolder
         this.id = id;
     }
 
-    @Column(name = ColumnNames.EXPERIMENT_COLUMN)
+    @ManyToOne(fetch = FetchType.LAZY)
     @NotNull(message = ValidationMessages.EXPERIMENT_NOT_NULL_MESSAGE)
-    public Long getParentId()
+    @JoinColumn(name = ColumnNames.EXPERIMENT_COLUMN, updatable = false)
+    public ExperimentPE getParent()
     {
-        return parentId;
+        return parent;
     }
 
-    public void setParentId(final Long parentId)
+    public void setParent(final ExperimentPE parent)
     {
-        this.parentId = parentId;
+        this.parent = parent;
     }
 
     //
@@ -131,7 +133,7 @@ public class AbstractAttachmentPE extends HibernateAbstractRegistratrationHolder
         final EqualsBuilder builder = new EqualsBuilder();
         builder.append(fileName, that.fileName);
         builder.append(version, that.version);
-        builder.append(parentId, that.parentId);
+        builder.append(parent, that.parent);
         return builder.isEquals();
     }
 
@@ -141,7 +143,7 @@ public class AbstractAttachmentPE extends HibernateAbstractRegistratrationHolder
         final HashCodeBuilder builder = new HashCodeBuilder();
         builder.append(fileName);
         builder.append(version);
-        builder.append(parentId);
+        builder.append(parent);
         return builder.toHashCode();
     }
 
diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/generic/shared/dto/ExperimentPE.java b/openbis/source/java/ch/systemsx/cisd/openbis/generic/shared/dto/ExperimentPE.java
index ca3301a1efa..fdc27b8f1db 100644
--- a/openbis/source/java/ch/systemsx/cisd/openbis/generic/shared/dto/ExperimentPE.java
+++ b/openbis/source/java/ch/systemsx/cisd/openbis/generic/shared/dto/ExperimentPE.java
@@ -17,6 +17,7 @@
 package ch.systemsx.cisd.openbis.generic.shared.dto;
 
 import java.util.Date;
+import java.util.Iterator;
 import java.util.List;
 
 import javax.persistence.CascadeType;
@@ -43,6 +44,7 @@ import org.hibernate.validator.Length;
 import org.hibernate.validator.NotNull;
 import org.hibernate.validator.Pattern;
 
+import ch.rinn.restrictions.Private;
 import ch.systemsx.cisd.common.utilities.ModifiedShortPrefixToStringStyle;
 import ch.systemsx.cisd.openbis.generic.shared.GenericSharedConstants;
 
@@ -62,6 +64,14 @@ public class ExperimentPE extends HibernateAbstractRegistratrationHolder impleme
 
     public static final Object EMPTY_ARRAY = new ExperimentPE[0];
 
+    public static final char HIDDEN_EXPERIMENT_PROPERTY_PREFIX_CHARACTER = '$';
+
+    public static final String HIDDEN_EXPERIMENT_PROPERTY_PREFIX =
+            Character.toString(HIDDEN_EXPERIMENT_PROPERTY_PREFIX_CHARACTER);
+
+    public static final String HIDDEN_EXPERIMENT_PROPERTY_PREFIX2 =
+            HIDDEN_EXPERIMENT_PROPERTY_PREFIX + HIDDEN_EXPERIMENT_PROPERTY_PREFIX;
+
     private transient Long id;
 
     private String code;
@@ -87,11 +97,6 @@ public class ExperimentPE extends HibernateAbstractRegistratrationHolder impleme
 
     private Date lastDataSetDate;
 
-    public ExperimentPE()
-    {
-        super();
-    }
-
     @Id
     @SequenceGenerator(name = SequenceNames.EXPERIMENT_SEQUENCE, sequenceName = SequenceNames.EXPERIMENT_SEQUENCE, allocationSize = 1)
     @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = SequenceNames.EXPERIMENT_SEQUENCE)
@@ -203,18 +208,46 @@ public class ExperimentPE extends HibernateAbstractRegistratrationHolder impleme
         setExperimentProperties(properties);
     }
 
-    @OneToMany(fetch = FetchType.LAZY)
-    @JoinColumn(name = ColumnNames.EXPERIMENT_COLUMN, updatable = false)
-    public List<AttachmentPE> getAttachments()
+    @OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, mappedBy = "parent")
+    @Private
+    public List<AttachmentPE> getExperimentAttachments()
     {
         return attachments;
     }
 
-    public void setAttachments(final List<AttachmentPE> attachments)
+    @Private
+    public void setExperimentAttachments(final List<AttachmentPE> attachments)
     {
         this.attachments = attachments;
     }
 
+    @Transient
+    public final List<AttachmentPE> getAttachments()
+    {
+        final List<AttachmentPE> list = getExperimentAttachments();
+        for (final Iterator<AttachmentPE> iter = list.iterator(); iter.hasNext(); /**/)
+        {
+            final AttachmentPE property = iter.next();
+            final boolean isHiddenFile = isHiddenFile(property.getFileName());
+            if (isHiddenFile)
+            {
+                iter.remove();
+            }
+            unescapeFileName(property);
+        }
+        return list;
+    }
+
+    // Package visibility to avoid bean conversion which will call an uninitialized field.
+    final void setAttachments(final List<AttachmentPE> attachments)
+    {
+        for (AttachmentPE experimentAttachment : attachments)
+        {
+            experimentAttachment.setParent(this);
+        }
+        setExperimentAttachments(attachments);
+    }
+
     @OneToMany(fetch = FetchType.LAZY)
     @JoinColumn(name = ColumnNames.EXPERIMENT_COLUMN, updatable = false)
     public List<ProcedurePE> getProcedures()
@@ -313,4 +346,31 @@ public class ExperimentPE extends HibernateAbstractRegistratrationHolder impleme
         builder.append("invalidation", getInvalidation());
         return builder.toString();
     }
+
+    public final static void unescapeFileName(final AbstractAttachmentPE attachment)
+    {
+        if (attachment != null)
+        {
+            final String fileName = attachment.getFileName();
+            if (fileName != null && fileName.startsWith(HIDDEN_EXPERIMENT_PROPERTY_PREFIX2))
+            {
+                attachment.setFileName(fileName.substring(1));
+            }
+        }
+    }
+
+    public final static boolean isHiddenFile(final String fileName)
+    {
+        return fileName.startsWith(HIDDEN_EXPERIMENT_PROPERTY_PREFIX)
+                && (fileName.length() == 1 || fileName.charAt(1) != HIDDEN_EXPERIMENT_PROPERTY_PREFIX_CHARACTER);
+    }
+
+    public final static String escapeFileName(final String fileName)
+    {
+        if (fileName != null && fileName.startsWith(HIDDEN_EXPERIMENT_PROPERTY_PREFIX))
+        {
+            return HIDDEN_EXPERIMENT_PROPERTY_PREFIX + fileName;
+        }
+        return fileName;
+    }
 }
diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/generic/shared/dto/FileAttachmentPE.java b/openbis/source/java/ch/systemsx/cisd/openbis/generic/shared/dto/FileAttachmentPE.java
index 0d194c4a53a..bf09663e2e8 100644
--- a/openbis/source/java/ch/systemsx/cisd/openbis/generic/shared/dto/FileAttachmentPE.java
+++ b/openbis/source/java/ch/systemsx/cisd/openbis/generic/shared/dto/FileAttachmentPE.java
@@ -48,7 +48,7 @@ public class FileAttachmentPE extends AbstractAttachmentPE
         return value;
     }
 
-    public void setValue(byte[] value)
+    public void setValue(final byte[] value)
     {
         this.value = value;
     }
diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/generic/shared/dto/ProcessingInstructionDTO.java b/openbis/source/java/ch/systemsx/cisd/openbis/generic/shared/dto/ProcessingInstructionDTO.java
index dcac257414c..46ae11dd307 100644
--- a/openbis/source/java/ch/systemsx/cisd/openbis/generic/shared/dto/ProcessingInstructionDTO.java
+++ b/openbis/source/java/ch/systemsx/cisd/openbis/generic/shared/dto/ProcessingInstructionDTO.java
@@ -16,7 +16,6 @@
 
 package ch.systemsx.cisd.openbis.generic.shared.dto;
 
-
 /**
  * Processing instruction needed to process raw data.
  * 
@@ -28,7 +27,7 @@ public class ProcessingInstructionDTO extends AbstractRegistratrationHolder
 
     public static final ProcessingInstructionDTO[] EMPTY_ARRAY = new ProcessingInstructionDTO[0];
 
-    transient private Long experimentID;
+    private ExperimentPE experiment;
 
     private String procedureTypeCode;
 
@@ -43,14 +42,14 @@ public class ProcessingInstructionDTO extends AbstractRegistratrationHolder
         super(null);
     }
 
-    public final Long getExperimentID()
+    public final ExperimentPE getExperiment()
     {
-        return experimentID;
+        return experiment;
     }
 
-    public final void setExperimentID(final Long experimentID)
+    public final void setExperiment(final ExperimentPE experiment)
     {
-        this.experimentID = experimentID;
+        this.experiment = experiment;
     }
 
     public final String getProcedureTypeCode()
-- 
GitLab