From 2ece0eccb2fcc8d1b0ee218e9ef43d1bfc0134db Mon Sep 17 00:00:00 2001
From: felmer <felmer>
Date: Wed, 17 Dec 2008 13:22:29 +0000
Subject: [PATCH] LMS-650 Introducing UELMethodWithParameters. Fixing a bug
 which reveals in Internet Explorer.

SVN: 9430
---
 .../ui/sample/SampleBrowserGrid.java          | 23 ++-----
 .../util/URLMethodWithParameters.java         | 52 ++++++++++++++++
 .../ExperimentAttachmentsSection.java         | 60 ++++++++-----------
 3 files changed, 83 insertions(+), 52 deletions(-)
 create mode 100644 openbis/source/java/ch/systemsx/cisd/openbis/generic/client/web/client/application/util/URLMethodWithParameters.java

diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/generic/client/web/client/application/ui/sample/SampleBrowserGrid.java b/openbis/source/java/ch/systemsx/cisd/openbis/generic/client/web/client/application/ui/sample/SampleBrowserGrid.java
index cadd6cc196a..236c6ac939e 100644
--- a/openbis/source/java/ch/systemsx/cisd/openbis/generic/client/web/client/application/ui/sample/SampleBrowserGrid.java
+++ b/openbis/source/java/ch/systemsx/cisd/openbis/generic/client/web/client/application/ui/sample/SampleBrowserGrid.java
@@ -55,6 +55,7 @@ import ch.systemsx.cisd.openbis.generic.client.web.client.application.ui.sample.
 import ch.systemsx.cisd.openbis.generic.client.web.client.application.ui.widget.PagingToolBarWithoutRefresh;
 import ch.systemsx.cisd.openbis.generic.client.web.client.application.util.GWTUtils;
 import ch.systemsx.cisd.openbis.generic.client.web.client.application.util.GxtTranslator;
+import ch.systemsx.cisd.openbis.generic.client.web.client.application.util.URLMethodWithParameters;
 import ch.systemsx.cisd.openbis.generic.client.web.client.dto.IColumnDefinition;
 import ch.systemsx.cisd.openbis.generic.client.web.client.dto.ListSampleCriteria;
 import ch.systemsx.cisd.openbis.generic.client.web.client.dto.ResultSet;
@@ -413,24 +414,12 @@ public final class SampleBrowserGrid extends LayoutContainer
         @Override
         protected void process(String exportDataKey)
         {
-            String url = createExportWindowURL(exportDataKey);
-            openURL(url);
+            URLMethodWithParameters methodWithParameters =
+                    new URLMethodWithParameters(GenericConstants.FILE_EXPORTER_DOWNLOAD_SERVLET_NAME);
+            methodWithParameters.addParameter(GenericConstants.EXPORT_CRITERIA_KEY_PARAMETER,
+                    exportDataKey);
+            Window.open(methodWithParameters.toString(), "", null);
         }
-
-        private void openURL(String url)
-        {
-            Window.open(url, "", null);
-        }
-    }
-
-    private static String createExportWindowURL(String exportDataKey)
-    {
-        final StringBuffer sb = new StringBuffer();
-        sb.append(GenericConstants.FILE_EXPORTER_DOWNLOAD_SERVLET_NAME);
-        sb.append("?");
-        sb.append(GenericConstants.EXPORT_CRITERIA_KEY_PARAMETER).append("=");
-        sb.append(exportDataKey);
-        return sb.toString();
     }
 
     protected boolean isExportEnabled()
diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/generic/client/web/client/application/util/URLMethodWithParameters.java b/openbis/source/java/ch/systemsx/cisd/openbis/generic/client/web/client/application/util/URLMethodWithParameters.java
new file mode 100644
index 00000000000..82f24747d93
--- /dev/null
+++ b/openbis/source/java/ch/systemsx/cisd/openbis/generic/client/web/client/application/util/URLMethodWithParameters.java
@@ -0,0 +1,52 @@
+/*
+ * Copyright 2008 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.client.web.client.application.util;
+
+/**
+ * 
+ *
+ * @author Franz-Josef Elmer
+ */
+public class URLMethodWithParameters
+{
+    private final StringBuilder builder;
+    
+    private char delim = '?';
+    
+    public URLMethodWithParameters(String methodName)
+    {
+        builder = new StringBuilder(methodName);
+    }
+    
+    public void addParameter(String parameterName, Object value)
+    {
+        builder.append(delim).append(parameterName).append('=');
+        if (value != null)
+        {
+            builder.append(value);
+        }
+        delim = '&';
+    }
+
+    @Override
+    public String toString()
+    {
+        return builder.toString();
+    }
+    
+    
+}
diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/plugin/generic/client/web/client/application/experiment/ExperimentAttachmentsSection.java b/openbis/source/java/ch/systemsx/cisd/openbis/plugin/generic/client/web/client/application/experiment/ExperimentAttachmentsSection.java
index e1f5224b08d..dc8bcc49acf 100644
--- a/openbis/source/java/ch/systemsx/cisd/openbis/plugin/generic/client/web/client/application/experiment/ExperimentAttachmentsSection.java
+++ b/openbis/source/java/ch/systemsx/cisd/openbis/plugin/generic/client/web/client/application/experiment/ExperimentAttachmentsSection.java
@@ -49,8 +49,12 @@ import ch.systemsx.cisd.openbis.generic.client.web.client.application.model.Atta
 import ch.systemsx.cisd.openbis.generic.client.web.client.application.model.ModelDataPropertyNames;
 import ch.systemsx.cisd.openbis.generic.client.web.client.application.ui.ColumnConfigFactory;
 import ch.systemsx.cisd.openbis.generic.client.web.client.application.util.IMessageProvider;
