Add update function in intent service 80/130080/4
authorzhangfan345 <zhangfan345@huawei.com>
Sat, 30 Jul 2022 02:38:41 +0000 (10:38 +0800)
committerFan Zhang <zhangfan345@huawei.com>
Tue, 2 Aug 2022 06:19:11 +0000 (06:19 +0000)
Signed-off-by: zhangfan345 <zhangfan345@huawei.com>
Issue-ID: USECASEUI-706
Change-Id: I0f027fd8b8876e1b24cbb40ecc50bacc20fa9837

intentanalysis/src/main/java/org/onap/usecaseui/intentanalysis/mapper/ExpectationMapper.java
intentanalysis/src/main/java/org/onap/usecaseui/intentanalysis/mapper/StateMapper.java
intentanalysis/src/main/java/org/onap/usecaseui/intentanalysis/service/ExpectationService.java
intentanalysis/src/main/java/org/onap/usecaseui/intentanalysis/service/StateService.java
intentanalysis/src/main/java/org/onap/usecaseui/intentanalysis/service/impl/ExpectationServiceImpl.java
intentanalysis/src/main/java/org/onap/usecaseui/intentanalysis/service/impl/IntentServiceImpl.java
intentanalysis/src/main/java/org/onap/usecaseui/intentanalysis/service/impl/StateServiceImpl.java
intentanalysis/src/main/resources/mybatis/sql/ExpectationMapper.xml
intentanalysis/src/main/resources/mybatis/sql/IntentMapper.xml
intentanalysis/src/main/resources/mybatis/sql/StateMapper.xml

index c05a344..b53a01b 100644 (file)
@@ -23,12 +23,15 @@ import java.util.List;
 @Mapper
 public interface ExpectationMapper {
 
-    void insertExpectation(@Param(value = "expectationList") List<Expectation> expectationList, @Param(value = "intentId") String intentId);
+    void insertExpectationList(@Param(value = "expectationList") List<Expectation> expectationList, @Param(value = "intentId") String intentId);
 
     List<Expectation> selectExpectationByIntentId(String intentId);
 
     void deleteExpectationByIntentId(String intentId);
 
-    void updateExpectation(List<Expectation> expectation);
+    void updateExpectation(Expectation expectation);
 
+    void insertExpectation(@Param(value = "expectation") Expectation expectation, @Param(value = "intentId") String intentId);
+
+    void deleteExpectationById(String expectationId);
 }
index ab7e47b..54a1eb4 100644 (file)
@@ -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> state, @Param(value = "expectationId") String expectationId);
+    void insertStateList(@Param(value = "stateList") List<State> state, @Param(value = "expectationId") String expectationId);
 
     List<State> 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);
 }
index 8d0759f..ba51de1 100644 (file)
@@ -30,4 +30,8 @@ public interface ExpectationService {
     void updateExpectationListById(List<Expectation> expectationList, String intentId);
 
     List<Expectation> getExpectationListByIntentId(String intentId);
+
+    void insertExpectation(Expectation expectation, String intentId);
+
+    void deleteExpectationById(String expectationId);
 }
index 187f964..6ee82d6 100644 (file)
@@ -29,4 +29,8 @@ public interface StateService {
     void updateStateListByExpectationId(List<State> stateList, String expectationId);
 
     List<State> getStateListByExpectationId(String expectationId);
+
+    void insertState(State state, String expectationId);
+
+    void deleteStateById(String stateId);
 }
index 5f21f58..5a79213 100644 (file)
@@ -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<Expectation> 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<Expectation> getExpectationListByIntentId(String intentId) {
         List<Expectation> expectationList = expectationMapper.selectExpectationByIntentId(intentId);
         for (Expectation expectation : expectationList) {
-            List<State> stateList =  stateService.getStateListByExpectationId(expectation.getExpectationId());
+            List<State> 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<String> 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);
+    }
 }
index 7372510..aaf824e 100644 (file)
@@ -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
index 134e84a..e154446 100644 (file)
@@ -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<State> 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<State> stateList, String expectationId){
+    public void updateStateListByExpectationId(List<State> stateList, String expectationId) {
+        List<State> 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<String> 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);
     }
 }
index 720bdd5..3b0923f 100644 (file)
@@ -12,7 +12,7 @@
         where intent_id = #{intentId}
     </select>
 
-    <insert id="insertExpectation">
+    <insert id="insertExpectationList">
         insert into expectation(expectation_id, expectation_name, target_moi, intent_id)
         values
         <foreach collection="expectationList" index="index" item="item" separator=",">
         where intent_id = #{intentId}
     </delete>
 
+    <update id="updateExpectation">
+        update expectation
+        <trim prefix="set" suffixOverrides=",">
+            <if test="expectationName != null">expectation_name = #{expectationName},</if>
+            <if test="targetMOI != null">target_moi = #{targetMOI},</if>
+        </trim>
+        where expectation_id = #{expectationId}
+    </update>
+
+    <insert id="insertExpectation">
+        insert into expectation(expectation_id, expectation_name, target_moi, intent_id)
+        values (#{expectation.expectationId}, #{expectation.expectationName}, #{expectation.targetMOI}, #{intentId})
+    </insert>
+
+    <delete id="deleteExpectationById">
+        delete
+        from expectation
+        where expectation_id = #{expectationId}
+    </delete>
+
 </mapper>
\ No newline at end of file
index 335ba6d..5e2847b 100644 (file)
@@ -21,7 +21,6 @@
     <update id="updateIntent" parameterType="org.onap.usecaseui.intentanalysis.bean.models.Intent">
         update intent
         <trim prefix="set" suffixOverrides=",">
-            <if test="intentId != null">intent_id = #{intentId},</if>
             <if test="intentName != null">intent_name = #{intentName},</if>
         </trim>
         where intent_id = #{intentId}
index 4850870..9e30981 100644 (file)
@@ -11,7 +11,7 @@
         where expectation_id = #{expectationId}
     </select>
 
-    <insert id="insertState" parameterType="java.util.ArrayList">
+    <insert id="insertStateList" parameterType="java.util.ArrayList">
         insert into state(state_id, state_name, expectation_id, is_satisfied, condition)
         values
         <foreach collection="stateList" index="index" item="item" separator=",">
         where expectation_id = #{expectationId}
     </delete>
 
+    <insert id="insertState">
+        insert into state(state_id, state_name, expectation_id, is_satisfied, condition)
+        values (#{state.stateId}, #{state.stateName}, #{expectationId}, #{state.isSatisfied}, #{state.condition})
+    </insert>
+
+    <update id="updateState" parameterType="java.util.List">
+        update state
+        <trim prefix="set" suffixOverrides=",">
+            <if test="stateName != null">state_name = #{stateName},</if>
+            <if test="isSatisfied != null">is_satisfied = #{isSatisfied},</if>
+            <if test="condition != null">condition = #{condition},</if>
+        </trim>
+        where state_id = #{stateId}
+    </update>
+
+    <delete id="deleteStateById">
+        delete
+        from state
+        where state_id = #{stateId}
+    </delete>
+
 </mapper>
\ No newline at end of file