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

ftl.rest.$$RestModelFromJsonConverterTemplateftl Maven / Gradle / Ivy

The newest version!
<#include "../common-lib.javaftl">
<#-- -------------------------------------------------------------------------------------------------------- -->
public final class ${JAVA_MODEL_CLASS.modelFromJsonConverterImplSimpleClassName} extends ModelFromJsonConverter<${JAVA_MODEL_CLASS.javaSimpleClassName}> {
    <#if HAS_PARENT>

    private final ${PARENT} parentConverter =
            new ${PARENT}();
    
    <#list JAVA_MODEL_CONVERTER_CHILDREN as CHILD>

    private final ${CHILD.modelFromJsonConverterImplSimpleClassName} ${CHILD.fromJsonConverterInstanceName} =
            new ${CHILD.modelFromJsonConverterImplSimpleClassName}();
    
<#-- ---------------------------------------------------------------------------------------------------------------------------------------------------------- -->
    <#if JAVA_MODEL_CLASS.modelClassReturnedByRestMethod>

    @Override
    public ${JAVA_MODEL_CLASS.javaSimpleClassName} fromJsonObject(final Map params) {
        final ${JAVA_MODEL_CLASS.javaSimpleClassName} model = new ${JAVA_MODEL_CLASS.javaSimpleClassName}();
        <#if JAVA_MODEL_CLASS.paramEntriesPresentAtThisOrAnyParent>
        readParamsToModel(params, model);
        <#elseif HAS_PARENT>
        parentConverter.readParamsToModel(params, model);
        
        return model;
    }
    
    <#if JAVA_MODEL_CLASS.paramEntriesPresentAtThisOrAnyParent>

    public void readParamsToModel(final Map params,
                                  final ${JAVA_MODEL_CLASS.javaSimpleClassName} model) {
        <#if HAS_PARENT>
        parentConverter.readParamsToModel(params, model);
        
        <#list JAVA_MODEL_CLASS.paramEntries as fieldEntry>
        <@setParamFieldValue entry=fieldEntry/>
        
    }
    
}
<#-- -------------------------------------------------------------------------------------------------------- -->
<#macro setParamFieldValue entry>
<#assign param = entry.key >
    <#if entry.value.object>
        <#assign element = entry.value>
        <#if param.modelWriteAccessorType.name() == "REFLECTION">
        setFieldValue(model, "${param.fieldName}", convertToObjectIfNotNull(${element.fromJsonConverterInstanceName}, params.get("${param.modelName}"), "${param.modelName}"));
        <#elseif param.modelWriteAccessorType.name() == "DIRECT">
        model.${param.fieldName} = convertToObjectIfNotNull(${element.fromJsonConverterInstanceName}, params.get("${param.modelName}"), "${param.modelName}");
        <#elseif param.modelWriteAccessorType.name() == "JAVA_BEAN">
        model.${param.setter}(convertToObjectIfNotNull(${element.fromJsonConverterInstanceName}, params.get("${param.modelName}"), "${param.modelName}"));
        
    <#elseif entry.value.iterable>
    <#assign element = entry.value.elementModelClass>
    <#assign containerType = entry.value.containerType>
    <#if entry.value.primitiveIterable>
        <#assign M = element.primitiveType.convertMethod >
        <#if param.modelWriteAccessorType.name() == "REFLECTION">
        setFieldValue(model, "${param.fieldName}", ${M}${containerType}(params.get("${param.modelName}"), "${param.modelName}"));
        <#elseif param.modelWriteAccessorType.name() == "DIRECT">
        model.${param.fieldName} = ${M}${containerType}(params.get("${param.modelName}"), "${param.modelName}");
        <#elseif param.modelWriteAccessorType.name() == "JAVA_BEAN">
        model.${param.setter}(${M}${containerType}(params.get("${param.modelName}"), "${param.modelName}"));
        
    <#elseif entry.value.enumIterable>
        <#if param.modelWriteAccessorType.name() == "REFLECTION">
        setFieldValue(model, "${param.fieldName}", toEnum${containerType}(${element.javaSimpleClassName}.class, params.get("${param.modelName}"), "${param.modelName}"));
        <#elseif param.modelWriteAccessorType.name() == "DIRECT">
        model.${param.fieldName} = toEnum${containerType}(${element.javaSimpleClassName}.class, params.get("${param.modelName}"), "${param.modelName}");
        <#elseif param.modelWriteAccessorType.name() == "JAVA_BEAN">
        model.${param.setter}(toEnum${containerType}(${element.javaSimpleClassName}.class, params.get("${param.modelName}"), "${param.modelName}"));
        
    <#elseif entry.value.objectIterable>
        <#assign containerType = entry.value.containerType>
        <#if param.modelWriteAccessorType.name() == "REFLECTION">
        setFieldValue(model, "${param.fieldName}", convertTo${containerType}IfNotNull(${element.fromJsonConverterInstanceName}, params.get("${param.modelName}"), "${param.modelName}"));
        <#elseif param.modelWriteAccessorType.name() == "DIRECT">
        model.${param.fieldName} = convertTo${containerType}IfNotNull(${element.fromJsonConverterInstanceName}, params.get("${param.modelName}"), "${param.modelName}");
        <#elseif param.modelWriteAccessorType.name() == "JAVA_BEAN">
        model.${param.setter}(convertTo${containerType}IfNotNull(${element.fromJsonConverterInstanceName}, params.get("${param.modelName}"), "${param.modelName}"));
        
    
    <#elseif entry.value.primitive>
        <#assign M = entry.value.primitiveType.convertMethod >
        <#if param.modelWriteAccessorType.name() == "REFLECTION">
        setFieldValue(model, "${param.fieldName}", ${M}(params.get("${param.modelName}"), "${param.modelName}"));
        <#elseif param.modelWriteAccessorType.name() == "DIRECT">
        model.${param.fieldName} = ${M}(params.get("${param.modelName}"), "${param.modelName}");
        <#elseif param.modelWriteAccessorType.name() == "JAVA_BEAN">
        model.${param.setter}(${M}(params.get("${param.modelName}"), "${param.modelName}"));
        
    <#elseif entry.value.enum>
        <#if param.modelWriteAccessorType.name() == "REFLECTION">
        setFieldValue(model, "${param.fieldName}", toEnum(${entry.value.javaSimpleClassName}.class, params.get("${param.modelName}"), "${param.modelName}"));
        <#elseif param.modelWriteAccessorType.name() == "DIRECT">
        model.${param.fieldName} = toEnum(${entry.value.javaSimpleClassName}.class, params.get("${param.modelName}"), "${param.modelName}");
        <#elseif param.modelWriteAccessorType.name() == "JAVA_BEAN">
        model.${param.setter}(toEnum(${entry.value.javaSimpleClassName}.class, params.get("${param.modelName}"), "${param.modelName}"));
        
    

<#-- -------------------------------------------------------------------------------------------------------- -->




© 2015 - 2024 Weber Informatics LLC | Privacy Policy