From 731c8ac3faac6b00bc39bdf2e3a0b6a511f94431 Mon Sep 17 00:00:00 2001 From: zhangfan345 Date: Sat, 30 Jul 2022 10:38:41 +0800 Subject: Add update function in intent service Signed-off-by: zhangfan345 Issue-ID: USECASEUI-706 Change-Id: I0f027fd8b8876e1b24cbb40ecc50bacc20fa9837 --- .../intentanalysis/mapper/ExpectationMapper.java | 7 ++-- .../intentanalysis/mapper/StateMapper.java | 9 ++++- .../intentanalysis/service/ExpectationService.java | 4 +++ .../intentanalysis/service/StateService.java | 4 +++ .../service/impl/ExpectationServiceImpl.java | 36 +++++++++++++++++-- .../service/impl/IntentServiceImpl.java | 10 +++--- .../service/impl/StateServiceImpl.java | 42 ++++++++++++++++++++-- .../resources/mybatis/sql/ExpectationMapper.xml | 22 +++++++++++- .../main/resources/mybatis/sql/IntentMapper.xml | 1 - .../src/main/resources/mybatis/sql/StateMapper.xml | 23 +++++++++++- 10 files changed, 142 insertions(+), 16 deletions(-) diff --git a/intentanalysis/src/main/java/org/onap/usecaseui/intentanalysis/mapper/ExpectationMapper.java b/intentanalysis/src/main/java/org/onap/usecaseui/intentanalysis/mapper/ExpectationMapper.java index c05a344..b53a01b 100644 --- a/intentanalysis/src/main/java/org/onap/usecaseui/intentanalysis/mapper/ExpectationMapper.java +++ b/intentanalysis/src/main/java/org/onap/usecaseui/intentanalysis/mapper/ExpectationMapper.java @@ -23,12 +23,15 @@ import java.util.List; @Mapper public interface ExpectationMapper { - void insertExpectation(@Param(value = "expectationList") List expectationList, @Param(value = "intentId") String intentId); + void insertExpectationList(@Param(value = "expectationList") List expectationList, @Param(value = "intentId") String intentId); List selectExpectationByIntentId(String intentId); void deleteExpectationByIntentId(String intentId); - void updateExpectation(List expectation); + void updateExpectation(Expectation expectation); + void insertExpectation(@Param(value = "expectation") Expectation expectation, @Param(value = "intentId") String intentId); + + void deleteExpectationById(String expectationId); } diff --git a/intentanalysis/src/main/java/org/onap/usecaseui/intentanalysis/mapper/StateMapper.java b/intentanalysis/src/main/java/org/onap/usecaseui/intentanalysis/mapper/StateMapper.java index ab7e47b..54a1eb4 100644 --- a/intentanalysis/src/main/java/org/onap/usecaseui/intentanalysis/mapper/StateMapper.java +++ b/intentanalysis/src/main/java/org/onap/usecaseui/intentanalysis/mapper/StateMapper.java @@ -18,15 +18,22 @@ package org.onap.usecaseui.intentanalysis.mapper; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Param; +import org.onap.usecaseui.intentanalysis.bean.models.Expectation; import org.onap.usecaseui.intentanalysis.bean.models.State; import java.util.List; @Mapper public interface StateMapper { - void insertState(@Param(value = "stateList") List state, @Param(value = "expectationId") String expectationId); + void insertStateList(@Param(value = "stateList") List state, @Param(value = "expectationId") String expectationId); List selectStateByExpectation(String expectationId); void deleteStateByExpectationId(String expectationId); + + void updateState(State state); + + void insertState(@Param(value = "state") State state, @Param(value = "expectationId") String expectationId); + + void deleteStateById(String stateId); } diff --git a/intentanalysis/src/main/java/org/onap/usecaseui/intentanalysis/service/ExpectationService.java b/intentanalysis/src/main/java/org/onap/usecaseui/intentanalysis/service/ExpectationService.java index 8d0759f..ba51de1 100644 --- a/intentanalysis/src/main/java/org/onap/usecaseui/intentanalysis/service/ExpectationService.java +++ b/intentanalysis/src/main/java/org/onap/usecaseui/intentanalysis/service/ExpectationService.java @@ -30,4 +30,8 @@ public interface ExpectationService { void updateExpectationListById(List expectationList, String intentId); List getExpectationListByIntentId(String intentId); + + void insertExpectation(Expectation expectation, String intentId); + + void deleteExpectationById(String expectationId); } diff --git a/intentanalysis/src/main/java/org/onap/usecaseui/intentanalysis/service/StateService.java b/intentanalysis/src/main/java/org/onap/usecaseui/intentanalysis/service/StateService.java index 187f964..6ee82d6 100644 --- a/intentanalysis/src/main/java/org/onap/usecaseui/intentanalysis/service/StateService.java +++ b/intentanalysis/src/main/java/org/onap/usecaseui/intentanalysis/service/StateService.java @@ -29,4 +29,8 @@ public interface StateService { void updateStateListByExpectationId(List stateList, String expectationId); List getStateListByExpectationId(String expectationId); + + void insertState(State state, String expectationId); + + void deleteStateById(String stateId); } diff --git a/intentanalysis/src/main/java/org/onap/usecaseui/intentanalysis/service/impl/ExpectationServiceImpl.java b/intentanalysis/src/main/java/org/onap/usecaseui/intentanalysis/service/impl/ExpectationServiceImpl.java index 5f21f58..5a79213 100644 --- a/intentanalysis/src/main/java/org/onap/usecaseui/intentanalysis/service/impl/ExpectationServiceImpl.java +++ b/intentanalysis/src/main/java/org/onap/usecaseui/intentanalysis/service/impl/ExpectationServiceImpl.java @@ -28,6 +28,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.util.ArrayList; import java.util.List; @Service @@ -40,6 +41,9 @@ public class ExpectationServiceImpl implements ExpectationService { @Autowired private StateService stateService; + @Autowired + private ExpectationService expectationService; + @Override public void createExpectationList(List expectationList, String intentId) { for (Expectation expectation : expectationList) { @@ -47,14 +51,14 @@ public class ExpectationServiceImpl implements ExpectationService { stateService.createStateList(expectation.getStateList(), expectation.getExpectationId()); } } - expectationMapper.insertExpectation(expectationList, intentId); + expectationMapper.insertExpectationList(expectationList, intentId); } @Override public List getExpectationListByIntentId(String intentId) { List expectationList = expectationMapper.selectExpectationByIntentId(intentId); for (Expectation expectation : expectationList) { - List stateList = stateService.getStateListByExpectationId(expectation.getExpectationId()); + List stateList = stateService.getStateListByExpectationId(expectation.getExpectationId()); expectation.setStateList(stateList); } return expectationList; @@ -76,7 +80,33 @@ public class ExpectationServiceImpl implements ExpectationService { LOGGER.error("Intent ID {} doesn't exist in database.", intentId); throw new IllegalArgumentException("This intent ID doesn't exist in database."); } - expectationMapper.updateExpectation(expectationDBList); + List expectationDBIdList = new ArrayList<>(); + for (Expectation expectationDB : expectationDBList) { + expectationDBIdList.add(expectationDB.getExpectationId()); + } + + for (Expectation expectation : expectationList) { + if (expectationDBIdList.contains(expectation.getExpectationId())) { + stateService.updateStateListByExpectationId(expectation.getStateList(), expectation.getExpectationId()); + expectationMapper.updateExpectation(expectation); + expectationDBIdList.remove(expectation.getExpectationId()); + } else { + expectationService.insertExpectation(expectation, intentId); + } + } + for (String expectationDBId : expectationDBIdList) { + expectationService.deleteExpectationById(expectationDBId); + } LOGGER.info("Expectations are successfully updated."); } + + @Override + public void insertExpectation(Expectation expectation, String intentId) { + expectationMapper.insertExpectation(expectation, intentId); + } + + @Override + public void deleteExpectationById(String expectationId) { + expectationMapper.deleteExpectationById(expectationId); + } } diff --git a/intentanalysis/src/main/java/org/onap/usecaseui/intentanalysis/service/impl/IntentServiceImpl.java b/intentanalysis/src/main/java/org/onap/usecaseui/intentanalysis/service/impl/IntentServiceImpl.java index 7372510..aaf824e 100644 --- a/intentanalysis/src/main/java/org/onap/usecaseui/intentanalysis/service/impl/IntentServiceImpl.java +++ b/intentanalysis/src/main/java/org/onap/usecaseui/intentanalysis/service/impl/IntentServiceImpl.java @@ -77,14 +77,14 @@ public class IntentServiceImpl implements IntentService { @Override public Intent updateIntent(Intent intent) { - String intentId = intent.getIntentId(); - Intent intentDB = intentMapper.selectIntentById(intentId); + Intent intentDB = intentMapper.selectIntentById(intent.getIntentId()); if (intentDB == null) { - LOGGER.error("intent id {} not exists in db.", intentId); + LOGGER.error("intent id {} not exists in db.", intent.getIntentId()); } - intentMapper.updateIntent(intentDB); + expectationService.updateExpectationListById(intent.getExpectationList(), intent.getIntentId()); + intentMapper.updateIntent(intent); LOGGER.info("update intent successfully."); - return intentMapper.selectIntentById(intentId); + return intentMapper.selectIntentById(intent.getIntentId()); } @Override diff --git a/intentanalysis/src/main/java/org/onap/usecaseui/intentanalysis/service/impl/StateServiceImpl.java b/intentanalysis/src/main/java/org/onap/usecaseui/intentanalysis/service/impl/StateServiceImpl.java index 134e84a..e154446 100644 --- a/intentanalysis/src/main/java/org/onap/usecaseui/intentanalysis/service/impl/StateServiceImpl.java +++ b/intentanalysis/src/main/java/org/onap/usecaseui/intentanalysis/service/impl/StateServiceImpl.java @@ -20,20 +20,28 @@ package org.onap.usecaseui.intentanalysis.service.impl; import org.onap.usecaseui.intentanalysis.bean.models.State; import org.onap.usecaseui.intentanalysis.mapper.StateMapper; import org.onap.usecaseui.intentanalysis.service.StateService; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import java.util.ArrayList; import java.util.List; @Service public class StateServiceImpl implements StateService { + private static Logger LOGGER = LoggerFactory.getLogger(StateServiceImpl.class); + @Autowired private StateMapper stateMapper; + @Autowired + private StateService stateService; + @Override public void createStateList(List stateList, String expectationId) { - stateMapper.insertState(stateList, expectationId); + stateMapper.insertStateList(stateList, expectationId); } @Override @@ -48,7 +56,37 @@ public class StateServiceImpl implements StateService { } @Override - public void updateStateListByExpectationId(List stateList, String expectationId){ + public void updateStateListByExpectationId(List stateList, String expectationId) { + List stateDBList = stateMapper.selectStateByExpectation(expectationId); + if (stateDBList == null) { + LOGGER.error("Expectation ID {} doesn't exist in database.", expectationId); + throw new IllegalArgumentException("This expectation ID doesn't exist in database."); + } + List stateDBIdList = new ArrayList<>(); + for (State stateDB : stateDBList) { + stateDBIdList.add(stateDB.getStateId()); + } + for (State state : stateList) { + if (stateDBIdList.contains(state.getStateId())) { + stateMapper.updateState(state); + stateDBIdList.remove(state.getStateId()); + } else { + stateService.insertState(state, expectationId); + } + } + for (String stateDBId : stateDBIdList) { + stateService.deleteStateById(stateDBId); + } + LOGGER.info("States are successfully updated."); + } + + @Override + public void insertState(State state, String expectationId) { + stateMapper.insertState(state, expectationId); + } + @Override + public void deleteStateById(String stateId) { + stateMapper.deleteStateById(stateId); } } diff --git a/intentanalysis/src/main/resources/mybatis/sql/ExpectationMapper.xml b/intentanalysis/src/main/resources/mybatis/sql/ExpectationMapper.xml index 720bdd5..3b0923f 100644 --- a/intentanalysis/src/main/resources/mybatis/sql/ExpectationMapper.xml +++ b/intentanalysis/src/main/resources/mybatis/sql/ExpectationMapper.xml @@ -12,7 +12,7 @@ where intent_id = #{intentId} - + insert into expectation(expectation_id, expectation_name, target_moi, intent_id) values @@ -25,4 +25,24 @@ where intent_id = #{intentId} + + update expectation + + expectation_name = #{expectationName}, + target_moi = #{targetMOI}, + + where expectation_id = #{expectationId} + + + + insert into expectation(expectation_id, expectation_name, target_moi, intent_id) + values (#{expectation.expectationId}, #{expectation.expectationName}, #{expectation.targetMOI}, #{intentId}) + + + + delete + from expectation + where expectation_id = #{expectationId} + + \ No newline at end of file diff --git a/intentanalysis/src/main/resources/mybatis/sql/IntentMapper.xml b/intentanalysis/src/main/resources/mybatis/sql/IntentMapper.xml index 335ba6d..5e2847b 100644 --- a/intentanalysis/src/main/resources/mybatis/sql/IntentMapper.xml +++ b/intentanalysis/src/main/resources/mybatis/sql/IntentMapper.xml @@ -21,7 +21,6 @@ update intent - intent_id = #{intentId}, intent_name = #{intentName}, where intent_id = #{intentId} diff --git a/intentanalysis/src/main/resources/mybatis/sql/StateMapper.xml b/intentanalysis/src/main/resources/mybatis/sql/StateMapper.xml index 4850870..9e30981 100644 --- a/intentanalysis/src/main/resources/mybatis/sql/StateMapper.xml +++ b/intentanalysis/src/main/resources/mybatis/sql/StateMapper.xml @@ -11,7 +11,7 @@ where expectation_id = #{expectationId} - + insert into state(state_id, state_name, expectation_id, is_satisfied, condition) values @@ -24,4 +24,25 @@ where expectation_id = #{expectationId} + + insert into state(state_id, state_name, expectation_id, is_satisfied, condition) + values (#{state.stateId}, #{state.stateName}, #{expectationId}, #{state.isSatisfied}, #{state.condition}) + + + + update state + + state_name = #{stateName}, + is_satisfied = #{isSatisfied}, + condition = #{condition}, + + where state_id = #{stateId} + + + + delete + from state + where state_id = #{stateId} + + \ No newline at end of file -- cgit 1.2.3-korg