summaryrefslogtreecommitdiffstats
path: root/openecomp-be/tools/zusammen-tools/src/main/java/org/openecomp/core/tools/commands/AddContributorCommand.java
blob: caa688d0075f84243c9016db56017216ff1fd5e1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
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));

  }


}