Files
KART-SERVER-JAVA/src/main/java/com/kri/kart/server/service/QuestionService.java

164 lines
6.4 KiB
Java

package com.kri.kart.server.service;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.kri.kart.server.common.enums.ProjectType;
import com.kri.kart.server.dto.project.ProjectRefDto;
import com.kri.kart.server.dto.question.QuestionDto;
import com.kri.kart.server.persistence.repository.QuestionRepository;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import lombok.RequiredArgsConstructor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Service;
@Service
@RequiredArgsConstructor
@ConditionalOnProperty(prefix = "kri.kart.database", name = "enabled", havingValue = "true")
public class QuestionService {
private final QuestionRepository questionRepository;
private final ObjectMapper objectMapper;
public List<QuestionDto> list(ProjectRefDto project, Integer userRole) {
boolean profiles =
project.projectType() == ProjectType.KIWI && Integer.valueOf(1001).equals(userRole);
return organize(questionRepository.list(project, profiles));
}
public List<QuestionDto> organize(List<QuestionDto> questions) {
List<QuestionDto> result = new ArrayList<>();
for (QuestionDto question : questions) {
if ("open".equals(question.getQtype())) {
expandOpen(question, result);
} else if ("rank".equals(question.getQtype())) {
expandRank(question, result);
} else {
result.add(question.toBuilder().build());
expandAttributes(question, result);
}
}
return result;
}
public List<QuestionDto> detail(ProjectRefDto project, String qid, Integer userRole) {
// The old API selects the first matching QID only after composite questions are expanded.
return list(project, userRole).stream()
.filter(row -> qid.equals(row.getQid()))
.limit(1)
.toList();
}
private void expandOpen(QuestionDto question, List<QuestionDto> result) {
JsonNode labels = object(question.getLabelInfo());
JsonNode attributes = object(question.getAttrInfo());
for (int index = 1; index <= labels.size(); index++) {
JsonNode label = entry(labels, index);
String suffix = " - " + text(label.get("label"));
if (suffix.equals(" - ")) {
suffix = "";
}
QuestionDto open =
question.toBuilder()
.qid(question.getQid() + "_" + index)
.variable(question.getVariable() + "_" + index)
.title(question.getTitle() + suffix)
.explain(question.getExplain() + suffix)
.qtype("num".equals(text(label.get("option"))) ? "num" : "text")
.labelInfo(label.toString())
.build();
result.add(open);
if ("attr".equals(question.getVtype())) {
for (int attributeIndex = 1; attributeIndex <= attributes.size(); attributeIndex++) {
JsonNode attribute = entry(attributes, attributeIndex);
result.add(
open.toBuilder()
.qid(open.getQid() + "_" + attributeIndex)
.variable(open.getVariable() + "_" + attributeIndex)
.title(open.getTitle() + " - " + requiredText(attribute, "label"))
.explain(open.getExplain() + " - " + requiredText(attribute, "label"))
.attrInfo(attribute.toString())
.build());
}
}
}
}
private void expandRank(QuestionDto question, List<QuestionDto> result) {
JsonNode value = object(question.getRankInfo()).get("val");
int count = value == null || value.isNull() ? 0 : Integer.parseInt(value.asText());
for (int rank = 1; rank <= count; rank++) {
String ranks =
IntStream.rangeClosed(1, rank)
.mapToObj(Integer::toString)
.collect(Collectors.joining("+"));
String suffix = " - " + ranks + "순위 까지";
// Preserve the old string replacement, including its handling of numeric/spaced JSON val.
String rankInfo =
question
.getRankInfo()
.replace("\r\n", "")
.replace("\"val\":\"" + count + "\"", "\"val\":\"" + rank + "\"");
result.add(
question.toBuilder()
.qid(question.getQid() + "_" + rank)
.variable(question.getVariable() + "_" + rank)
.title(question.getTitle() + suffix)
.explain(question.getExplain() + suffix)
.rankInfo(rankInfo)
.build());
}
}
private void expandAttributes(QuestionDto question, List<QuestionDto> result) {
if (!"attr".equals(question.getVtype())) {
return;
}
JsonNode attributes = object(question.getAttrInfo());
for (int index = 1; index <= attributes.size(); index++) {
JsonNode attribute = entry(attributes, index);
String suffix = "_" + requiredText(attribute, "value");
result.add(
question.toBuilder()
.qid(question.getQid() + suffix)
.variable(question.getVariable() + suffix)
.title(question.getTitle() + " - " + requiredText(attribute, "label"))
.explain(question.getExplain() + " - " + requiredText(attribute, "label"))
.attrInfo(attribute.toString())
.build());
}
}
private JsonNode object(String json) {
try {
JsonNode parsed = json == null || json.isEmpty() ? null : objectMapper.readTree(json);
return parsed != null && parsed.isObject() ? parsed : objectMapper.createObjectNode();
} catch (JsonProcessingException ignored) {
// Match Common.GetJObjectFromString: invalid metadata becomes an empty object.
return objectMapper.createObjectNode();
}
}
private JsonNode entry(JsonNode object, int index) {
JsonNode value = object.get(Integer.toString(index));
if (value == null || !value.isObject()) {
throw new IllegalStateException("Missing sequential question label " + index);
}
return value;
}
private String requiredText(JsonNode object, String field) {
JsonNode value = object.get(field);
if (value == null) {
throw new IllegalStateException("Missing question label field " + field);
}
return text(value);
}
private String text(JsonNode value) {
return value == null || value.isNull() ? "" : value.asText();
}
}