JSON grid가 아닌 필드배열 처리 패치
This commit is contained in:
@@ -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,7 +39,7 @@ 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();
|
||||||
@@ -48,11 +50,13 @@ public class JSONMessage extends Message {
|
|||||||
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,13 +103,7 @@ public class JSONMessage extends Message {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.data == null) {
|
ObjectNode node = (ObjectNode) this.data.get(getName());
|
||||||
logger.error("Parsed data is null");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
//ObjectNode node = (ObjectNode) this.data.get(getName());
|
|
||||||
/*
|
|
||||||
if(node == null) {
|
if(node == null) {
|
||||||
// if layoutname not appended
|
// if layoutname not appended
|
||||||
setValue(this.data, getName());
|
setValue(this.data, getName());
|
||||||
@@ -113,36 +111,12 @@ public class JSONMessage extends Message {
|
|||||||
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,37 +190,23 @@ 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)) {
|
||||||
// 배열이 문자열 값만 포함하는지 확인
|
|
||||||
boolean isSimpleArray = true;
|
|
||||||
for (int i = 0; i < arrayNode.size(); i++) {
|
|
||||||
if (!arrayNode.get(i).isValueNode()) {
|
|
||||||
isSimpleArray = false;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (isSimpleArray) {
|
|
||||||
// 문자열 배열은 직접 값 설정
|
|
||||||
try {
|
try {
|
||||||
setString(parentPath, arrayNode.toString());
|
// 아직 clone 되지 않았으면 생성 안함.
|
||||||
} catch (Exception e) {
|
item = this.getItem(denomalizeXPath(parentPath));
|
||||||
logger.error("Error setting simple array: " + parentPath, e);
|
if (item != null) {
|
||||||
// 개별 요소 설정
|
// clone 후에는 -1로
|
||||||
for (int i = 0; i < arrayNode.size(); i++) {
|
item = getOrCreateItem(denomalizeXPath(parentPath));
|
||||||
try {
|
if (item != null) {
|
||||||
String itemPath = parentPath + "[" + i + "]";
|
item.setIndex(-1);
|
||||||
setString(itemPath, arrayNode.get(i).asText());
|
|
||||||
} catch (Exception ex) {
|
|
||||||
logger.error("Error setting array item: " + parentPath + "[" + i + "]", ex);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} catch (InvalidNodeException ie) {
|
||||||
|
// continue silently
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
// 복잡한 배열 구조 처리
|
|
||||||
for(int i = 0; i < arrayNode.size(); i++) {
|
for(int i = 0; i < arrayNode.size(); i++) {
|
||||||
JsonNode arrayItem = arrayNode.get(i);
|
JsonNode arrayItem = arrayNode.get(i);
|
||||||
|
|
||||||
if(StringUtils.isNotEmpty(parentPath)) {
|
if(StringUtils.isNotEmpty(parentPath)) {
|
||||||
try {
|
try {
|
||||||
item = getOrCreateItem(denomalizeXPath(parentPath + "[" + i + "]"));
|
item = getOrCreateItem(denomalizeXPath(parentPath + "[" + i + "]"));
|
||||||
@@ -247,17 +217,15 @@ public class JSONMessage extends Message {
|
|||||||
doSetValue(arrayItem, validationTargets, parentPath + "[" + i + "]");
|
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
|
||||||
@@ -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,7 +461,7 @@ 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);
|
||||||
@@ -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);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user