메타데이터 로드 폴더 방식 추가

This commit is contained in:
2025-06-16 15:40:22 +09:00
parent b1fbd556e2
commit 1784c750f3
2 changed files with 74 additions and 28 deletions

View File

@@ -11,9 +11,12 @@ import org.springframework.stereotype.Component;
import java.io.*; import java.io.*;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.stream.Stream;
@Component @Component
@Slf4j @Slf4j
@@ -27,43 +30,86 @@ public class JsonFileReader {
this.objectMapper = new ObjectMapper(); this.objectMapper = new ObjectMapper();
} }
public List<Map<String, Object>> readJsonFile(String fileName, String listName) { public List<Map<String, Object>> readJsonFile(String metaName, String listName) {
List<Map<String, Object>> resultMap = new ArrayList<>(); List<Map<String, Object>> resultMap = new ArrayList<>();
try { try {
File file = new File(metaDataPath, fileName); File metaFile = new File(metaDataPath, metaName);
log.info("loadDataFromJsonFile path: {}", file.getPath()); log.info("Trying to load data from: {}", metaFile.getPath());
if (!file.exists()) { if (metaFile.isDirectory()) {
log.error("File not found: {}", fileName); // 디렉토리인 경우 모든 JSON 파일을 읽어서 결과를 합침
return null; log.info("Found directory: {}, reading all JSON files", metaFile.getPath());
try (Stream<Path> paths = Files.walk(metaFile.toPath())) {
List<File> jsonFiles = paths
.filter(Files::isRegularFile)
.filter(p -> p.toString().toLowerCase().endsWith(".json"))
.map(Path::toFile)
.toList();
if (jsonFiles.isEmpty()) {
log.warn("No JSON files found in directory: {}", metaFile.getPath());
return null;
}
for (File jsonFile : jsonFiles) {
log.info("Processing JSON file: {}", jsonFile.getPath());
List<Map<String, Object>> fileResult = parseJsonFile(jsonFile, listName);
if (fileResult != null) {
resultMap.addAll(fileResult);
}
}
}
} else {
// 일반 파일인 경우
String jsonFileName = metaName + "Data.json";
metaFile = new File(metaDataPath, jsonFileName);
log.info("Original path not found, trying: {}", metaFile.getPath());
if (!metaFile.exists()) {
log.error("Neither directory nor file found: {} or {}", metaName, jsonFileName);
return null;
}
return parseJsonFile(metaFile, listName);
} }
} catch (IOException e) {
log.error("Failed to read from path: {}", metaName, e);
throw new MetaDataException("Failed to read from path: " + metaName, e);
}
return resultMap;
}
private List<Map<String, Object>> parseJsonFile(File file, String listName) {
try {
log.debug("Parsing JSON file: {}", file.getPath());
String fileContent = readFileAsString(file); String fileContent = readFileAsString(file);
JsonNode rootNode = objectMapper.readTree(fileContent); JsonNode rootNode = objectMapper.readTree(fileContent);
JsonNode listNode = rootNode.get(listName); JsonNode listNode = rootNode.get(listName);
if (listNode == null) { if (listNode == null) {
throw new MetaDataException("List name not found: " + listName); log.warn("List name '{}' not found in file: {}", listName, file.getPath());
return null;
} }
resultMap = objectMapper.readValue( List<Map<String, Object>> resultMap = objectMapper.readValue(
listNode.toString(), listNode.toString(),
new TypeReference<>() { new TypeReference<>() {}
}
); );
log.debug("Successfully parsed JSON file: {}, items count: {}", fileName, resultMap.size()); log.debug("Successfully parsed JSON file: {}, items count: {}", file.getName(), resultMap.size());
return resultMap;
} catch (JsonProcessingException e) { } catch (JsonProcessingException e) {
log.error("Failed to parse JSON from file: {}", fileName, e); log.error("Failed to parse JSON from file: {}", file.getName(), e);
throw new MetaDataException("Failed to parse JSON from file: " + fileName, e); throw new MetaDataException("Failed to parse JSON from file: " + file.getName(), e);
} catch (IOException e) { } catch (IOException e) {
log.error("Failed to read file: {}", fileName, e); log.error("Failed to read file: {}", file.getName(), e);
throw new MetaDataException("Failed to read file: " + fileName, e); throw new MetaDataException("Failed to read file: " + file.getName(), e);
} }
return resultMap;
} }
private String readFileAsString(File file) throws IOException { private String readFileAsString(File file) throws IOException {

View File

@@ -3,17 +3,17 @@ package com.caliverse.admin.domain.entity;
public enum EMetaData { public enum EMetaData {
NONE("", false), NONE("", false),
ITEM_DATA("ItemData.json", true), ITEM_DATA("Item", true),
CLOTH_TYPE_DATA("ClothEquipTypeData.json", true), CLOTH_TYPE_DATA("ClothEquipType", true),
TOOL_DATA("ToolData.json", true), TOOL_DATA("Tool", true),
BAN_WORD_DATA("BanWordData.json", true), BAN_WORD_DATA("BanWord", true),
TEXT_STRING_DATA("TextStringData.json", true), TEXT_STRING_DATA("TextString", true),
QUEST_DATA("QuestData.json", true), QUEST_DATA("Quest", true),
LAND_DATA("LandData.json", true), LAND_DATA("Land", true),
BUILDING_DATA("BuildingData.json", true), BUILDING_DATA("Building", true),
BATTLE_CONFIG_DATA("BattleFFAConfigData.json", true), BATTLE_CONFIG_DATA("BattleFFAConfig", true),
BATTLE_REWARD_DATA("BattleFFARewardData.json", true), BATTLE_REWARD_DATA("BattleFFAReward", true),
SYSTEM_MAIL_DATA("SystemMailData.json", true) SYSTEM_MAIL_DATA("SystemMail", true)
; ;