KnowledgeApi.java 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. package nju.seec.helper.api.bok;
  2. import lombok.AllArgsConstructor;
  3. import lombok.Data;
  4. import nju.seec.helper.util.Consts;
  5. import nju.seec.helper.util.RestRequestUtil;
  6. import nju.seec.helper.util.cache.GuavaCacheUtils;
  7. import nju.seec.helper.vo.knowledge.BokKnowledgeNodeVO;
  8. import org.springframework.beans.factory.annotation.Value;
  9. import org.springframework.stereotype.Component;
  10. import java.util.List;
  11. /**
  12. * @author cst
  13. */
  14. @Component
  15. public class KnowledgeApi {
  16. private final RestRequestUtil restRequestUtil;
  17. private final GuavaCacheUtils cacheUtils;
  18. @Value("${bok.knowledgeUrl}")
  19. private String knowledgeUrl;
  20. @Value("${bok.knowledgeGetAllUrl}")
  21. private String knowledgeGetAllUrl;
  22. private static final String BOK_KNOWLEDGE_CACHE_NAME = Consts.SYS_NAME + "_bok_knowledge";
  23. public KnowledgeApi(RestRequestUtil restRequestUtil, GuavaCacheUtils cacheUtils) {
  24. this.restRequestUtil = restRequestUtil;
  25. this.cacheUtils = cacheUtils;
  26. }
  27. @SuppressWarnings("unchecked")
  28. public List<BokKnowledgeNodeVO> getAllKnowledgeNodes() {
  29. Object cache = cacheUtils.get(BOK_KNOWLEDGE_CACHE_NAME, "all");
  30. if (cache instanceof List) {
  31. return (List<BokKnowledgeNodeVO>) cache;
  32. }
  33. List<BokKnowledgeNodeVO> bokKnowledgeNodeVOList = restRequestUtil.sendPostRequest(knowledgeGetAllUrl, KnowledgeGetDTO.of("", "part of"), List.class);
  34. cacheUtils.set(BOK_KNOWLEDGE_CACHE_NAME, "all", bokKnowledgeNodeVOList);
  35. return bokKnowledgeNodeVOList;
  36. }
  37. @AllArgsConstructor(staticName = "of")
  38. @Data
  39. private static class KnowledgeGetDTO {
  40. private String name;
  41. private String relation;
  42. }
  43. }