commit 15c7afbe25d4ade2a5224c86336ae7b1ddf00a83
Author: Vojtěch Danišík <32537562+danisik@users.noreply.github.com>
Date:   Tue Aug 4 17:24:32 2020 +0200

    re #8153 Assembly -> Configuration synchronization

diff --git a/src/main/java/vldc/aswi/domain/Assembly.java b/src/main/java/vldc/aswi/domain/Assembly.java
index 274e43b5..fdb7308d 100644
--- a/src/main/java/vldc/aswi/domain/Assembly.java
+++ b/src/main/java/vldc/aswi/domain/Assembly.java
@@ -39,7 +39,7 @@ public class Assembly extends EntityParent {
     private List<Configuration> configurations;
 
     /** List of parameters, which using this assembly. */
-    @OneToMany(mappedBy = "assembly", fetch = FetchType.EAGER, orphanRemoval = true)
+    @OneToMany(mappedBy = "assembly", fetch = FetchType.EAGER)
     private List<Parameter> parameters;
 
     /**
diff --git a/src/main/java/vldc/aswi/service/parameter/ParameterInConfigurationManager.java b/src/main/java/vldc/aswi/service/parameter/ParameterInConfigurationManager.java
index 146223bf..fd55d7d6 100644
--- a/src/main/java/vldc/aswi/service/parameter/ParameterInConfigurationManager.java
+++ b/src/main/java/vldc/aswi/service/parameter/ParameterInConfigurationManager.java
@@ -1,7 +1,9 @@
 package vldc.aswi.service.parameter;
 
+import vldc.aswi.domain.parameter.Parameter;
 import vldc.aswi.domain.parameter.ParameterInConfiguration;
 
+import java.util.ArrayList;
 import java.util.List;
 
 /**
@@ -21,4 +23,16 @@ public interface ParameterInConfigurationManager {
      * @return List of deleted parameters in configuration.
      */
     boolean deleteParametersInConfiguration(Long id, boolean configurationUsed);
+
+    /**
+     * Add newly created parameters to all configurations, which are created from current assembly
+     * @param assemblyId - ID of current assembly.
+     */
+    void addParameterIntoConfigurations(Long assemblyId, ArrayList<Parameter> parameters);
+
+    /**
+     * Update all parameter in configurations orders.
+     * @param parameters - list of parameters for current assembly.
+     */
+    void updateParameterInConfigurationsOrder(List<Parameter> parameters);
 }
diff --git a/src/main/java/vldc/aswi/service/parameter/ParameterInConfigurationManagerImpl.java b/src/main/java/vldc/aswi/service/parameter/ParameterInConfigurationManagerImpl.java
index cc8a2254..47bca3e8 100644
--- a/src/main/java/vldc/aswi/service/parameter/ParameterInConfigurationManagerImpl.java
+++ b/src/main/java/vldc/aswi/service/parameter/ParameterInConfigurationManagerImpl.java
@@ -7,8 +7,12 @@ import org.springframework.context.event.EventListener;
 import org.springframework.core.annotation.Order;
 import org.springframework.dao.InvalidDataAccessResourceUsageException;
 import org.springframework.stereotype.Service;
+import vldc.aswi.dao.AssemblyRepository;
 import vldc.aswi.dao.parameter.ParameterInConfigurationRepository;
+import vldc.aswi.domain.Assembly;
+import vldc.aswi.domain.Configuration;
 import vldc.aswi.domain.Function;
+import vldc.aswi.domain.parameter.Parameter;
 import vldc.aswi.domain.parameter.ParameterInConfiguration;
 
 import javax.transaction.Transactional;
@@ -27,6 +31,10 @@ public class ParameterInConfigurationManagerImpl implements ParameterInConfigura
     @Autowired
     private ParameterInConfigurationRepository parameterInConfigurationRepository;
 
