JSON grid가 아닌 필드배열 처리 패치

This commit is contained in:
curry772
2026-07-06 09:23:22 +09:00
parent a1d822c2d7
commit cd5cb4c0b5
@@ -10,6 +10,7 @@ import org.apache.commons.lang3.StringUtils;
import com.eactive.eai.transformer.SystemKeys; import com.eactive.eai.transformer.SystemKeys;
import com.eactive.eai.transformer.layout.Converter; import com.eactive.eai.transformer.layout.Converter;
import com.eactive.eai.transformer.layout.Group;
import com.eactive.eai.transformer.layout.InvalidNodeException; import com.eactive.eai.transformer.layout.InvalidNodeException;
import com.eactive.eai.transformer.layout.Item; import com.eactive.eai.transformer.layout.Item;
import com.eactive.eai.transformer.layout.Types; import com.eactive.eai.transformer.layout.Types;
@@ -18,6 +19,7 @@ import com.eactive.eai.transformer.util.Logger;
import com.fasterxml.jackson.core.JsonFactory; import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.json.JsonReadFeature;
import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
@@ -37,22 +39,24 @@ public class JSONMessage extends Message {
protected JsonNode data; protected JsonNode data;
protected ObjectMapper mapper = null; protected ObjectMapper mapper = null;
protected JsonFactory factory = null; protected JsonFactory factory = null;
protected boolean bStaled = false; protected boolean bStaled = true;
public JSONMessage() { public JSONMessage() {
super(); super();
mapper = new ObjectMapper(); mapper = new ObjectMapper();
// if necessary, add proper options -> use system property config ? // if necessary, add proper options -> use system property config ?
mapper.enable(JsonGenerator.Feature.WRITE_BIGDECIMAL_AS_PLAIN); mapper.enable(JsonGenerator.Feature.WRITE_BIGDECIMAL_AS_PLAIN);
mapper.enable(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS); mapper.enable(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS);
mapper.setNodeFactory(JsonNodeFactory.withExactBigDecimals(true)); mapper.setNodeFactory(JsonNodeFactory.withExactBigDecimals(true));
factory = mapper.getFactory(); factory = mapper.getFactory();
factory.configure(JsonReadFeature.ALLOW_UNESCAPED_CONTROL_CHARS.mappedFeature(), true);
} }
@Override @Override
public void createObject() { public void createObject() {
data = mapper.createObjectNode(); // data = mapper.createObjectNode();
data = null;
} }
@Override @Override
@@ -99,50 +103,20 @@ public class JSONMessage extends Message {
} }
} }
if (this.data == null) { ObjectNode node = (ObjectNode) this.data.get(getName());
logger.error("Parsed data is null"); if(node == null) {
return;
}
//ObjectNode node = (ObjectNode) this.data.get(getName());
/*
if(node == null) {
// if layoutname not appended // if layoutname not appended
setValue(this.data, getName()); setValue(this.data, getName());
} }
else { else {
setValue(node, getName()); setValue(node, getName());
} }
*/
ObjectNode node = null;
// 변경된 부분: NullNode 검사 추가
if (this.data.has(getName()) && !this.data.get(getName()).isNull()) {
JsonNode nameNode = this.data.get(getName());
if (nameNode.isObject()) {
node = (ObjectNode) nameNode;
// 배열 데이터 처리 개선
processArrayNodes(this.data);
// 값 설정
setValue(node, getName());
} else {
logger.warn("Node with name '" + getName() + "' is not an object, skipping setValue operation");
}
} else {
// layoutname이 추가되지 않은 경우
setValue(this.data, getName());
}
} catch (Exception e) { } catch (Exception e) {
throw new MessageSetDataException(e.getMessage(), this, data, e); throw new MessageSetDataException(e.getMessage(), this, data, e);
} }
} }
public void setValue(JsonNode node, String itempPath) throws InvalidAccessException { public void setValue(JsonNode node, String itempPath) throws InvalidAccessException {
try { try {
// this.value = Converter.convert(value, getDataTypeId()); // this.value = Converter.convert(value, getDataTypeId());
/** /**
@@ -150,7 +124,6 @@ public class JSONMessage extends Message {
*/ */
HashMap validationTargets = new HashMap(); HashMap validationTargets = new HashMap();
doSetValue(node, validationTargets, itempPath); doSetValue(node, validationTargets, itempPath);
// validation // validation
@@ -176,18 +149,29 @@ public class JSONMessage extends Message {
protected void doSetValue(JsonNode node, HashMap validationTargets, String parentPath) { protected void doSetValue(JsonNode node, HashMap validationTargets, String parentPath) {
Item item = null; Item item = null;
if (node.isNull()) {
// null이면 타겟 형식에서 Grid인지 확인 해 봄
String itemPath = parentPath + "[-1]";
item = this.getItem(itemPath);
if (item != null && item instanceof Group && StringUtils.equalsAny(item.getOccType(), "*", "+")) {
item = getOrCreateItem(denomalizeXPath(parentPath));
if (item != null) {
item.setIndex(-1);
}
}
return;
}
if( node.isObject()) { if( node.isObject()) {
Iterator<String> fieldNames = node.fieldNames(); Iterator<String> fieldNames = node.fieldNames();
if(StringUtils.isNotEmpty(parentPath)) { if(StringUtils.isNotEmpty(parentPath)) {
item = getOrCreateItem(denomalizeXPath(parentPath)); item = getOrCreateItem(denomalizeXPath(parentPath));
if(SystemKeys.isValidationEnabled() && StringUtils.isNotEmpty(item.getOccRef())) { if(SystemKeys.isValidationEnabled() && StringUtils.isNotEmpty(item.getOccRef())) {
validationTargets.put(item.getParent().getItemPath() + Types.FIELD_SEPARATOR + item.getOccRef(), item); validationTargets.put(item.getParent().getItemPath() + Types.FIELD_SEPARATOR + item.getOccRef(), item);
} }
} }
while(fieldNames.hasNext()) { while(fieldNames.hasNext()) {
String fieldName = fieldNames.next(); String fieldName = fieldNames.next();
JsonNode fieldValue = node.get(fieldName); JsonNode fieldValue = node.get(fieldName);
try { try {
String path = null; String path = null;
@@ -206,58 +190,42 @@ public class JSONMessage extends Message {
} }
else if( node.isArray() ) { else if( node.isArray() ) {
ArrayNode arrayNode = (ArrayNode) node; ArrayNode arrayNode = (ArrayNode) node;
if(arrayNode.size() == 0 && StringUtils.isNotEmpty(parentPath)) {
// 배열이 문자열 값만 포함하는지 확인 try {
boolean isSimpleArray = true; // 아직 clone 되지 않았으면 생성 안함.
for (int i = 0; i < arrayNode.size(); i++) { item = this.getItem(denomalizeXPath(parentPath));
if (!arrayNode.get(i).isValueNode()) { if (item != null) {
isSimpleArray = false; // clone 후에는 -1로
break; item = getOrCreateItem(denomalizeXPath(parentPath));
if (item != null) {
item.setIndex(-1);
}
}
} catch (InvalidNodeException ie) {
// continue silently
}
}
for(int i = 0; i < arrayNode.size(); i++) {
JsonNode arrayItem = arrayNode.get(i);
if(StringUtils.isNotEmpty(parentPath)) {
try {
item = getOrCreateItem(denomalizeXPath(parentPath + "[" + i + "]"));
} catch(InvalidNodeException ie) {
continue;
}
} }
} doSetValue(arrayItem, validationTargets, parentPath + "[" + i + "]");
}
if (isSimpleArray) {
// 문자열 배열은 직접 값 설정
try {
setString(parentPath, arrayNode.toString());
} catch (Exception e) {
logger.error("Error setting simple array: " + parentPath, e);
// 개별 요소 설정
for (int i = 0; i < arrayNode.size(); i++) {
try {
String itemPath = parentPath + "[" + i + "]";
setString(itemPath, arrayNode.get(i).asText());
} catch (Exception ex) {
logger.error("Error setting array item: " + parentPath + "[" + i + "]", ex);
}
}
}
} else {
// 복잡한 배열 구조 처리
for (int i = 0; i < arrayNode.size(); i++) {
JsonNode arrayItem = arrayNode.get(i);
if (StringUtils.isNotEmpty(parentPath)) {
try {
item = getOrCreateItem(denomalizeXPath(parentPath + "[" + i + "]"));
} catch (InvalidNodeException ie) {
continue;
}
}
doSetValue(arrayItem, validationTargets, parentPath + "[" + i + "]");
}
}
} }
else { else {
String itemPath = parentPath; String itemPath = parentPath;
item = getOrCreateItem(denomalizeXPath(itemPath)); item = getOrCreateItem(denomalizeXPath(itemPath));
String value = node.asText(); String value = node.asText();
if (node.isNull()) return;
if(node == null || node.isNull()) { if(value == null || value.length() == 0) {
setObject(itemPath, null); // 실제 null 값 설정
} else if(value == null || value.length() == 0) {
setObject( itemPath, value ); setObject( itemPath, value );
} else { }
else {
int itemType = item.getDataTypeId(); int itemType = item.getDataTypeId();
try { try {
switch(itemType) { switch(itemType) {
@@ -372,7 +340,6 @@ public class JSONMessage extends Message {
String val = getStringValue(item.getValue(),item); String val = getStringValue(item.getValue(),item);
int itemType = item.getDataTypeId(); int itemType = item.getDataTypeId();
if (isMask && "Y".equals(item.getMask()) && (Types.TYPE_STRING == itemType )){ if (isMask && "Y".equals(item.getMask()) && (Types.TYPE_STRING == itemType )){
val = maskString2(val, item.getMaskLength(), item.getMaskOffset()); val = maskString2(val, item.getMaskLength(), item.getMaskOffset());
item.setValue(val); item.setValue(val);
@@ -386,7 +353,8 @@ public class JSONMessage extends Message {
setJsonArrayValue(arr, item); setJsonArrayValue(arr, item);
obj.set(prefix+item.getName(), arr); obj.set(prefix+item.getName(), arr);
}else{ }else{
arr.add(value); // arr.add(value);
setJsonArrayValue(arr, item); //IBK에서 사용.
} }
} else { //FIELD } else { //FIELD
@@ -418,7 +386,7 @@ public class JSONMessage extends Message {
logger.error("not support parent type JSONArray"); logger.error("not support parent type JSONArray");
throw new Exception("not support parent type JSONArray"); throw new Exception("not support parent type JSONArray");
} }
if (item.getChildCount() > 0){ if (item.getChildCount() > 0 ){
stack.push(current); stack.push(current);
} }
break; break;
@@ -448,33 +416,6 @@ public class JSONMessage extends Message {
node.putNull(fieldName); node.putNull(fieldName);
return; return;
} }
if(value instanceof JsonNode) {
node.set(fieldName, (JsonNode) value);
return;
}
// New code to check if string is JSON array
if (itemType == Types.TYPE_STRING && value instanceof String) {
String strValue = (String) value;
if ((strValue.startsWith("[") && strValue.endsWith("]")) ||
(strValue.startsWith("{") && strValue.endsWith("}"))) {
try {
// Try to parse as JSON
JsonNode jsonNode = mapper.readTree(strValue);
node.set(fieldName, jsonNode);
return;
} catch (Exception e) {
// Not valid JSON, continue with normal processing
logger.debug("Failed to parse as JSON, using as string: " + e.getMessage());
}
}
}
if (!(value instanceof byte[])) {
logger.info("value : " + value.toString());
}
switch(itemType) { switch(itemType) {
case Types.TYPE_INT : case Types.TYPE_INT :
case Types.TYPE_SHORT : case Types.TYPE_SHORT :
@@ -484,8 +425,9 @@ public class JSONMessage extends Message {
node.put(fieldName, (long) value); node.put(fieldName, (long) value);
break; break;
case Types.TYPE_BIGDECIMAL : case Types.TYPE_BIGDECIMAL :
BigDecimal bd = (BigDecimal)value; // BigDecimal bd = (BigDecimal)value;
node.put(fieldName, bd); // node.put(fieldName, bd.floatValue());
node.put(fieldName, (BigDecimal)value);
break; break;
case Types.TYPE_DOUBLE : case Types.TYPE_DOUBLE :
node.put(fieldName, (double) value); node.put(fieldName, (double) value);
@@ -497,12 +439,7 @@ public class JSONMessage extends Message {
node.put(fieldName, (boolean) value); node.put(fieldName, (boolean) value);
break; break;
case Types.TYPE_STRING : case Types.TYPE_STRING :
node.put(fieldName, (String) value); node.put(fieldName, (String) value);
if (value instanceof JsonNode) {
logger.info("value : " + (String) value );
}
break; break;
default : default :
node.put(fieldName, (String) value); node.put(fieldName, (String) value);
@@ -524,13 +461,13 @@ public class JSONMessage extends Message {
arrayNode.add( (long) value); arrayNode.add( (long) value);
break; break;
case Types.TYPE_BIGDECIMAL : case Types.TYPE_BIGDECIMAL :
arrayNode.add( (float) value); arrayNode.add( (BigDecimal)value);
break; break;
case Types.TYPE_DOUBLE : case Types.TYPE_DOUBLE :
arrayNode.add( (double) value); arrayNode.add( (double) value);
break; break;
case Types.TYPE_FLOAT : case Types.TYPE_FLOAT :
arrayNode.add( (float) value); arrayNode.add( (float) value);
break; break;
case Types.TYPE_BOOLEAN : case Types.TYPE_BOOLEAN :
arrayNode.add( (boolean) value); arrayNode.add( (boolean) value);
@@ -539,7 +476,7 @@ public class JSONMessage extends Message {
arrayNode.add( (String) value); arrayNode.add( (String) value);
break; break;
default : default :
arrayNode.add( (String) value); arrayNode.add( (String) value);
} }
} }
@@ -583,8 +520,7 @@ public class JSONMessage extends Message {
} }
@Override @Override
public Object getData() { public Object getData() {
if (bStaled || this.data == null) {
if (bStaled) {
try { try {
this.data = getJSONData(); this.data = getJSONData();
} catch(Exception e) { } catch(Exception e) {
@@ -604,7 +540,6 @@ public class JSONMessage extends Message {
return null; return null;
} }
if(this.data == null) return null; if(this.data == null) return null;
if(isPretty) return data.toPrettyString(); if(isPretty) return data.toPrettyString();
else return data.toString(); else return data.toString();
} }
@@ -703,6 +638,36 @@ public class JSONMessage extends Message {
} }
} }
private boolean isValid(String value,int length,int pointLength){
String[] data = value.replaceAll(" ", "").split("[.]");
if (pointLength > 0 ){
if (data.length ==1){
if (data[0].length() > length - pointLength - 1 ){ //정수부 오류
return false;
}
}else if (data.length ==2){
if (data[0].length() > length - pointLength - 1 ){ //정수부 오류
return false;
}
if (data[1].length() > pointLength ){ //실수부 오류
return false;
}
}else{
//오류
return false;
}
}else{
if (data.length != 1){ //소수점이 들어와서 오류
return false;
}
if (data[0].length() > length - pointLength - 1){ //정수부 오류
return false;
}
}
return true;
}
// private Object toJsonObjectType(int itemType, String value) { // private Object toJsonObjectType(int itemType, String value) {
// Object jsonValueObject = null; // Object jsonValueObject = null;
// //
@@ -736,70 +701,11 @@ public class JSONMessage extends Message {
return getName() + " (" + MESSAGE_NAME + ")"; return getName() + " (" + MESSAGE_NAME + ")";
} }
// 배열 처리 메소드
private void processArrayNodes(JsonNode rootNode) {
Iterator<String> fieldNames = rootNode.fieldNames();
while (fieldNames.hasNext()) {
String fieldName = fieldNames.next();
JsonNode node = rootNode.get(fieldName);
// data_part와 같은 객체 노드 처리
if (node.isObject()) {
processArrayNodes(node);
}
// 배열 노드 처리
else if (node.isArray()) {
ArrayNode arrayNode = (ArrayNode) node;
// 배열 노드 처리
if (arrayNode.size() > 0) {
// 배열 데이터 설정
try {
if(this.data instanceof ObjectNode) {
ObjectNode objNode = (ObjectNode) this.data;
objNode.set(fieldName, arrayNode);
} else if (rootNode instanceof ObjectNode) {
((ObjectNode) rootNode).set(fieldName, arrayNode);
}
for(int i=0; i < arrayNode.size(); i++) {
JsonNode itemNode = arrayNode.get(i);
String itemPath = fieldName + "[" + i + "]";
if(itemNode.isObject()) {
processArrayNodes(itemNode);
}
}
} catch (Exception e) {
logger.error("Error setting array data for field: " + fieldName, e);
// 배열 요소를 개별적으로 처리
for (int i = 0; i < arrayNode.size(); i++) {
try {
String itemPath = fieldName + "[" + i + "]";
JsonNode itemNode = arrayNode.get(i);
if (itemNode.isTextual()) {
setString(itemPath, itemNode.asText());
} else {
setString(itemPath, itemNode.toString());
}
} catch (Exception ex) {
logger.error("Error setting array item [" + i + "] for field: " + fieldName, ex);
}
}
}
}
}
}
}
public String toString(){ public String toString(){
// if staled -> reorganize data // if staled -> reorganize data
String jsonString = null; String jsonString = null;
try { try {
jsonString = (String)getData(); jsonString = toLogString();
} catch (Exception e) { } catch (Exception e) {
logger.error("[JSONMessage] " + e.getMessage(), e); logger.error("[JSONMessage] " + e.getMessage(), e);
} }