+import ch.systemsx.cisd.openbis.generic.client.web.client.application.util.URLMethodWithParameters;
 import ch.systemsx.cisd.openbis.generic.client.web.client.dto.Attachment;
+import ch.systemsx.cisd.openbis.generic.client.web.client.dto.DatabaseInstance;
 import ch.systemsx.cisd.openbis.generic.client.web.client.dto.Experiment;
+import ch.systemsx.cisd.openbis.generic.client.web.client.dto.Group;
+import ch.systemsx.cisd.openbis.generic.client.web.client.dto.Project;
 
 /**
  * {@link SectionPanel} containing experiment attachments.
@@ -116,7 +120,7 @@ public class ExperimentAttachmentsSection extends SectionPanel
 
                     if (ModelDataPropertyNames.FILE_NAME.equals(column))
                     {
-                        Window.open(createURL(version, fileName, experiment), "Download file", "");
+                        downloadAttachment(fileName, version);
                     } else if (ModelDataPropertyNames.VERSIONS.equals(column))
                     {
                         List<Attachment> versions = cast(selectedItem);
@@ -185,7 +189,7 @@ public class ExperimentAttachmentsSection extends SectionPanel
                         Attachment selectedAttachment =
                                 (Attachment) selectedItem.get(ModelDataPropertyNames.OBJECT);
                         int version = selectedAttachment.getVersion();
-                        Window.open(createURL(version, fileName, experiment), "Download file", "");
+                        downloadAttachment(fileName, version);
                     }
                     attachmentGrid.getSelectionModel().deselectAll();
                 }
@@ -196,40 +200,26 @@ public class ExperimentAttachmentsSection extends SectionPanel
         return panel;
     }
 
-    final static String createURL(final int version, final String fileName, final Experiment exp)
+    private void downloadAttachment(String fileName, int version)
     {
-        final StringBuffer buffer = new StringBuffer();
-        final String projectCode = exp.getProject().getCode();
-        final String groupCode = exp.getProject().getGroup().getCode();
-        final String experimentCode = exp.getCode();
-        final String instanceCode = exp.getProject().getGroup().getInstance().getCode();
-        buffer.append(GenericConstants.EXPERIMENT_ATTACHMENT_DOWNLOAD_SERVLET_NAME)
-
-        .append("?")
-
-        .append(GenericConstants.VERSION_PARAMETER).append("=").append(version)
-
-        .append("&")
-
-        .append(GenericConstants.FILE_NAME_PARAMETER).append("=").append(fileName)
-
-        .append("&")
-
-        .append(GenericConstants.PROJECT_PARAMETER).append("=").append(projectCode)
-
-        .append("&")
-
-        .append(GenericConstants.GROUP_PARAMETER).append("=").append(groupCode)
-
-        .append("&")
-
-        .append(GenericConstants.EXPERIMENT_PARAMETER).append("=").append(experimentCode)
-
-        .append("&")
-
-        .append(GenericConstants.DATABASE_PARAMETER).append("=").append(instanceCode);
-
-        return buffer.toString();
+        Window.open(createURL(version, fileName, experiment), "", null);
+    }
+    
+    private final static String createURL(final int version, final String fileName, final Experiment exp)
+    {
+        Project project = exp.getProject();
+        Group group = project.getGroup();
+        DatabaseInstance instance = group.getInstance();
+        URLMethodWithParameters methodWithParameters =
+                new URLMethodWithParameters(
+                        GenericConstants.EXPERIMENT_ATTACHMENT_DOWNLOAD_SERVLET_NAME);
+        methodWithParameters.addParameter(GenericConstants.VERSION_PARAMETER, version);
+        methodWithParameters.addParameter(GenericConstants.FILE_NAME_PARAMETER, fileName);
+        methodWithParameters.addParameter(GenericConstants.PROJECT_PARAMETER, project.getCode());
+        methodWithParameters.addParameter(GenericConstants.GROUP_PARAMETER, group.getCode());
+        methodWithParameters.addParameter(GenericConstants.EXPERIMENT_PARAMETER, exp.getCode());
+        methodWithParameters.addParameter(GenericConstants.DATABASE_PARAMETER, instance.getCode());
+        return methodWithParameters.toString();
     }
 
     private List<ColumnConfig> defineAttachmentColumns()
-- 
GitLab