Format Java code with respect to ONAP Code Style
[externalapi/nbi.git] / src / main / java / org / onap / nbi / apis / serviceorder / workflow / SOTaskManager.java
1 /**\r
2  * Copyright (c) 2018 Orange\r
3  *\r
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with\r
5  * the License. You may obtain a copy of the License at\r
6  *\r
7  * http://www.apache.org/licenses/LICENSE-2.0\r
8  *\r
9  * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on\r
10  * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the\r
11  * specific language governing permissions and limitations under the License.\r
12  */\r
13 \r
14 package org.onap.nbi.apis.serviceorder.workflow;\r
15 \r
16 import org.onap.nbi.apis.serviceorder.model.OrderItemRelationship;\r
17 import org.onap.nbi.apis.serviceorder.model.ServiceOrder;\r
18 import org.onap.nbi.apis.serviceorder.model.ServiceOrderItem;\r
19 import org.onap.nbi.apis.serviceorder.model.orchestrator.ExecutionTask;\r
20 import org.onap.nbi.apis.serviceorder.model.orchestrator.ServiceOrderInfo;\r
21 import org.onap.nbi.apis.serviceorder.repositories.ExecutionTaskRepository;\r
22 import org.onap.nbi.apis.serviceorder.utils.JsonEntityConverter;\r
23 import org.slf4j.Logger;\r
24 import org.slf4j.LoggerFactory;\r
25 import org.springframework.beans.factory.annotation.Autowired;\r
26 import org.springframework.scheduling.annotation.EnableScheduling;\r
27 import org.springframework.stereotype.Service;\r
28 \r
29 import java.util.*;\r
30 import java.util.Map.Entry;\r
31 \r
32 @Service\r
33 @EnableScheduling\r
34 public class SOTaskManager {\r
35 \r
36     @Autowired\r
37     private ExecutionTaskRepository executionTaskRepository;\r
38 \r
39     @Autowired\r
40     private SOTaskProcessor soTaskProcessor;\r
41 \r
42     private static final Logger LOGGER = LoggerFactory.getLogger(SOTaskManager.class);\r
43 \r
44     /**\r
45      * @param orderItems\r
46      * @param serviceOrderInfoJson\r
47      */\r
48     private void registerOrderItemExecutionPlan(List<ServiceOrderItem> orderItems, String serviceOrderInfoJson) {\r
49         List<ExecutionTask> executionTasksSaved = new ArrayList<>();\r
50         Map<String, Long> internalIdOrderItemsMap = new HashMap<>();\r
51         if (orderItems != null) {\r
52             // first we save create all the execution tasks with order item id in relied tasks\r
53             for (ServiceOrderItem orderItem : orderItems) {\r
54                 ExecutionTask task = new ExecutionTask();\r
55                 task.setOrderItemId(orderItem.getId());\r
56                 task.setCreateDate(new Date());\r
57                 StringBuilder sb = new StringBuilder();\r
58                 for (OrderItemRelationship orderItemRelationship : orderItem.getOrderItemRelationship()) {\r
59                     sb.append(orderItemRelationship.getId()).append(" ");\r
60                 }\r
61                 task.setReliedTasks(sb.toString());\r
62                 task.setServiceOrderInfoJson(serviceOrderInfoJson);\r
63                 ExecutionTask savedTask = executionTaskRepository.save(task);\r
64                 executionTasksSaved.add(savedTask);\r
65 \r
66                 internalIdOrderItemsMap.put(savedTask.getOrderItemId(), savedTask.getInternalId());\r
67             }\r
68             // then we replace all orderitem ids in reliedtasks field with internalid of the tasks\r
69             for (ExecutionTask executionTask : executionTasksSaved) {\r
70                 List<String> reliedOrderItemsIds =\r
71                         new ArrayList<String>(Arrays.asList(executionTask.getReliedTasks().split(" ")));\r
72                 List<String> reliedTasksInternalIds = new ArrayList<String>();\r
73                 for (Entry<String, Long> entry : internalIdOrderItemsMap.entrySet()) {\r
74                     if (reliedOrderItemsIds.contains(entry.getKey())) {\r
75                         reliedTasksInternalIds.add(entry.getValue().toString());\r
76                     }\r
77                 }\r
78                 String reliedTasksString = String.join(" ", reliedTasksInternalIds);\r
79                 executionTask.setReliedTasks(reliedTasksString);\r
80                 if (LOGGER.isDebugEnabled()) {\r
81                     LOGGER.debug("saving task with id {} , orderItemId {} , reliedtasks {}",\r
82                             executionTask.getInternalId(), executionTask.getOrderItemId(),\r
83                             executionTask.getReliedTasks());\r
84                 }\r
85                 executionTaskRepository.save(executionTask);\r
86             }\r
87         }\r
88     }\r
89 \r
90     /**\r
91      *\r
92      * @param serviceOrder\r
93      * @param serviceOrderInfo\r
94      */\r
95     public void registerServiceOrder(ServiceOrder serviceOrder, ServiceOrderInfo serviceOrderInfo) {\r
96         String serviceOrderInfoJson = JsonEntityConverter.convertServiceOrderInfoToJson(serviceOrderInfo);\r
97         registerOrderItemExecutionPlan(serviceOrder.getOrderItem(), serviceOrderInfoJson);\r
98     }\r
99 \r
100 }\r