diff options
Diffstat (limited to 'openecomp-be/tools/zusammen-tools/src/main/java/org/openecomp/core/tools/commands/AddContributorCommand.java')
-rw-r--r-- | openecomp-be/tools/zusammen-tools/src/main/java/org/openecomp/core/tools/commands/AddContributorCommand.java | 104 |
1 files changed, 104 insertions, 0 deletions
diff --git a/openecomp-be/tools/zusammen-tools/src/main/java/org/openecomp/core/tools/commands/AddContributorCommand.java b/openecomp-be/tools/zusammen-tools/src/main/java/org/openecomp/core/tools/commands/AddContributorCommand.java new file mode 100644 index 0000000000..caa688d007 --- /dev/null +++ b/openecomp-be/tools/zusammen-tools/src/main/java/org/openecomp/core/tools/commands/AddContributorCommand.java @@ -0,0 +1,104 @@ +package org.openecomp.core.tools.commands; + +import org.openecomp.core.tools.concurrent.ItemAddContributorsTask; +import org.openecomp.core.tools.exceptions.CommandExecutionRuntimeException; +import org.openecomp.core.tools.store.ItemHandler; +import org.openecomp.core.tools.store.NotificationHandler; +import org.openecomp.core.tools.store.PermissionHandler; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.Collection; +import java.util.List; +import java.util.concurrent.Callable; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +public class AddContributorCommand { + + + private static final int DEFAULT_THREAD_NUMBER = 8; + private static final String ERROR_TRYING_TO_READ_FILE = "Error while trying to read item list"; + private static final String COMMAND_ADD_CONTRIBUTOR_FAILED = + "Command AddContributor execution failed."; + + private AddContributorCommand() { + // it's a utility class, prevent instantiation + } + + public static void add(String itemListPath, String userListPath) { + + List<String> itemList; + try { + itemList = getItemList(itemListPath); + } catch (IOException e) { + throw new CommandExecutionRuntimeException(ERROR_TRYING_TO_READ_FILE + + "from:" + itemListPath, e); + } + List<String> userList; + try { + userList = load(userListPath).collect(Collectors.toList()); + } catch (IOException e) { + throw new CommandExecutionRuntimeException(ERROR_TRYING_TO_READ_FILE + + "from:" + userListPath, e); + } + + List<ItemAddContributorsTask> tasks = + itemList.stream().map(itemid -> createTask(itemid, userList)).collect(Collectors.toList()); + + ExecutorService executor = null; + + try { + executor = Executors.newFixedThreadPool(DEFAULT_THREAD_NUMBER); + executeAllTasks(executor, tasks); + } catch (InterruptedException e) { + throw new CommandExecutionRuntimeException(COMMAND_ADD_CONTRIBUTOR_FAILED, e); + } finally { + if (executor != null) { + executor.shutdownNow(); + } + } + } + + private static List<String> getItemList(String itemListPath) throws IOException { + List<String> itemList; + if (itemListPath != null) { + itemList = load(itemListPath).collect(Collectors.toList()); + } else { + itemList = new ItemHandler().getItemList(); + } + + return itemList; + } + + private static void executeAllTasks(ExecutorService executor, + Collection<? extends Callable<String>> tasks) + throws InterruptedException { + List<Future<String>> futureTasks; + futureTasks = executor.invokeAll(tasks); + boolean isThreadOpen = true; + while (isThreadOpen) { + isThreadOpen = futureTasks.stream().anyMatch(future -> !future.isDone()); + + } + } + + + + private static ItemAddContributorsTask createTask(String itemId, List<String> users) { + return new ItemAddContributorsTask(new PermissionHandler(), new NotificationHandler(), + itemId, users); + } + + private static Stream<String> load(String filePath) + throws IOException { + return Files.lines(Paths.get(filePath)); + + } + + +} |