package nju.seec.helper.api.bok; import lombok.AllArgsConstructor; import lombok.Data; import nju.seec.helper.util.Consts; import nju.seec.helper.util.RestRequestUtil; import nju.seec.helper.util.cache.GuavaCacheUtils; import nju.seec.helper.vo.knowledge.BokKnowledgeNodeVO; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; import java.util.List; /** * @author cst */ @Component public class KnowledgeApi { private final RestRequestUtil restRequestUtil; private final GuavaCacheUtils cacheUtils; @Value("${bok.knowledgeUrl}") private String knowledgeUrl; @Value("${bok.knowledgeGetAllUrl}") private String knowledgeGetAllUrl; private static final String BOK_KNOWLEDGE_CACHE_NAME = Consts.SYS_NAME + "_bok_knowledge"; public KnowledgeApi(RestRequestUtil restRequestUtil, GuavaCacheUtils cacheUtils) { this.restRequestUtil = restRequestUtil; this.cacheUtils = cacheUtils; } @SuppressWarnings("unchecked") public List getKnowledgeNodes() { Object cache = cacheUtils.get(BOK_KNOWLEDGE_CACHE_NAME, "all"); if (cache instanceof List) { return (List) cache; } List bokKnowledgeNodeVOList = restRequestUtil.sendPostRequest(knowledgeGetAllUrl, KnowledgeGetDTO.of("", "part of"), List.class); cacheUtils.set(BOK_KNOWLEDGE_CACHE_NAME, "all", bokKnowledgeNodeVOList); return bokKnowledgeNodeVOList; } @AllArgsConstructor(staticName = "of") @Data private static class KnowledgeGetDTO { private String name; private String relation; } }