+    /** Autowired assembly repository. */
+    @Autowired
+    private AssemblyRepository assemblyRepository;
+
     /**
      * Initialization setup for parameter in configuration manager.
      * Check if table "Parametr_konfigurace" exists.
@@ -59,6 +67,32 @@ public class ParameterInConfigurationManagerImpl implements ParameterInConfigura
         return retVal;
     }
 
+    /**
+     * Add newly created parameters to all configurations, which are created from current assembly
+     * @param assemblyId - ID of current assembly.
+     */
+    @Override
+    public void addParameterIntoConfigurations(Long assemblyId, ArrayList<Parameter> parameters) {
+
+        Assembly assembly = this.assemblyRepository.getById(assemblyId);
+        List<Configuration> configurations = assembly.getConfigurations();
+
+        for (Parameter parameter : parameters) {
+
+            for (Configuration configuration : configurations) {
+                ParameterInConfiguration parameterInConfiguration = new ParameterInConfiguration();
+
+                parameterInConfiguration.setParameter(parameter);
+                parameterInConfiguration.setConfiguration(configuration);
+                parameterInConfiguration.setColumnName(parameter.getName());
+                // Set temporary parameter order.
+                parameterInConfiguration.setParameterOrder(0);
+
+                this.parameterInConfigurationRepository.save(parameterInConfiguration);
+            }
+        }
+    }
+
     /**
      * Delete all parameters in configuration by configuration id.
      * @param id - ID of parameter / configuration.
@@ -106,4 +140,22 @@ public class ParameterInConfigurationManagerImpl implements ParameterInConfigura
             return false;
         }
     }
+
+    /**
+     * Update all parameter in configurations orders.
+     * @param parameters - list of parameters for current assembly.
+     */
+    @Override
+    public void updateParameterInConfigurationsOrder(List<Parameter> parameters) {
+
+        // Update all parameters in configuration order.
+        for (Parameter parameter : parameters) {
+
+            for (ParameterInConfiguration parameterInConfiguration : this.parameterInConfigurationRepository.getAllByParameterId(parameter.getId())) {
+
+                parameterInConfiguration.setParameterOrder(parameter.getParameterOrder());
+                this.parameterInConfigurationRepository.save(parameterInConfiguration);
+            }
+        }
+    }
 }
diff --git a/src/main/java/vldc/aswi/service/parameter/ParameterManager.java b/src/main/java/vldc/aswi/service/parameter/ParameterManager.java
index a246090b..ae938e44 100644
--- a/src/main/java/vldc/aswi/service/parameter/ParameterManager.java
+++ b/src/main/java/vldc/aswi/service/parameter/ParameterManager.java
@@ -2,6 +2,7 @@ package vldc.aswi.service.parameter;
 
 import vldc.aswi.domain.parameter.Parameter;
 
+import java.util.ArrayList;
 import java.util.List;
 
 /**
@@ -20,7 +21,7 @@ public interface ParameterManager {
      * @param assemblyId - Assembly in which parameter is presented.
      * @param parameterValuesList - Parameter values.
      */
-    void addParameters(Long assemblyId, List<Parameter> parameterValuesList);
+    ArrayList<Parameter> addParameters(Long assemblyId, List<Parameter> parameterValuesList);
 
     /**
      * Update parameter with given values.
diff --git a/src/main/java/vldc/aswi/service/parameter/ParameterManagerImpl.java b/src/main/java/vldc/aswi/service/parameter/ParameterManagerImpl.java
index 4db8ccf0..6a4e6d0d 100644
--- a/src/main/java/vldc/aswi/service/parameter/ParameterManagerImpl.java
+++ b/src/main/java/vldc/aswi/service/parameter/ParameterManagerImpl.java
@@ -102,9 +102,9 @@ public class ParameterManagerImpl implements ParameterManager {
      * @param parameterValuesList - Parameter values.
      */
     @Override
