From 62403a7ff9bf55ee6157fbfcff209c4f1384a95e Mon Sep 17 00:00:00 2001
From: felmer <felmer>
Date: Mon, 11 Jun 2012 09:19:44 +0000
Subject: [PATCH] SP-122, BIS-81: allow to have your own enabled technologies

SVN: 25636
---
 .../SetEnableTechnologiesVariableAction.java  | 44 ++++++++++++++++++-
 .../cisd/openbis/installer/izpack/Utils.java  |  7 ++-
 ...tEnableTechnologiesVariableActionTest.java | 27 ++++++++++--
 3 files changed, 71 insertions(+), 7 deletions(-)

diff --git a/installation/source/java/ch/systemsx/cisd/openbis/installer/izpack/SetEnableTechnologiesVariableAction.java b/installation/source/java/ch/systemsx/cisd/openbis/installer/izpack/SetEnableTechnologiesVariableAction.java
index 2900beabc31..cc364e8797e 100644
--- a/installation/source/java/ch/systemsx/cisd/openbis/installer/izpack/SetEnableTechnologiesVariableAction.java
+++ b/installation/source/java/ch/systemsx/cisd/openbis/installer/izpack/SetEnableTechnologiesVariableAction.java
@@ -19,6 +19,11 @@ package ch.systemsx.cisd.openbis.installer.izpack;
 import static ch.systemsx.cisd.openbis.installer.izpack.SetTechnologyCheckBoxesAction.ENABLED_TECHNOLOGIES_KEY;
 
 import java.io.File;
+import java.util.HashSet;
+import java.util.Properties;
+import java.util.Set;
+
+import org.apache.commons.lang.StringUtils;
 
 import com.izforge.izpack.api.data.AutomatedInstallData;
 import com.izforge.izpack.api.data.PanelActionConfiguration;
@@ -68,10 +73,10 @@ public class SetEnableTechnologiesVariableAction implements PanelAction
         if (isFirstTimeInstallation == false)
         {
             File asConfigFile = new File(installDir, Utils.AS_PATH + Utils.SERVICE_PROPERTIES_PATH);
-            Utils.updateOrAppendProperty(asConfigFile, ENABLED_TECHNOLOGIES_KEY, newTechnologyList);
+            modifyPropertyEnabledTechnologies(asConfigFile, data);
             File dssConfigFile =
                     new File(installDir, Utils.DSS_PATH + Utils.SERVICE_PROPERTIES_PATH);
-            Utils.updateOrAppendProperty(dssConfigFile, ENABLED_TECHNOLOGIES_KEY, newTechnologyList);
+            modifyPropertyEnabledTechnologies(dssConfigFile, data);
         }
     }
 
@@ -89,4 +94,39 @@ public class SetEnableTechnologiesVariableAction implements PanelAction
         return builder.toString();
     }
     
+    private void modifyPropertyEnabledTechnologies(File configFile, AutomatedInstallData data)
+    {
+        Set<String> allTechnologies = new HashSet<String>();
+        CommaSeparatedListBuilder builder = new CommaSeparatedListBuilder();
+        for (String technology : GlobalInstallationContext.TECHNOLOGIES)
+        {
+            String lowerCasedTechnology = technology.toLowerCase();
+            allTechnologies.add(lowerCasedTechnology);
+            String technologyFlag = data.getVariable(technology);
+            if (Boolean.TRUE.toString().equalsIgnoreCase(technologyFlag))
+            {
+                builder.append(lowerCasedTechnology);
+            }
+        }
+        Properties properties = Utils.tryToGetProperties(configFile);
+        if (properties != null)
+        {
+            String property = properties.getProperty(ENABLED_TECHNOLOGIES_KEY);
+            if (StringUtils.isNotBlank(property))
+            {
+                String[] splittedProperty = property.split(",");
+                for (String term : splittedProperty)
+                {
+                    String trimmedTerm = term.trim();
+                    if (allTechnologies.contains(trimmedTerm) == false)
+                    {
+                        builder.append(trimmedTerm);
+                    }
+                }
+            }
+        }
+        Utils.updateOrAppendProperty(configFile, ENABLED_TECHNOLOGIES_KEY, builder.toString());
+    }
+
+    
 }
