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

ftl.data.mongo.$$MongoEntityToDBConverterTemplateftl Maven / Gradle / Ivy

There is a newer version: 0.11
Show newest version
<#include "../../common-lib.javaftl">
<#-- -------------------------------------------------------------------------------------------------------- -->
public final class ${JAVA_MODEL_CLASS.entityToDBConverterImplSimpleClassName} extends ${JAVA_MODEL_CLASS.entityToDBConverterParentSimpleClassName}<${JAVA_MODEL_CLASS.javaSimpleClassName}, Document> {

    <#list JAVA_MODEL_CONVERTER_CHILDREN as CHILD>
    <#assign className=CHILD.javaSimpleClassName>
    private final ${CHILD.entityToDBConverterImplSimpleClassName} ${CHILD.toDBConverterInstanceName} =
            new ${CHILD.entityToDBConverterImplSimpleClassName}();

    
    @Override
    public Document toDB(final ${JAVA_MODEL_CLASS.javaSimpleClassName} model,
                         final boolean withId) {
        final Document document = new Document();
        <#list JAVA_MODEL_CLASS.paramEntries as fieldEntry>
        <@setParam entry=fieldEntry/>
        
        return document;
    }

    <#if ID??>
    @Override
    public Object getId(final ${JAVA_MODEL_CLASS.javaSimpleClassName} model) {
        return require(${getId()}, "Entity does not contain document id: ?", model);
    }
    <#if ID_NOT_FINAL>

    @Override
    public void setId(final Document document,
                      final ${JAVA_MODEL_CLASS.javaSimpleClassName} model) {
        final Object id = document.get("_id");
        if (id != null) {
            <#assign M = ID.value.primitiveType.convertMethod >
            <#if ID.key.modelWriteAccessorType.name() == "REFLECTION">
            setFieldValue(model, "${ID.key.fieldName}", ${M}(id, "${ID.key.fieldName}"));
            <#elseif ID.key.modelWriteAccessorType.name() == "DIRECT">
            model.${ID.key.fieldName} = ${M}(id, "${ID.key.fieldName}");
            <#elseif ID.key.modelWriteAccessorType.name() == "JAVA_BEAN">
            model.${ID.key.setter}(${M}(id, "${ID.key.fieldName}"));
            
        }
    }
    
    
}
<#-- ------------------------------------------------------------------------------------------------ -->
<#-- ---------------- https://freemarker.apache.org/docs/ref_directive_function.html ---------------- -->
<#-- ------------------------------------------------------------------------------------------------ -->
<#function getId>
<#assign RESULT>
<#if ID??>
    <#if ID.key.modelWriteAccessorType.name() == "REFLECTION">
        getFieldValue(model, "${ID.key.fieldName}")
    <#elseif ID.key.modelWriteAccessorType.name() == "DIRECT">
        model.${ID.key.fieldName}
    <#elseif ID.key.modelWriteAccessorType.name() == "JAVA_BEAN">
        model.${ID.key.getter}()
    
<#else>
    null


<#return RESULT?trim>

<#-- -------------------------------------------------------------------------------------------------------- -->
<#macro setParam entry>
<#assign param = entry.key >
<#if entry.value.object>
<#assign element = entry.value>
<#if param.modelReadAccessorType.name() == "REFLECTION">
        document.append("${param.modelName}", convertToObjectIfNotNull(${element.toDBConverterInstanceName}, (${element.javaSimpleClassName}) getFieldValue(model, "${param.fieldName}")));
<#elseif param.modelReadAccessorType.name() == "DIRECT">
        document.append("${param.modelName}", convertToObjectIfNotNull(${element.toDBConverterInstanceName}, model.${param.fieldName}));
<#elseif param.modelReadAccessorType.name() == "JAVA_BEAN">
        document.append("${param.modelName}", convertToObjectIfNotNull(${element.toDBConverterInstanceName}, model.${param.getter}()));

<#elseif entry.value.iterable>
<#assign element = entry.value.elementModelClass>
<#if entry.value.primitiveIterable || entry.value.enumIterable>
<#if param.modelReadAccessorType.name() == "REFLECTION">
        document.append("${param.modelName}", getFieldValue(model, "${param.fieldName}"));
<#elseif param.modelReadAccessorType.name() == "DIRECT">
        document.append("${param.modelName}", model.${param.fieldName});
<#elseif param.modelReadAccessorType.name() == "JAVA_BEAN">
        document.append("${param.modelName}", model.${param.getter}());

<#elseif entry.value.objectIterable>
<#if param.modelReadAccessorType.name() == "REFLECTION">
        document.append("${param.modelName}", convertToListIfNotNull(${element.toDBConverterInstanceName}, (List<${element.javaSimpleClassName}>) getFieldValue(model, "${param.fieldName}")));
<#elseif param.modelReadAccessorType.name() == "DIRECT">
        document.append("${param.modelName}", convertToListIfNotNull(${element.toDBConverterInstanceName}, model.${param.fieldName}));
<#elseif param.modelReadAccessorType.name() == "JAVA_BEAN">
        document.append("${param.modelName}", convertToListIfNotNull(${element.toDBConverterInstanceName}, model.${param.getter}()));


<#elseif entry.value.primitive || entry.value.enum>
<#if param.id>
<#if param.modelReadAccessorType.name() == "REFLECTION">
        final Object id = getFieldValue(model, "${param.fieldName}");
<#elseif param.modelReadAccessorType.name() == "DIRECT">
        final Object id = model.${param.fieldName};
<#elseif param.modelReadAccessorType.name() == "JAVA_BEAN">
        final Object id = model.${param.getter}();

        if (id != null && withId) {
            document.append("${param.modelName}", id);
        }
<#else>
<#if param.modelReadAccessorType.name() == "REFLECTION">
        document.append("${param.modelName}", getFieldValue(model, "${param.fieldName}"));
<#elseif param.modelReadAccessorType.name() == "DIRECT">
        document.append("${param.modelName}", model.${param.fieldName});
<#elseif param.modelReadAccessorType.name() == "JAVA_BEAN">
        document.append("${param.modelName}", model.${param.getter}());




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




© 2015 - 2024 Weber Informatics LLC | Privacy Policy