All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.regnosys.rosetta.translate.datamodel.json.schema.JsonSchemaToClasses Maven / Gradle / Ivy

There is a newer version: 11.25.1
Show newest version
package com.regnosys.rosetta.translate.datamodel.json.schema;

import static com.regnosys.rosetta.common.util.StringExtensions.toFirstLower;
import static com.regnosys.rosetta.common.util.StringExtensions.toFirstUpper;

import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import com.regnosys.rosetta.translate.datamodel.Attribute;
import com.regnosys.rosetta.translate.datamodel.Schema;
import com.regnosys.rosetta.translate.datamodel.json.JsonAttribute;
import com.regnosys.rosetta.translate.datamodel.json.JsonEntity;
import com.regnosys.rosetta.translate.datamodel.json.schema.JavaFileWriter.PojoAttribute;
import com.regnosys.rosetta.translate.datamodel.json.schema.JavaFileWriter.PojoClass;

public class JsonSchemaToClasses {
	
	private static final List PRIMITIVE_TYPES = Arrays.asList("string", "integer", "boolean");
	
	private final Map entityTypeToClassNameMap;
	private final Map entityNameToClassNameMap;
	
	public JsonSchemaToClasses(Map entityTypeToClassNameMap, Map entityNameToClassNameMap) {
		this.entityTypeToClassNameMap = entityTypeToClassNameMap;
		this.entityNameToClassNameMap = entityNameToClassNameMap;
	}
	
	public Set translate(Schema jsonSchema, String topClassName) {
		Map visitor = new HashMap<>();
		createClass(topClassName, jsonSchema.getAttributes(), "container", topClassName, visitor);
		return new HashSet<>(visitor.values());
	}
	
	private PojoClass createClass(JsonEntity entity, String parentName, Map visitor) {
		String className = 
				entityTypeToClassNameMap.getOrDefault(entity.getType().toLowerCase(), 
				entityNameToClassNameMap.getOrDefault(entity.getName().getName().toLowerCase(), 
						entity.getName().getName()));
		return createClass(className, entity.getAttributes(), entity.getType(), parentName, visitor);			
	}
	
	private PojoClass createClass(String name, Collection attributes, String entityType, String parentName, Map visitor) {
		List properties = attributes.stream()
				.map(JsonAttribute.class::cast)
				.map(a -> createAttribute(a, parentName, visitor))
				.collect(Collectors.toList());

		String className = toFirstUpper(toCamelCase(name));
		PojoClass pojoClass = new PojoClass(className, properties);
		
		// check for any duplicate classes, and prepend with parent class name if there's a name clash
		PojoClass existingPojoClass = visitor.get(className);
		if (existingPojoClass != null && !pojoClass.equals(existingPojoClass)) {
			className = toFirstUpper(toCamelCase(parentName)) + className;
			pojoClass = new PojoClass(className, properties);
		}
		
		visitor.put(className, pojoClass);
		
		return pojoClass;	
	}
	
	private PojoAttribute createAttribute(JsonAttribute attribute, String parentName, Map visitor) {
		JsonEntity entity = (JsonEntity) attribute.getType();
		String jsonProperty = attribute.getName();
		String formattedName = toCamelCase(jsonProperty);

		if (entity.hasData() || !entity.hasChild()) {
			String entityType = entity.getType().toLowerCase();
			String primitiveType = PRIMITIVE_TYPES.contains(entityType) ? entityType : "String";
			return new PojoAttribute(jsonProperty, formattedName, primitiveType, attribute.getCardinality());
		} else {
			PojoClass attributeClass = createClass(entity, parentName, visitor);
			return new PojoAttribute(jsonProperty, formattedName, attributeClass.getName(), attribute.getCardinality());
		}
	}
	
	private String toCamelCase(String name) {
        char[] chars = name.toCharArray();
        for (int i = 0; i < chars.length; i++) {
            char c = chars[i];
            if (!(Character.isLetter(c) || Character.isDigit(c))) {
                chars[i] = '_';
                if (i + 1 < chars.length) {
                    chars[i + 1] = Character.toUpperCase(chars[i + 1]);
                }
            }
        }
        return toFirstLower(String.valueOf(chars).replaceAll("_", ""));
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy