|
|
@@ -1,20 +1,19 @@
|
|
|
-package nju.seec.helper.api;
|
|
|
+package nju.seec.helper.api.bok;
|
|
|
|
|
|
import com.fasterxml.jackson.annotation.JsonProperty;
|
|
|
import com.google.common.collect.ImmutableMap;
|
|
|
import com.google.common.collect.Lists;
|
|
|
import com.google.common.collect.Maps;
|
|
|
import lombok.Data;
|
|
|
-import lombok.extern.slf4j.Slf4j;
|
|
|
import nju.seec.helper.dto.question.BaseQuestionDTO;
|
|
|
import nju.seec.helper.dto.question.ChoiceQuestionDTO;
|
|
|
import nju.seec.helper.dto.question.TrueOrFalseQuestionDTO;
|
|
|
import nju.seec.helper.enums.ExceptionType;
|
|
|
import nju.seec.helper.enums.QuestionType;
|
|
|
import nju.seec.helper.exception.HelperException;
|
|
|
-import nju.seec.helper.util.GuavaCacheUtil;
|
|
|
+import nju.seec.helper.util.cache.GuavaCacheUtils;
|
|
|
import nju.seec.helper.util.RestRequestUtil;
|
|
|
-import nju.seec.helper.vo.question.BokQuestion;
|
|
|
+import nju.seec.helper.vo.question.BokQuestionVO;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.data.domain.Page;
|
|
|
import org.springframework.data.domain.PageImpl;
|
|
|
@@ -32,11 +31,10 @@ import java.util.stream.Collectors;
|
|
|
* <p>
|
|
|
* updated by cst
|
|
|
*/
|
|
|
-@Slf4j
|
|
|
@Component
|
|
|
-public class BokApi {
|
|
|
+public class BokQuestionApi {
|
|
|
private final RestRequestUtil restRequestUtil;
|
|
|
- private final GuavaCacheUtil cacheUtils;
|
|
|
+ private final GuavaCacheUtils cacheUtils;
|
|
|
@Value("${bok.tqUrl}")
|
|
|
private String tqUrl;
|
|
|
@Value("${bok.tqStemSearchUrl}")
|
|
|
@@ -44,12 +42,12 @@ public class BokApi {
|
|
|
@Value("${bok.tqIdSearchUrl}")
|
|
|
private String tqIdSearchUrl;
|
|
|
|
|
|
- public BokApi(RestRequestUtil restRequestUtil, GuavaCacheUtil cacheUtils) {
|
|
|
+ public BokQuestionApi(RestRequestUtil restRequestUtil, GuavaCacheUtils cacheUtils) {
|
|
|
this.restRequestUtil = restRequestUtil;
|
|
|
this.cacheUtils = cacheUtils;
|
|
|
}
|
|
|
|
|
|
- public Page<BokQuestion> bokFindByStemLike(String stem, Pageable pageable) {
|
|
|
+ public Page<BokQuestionVO> bokFindByStemLike(String stem, Pageable pageable) {
|
|
|
Map<String, String> urlParams = ImmutableMap.of(
|
|
|
"content", stem,
|
|
|
"page", String.valueOf(1 + pageable.getPageNumber()),
|
|
|
@@ -57,18 +55,18 @@ public class BokApi {
|
|
|
);
|
|
|
BokSearchResult result = restRequestUtil.sendGetRequest(tqStemSearchUrl, BokSearchResult.class, urlParams);
|
|
|
|
|
|
- List<BokQuestion> bokQuestions = result.getEmbedded().getQuestions();
|
|
|
- cacheUtils.setAll(BOK_CACHE_NAME, bokQuestions.parallelStream().collect(Collectors.toMap(BokQuestion::getId, bokQuestion -> bokQuestion)));
|
|
|
- return new PageImpl<>(bokQuestions, pageable, result.getPage().getTotalElements());
|
|
|
+ List<BokQuestionVO> bokQuestionVOS = result.getEmbedded().getQuestions();
|
|
|
+ cacheUtils.setAll(BOK_QUESTION_CACHE_NAME, bokQuestionVOS.parallelStream().collect(Collectors.toMap(BokQuestionVO::getId, bokQuestion -> bokQuestion)));
|
|
|
+ return new PageImpl<>(bokQuestionVOS, pageable, result.getPage().getTotalElements());
|
|
|
}
|
|
|
|
|
|
- private static String BOK_CACHE_NAME = "BOK";
|
|
|
+ private static final String BOK_QUESTION_CACHE_NAME = "helper_bok_question";
|
|
|
|
|
|
@SuppressWarnings({"unchecked", "rawtypes"})
|
|
|
- public List<BokQuestion> bokFindByIdIn(final List<String> ids) {
|
|
|
- Map<String, BokQuestion> bokQuestionsMap = Maps.newHashMapWithExpectedSize(ids.size());
|
|
|
+ public List<BokQuestionVO> bokFindByIdIn(final List<String> ids) {
|
|
|
+ Map<String, BokQuestionVO> bokQuestionsMap = Maps.newHashMapWithExpectedSize(ids.size());
|
|
|
|
|
|
- final Map<String, BokQuestion> cacheBokQuestionsMap = (Map) cacheUtils.multiGet(BOK_CACHE_NAME, ids);
|
|
|
+ final Map<String, BokQuestionVO> cacheBokQuestionsMap = (Map) cacheUtils.multiGet(BOK_QUESTION_CACHE_NAME, ids);
|
|
|
bokQuestionsMap.putAll(cacheBokQuestionsMap);
|
|
|
|
|
|
List<String> requestIds = Lists.newArrayList(ids);
|
|
|
@@ -78,12 +76,12 @@ public class BokApi {
|
|
|
Map<String, String> urlParams = ImmutableMap.of("id", requestIds.stream().reduce((a, b) -> a + "," + b).orElse(""));
|
|
|
|
|
|
BokSearchResult result = restRequestUtil.sendGetRequest(tqIdSearchUrl, BokSearchResult.class, urlParams);
|
|
|
- List<BokQuestion> bokQuestions = result.getEmbedded().getQuestions();
|
|
|
+ List<BokQuestionVO> bokQuestionVOList = result.getEmbedded().getQuestions();
|
|
|
|
|
|
- Map<String, BokQuestion> remoteBokQuestionsMap = bokQuestions.parallelStream().collect(Collectors.toMap(BokQuestion::getId, bokQuestion -> bokQuestion));
|
|
|
+ Map<String, BokQuestionVO> remoteBokQuestionsMap = bokQuestionVOList.parallelStream().collect(Collectors.toMap(BokQuestionVO::getId, bokQuestion -> bokQuestion));
|
|
|
|
|
|
bokQuestionsMap.putAll(remoteBokQuestionsMap);
|
|
|
- cacheUtils.setAll(BOK_CACHE_NAME, (Map) remoteBokQuestionsMap);
|
|
|
+ cacheUtils.setAll(BOK_QUESTION_CACHE_NAME, (Map) remoteBokQuestionsMap);
|
|
|
}
|
|
|
|
|
|
return ids.stream()
|
|
|
@@ -91,27 +89,27 @@ public class BokApi {
|
|
|
.collect(Collectors.toList());
|
|
|
}
|
|
|
|
|
|
- public BokQuestion bokFindById(String questionId) {
|
|
|
- Object cachedBokQuestion = cacheUtils.get(BOK_CACHE_NAME, questionId);
|
|
|
- if (cachedBokQuestion instanceof BokQuestion) {
|
|
|
- return (BokQuestion) cachedBokQuestion;
|
|
|
+ public BokQuestionVO bokFindById(String questionId) {
|
|
|
+ Object cachedBokQuestion = cacheUtils.get(BOK_QUESTION_CACHE_NAME, questionId);
|
|
|
+ if (cachedBokQuestion instanceof BokQuestionVO) {
|
|
|
+ return (BokQuestionVO) cachedBokQuestion;
|
|
|
}
|
|
|
- BokQuestion bokQuestion = restRequestUtil.sendGetRequest(tqUrl + "/" + questionId, BokQuestion.class, Collections.emptyMap());
|
|
|
- cacheUtils.set(BOK_CACHE_NAME, bokQuestion.getId(), bokQuestion);
|
|
|
- return bokQuestion;
|
|
|
+ BokQuestionVO bokQuestionVO = restRequestUtil.sendGetRequest(tqUrl + "/" + questionId, BokQuestionVO.class, Collections.emptyMap());
|
|
|
+ cacheUtils.set(BOK_QUESTION_CACHE_NAME, bokQuestionVO.getId(), bokQuestionVO);
|
|
|
+ return bokQuestionVO;
|
|
|
}
|
|
|
|
|
|
- public BokQuestion createQuestion(String questionId, BaseQuestionDTO baseQuestionDTO) {
|
|
|
- BokQuestion bokQuestion = getQuestion(baseQuestionDTO);
|
|
|
- bokQuestion.setId(questionId);
|
|
|
- bokQuestion = restRequestUtil.sendPostRequest(tqUrl, bokQuestion, BokQuestion.class);
|
|
|
- return bokQuestion;
|
|
|
+ public BokQuestionVO createQuestion(String questionId, BaseQuestionDTO baseQuestionDTO) {
|
|
|
+ BokQuestionVO bokQuestionVO = getQuestion(baseQuestionDTO);
|
|
|
+ bokQuestionVO.setId(questionId);
|
|
|
+ bokQuestionVO = restRequestUtil.sendPostRequest(tqUrl, bokQuestionVO, BokQuestionVO.class);
|
|
|
+ return bokQuestionVO;
|
|
|
}
|
|
|
|
|
|
- public BokQuestion modifyQuestion(String questionId, BaseQuestionDTO baseQuestionDTO) {
|
|
|
- BokQuestion bokQuestion = getQuestion(baseQuestionDTO);
|
|
|
- bokQuestion.setId(questionId);
|
|
|
- restRequestUtil.sendPutRequest(tqUrl + "/" + questionId, bokQuestion);
|
|
|
+ public BokQuestionVO modifyQuestion(String questionId, BaseQuestionDTO baseQuestionDTO) {
|
|
|
+ BokQuestionVO bokQuestionVO = getQuestion(baseQuestionDTO);
|
|
|
+ bokQuestionVO.setId(questionId);
|
|
|
+ restRequestUtil.sendPutRequest(tqUrl + "/" + questionId, bokQuestionVO);
|
|
|
return bokFindById(questionId);
|
|
|
}
|
|
|
|
|
|
@@ -119,31 +117,31 @@ public class BokApi {
|
|
|
restRequestUtil.sendDeleteRequest(tqUrl + "/" + questionId);
|
|
|
}
|
|
|
|
|
|
- private BokQuestion getQuestion(BaseQuestionDTO baseQuestionDTO) {
|
|
|
- BokQuestion bokQuestion = new BokQuestion();
|
|
|
- bokQuestion.setType(baseQuestionDTO.getType());
|
|
|
- bokQuestion.setStem(baseQuestionDTO.getStem());
|
|
|
- bokQuestion.setKeyPoints(baseQuestionDTO.getKeyPoints());
|
|
|
- bokQuestion.setTags(baseQuestionDTO.getTags());
|
|
|
- bokQuestion.setKnowledgeId(baseQuestionDTO.getKnowledgeId());
|
|
|
+ private BokQuestionVO getQuestion(BaseQuestionDTO baseQuestionDTO) {
|
|
|
+ BokQuestionVO bokQuestionVO = new BokQuestionVO();
|
|
|
+ bokQuestionVO.setType(baseQuestionDTO.getType());
|
|
|
+ bokQuestionVO.setStem(baseQuestionDTO.getStem());
|
|
|
+ bokQuestionVO.setKeyPoints(baseQuestionDTO.getKeyPoints());
|
|
|
+ bokQuestionVO.setTags(baseQuestionDTO.getTags());
|
|
|
+ bokQuestionVO.setKnowledgeId(baseQuestionDTO.getKnowledgeId());
|
|
|
|
|
|
final QuestionType questionType = QuestionType.getQuestionType(baseQuestionDTO.getType());
|
|
|
switch (Objects.requireNonNull(questionType)) {
|
|
|
case CHOICE:
|
|
|
ChoiceQuestionDTO choiceQuestionDTO = (ChoiceQuestionDTO) baseQuestionDTO;
|
|
|
- bokQuestion.setOptions(choiceQuestionDTO.getOptions());
|
|
|
- bokQuestion.setAnswer(choiceQuestionDTO.getAnswer());
|
|
|
- bokQuestion.setAnalysis(choiceQuestionDTO.getAnalysis());
|
|
|
+ bokQuestionVO.setOptions(choiceQuestionDTO.getOptions());
|
|
|
+ bokQuestionVO.setAnswer(choiceQuestionDTO.getAnswer());
|
|
|
+ bokQuestionVO.setAnalysis(choiceQuestionDTO.getAnalysis());
|
|
|
break;
|
|
|
case TRUE_FALSE:
|
|
|
TrueOrFalseQuestionDTO trueOrFalseQuestionDTO = (TrueOrFalseQuestionDTO) baseQuestionDTO;
|
|
|
- bokQuestion.setAnswer(String.valueOf(trueOrFalseQuestionDTO.getAnswer()));
|
|
|
- bokQuestion.setAnalysis(trueOrFalseQuestionDTO.getAnalysis());
|
|
|
+ bokQuestionVO.setAnswer(String.valueOf(trueOrFalseQuestionDTO.getAnswer()));
|
|
|
+ bokQuestionVO.setAnalysis(trueOrFalseQuestionDTO.getAnalysis());
|
|
|
break;
|
|
|
default:
|
|
|
throw HelperException.of(ExceptionType.ERROR, "不支持的题目类型");
|
|
|
}
|
|
|
- return bokQuestion;
|
|
|
+ return bokQuestionVO;
|
|
|
}
|
|
|
|
|
|
@Data
|
|
|
@@ -154,7 +152,7 @@ public class BokApi {
|
|
|
|
|
|
@Data
|
|
|
private static class Embedded {
|
|
|
- private List<BokQuestion> questions = Collections.emptyList();
|
|
|
+ private List<BokQuestionVO> questions = Collections.emptyList();
|
|
|
}
|
|
|
|
|
|
@Data
|