-    public void addParameters(Long assemblyId, List<Parameter> parameterValuesList) {
+    public ArrayList<Parameter> addParameters(Long assemblyId, List<Parameter> parameterValuesList) {
         Assembly assembly = this.assemblyRepository.getById(assemblyId);
-        if (parameterValuesList == null) return;
+        ArrayList<Parameter> newParameters = new ArrayList<>();
 
         for (Parameter parameterValues : parameterValuesList) {
             // Create new parameter and assign him all values.
@@ -159,8 +159,10 @@ public class ParameterManagerImpl implements ParameterManager {
             parameter.setOperators(operatorList);
 
             // Insert parameter into database.
-            this.parameterRepository.save(parameter);
+            Parameter newParameter = this.parameterRepository.save(parameter);
+            newParameters.add(newParameter);
         }
+        return newParameters;
     }
 
     /**
@@ -193,21 +195,34 @@ public class ParameterManagerImpl implements ParameterManager {
             newAssemblyParametersUpdated.retainAll(currentAssemblyParametersUpdated);
 
             // If no parameter has been deleted or added, then updating is unnecessary.
-            if (newAssemblyParametersNew.size() == 0 && currentAssemblyParametersDeleted.size() == 0 && newAssemblyParametersUpdated.size() == 0)
+            if (newAssemblyParametersNew.size() == 0 && currentAssemblyParametersDeleted.size() == 0 && newAssemblyParametersUpdated.size() == 0) {
                 return;
+            }
 
-            // Remove deleted parameters.
-            for (Parameter parameterWithValues : currentAssemblyParametersDeleted) {
-                deleteParameter(parameterWithValues.getId());
+            if (currentAssemblyParametersDeleted.size() > 0) {
+                // Remove deleted parameters.
+                for (Parameter parameterWithValues : currentAssemblyParametersDeleted) {
+                    deleteParameter(parameterWithValues.getId());
+                }
             }
 
-            // Update rest parameters.
-            updateParameters(newAssemblyParametersUpdated);
+            if (newAssemblyParametersUpdated.size() > 0) {
+                // Update rest parameters.
+                updateParameters(newAssemblyParametersUpdated);
+            }
+
+            if (newAssemblyParametersNew.size() > 0) {
+                // Add new parameters.
+                ArrayList<Parameter> newParameters = addParameters(assemblyId, newAssemblyParametersNew);
+
+                // Create new parameters in configuration in all configurations, which are created from current assembly.
+                this.parameterInConfigurationManager.addParameterIntoConfigurations(assemblyId, newParameters);
+            }
 
-            // Add new parameters
-            addParameters(assemblyId, newAssemblyParametersNew);
+            List<Parameter> newParameters = this.parameterRepository.getByAssemblyId(assemblyId);
+            assembly.setParameters(newParameters);
 
-            assembly.setParameters(this.parameterRepository.getByAssemblyId(assemblyId));
+            this.parameterInConfigurationManager.updateParameterInConfigurationsOrder(newParameters);
         }
     }
 
diff --git a/src/main/java/vldc/aswi/web/controller/AssemblyController.java b/src/main/java/vldc/aswi/web/controller/AssemblyController.java
index 909824aa..fa7c848c 100644
--- a/src/main/java/vldc/aswi/web/controller/AssemblyController.java
+++ b/src/main/java/vldc/aswi/web/controller/AssemblyController.java
@@ -175,6 +175,15 @@ public class AssemblyController extends BasicController {
 
         if (generateTable != null)
         {
+            Assembly assembly = this.assemblyManager.getAssemblyById(Long.parseLong(id));
+            newConfiguration.setAssembly(assembly);
+            newConfiguration.setTableName(assembly.getName());
+
+            // If configuration name is empty, then replace it by assembly name.
+            if (newConfiguration.getName().equals("")) {
+                newConfiguration.setName(assembly.getName());
+            }
+
             System.out.println("Generuj tabulku");
             prepareForTable(newConfiguration);
             System.out.println("Generuj tabulku");
@@ -183,6 +192,7 @@ public class AssemblyController extends BasicController {
             modelMap.addAttribute("contingencyTableRows", this.sqlQueryManager.getContingencyTableRow(newConfiguration));
             Comparator<ParameterInConfiguration> comparator = Comparator.comparingInt(o -> o.getParameter().getParameterOrder());
             initializeFields(newConfiguration);
+
             modelMap.addAttribute("configuration", newConfiguration);
             modelMap.addAttribute("comparator", comparator);
             modelMap.addAttribute("formAction", "/assembly?assemblyID=" + newConfiguration.getAssembly().getId());
@@ -310,6 +320,7 @@ public class AssemblyController extends BasicController {
 
             // TODO: chybí defaultValue.
             Long assemblyID = this.assemblyManager.updateAssembly(assembly);
+
             this.parameterManager.updateParameters(assemblyID, assembly.getParameters());
             Assembly updatedAssembly = this.assemblyManager.getAssemblyById(assemblyID);
 
