{{>licenseInfo}}
{{#models}}{{#model}}

#include "{{classname}}.h"

{{#modelNamespaceDeclarations}}
namespace {{this}} {
{{/modelNamespaceDeclarations}}

{{classname}}::{{classname}}()
{
    {{#vars}}{{#isNotContainer}}{{#isPrimitiveType}}m_{{name}} = {{{defaultValue}}};
    {{/isPrimitiveType}}{{^isPrimitiveType}}{{#isString}}m_{{name}} = {{{defaultValue}}};
    {{/isString}}{{#isDateTime}}m_{{name}} = {{{defaultValue}}};
    {{/isDateTime}}{{/isPrimitiveType}}{{/isNotContainer}}{{^required}}m_{{name}}IsSet = false;
    {{/required}}{{/vars}}
}

{{classname}}::~{{classname}}()
{
}

void {{classname}}::validate()
{
    // TODO: implement validation
}

web::json::value {{classname}}::toJson() const
{
    web::json::value val = web::json::value::object();

    {{#vars}}{{#isPrimitiveType}}{{^isListContainer}}{{^required}}if(m_{{name}}IsSet)
    {
        val[U("{{baseName}}")] = ModelBase::toJson(m_{{name}});
    }
    {{/required}}{{#required}}val[U("{{baseName}}")] = ModelBase::toJson(m_{{name}});
    {{/required}}{{/isListContainer}}{{/isPrimitiveType}}{{#isListContainer}}{
        std::vector<web::json::value> jsonArray;
        for( auto& item : m_{{name}} )
        {
            jsonArray.push_back(ModelBase::toJson(item));
        }
        {{#required}}val[U("{{baseName}}")] = web::json::value::array(jsonArray);
        {{/required}}{{^required}}
        if(jsonArray.size() > 0)
        {
            val[U("{{baseName}}")] = web::json::value::array(jsonArray);
        }
        {{/required}}
    }
    {{/isListContainer}}{{^isListContainer}}{{^isPrimitiveType}}{{^required}}if(m_{{name}}IsSet)
    {
        val[U("{{baseName}}")] = ModelBase::toJson(m_{{name}});
    }
    {{/required}}{{#required}}val[U("{{baseName}}")] = ModelBase::toJson(m_{{name}});
    {{/required}}{{/isPrimitiveType}}{{/isListContainer}}{{/vars}}

    return val;
}

void {{classname}}::fromJson(web::json::value& val)
{
    {{#vars}}{{#isPrimitiveType}}{{^isListContainer}}{{^required}}if(val.has_field(U("{{baseName}}")))
    {
        {{setter}}(ModelBase::{{baseType}}FromJson(val[U("{{baseName}}")]));
    }
    {{/required}}{{#required}}{{setter}}(ModelBase::{{baseType}}FromJson(val[U("{{baseName}}")]));
    {{/required}}{{/isListContainer}}{{/isPrimitiveType}}{{#isListContainer}}{
        m_{{name}}.clear();
        std::vector<web::json::value> jsonArray;
        {{^required}}if(val.has_field(U("{{baseName}}")))
        {
        {{/required}}
        for( auto& item : val[U("{{baseName}}")].as_array() )
        {
            {{#isPrimitiveType}}m_{{name}}.push_back(ModelBase::{{items.baseType}}FromJson(item));
            {{/isPrimitiveType}}{{^isPrimitiveType}}{{#items.isString}}m_{{name}}.push_back(ModelBase::stringFromJson(item));
            {{/items.isString}}{{^items.isString}}{{#items.isDateTime}}m_{{name}}.push_back(ModelBase::dateFromJson(item));
            {{/items.isDateTime}}{{^items.isDateTime}}
            if(item.is_null())
            {
                m_{{name}}.push_back( {{{items.datatype}}}(nullptr) );
            }
            else
            {
                {{{items.datatype}}} newItem({{{items.defaultValue}}});
                newItem->fromJson(item);
                m_{{name}}.push_back( newItem );
            }
            {{/items.isDateTime}}{{/items.isString}}{{/isPrimitiveType}}
        }
        {{^required}}
        }
        {{/required}}
    }
    {{/isListContainer}}{{^isListContainer}}{{^isPrimitiveType}}{{^required}}if(val.has_field(U("{{baseName}}")))
    {
        {{#isString}}{{setter}}(ModelBase::stringFromJson(val[U("{{baseName}}")]));
        {{/isString}}{{^isString}}{{#isDateTime}}{{setter}}(ModelBase::dateFromJson(val[U("{{baseName}}")]));
        {{/isDateTime}}{{^isDateTime}}if(!val[U("{{baseName}}")].is_null())
        {
            {{{datatype}}} newItem({{{defaultValue}}});
            newItem->fromJson(val[U("{{baseName}}")]);
            {{setter}}( newItem );
        }
        {{/isDateTime}}{{/isString}}
    }
    {{/required}}{{#required}}{{#isString}}{{setter}}(ModelBase::stringFromJson(val[U("{{baseName}}")]));
    {{/isString}}{{^isString}}{{#isDateTime}}{{setter}}(ModelBase::dateFromJson(val[U("{{baseName}}")]));
    {{/isDateTime}}{{^isDateTime}}{{#vendorExtensions.x-codegen-file}}{{setter}}(ModelBase::fileFromJson(val[U("{{baseName}}")]));
    {{/vendorExtensions.x-codegen-file}}{{^vendorExtensions.x-codegen-file}}{{{datatype}}} new{{name}}({{{defaultValue}}});
    new{{name}}->fromJson(val[U("{{baseName}}")]);
    {{setter}}( newItem );
    {{/vendorExtensions.x-codegen-file}}{{/isDateTime}}{{/isString}}{{/required}}{{/isPrimitiveType}}{{/isListContainer}}{{/vars}}
}

void {{classname}}::toMultipart(std::shared_ptr<MultipartFormData> multipart, const utility::string_t& prefix) const
{
    utility::string_t namePrefix = prefix;
    if(namePrefix.size() > 0 && namePrefix[namePrefix.size() - 1] != U('.'))
    {
        namePrefix += U(".");
    }

    {{#vars}}{{#isPrimitiveType}}{{^isListContainer}}{{^required}}if(m_{{name}}IsSet)
    {
        multipart->add(ModelBase::toHttpContent(namePrefix + U("{{baseName}}"), m_{{name}}));
    }
    {{/required}}{{#required}}multipart->add(ModelBase::toHttpContent(namePrefix + U("{{baseName}}"), m_{{name}}));
    {{/required}}{{/isListContainer}}{{/isPrimitiveType}}{{#isListContainer}}{
        std::vector<web::json::value> jsonArray;
        for( auto& item : m_{{name}} )
        {
            jsonArray.push_back(ModelBase::toJson(item));
        }
        {{#required}}multipart->add(ModelBase::toHttpContent(namePrefix + U("{{baseName}}"), web::json::value::array(jsonArray), U("application/json")));
        {{/required}}{{^required}}
        if(jsonArray.size() > 0)
        {
            multipart->add(ModelBase::toHttpContent(namePrefix + U("{{baseName}}"), web::json::value::array(jsonArray), U("application/json")));
        }
        {{/required}}
    }
    {{/isListContainer}}{{^isListContainer}}{{^isPrimitiveType}}{{^required}}if(m_{{name}}IsSet)
    {
        {{#isString}}multipart->add(ModelBase::toHttpContent(namePrefix + U("{{baseName}}"), m_{{name}}));
        {{/isString}}{{^isString}}{{#isDateTime}}multipart->add(ModelBase::toHttpContent(namePrefix + U("{{baseName}}"), m_{{name}}));
        {{/isDateTime}}{{^isDateTime}}if (m_{{name}}.get())
        {
            m_{{name}}->toMultipart(multipart, U("{{baseName}}."));
        }
        {{/isDateTime}}{{/isString}}
    }
    {{/required}}{{#required}}{{#isString}}multipart->add(ModelBase::toHttpContent(namePrefix + U("{{baseName}}"), m_{{name}}));
    {{/isString}}{{^isString}}{{#isDateTime}}multipart->add(ModelBase::toHttpContent(namePrefix + U("{{baseName}}"), m_{{name}}));
    {{/isDateTime}}{{^isDateTime}}{{#vendorExtensions.x-codegen-file}}multipart->add(ModelBase::toHttpContent(namePrefix + U("{{baseName}}"), m_{{name}}));
    {{/vendorExtensions.x-codegen-file}}{{^vendorExtensions.x-codegen-file}}m_{{name}}->toMultipart(multipart, U("{{baseName}}."));
    {{/vendorExtensions.x-codegen-file}}{{/isDateTime}}{{/isString}}{{/required}}{{/isPrimitiveType}}{{/isListContainer}}{{/vars}}
}

void {{classname}}::fromMultiPart(std::shared_ptr<MultipartFormData> multipart, const utility::string_t& prefix)
{
    utility::string_t namePrefix = prefix;
    if(namePrefix.size() > 0 && namePrefix[namePrefix.size() - 1] != U('.'))
    {
        namePrefix += U(".");
    }

    {{#vars}}{{#isPrimitiveType}}{{^isListContainer}}{{^required}}if(multipart->hasContent(U("{{baseName}}")))
    {
        {{setter}}(ModelBase::{{baseType}}FromHttpContent(multipart->getContent(U("{{baseName}}"))));
    }
    {{/required}}{{#required}}{{setter}}(ModelBase::{{baseType}}FromHttpContent(multipart->getContent(U("{{baseName}}"))));
    {{/required}}{{/isListContainer}}{{/isPrimitiveType}}{{#isListContainer}}{
        m_{{name}}.clear();
        {{^required}}if(multipart->hasContent(U("{{baseName}}")))
        {
        {{/required}}

        web::json::value jsonArray = web::json::value::parse(ModelBase::stringFromHttpContent(multipart->getContent(U("{{baseName}}"))));
        for( auto& item : jsonArray.as_array() )
        {
            {{#isPrimitiveType}}m_{{name}}.push_back(ModelBase::{{items.baseType}}FromJson(item));
            {{/isPrimitiveType}}{{^isPrimitiveType}}{{#items.isString}}m_{{name}}.push_back(ModelBase::stringFromJson(item));
            {{/items.isString}}{{^items.isString}}{{#items.isDateTime}}m_{{name}}.push_back(ModelBase::dateFromJson(item));
            {{/items.isDateTime}}{{^items.isDateTime}}
            if(item.is_null())
            {
                m_{{name}}.push_back( {{{items.datatype}}}(nullptr) );
            }
            else
            {
                {{{items.datatype}}} newItem({{{items.defaultValue}}});
                newItem->fromJson(item);
                m_{{name}}.push_back( newItem );
            }
            {{/items.isDateTime}}{{/items.isString}}{{/isPrimitiveType}}
        }
        {{^required}}
        }
        {{/required}}
    }
    {{/isListContainer}}{{^isListContainer}}{{^isPrimitiveType}}{{^required}}if(multipart->hasContent(U("{{baseName}}")))
    {
        {{#isString}}{{setter}}(ModelBase::stringFromHttpContent(multipart->getContent(U("{{baseName}}"))));
        {{/isString}}{{^isString}}{{#isDateTime}}{{setter}}(ModelBase::dateFromHttpContent(multipart->getContent(U("{{baseName}}"))));
        {{/isDateTime}}{{^isDateTime}}if(multipart->hasContent(U("{{baseName}}")))
        {
            {{{datatype}}} newItem({{{defaultValue}}});
            newItem->fromMultiPart(multipart, U("{{baseName}}."));
            {{setter}}( newItem );
        }
        {{/isDateTime}}{{/isString}}
    }
    {{/required}}{{#required}}{{#isString}}{{setter}}(ModelBase::stringFromHttpContent(multipart->getContent(U("{{baseName}}"))));
    {{/isString}}{{^isString}}{{#isDateTime}}{{setter}}(ModelBase::dateFromHttpContent(multipart->getContent(U("{{baseName}}"))));
    {{/isDateTime}}{{^isDateTime}}{{#vendorExtensions.x-codegen-file}}{{setter}}(multipart->getContent(U("{{baseName}}")));
    {{/vendorExtensions.x-codegen-file}}{{^vendorExtensions.x-codegen-file}}{{{datatype}}} new{{name}}({{{defaultValue}}});
    new{{name}}->fromMultiPart(multipart, U("{{baseName}}."));
    {{setter}}( new{{name}} );
    {{/vendorExtensions.x-codegen-file}}{{/isDateTime}}{{/isString}}{{/required}}{{/isPrimitiveType}}{{/isListContainer}}{{/vars}}
}


{{#vars}}{{^isNotContainer}}{{{datatype}}}& {{classname}}::{{getter}}()
{
    return m_{{name}};
}
{{/isNotContainer}}{{#isNotContainer}}{{{datatype}}} {{classname}}::{{getter}}() const
{
    return m_{{name}};
}
void {{classname}}::{{setter}}({{{datatype}}} value)
{
    m_{{name}} = value;
    {{^required}}m_{{name}}IsSet = true;{{/required}}
}
{{/isNotContainer}}
{{^required}}bool {{classname}}::{{baseName}}IsSet() const
{
    return m_{{name}}IsSet;
}
void {{classname}}::unset{{name}}()
{
    m_{{name}}IsSet = false;
}
{{/required}}
{{/vars}}

{{#modelNamespaceDeclarations}}
}
{{/modelNamespaceDeclarations}}

{{/model}}
{{/models}}