diff --git a/installation/source/java/ch/systemsx/cisd/openbis/installer/izpack/Utils.java b/installation/source/java/ch/systemsx/cisd/openbis/installer/izpack/Utils.java
index 67db2c2a8d6..58143e61c9e 100644
--- a/installation/source/java/ch/systemsx/cisd/openbis/installer/izpack/Utils.java
+++ b/installation/source/java/ch/systemsx/cisd/openbis/installer/izpack/Utils.java
@@ -98,12 +98,17 @@ class Utils
     }
     
     private static Properties tryToGetServiceProperties(File installDir, String relativePath)
+    {
+        return tryToGetProperties(new File(installDir, relativePath));
+    }
+
+    public static Properties tryToGetProperties(File configFile)
     {
         Properties properties = new Properties();
         FileReader fileReader = null;
         try
         {
-            fileReader = new FileReader(new File(installDir, relativePath));
+            fileReader = new FileReader(configFile);
             properties.load(fileReader);
             return properties;
         } catch (Exception ex)
diff --git a/installation/sourceTest/java/ch/systemsx/cisd/openbis/installer/izpack/SetEnableTechnologiesVariableActionTest.java b/installation/sourceTest/java/ch/systemsx/cisd/openbis/installer/izpack/SetEnableTechnologiesVariableActionTest.java
index c8103b132e4..9c7bc8f010d 100644
--- a/installation/sourceTest/java/ch/systemsx/cisd/openbis/installer/izpack/SetEnableTechnologiesVariableActionTest.java
+++ b/installation/sourceTest/java/ch/systemsx/cisd/openbis/installer/izpack/SetEnableTechnologiesVariableActionTest.java
@@ -119,20 +119,39 @@ public class SetEnableTechnologiesVariableActionTest extends AbstractFileSystemT
     }
 
     @Test
-    public void testUpdateInstallationWithoutCorePluginsFolder()
+    public void testUpdateInstallationWithOtherEnabledTechnologiesInAs()
     {
         FileUtilities.deleteRecursively(corePluginsFolder);
         FileUtilities.writeToFile(configFile, "abc = 123\n" + ENABLED_TECHNOLOGIES_KEY
-                + "=proteomics");
+                + "=proteomics, my-tech");
         Properties variables = new Properties();
         variables.setProperty(TECHNOLOGY_PROTEOMICS, "true");
         variables.setProperty(TECHNOLOGY_SCREENING, "false");
 
         updateEnabledTechnologyProperties(variables, false);
 
-        assertEquals("[abc = 123, " + ENABLED_TECHNOLOGIES_KEY + "=proteomics]", FileUtilities
+        assertEquals("[abc = 123, " + ENABLED_TECHNOLOGIES_KEY + "=proteomics, my-tech]",
+                FileUtilities.loadToStringList(configFile).toString());
+        assertEquals("[, " + ENABLED_TECHNOLOGIES_KEY + " = proteomics]", FileUtilities
+                .loadToStringList(dssConfigFile).toString());
+    }
+    
+    @Test
+    public void testUpdateInstallationWithOtherEnabledTechnologiesInDss()
+    {
+        FileUtilities.writeToFile(configFile, ENABLED_TECHNOLOGIES_KEY
+                + "=proteomics");
+        FileUtilities.writeToFile(dssConfigFile, "abc = 123\n" + ENABLED_TECHNOLOGIES_KEY
+                + " =my-tech,screening");
+        Properties variables = new Properties();
+        variables.setProperty(TECHNOLOGY_PROTEOMICS, "true");
+        variables.setProperty(TECHNOLOGY_SCREENING, "false");
+        
+        updateEnabledTechnologyProperties(variables, false);
+        
+        assertEquals("[" + ENABLED_TECHNOLOGIES_KEY + "=proteomics]", FileUtilities
                 .loadToStringList(configFile).toString());
-        assertEquals("[, " + ENABLED_TECHNOLOGIES_KEY + " = proteomics]",
+        assertEquals("[abc = 123, " + ENABLED_TECHNOLOGIES_KEY + " = proteomics, my-tech]",
                 FileUtilities.loadToStringList(dssConfigFile).toString());
     }
     
-- 
GitLab