From fd66be4eb43f2f6bca3073e0cc2d76d131762800 Mon Sep 17 00:00:00 2001
From: Gaetan Boismal <gaetan.boismal@forgerock.com>
Date: Mon, 07 Nov 2016 10:05:36 +0000
Subject: [PATCH] OPENDJ-3454 makeldif: Fix multiple branches support

---
 opendj-core/src/main/java/org/forgerock/opendj/ldif/TemplateFile.java |   28 ++++++++++++++++++++--------
 1 files changed, 20 insertions(+), 8 deletions(-)

diff --git a/opendj-core/src/main/java/org/forgerock/opendj/ldif/TemplateFile.java b/opendj-core/src/main/java/org/forgerock/opendj/ldif/TemplateFile.java
index 6dd3d11..949ced1 100644
--- a/opendj-core/src/main/java/org/forgerock/opendj/ldif/TemplateFile.java
+++ b/opendj-core/src/main/java/org/forgerock/opendj/ldif/TemplateFile.java
@@ -1409,23 +1409,35 @@
         /**
          * Performs any necessary processing to ensure that the branch
          * initialization is completed. In particular, it should make sure that
-         * all referenced subordinate templates actually exist in the template
-         * file.
+         * all referenced subordinate templates actually exist in the template file.
          */
-        private void completeBranchInitialization(final Map<String, Template> templates,
-                            boolean generateBranches) throws DecodeException {
+        private void completeBranchInitialization(final Map<String, Template> templates, boolean generateBranches)
+                throws DecodeException {
             subordinateTemplates = new ArrayList<>();
-            for (int i = 0; i < subordinateTemplateNames.size(); i++) {
-                subordinateTemplates.add(templates.get(subordinateTemplateNames.get(i).toLowerCase()));
-                if (subordinateTemplates.get(i) == null) {
+            for (final String templateName : subordinateTemplateNames) {
+                final Template refTemplate = templates.get(templateName.toLowerCase());
+                if (refTemplate == null) {
                     throw DecodeException.fatalError(ERR_ENTRY_GENERATOR_UNDEFINED_BRANCH_SUBORDINATE.get(
-                            branchDN.toString(), subordinateTemplateNames.get(i)));
+                            branchDN.toString(), templateName));
                 }
+                subordinateTemplates.add(copyTemplate(templates, refTemplate));
             }
 
             nextEntry = buildBranchEntry(generateBranches);
         }
 
+        private Template copyTemplate(final Map<String, Template> allTemplates, final Template templateToCopy)
+                throws DecodeException {
+            final Template template =  new Template(templateToCopy.templateFile,
+                                                    templateToCopy.name,
+                                                    templateToCopy.rdnAttributes,
+                                                    templateToCopy.subTemplateNames,
+                                                    templateToCopy.numEntriesPerTemplate,
+                                                    templateToCopy.templateLines);
+            template.completeTemplateInitialization(allTemplates);
+            return template;
+        }
+
         DN getBranchDN() {
             return branchDN;
         }

--
Gitblit v1.10.0