From ef632b7eec69d4cce09c45c299a0b0fa0fd69b3c Mon Sep 17 00:00:00 2001
From: jvergara <jvergara@localhost>
Date: Mon, 02 Jun 2008 20:44:47 +0000
Subject: [PATCH] Fix for issue 3311 (import-ldif --templateFile does not work when the server is  running) The fix consists on not assuming that the ldif file path is required to launch an import (the ds-task-import task definition has been modified for this) and allowing to create a task that imports contents based on a  template.

---
 opends/src/server/org/opends/server/tasks/ImportTask.java |   64 +++++++++++++++++++++++++++++++-
 1 files changed, 62 insertions(+), 2 deletions(-)

diff --git a/opends/src/server/org/opends/server/tasks/ImportTask.java b/opends/src/server/org/opends/server/tasks/ImportTask.java
index 76a882b..6a54a24 100644
--- a/opends/src/server/org/opends/server/tasks/ImportTask.java
+++ b/opends/src/server/org/opends/server/tasks/ImportTask.java
@@ -30,8 +30,10 @@
 
 import static org.opends.messages.TaskMessages.*;
 import static org.opends.messages.ToolMessages.*;
+import static org.opends.server.loggers.ErrorLogger.logError;
 import static org.opends.server.loggers.debug.DebugLogger.*;
 import org.opends.server.loggers.debug.DebugTracer;
+import org.opends.server.tools.makeldif.TemplateFile;
 import org.opends.server.types.DebugLogLevel;
 import static org.opends.server.util.StaticUtils.*;
 import static org.opends.server.config.ConfigConstants.*;
@@ -57,11 +59,13 @@
 import org.opends.server.types.ResultCode;
 import org.opends.server.types.SearchFilter;
 
+import java.io.File;
 import java.util.HashSet;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Map;
 import java.util.HashMap;
+import java.util.Random;
 
 /**
  * This class provides an implementation of a Directory Server task that can
@@ -87,6 +91,14 @@
             INFO_IMPORT_ARG_LDIF_FILE.get());
 
     argDisplayMap.put(
+        ATTR_IMPORT_TEMPLATE_FILE,
+        INFO_IMPORT_ARG_TEMPLATE_FILE.get());
+
+    argDisplayMap.put(
+        ATTR_IMPORT_RANDOM_SEED,
+        INFO_IMPORT_ARG_RANDOM_SEED.get());
+
+    argDisplayMap.put(
             ATTR_IMPORT_APPEND,
             INFO_IMPORT_ARG_APPEND.get());
 
@@ -169,6 +181,8 @@
   ArrayList<String>  includeBranchStrings    = null;
   ArrayList<String>  includeFilterStrings    = null;
   ArrayList<String>  ldifFiles               = null;
+  String templateFile = null;
+  int randomSeed = 0;
 
   private LDIFImportConfig importConfig;
 
@@ -209,6 +223,7 @@
     Entry taskEntry = getTaskEntry();
 
     AttributeType typeLdifFile;
+    AttributeType typeTemplateFile;
     AttributeType typeAppend;
     AttributeType typeReplaceExisting;
     AttributeType typeBackendID;
@@ -225,9 +240,12 @@
     AttributeType typeIsCompressed;
     AttributeType typeIsEncrypted;
     AttributeType typeClearBackend;
+    AttributeType typeRandomSeed;
 
     typeLdifFile =
          getAttributeType(ATTR_IMPORT_LDIF_FILE, true);
+    typeTemplateFile =
+         getAttributeType(ATTR_IMPORT_TEMPLATE_FILE, true);
     typeAppend =
          getAttributeType(ATTR_IMPORT_APPEND, true);
     typeReplaceExisting =
@@ -260,12 +278,17 @@
          getAttributeType(ATTR_IMPORT_IS_ENCRYPTED, true);
     typeClearBackend =
          getAttributeType(ATTR_IMPORT_CLEAR_BACKEND, true);
+    typeRandomSeed =
+         getAttributeType(ATTR_IMPORT_RANDOM_SEED, true);
 
     List<Attribute> attrList;
 
     attrList = taskEntry.getAttribute(typeLdifFile);
     ldifFiles = TaskUtils.getMultiValueString(attrList);
 
+    attrList = taskEntry.getAttribute(typeTemplateFile);
+    templateFile = TaskUtils.getSingleValueString(attrList);
+
     attrList = taskEntry.getAttribute(typeAppend);
     append = TaskUtils.getBoolean(attrList, false);
 
@@ -314,6 +337,9 @@
     attrList = taskEntry.getAttribute(typeClearBackend);
     clearBackend = TaskUtils.getBoolean(attrList, false);
 
+    attrList = taskEntry.getAttribute(typeRandomSeed);
+    randomSeed = TaskUtils.getSingleValueInteger(attrList, 0);
+
     // Make sure that either the "includeBranchStrings" argument or the
     // "backendID" argument was provided.
     if(includeBranchStrings.isEmpty() && backendID == null)
@@ -756,8 +782,42 @@
     }
 
     // Create the LDIF import configuration to use when reading the LDIF.
-    ArrayList<String> fileList = new ArrayList<String>(ldifFiles);
-    importConfig = new LDIFImportConfig(fileList);
+    if (templateFile != null)
+    {
+      Random random;
+      try
+      {
+        random = new Random(randomSeed);
+      }
+      catch (Exception e)
+      {
+        random = new Random();
+      }
+
+      String resourcePath = DirectoryServer.getServerRoot() + File.separator +
+                            PATH_MAKELDIF_RESOURCE_DIR;
+      TemplateFile tf = new TemplateFile(resourcePath, random);
+
+      ArrayList<Message> warnings = new ArrayList<Message>();
+      try
+      {
+        tf.parse(templateFile, warnings);
+      }
+      catch (Exception e)
+      {
+        Message message = ERR_LDIFIMPORT_CANNOT_PARSE_TEMPLATE_FILE.get(
+            templateFile, e.getMessage());
+        logError(message);
+        return TaskState.STOPPED_BY_ERROR;
+      }
+
+      importConfig = new LDIFImportConfig(tf);
+    }
+    else
+    {
+      ArrayList<String> fileList = new ArrayList<String>(ldifFiles);
+      importConfig = new LDIFImportConfig(fileList);
+    }
     importConfig.setAppendToExistingData(append);
     importConfig.setReplaceExistingEntries(replaceExisting);
     importConfig.setCompressed(isCompressed);

--
Gitblit v1.10.0