2 * Copyright (C) 2022 CMCC, Inc. and others. All rights reserved.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org.onap.usecaseui.intentanalysis.intentBaseService.contextService;
18 import lombok.extern.slf4j.Slf4j;
19 import org.apache.commons.collections.CollectionUtils;
20 import org.apache.commons.lang.StringUtils;
21 import org.onap.usecaseui.intentanalysis.bean.enums.OperatorType;
22 import org.onap.usecaseui.intentanalysis.bean.models.Condition;
23 import org.onap.usecaseui.intentanalysis.bean.models.Context;
24 import org.onap.usecaseui.intentanalysis.bean.models.Intent;
25 import org.onap.usecaseui.intentanalysis.intentBaseService.IntentManagementFunction;
26 import org.onap.usecaseui.intentanalysis.service.IntentService;
27 import org.onap.usecaseui.intentanalysis.util.CommonUtil;
28 import org.springframework.beans.factory.annotation.Autowired;
29 import org.springframework.context.ApplicationContext;
30 import org.springframework.stereotype.Service;
32 import java.util.ArrayList;
33 import java.util.List;
36 public class IntentContextService {
39 private IntentService intentService;
42 ApplicationContext applicationContext;
44 public void updateChindIntentContext(Intent originIntent, Intent intent){
45 List<Context> contextList = intent.getIntentContexts();
46 if (CollectionUtils.isEmpty(contextList)) {
47 contextList = new ArrayList<>();
49 Condition condition1 = new Condition();
50 condition1.setConditionId(CommonUtil.getUUid());
51 condition1.setConditionName(originIntent.getIntentName() + "id");
52 condition1.setOperator(OperatorType.EQUALTO);
53 condition1.setConditionValue(originIntent.getIntentId());
55 Context context = new Context();
56 context.setContextName("parentIntent info");
57 context.setContextId(CommonUtil.getUUid());
58 List<Condition> conditionList = new ArrayList<>();
59 conditionList.add(condition1);
60 context.setContextConditions(conditionList);
61 contextList.add(context);
62 intent.setIntentContexts(contextList);
66 public void updateParentIntentContext(Intent originIntent, Intent intent){
67 List<Context> contextList = originIntent.getIntentContexts();
68 if (CollectionUtils.isEmpty(contextList)) {
69 contextList = new ArrayList<>();
72 Condition condition1 = new Condition();
73 condition1.setConditionId(CommonUtil.getUUid());
74 condition1.setConditionName(intent.getIntentName() + "id");
75 condition1.setOperator(OperatorType.EQUALTO);
76 condition1.setConditionValue(intent.getIntentId());
78 boolean isSubIntentInfoExist = false;
79 for (Context context : contextList) {
80 if (context.getContextName().contains("subIntent info")){
81 List<Condition> conditionList = context.getContextConditions();
82 if (CollectionUtils.isEmpty(conditionList)) {
83 conditionList = new ArrayList<>();
85 conditionList.add(condition1);
86 context.setContextConditions(conditionList);
87 isSubIntentInfoExist = true;
91 if (isSubIntentInfoExist != true){
92 Context context = new Context();
93 context.setContextName("subIntent info");
94 context.setContextId(CommonUtil.getUUid());
95 List<Condition> conditionList = new ArrayList<>();
96 conditionList.add(condition1);
97 context.setContextConditions(conditionList);
98 contextList.add(context);
100 originIntent.setIntentContexts(contextList);
103 public void updateIntentOwnerHandlerContext(Intent intent, IntentManagementFunction intentOwner, IntentManagementFunction intentHandler){
104 List<Context> contextList = intent.getIntentContexts();
105 if (CollectionUtils.isEmpty(contextList)){
106 contextList = new ArrayList<>();
109 Condition condition1 = new Condition();
110 condition1.setConditionId(CommonUtil.getUUid());
111 condition1.setConditionName("owner class name");
112 condition1.setOperator(OperatorType.EQUALTO);
113 condition1.setConditionValue(intentOwner.getClass().getName());
115 Context context = new Context();
116 context.setContextName("owner info");
117 context.setContextId(CommonUtil.getUUid());
118 List<Condition> conditionList = new ArrayList<>();
119 conditionList.add(condition1);
120 context.setContextConditions(conditionList);
121 contextList.add(context);
123 Condition condition2 = new Condition();
124 condition2.setConditionId(CommonUtil.getUUid());
125 condition2.setConditionName("handler class name");
126 condition2.setOperator(OperatorType.EQUALTO);
127 condition2.setConditionValue(intentHandler.getClass().getName());
129 Context context2 = new Context();
130 context2.setContextName("handler info");
131 context2.setContextId(CommonUtil.getUUid());
132 List<Condition> conditionList2 = new ArrayList<>();
133 conditionList2.add(condition2);
134 context2.setContextConditions(conditionList2);
135 contextList.add(context2);
137 intent.setIntentContexts(contextList);
140 public List<Intent> getSubIntentInfoFromContext(Intent originIntent){
141 List<Intent> subIntentList = new ArrayList<>();
143 // List<Context> contextList = originIntent.getIntentContexts();
144 Intent dbIntent = intentService.getIntent(originIntent.getIntentId());
145 List<Context> contextList = dbIntent.getIntentContexts();
146 for (Context context : contextList) {
147 if (context.getContextName().contains("subIntent info")){
148 List<Condition> conditionList = context.getContextConditions();
149 for (Condition condition : conditionList) {
150 String subIntentId = condition.getConditionValue();
151 Intent subInent = intentService.getIntent(subIntentId);
152 subIntentList.add(subInent);
156 return subIntentList;
159 public IntentManagementFunction getHandlerInfo(Intent intent){
160 List<Context> contextList = intent.getIntentContexts();
161 IntentManagementFunction handler = new IntentManagementFunction();
163 for (Context context : contextList) {
164 if (context.getContextName().contains("handler info")) {
165 List<Condition> conditionList = context.getContextConditions();
166 String handlerClassName = conditionList.get(0).getConditionValue();
167 String handleName = StringUtils.substringAfterLast(handlerClassName,".");
168 handler = (IntentManagementFunction) applicationContext
169 .getBean(handleName);
175 public void deleteSubIntentContext(Intent intent, String deleteIntentId){
176 List<Context> contextList = intent.getIntentContexts();
177 for (Context context : contextList) {
178 if (context.getContextName().contains("subIntent info")) {
179 List<Condition> conditionList = context.getContextConditions();
180 List<Condition> newConditionList = new ArrayList<>();
181 for (Condition condition : conditionList) {
182 if (!StringUtils.equals(condition.getConditionValue(), deleteIntentId)){
183 newConditionList.add(condition);
186 context.setContextConditions(newConditionList);
189 log.info("deleteSubIntentContext from intent finished");