From 40d95f90d0740982a8a22e2ae318b4a8236ea3ba Mon Sep 17 00:00:00 2001 From: zhangfan345 Date: Tue, 2 Aug 2022 17:12:30 +0800 Subject: Add exception for intent analysis Issue-ID: USECASEUI-707 Signed-off-by: zhangfan345 Change-Id: I44cf51cdfe3b83cf4a2166d4a83992e3ae06e792 --- .../intentanalysis/common/ResponseConsts.java | 42 +++++++++++++++++ .../intentanalysis/exception/CommonException.java | 26 +++++++++++ .../exception/DataBaseException.java | 23 ++++++++++ .../intentanalysis/exception/ErrorMessage.java | 34 ++++++++++++++ .../intentanalysis/mapper/ExpectationMapper.java | 10 ++--- .../intentanalysis/mapper/IntentMapper.java | 6 +-- .../intentanalysis/mapper/StateMapper.java | 10 ++--- .../service/impl/ExpectationServiceImpl.java | 52 +++++++++++++++++++--- .../service/impl/IntentServiceImpl.java | 39 ++++++++++++---- .../service/impl/StateServiceImpl.java | 47 ++++++++++++++++--- 10 files changed, 253 insertions(+), 36 deletions(-) create mode 100644 intentanalysis/src/main/java/org/onap/usecaseui/intentanalysis/common/ResponseConsts.java create mode 100644 intentanalysis/src/main/java/org/onap/usecaseui/intentanalysis/exception/CommonException.java create mode 100644 intentanalysis/src/main/java/org/onap/usecaseui/intentanalysis/exception/DataBaseException.java create mode 100644 intentanalysis/src/main/java/org/onap/usecaseui/intentanalysis/exception/ErrorMessage.java (limited to 'intentanalysis/src') diff --git a/intentanalysis/src/main/java/org/onap/usecaseui/intentanalysis/common/ResponseConsts.java b/intentanalysis/src/main/java/org/onap/usecaseui/intentanalysis/common/ResponseConsts.java new file mode 100644 index 0000000..fd63581 --- /dev/null +++ b/intentanalysis/src/main/java/org/onap/usecaseui/intentanalysis/common/ResponseConsts.java @@ -0,0 +1,42 @@ +/* + * Copyright 2022 Huawei Technologies Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.onap.usecaseui.intentanalysis.common; + +public final class ResponseConsts { + private ResponseConsts() { + throw new IllegalStateException("ResponseConsts class"); + } + + /** + * insert data to database failed. + */ + public static final int RET_INSERT_DATA_FAIL = 10001; + + /** + * update data from database failed. + */ + public static final int RET_UPDATE_DATA_FAIL = 10002; + + /** + * query data from database is empty. + */ + public static final int RET_QUERY_DATA_EMPTY = 10003; + + /** + * delete data from database failed. + */ + public static final int RET_DELETE_DATA_FAIL = 10004; +} diff --git a/intentanalysis/src/main/java/org/onap/usecaseui/intentanalysis/exception/CommonException.java b/intentanalysis/src/main/java/org/onap/usecaseui/intentanalysis/exception/CommonException.java new file mode 100644 index 0000000..7f136fa --- /dev/null +++ b/intentanalysis/src/main/java/org/onap/usecaseui/intentanalysis/exception/CommonException.java @@ -0,0 +1,26 @@ +/* + * Copyright 2022 Huawei Technologies Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.onap.usecaseui.intentanalysis.exception; + +public class CommonException extends RuntimeException{ + + private ErrorMessage errMsg; + + public CommonException(String message, int ret) { + super(message); + errMsg = new ErrorMessage(ret, null); + } +} diff --git a/intentanalysis/src/main/java/org/onap/usecaseui/intentanalysis/exception/DataBaseException.java b/intentanalysis/src/main/java/org/onap/usecaseui/intentanalysis/exception/DataBaseException.java new file mode 100644 index 0000000..b7121d1 --- /dev/null +++ b/intentanalysis/src/main/java/org/onap/usecaseui/intentanalysis/exception/DataBaseException.java @@ -0,0 +1,23 @@ +/* + * Copyright 2022 Huawei Technologies Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.onap.usecaseui.intentanalysis.exception; + +public class DataBaseException extends CommonException { + + public DataBaseException(String message, int ret) { + super(message, ret); + } +} diff --git a/intentanalysis/src/main/java/org/onap/usecaseui/intentanalysis/exception/ErrorMessage.java b/intentanalysis/src/main/java/org/onap/usecaseui/intentanalysis/exception/ErrorMessage.java new file mode 100644 index 0000000..5ced950 --- /dev/null +++ b/intentanalysis/src/main/java/org/onap/usecaseui/intentanalysis/exception/ErrorMessage.java @@ -0,0 +1,34 @@ +/* + * Copyright 2022 Huawei Technologies Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.onap.usecaseui.intentanalysis.exception; + +import java.util.List; +import lombok.Getter; +import lombok.Setter; + +@Getter +@Setter +public class ErrorMessage { + + private int retCode; + + private List params; + + public ErrorMessage(int retCode, List params) { + this.retCode = retCode; + this.params = params; + } +} 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 b53a01b..9e910b5 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,15 +23,15 @@ import java.util.List; @Mapper public interface ExpectationMapper { - void insertExpectationList(@Param(value = "expectationList") List expectationList, @Param(value = "intentId") String intentId); + int insertExpectationList(@Param(value = "expectationList") List expectationList, @Param(value = "intentId") String intentId); List selectExpectationByIntentId(String intentId); - void deleteExpectationByIntentId(String intentId); + int deleteExpectationByIntentId(String intentId); - void updateExpectation(Expectation expectation); + int updateExpectation(Expectation expectation); - void insertExpectation(@Param(value = "expectation") Expectation expectation, @Param(value = "intentId") String intentId); + int insertExpectation(@Param(value = "expectation") Expectation expectation, @Param(value = "intentId") String intentId); - void deleteExpectationById(String expectationId); + int deleteExpectationById(String expectationId); } diff --git a/intentanalysis/src/main/java/org/onap/usecaseui/intentanalysis/mapper/IntentMapper.java b/intentanalysis/src/main/java/org/onap/usecaseui/intentanalysis/mapper/IntentMapper.java index e3bd6f3..7cf69a0 100644 --- a/intentanalysis/src/main/java/org/onap/usecaseui/intentanalysis/mapper/IntentMapper.java +++ b/intentanalysis/src/main/java/org/onap/usecaseui/intentanalysis/mapper/IntentMapper.java @@ -24,13 +24,13 @@ import java.util.List; @Mapper public interface IntentMapper { - void insertIntent(Intent intent); + int insertIntent(Intent intent); - void updateIntent(Intent intent); + int updateIntent(Intent intent); Intent selectIntentById(String intentId); List selectIntents(); - void deleteIntentById(String intentId); + int deleteIntentById(String intentId); } 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 54a1eb4..ad7d3a1 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 @@ -25,15 +25,15 @@ import java.util.List; @Mapper public interface StateMapper { - void insertStateList(@Param(value = "stateList") List state, @Param(value = "expectationId") String expectationId); + int insertStateList(@Param(value = "stateList") List state, @Param(value = "expectationId") String expectationId); List selectStateByExpectation(String expectationId); - void deleteStateByExpectationId(String expectationId); + int deleteStateByExpectationId(String expectationId); - void updateState(State state); + int updateState(State state); - void insertState(@Param(value = "state") State state, @Param(value = "expectationId") String expectationId); + int insertState(@Param(value = "state") State state, @Param(value = "expectationId") String expectationId); - void deleteStateById(String stateId); + int 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 5a79213..af7a2e9 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 @@ -19,6 +19,7 @@ package org.onap.usecaseui.intentanalysis.service.impl; import org.onap.usecaseui.intentanalysis.bean.models.Expectation; import org.onap.usecaseui.intentanalysis.bean.models.State; +import org.onap.usecaseui.intentanalysis.exception.DataBaseException; import org.onap.usecaseui.intentanalysis.mapper.ExpectationMapper; import org.onap.usecaseui.intentanalysis.service.ExpectationService; import org.onap.usecaseui.intentanalysis.service.StateService; @@ -26,6 +27,7 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.onap.usecaseui.intentanalysis.common.ResponseConsts; import java.util.ArrayList; @@ -51,12 +53,22 @@ public class ExpectationServiceImpl implements ExpectationService { stateService.createStateList(expectation.getStateList(), expectation.getExpectationId()); } } - expectationMapper.insertExpectationList(expectationList, intentId); + int res = expectationMapper.insertExpectationList(expectationList, intentId); + if (res < 1) { + String msg = "Create expectation to database failed."; + LOGGER.error(msg); + throw new DataBaseException(msg, ResponseConsts.RET_INSERT_DATA_FAIL); + } } @Override public List getExpectationListByIntentId(String intentId) { List expectationList = expectationMapper.selectExpectationByIntentId(intentId); + if (expectationList == null) { + String msg = String.format("Intent id %s doesn't exist in database.", intentId); + LOGGER.error(msg); + throw new DataBaseException(msg, ResponseConsts.RET_QUERY_DATA_EMPTY); + } for (Expectation expectation : expectationList) { List stateList = stateService.getStateListByExpectationId(expectation.getExpectationId()); expectation.setStateList(stateList); @@ -67,7 +79,17 @@ public class ExpectationServiceImpl implements ExpectationService { @Override public void deleteExpectationListById(String intentId) { List expectationList = expectationMapper.selectExpectationByIntentId(intentId); - expectationMapper.deleteExpectationByIntentId(intentId); + if (expectationList == null) { + String msg = String.format("Intent id %s doesn't exist in database.", intentId); + LOGGER.error(msg); + throw new DataBaseException(msg, ResponseConsts.RET_QUERY_DATA_EMPTY); + } + int res = expectationMapper.deleteExpectationByIntentId(intentId); + if (res < 1) { + String msg = "Delete expectation in database failed."; + LOGGER.error(msg); + throw new DataBaseException(msg, ResponseConsts.RET_DELETE_DATA_FAIL); + } for (Expectation expectation : expectationList) { stateService.deleteStateListByExpectationId(expectation.getExpectationId()); } @@ -77,8 +99,9 @@ public class ExpectationServiceImpl implements ExpectationService { public void updateExpectationListById(List expectationList, String intentId) { List expectationDBList = expectationMapper.selectExpectationByIntentId(intentId); if (expectationDBList == null) { - LOGGER.error("Intent ID {} doesn't exist in database.", intentId); - throw new IllegalArgumentException("This intent ID doesn't exist in database."); + String msg = String.format("Intent id %s doesn't exist in database.", intentId); + LOGGER.error(msg); + throw new DataBaseException(msg, ResponseConsts.RET_QUERY_DATA_EMPTY); } List expectationDBIdList = new ArrayList<>(); for (Expectation expectationDB : expectationDBList) { @@ -88,7 +111,12 @@ public class ExpectationServiceImpl implements ExpectationService { for (Expectation expectation : expectationList) { if (expectationDBIdList.contains(expectation.getExpectationId())) { stateService.updateStateListByExpectationId(expectation.getStateList(), expectation.getExpectationId()); - expectationMapper.updateExpectation(expectation); + int res = expectationMapper.updateExpectation(expectation); + if (res < 1) { + String msg = "Update expectation in database failed."; + LOGGER.error(msg); + throw new DataBaseException(msg, ResponseConsts.RET_UPDATE_DATA_FAIL); + } expectationDBIdList.remove(expectation.getExpectationId()); } else { expectationService.insertExpectation(expectation, intentId); @@ -102,11 +130,21 @@ public class ExpectationServiceImpl implements ExpectationService { @Override public void insertExpectation(Expectation expectation, String intentId) { - expectationMapper.insertExpectation(expectation, intentId); + int res = expectationMapper.insertExpectation(expectation, intentId); + if (res < 1) { + String msg = "Create expectation to database failed."; + LOGGER.error(msg); + throw new DataBaseException(msg, ResponseConsts.RET_INSERT_DATA_FAIL); + } } @Override public void deleteExpectationById(String expectationId) { - expectationMapper.deleteExpectationById(expectationId); + int res = expectationMapper.deleteExpectationById(expectationId); + if (res < 1) { + String msg = "Delete expectation in database failed."; + LOGGER.error(msg); + throw new DataBaseException(msg, ResponseConsts.RET_DELETE_DATA_FAIL); + } } } 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 aaf824e..21dfe21 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 @@ -26,6 +26,8 @@ import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import org.onap.usecaseui.intentanalysis.exception.DataBaseException; +import org.onap.usecaseui.intentanalysis.common.ResponseConsts; import java.util.ArrayList; import java.util.List; @@ -44,7 +46,9 @@ public class IntentServiceImpl implements IntentService { public List getIntentList() { List intentList = intentMapper.selectIntents(); if (intentList == null || intentList.size() <= 0) { - return new ArrayList<>(); + String msg = "Intent list doesn't exist in the intent database."; + LOGGER.error(msg); + throw new DataBaseException(msg, ResponseConsts.RET_QUERY_DATA_EMPTY); } for (Intent intent : intentList) { intent.setExpectationList(expectationService.getExpectationListByIntentId(intent.getIntentId())); @@ -59,16 +63,21 @@ public class IntentServiceImpl implements IntentService { intent.setExpectationList(expectationService.getExpectationListByIntentId(intent.getIntentId())); return intent; } else { - String msg = "Intent Id requested doesn't exist in the intent database"; + String msg = String.format("Intent id %s doesn't exist in database.", intentId); LOGGER.error(msg); - throw new IllegalArgumentException(msg); + throw new DataBaseException(msg, ResponseConsts.RET_QUERY_DATA_EMPTY); } } @Transactional(rollbackFor = RuntimeException.class) @Override public Intent createIntent(Intent intent) { - intentMapper.insertIntent(intent); + int res = intentMapper.insertIntent(intent); + if (res < 1) { + String msg = "Create intent to database failed."; + LOGGER.error(msg); + throw new DataBaseException(msg, ResponseConsts.RET_INSERT_DATA_FAIL); + } // saving expectation list into expectation table expectationService.createExpectationList(intent.getExpectationList(), intent.getIntentId()); LOGGER.info("Intent was successfully created."); @@ -79,18 +88,30 @@ public class IntentServiceImpl implements IntentService { public Intent updateIntent(Intent intent) { Intent intentDB = intentMapper.selectIntentById(intent.getIntentId()); if (intentDB == null) { - LOGGER.error("intent id {} not exists in db.", intent.getIntentId()); + String msg = String.format("Intent id %s doesn't exist in database.", intent.getIntentId()); + LOGGER.error(msg); + throw new DataBaseException(msg, ResponseConsts.RET_QUERY_DATA_EMPTY); } expectationService.updateExpectationListById(intent.getExpectationList(), intent.getIntentId()); - intentMapper.updateIntent(intent); - LOGGER.info("update intent successfully."); + int res = intentMapper.updateIntent(intent); + if (res < 1) { + String msg = "Update intent in database failed."; + LOGGER.error(msg); + throw new DataBaseException(msg, ResponseConsts.RET_UPDATE_DATA_FAIL); + } + LOGGER.info("Update intent successfully."); return intentMapper.selectIntentById(intent.getIntentId()); } @Override public void deleteIntentById(String intentId) { - intentMapper.deleteIntentById(intentId); + int res = intentMapper.deleteIntentById(intentId); + if (res < 1) { + String msg = "Delete intent in database failed."; + LOGGER.error(msg); + throw new DataBaseException(msg, ResponseConsts.RET_DELETE_DATA_FAIL); + } expectationService.deleteExpectationListById(intentId); - LOGGER.info("intent has been deleted successfully."); + LOGGER.info("Intent has been deleted successfully."); } } 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 e154446..06fa9fd 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 @@ -18,6 +18,8 @@ package org.onap.usecaseui.intentanalysis.service.impl; import org.onap.usecaseui.intentanalysis.bean.models.State; +import org.onap.usecaseui.intentanalysis.common.ResponseConsts; +import org.onap.usecaseui.intentanalysis.exception.DataBaseException; import org.onap.usecaseui.intentanalysis.mapper.StateMapper; import org.onap.usecaseui.intentanalysis.service.StateService; import org.slf4j.Logger; @@ -41,26 +43,42 @@ public class StateServiceImpl implements StateService { @Override public void createStateList(List stateList, String expectationId) { - stateMapper.insertStateList(stateList, expectationId); + int res = stateMapper.insertStateList(stateList, expectationId); + if (res < 1) { + String msg = "Create state to database failed."; + LOGGER.error(msg); + throw new DataBaseException(msg, ResponseConsts.RET_INSERT_DATA_FAIL); + } } @Override public List getStateListByExpectationId(String expectationId) { List stateList = stateMapper.selectStateByExpectation(expectationId); + if (stateList == null) { + String msg = String.format("Expectation id %s doesn't exist in database.", expectationId); + LOGGER.error(msg); + throw new DataBaseException(msg, ResponseConsts.RET_QUERY_DATA_EMPTY); + } return stateList; } @Override public void deleteStateListByExpectationId(String expectationId) { - stateMapper.deleteStateByExpectationId(expectationId); + int res = stateMapper.deleteStateByExpectationId(expectationId); + if (res < 1) { + String msg = "Delete state in database failed."; + LOGGER.error(msg); + throw new DataBaseException(msg, ResponseConsts.RET_DELETE_DATA_FAIL); + } } @Override 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."); + String msg = String.format("Expectation id %s doesn't exist in database.", expectationId); + LOGGER.error(msg); + throw new DataBaseException(msg, ResponseConsts.RET_QUERY_DATA_EMPTY); } List stateDBIdList = new ArrayList<>(); for (State stateDB : stateDBList) { @@ -68,7 +86,12 @@ public class StateServiceImpl implements StateService { } for (State state : stateList) { if (stateDBIdList.contains(state.getStateId())) { - stateMapper.updateState(state); + int res = stateMapper.updateState(state); + if (res < 1) { + String msg = "Update state in database failed."; + LOGGER.error(msg); + throw new DataBaseException(msg, ResponseConsts.RET_UPDATE_DATA_FAIL); + } stateDBIdList.remove(state.getStateId()); } else { stateService.insertState(state, expectationId); @@ -82,11 +105,21 @@ public class StateServiceImpl implements StateService { @Override public void insertState(State state, String expectationId) { - stateMapper.insertState(state, expectationId); + int res = stateMapper.insertState(state, expectationId); + if (res < 1) { + String msg = "Create state to database failed."; + LOGGER.error(msg); + throw new DataBaseException(msg, ResponseConsts.RET_INSERT_DATA_FAIL); + } } @Override public void deleteStateById(String stateId) { - stateMapper.deleteStateById(stateId); + int res = stateMapper.deleteStateById(stateId); + if (res < 1) { + String msg = "Delete state in database failed."; + LOGGER.error(msg); + throw new DataBaseException(msg, ResponseConsts.RET_DELETE_DATA_FAIL); + } } } -- cgit 1.2.3-korg