Added oparent to sdc main
[sdc.git] / openecomp-be / tools / zusammen-tools / src / main / java / org / openecomp / core / tools / concurrent / ItemAddContributorsTask.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 - 2019 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.core.tools.concurrent;
22
23 import org.openecomp.core.tools.store.NotificationHandler;
24 import org.openecomp.core.tools.store.PermissionHandler;
25
26 import java.util.ArrayList;
27 import java.util.List;
28 import java.util.Optional;
29 import java.util.concurrent.Callable;
30
31
32 public class ItemAddContributorsTask implements Callable<String> {
33
34   private static final String CONTRIBUTOR = "Contributor";
35   private static final String SUCCESSFUL_RETURN_MESSAGE = "Users added successfully as " +
36       "contributors to item id:%s.";
37   private final String itemId;
38   private final List<String> users;
39   private final PermissionHandler permissionHandler;
40   private final NotificationHandler notificationHandler;
41
42   public ItemAddContributorsTask(PermissionHandler permissionHandler, NotificationHandler
43       notificationHandler, String itemId, List<String> users) {
44     this.itemId = itemId.trim();
45     this.users = new ArrayList<>(users);
46     this.permissionHandler = permissionHandler;
47     this.notificationHandler = notificationHandler;
48   }
49
50   @Override
51   public String call() {
52     users.forEach(this::handleUser);
53     return String.format(SUCCESSFUL_RETURN_MESSAGE, itemId);
54   }
55
56   private void handleUser(String user) {
57     Optional<String> userPermission = getUserPermission(user);
58     if (!userPermission.isPresent()) {
59       setUserPermission(user, CONTRIBUTOR);
60       registerUserNotificationSubscription(user);
61     }
62   }
63
64   private void registerUserNotificationSubscription(String user) {
65     notificationHandler.registerNotificationForUserOnEntity(user, itemId);
66   }
67
68   private void setUserPermission(String user, String permission) {
69     permissionHandler.setItemUserPermission(itemId, user, permission);
70   }
71
72   private Optional<String> getUserPermission(String user) {
73     return permissionHandler.getItemUserPermission(itemId, user);
74   }
75 }