메타데이터 로드 폴더 방식 추가
This commit is contained in:
@@ -11,9 +11,12 @@ import org.springframework.stereotype.Component;
|
||||
|
||||
import java.io.*;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
@Component
|
||||
@Slf4j
|
||||
@@ -27,43 +30,86 @@ public class JsonFileReader {
|
||||
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<>();
|
||||
|
||||
try {
|
||||
File file = new File(metaDataPath, fileName);
|
||||
log.info("loadDataFromJsonFile path: {}", file.getPath());
|
||||
File metaFile = new File(metaDataPath, metaName);
|
||||
log.info("Trying to load data from: {}", metaFile.getPath());
|
||||
|
||||
if (!file.exists()) {
|
||||
log.error("File not found: {}", fileName);
|
||||
return null;
|
||||
if (metaFile.isDirectory()) {
|
||||
// 디렉토리인 경우 모든 JSON 파일을 읽어서 결과를 합침
|
||||
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);
|
||||
JsonNode rootNode = objectMapper.readTree(fileContent);
|
||||
JsonNode listNode = rootNode.get(listName);
|
||||
|
||||
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(),
|
||||
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) {
|
||||
log.error("Failed to parse JSON from file: {}", fileName, e);
|
||||
throw new MetaDataException("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: " + file.getName(), e);
|
||||
} catch (IOException e) {
|
||||
log.error("Failed to read file: {}", fileName, e);
|
||||
throw new MetaDataException("Failed to read file: " + fileName, e);
|
||||
log.error("Failed to read file: {}", file.getName(), e);
|
||||
throw new MetaDataException("Failed to read file: " + file.getName(), e);
|
||||
}
|
||||
|
||||
return resultMap;
|
||||
}
|
||||
|
||||
private String readFileAsString(File file) throws IOException {
|
||||
|
||||
@@ -3,17 +3,17 @@ package com.caliverse.admin.domain.entity;
|
||||
public enum EMetaData {
|
||||
|
||||
NONE("", false),
|
||||
ITEM_DATA("ItemData.json", true),
|
||||
CLOTH_TYPE_DATA("ClothEquipTypeData.json", true),
|
||||
TOOL_DATA("ToolData.json", true),
|
||||
BAN_WORD_DATA("BanWordData.json", true),
|
||||
TEXT_STRING_DATA("TextStringData.json", true),
|
||||
QUEST_DATA("QuestData.json", true),
|
||||
LAND_DATA("LandData.json", true),
|
||||
BUILDING_DATA("BuildingData.json", true),
|
||||
BATTLE_CONFIG_DATA("BattleFFAConfigData.json", true),
|
||||
BATTLE_REWARD_DATA("BattleFFARewardData.json", true),
|
||||
SYSTEM_MAIL_DATA("SystemMailData.json", true)
|
||||
ITEM_DATA("Item", true),
|
||||
CLOTH_TYPE_DATA("ClothEquipType", true),
|
||||
TOOL_DATA("Tool", true),
|
||||
BAN_WORD_DATA("BanWord", true),
|
||||
TEXT_STRING_DATA("TextString", true),
|
||||
QUEST_DATA("Quest", true),
|
||||
LAND_DATA("Land", true),
|
||||
BUILDING_DATA("Building", true),
|
||||
BATTLE_CONFIG_DATA("BattleFFAConfig", true),
|
||||
BATTLE_REWARD_DATA("BattleFFAReward", true),
|
||||
SYSTEM_MAIL_DATA("SystemMail", true)
|
||||
|
||||
;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user