<%#
 Copyright 2013-2021 the original author or authors from the BegCode project.

 This file is part of the JHipster project, see https://www.jhipster.tech/
 for more information.

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

      https://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
-%>
<%_ const instanceType = restClass;
const instanceName = restInstance;
const mapper = entityInstance  + 'Mapper';
const dtoToEntity = mapper + '.' + 'toEntity';
const entityToDto = mapper + '.' + 'toDto';
const entityToDtoReference = mapper + '::' + 'toDto';
const returnPrefix = (returnDirectly && !searchEngine) ? 'return' : instanceType + ' result =';
const skipRelationships = this._reflexRelationship(relationships, entityClass);
let resultEntity;
let mapsIdEntityInstance;
let mapsIdRepoInstance;
let otherEntityName;
if (isUsingMapsId) {
    const mapsIdEntity = mapsIdAssoc.otherEntityNameCapitalized;
    mapsIdEntityInstance = mapsIdEntity.charAt(0).toLowerCase() + mapsIdEntity.slice(1);
    mapsIdRepoInstance = `${mapsIdEntityInstance}Repository`;
}
_%>
<%_ if (dtoMapstruct) {
        resultEntity = persistInstance; _%>
        <%_ if (extendData) { _%>
        Map<String, Object> extData = <%= instanceName %>.getExtData();
        <%_ } _%>
        <%= persistClass %> <%= persistInstance %> = <%= dtoToEntity %>(<%= instanceName %>);
        <%_ if (isUsingMapsId) { _%>
        <%= mapsIdAssoc.otherEntity.primaryKey.type %> <%= otherEntityName %>Id = <%= instanceName %>.get<%= mapsIdAssoc.relationshipNameCapitalized %>().get<%= primaryKey.nameCapitalized %>();
        <% if (ormTool !== 'mybatis') { %><%= mapsIdRepoInstance %>.findById(<%= otherEntityName %>Id)<% } else { %><%= mapsIdRepoInstance %>.findById(<%= otherEntityName %>Id)<% } %>.ifPresent(<% if (ormTool !== 'mybatis') { %><%= persistInstance %>::<%_ if (fluentMethods === false) { _%>set<%= mapsIdAssoc.relationshipNameCapitalized %> <%_ } else { _%><%= mapsIdAssoc.relationshipName %><%_ } _%><% } else { %><%= persistInstance %>Temp -> <%= persistInstance %>.setId(<%= otherEntityName %>Id)<% } %>);
        <%_ } _%>
        <%_ if (fieldsContainParentAndChildren) { _%>
        clearChildrenCache();
        <%_ }  _%>
        <%_ if (ormTool === 'mybatis') { _%>
        this.saveOrUpdate(<%= persistInstance %>);
        <%_ } else { _%>
        <%= persistInstance %> = <%= entityInstance %>Repository.save(<%= persistInstance %>);
        <%_ } _%>
        <%_ if (extendData) { _%>
            <%_ if (ormTool !== 'mybatis') { _%>
            <%_ if (multiModel) { %>
        <%= persistInstance %>.setExtData(extData);
        commonExtDataService.saveExtDataByModelId(<%= persistInstance %>, <%= instanceName %>.get<%= upperFirstCamelCase(multiModel)%>Id());
            <%_ } else { _%>
        <%= persistInstance %>.setExtData(extData);
        commonExtDataService.saveExtData(<%= persistInstance %>);
            <%_ } _%>
            <%_ } else { _%>
        this.saveExtData(<%= persistInstance %>.getId(), extData);
            <%_ } _%>
        <%_ } _%>
        <%= returnPrefix %> <%= entityToDto %>(<% if (ormTool !== 'mybatis') { %><%= persistInstance %><% } else { %>this.getById(<%= persistInstance %>.get<%= primaryKey.nameCapitalized %>())<% } %>);
        <%_ if (extendData && ormTool === 'mybatis') { _%>
        result.setExtData(this.getExtData(result.get<%= primaryKey.nameCapitalized %>()));
        <%_ }  _%>
    <%_ } else {
            resultEntity = 'result'; _%>
        <%_ if (isUsingMapsId) { _%>
        <%= mapsIdAssoc.otherEntity.primaryKey.type %> <%= otherEntityName %>Id = <%= instanceName %>.get<%= mapsIdAssoc.relationshipNameCapitalized %>().get<%= primaryKey.nameCapitalized %>();
        <%= mapsIdRepoInstance %>.findById(<%= otherEntityName %>Id).ifPresent(<%= instanceName %>::<%_ if (!fluentMethods) { _%>set<%= mapsIdAssoc.relationshipNameCapitalized %> <%_ } else { _%><%= otherEntityName %><%_ } _%>);
        <%_ } _%>
        <%_ if (ormTool === 'mybatis') { _%>
            <%_ if (isController) { _%>
        <%= returnPrefix %> <%= entityInstance %>Repository.saveAndGet(<%= persistInstance %>);
            <%_ } else { _%>
        saveOrUpdate(<%= persistInstance %>);
        <%= returnPrefix %> <%= entityInstance %>Repository.selectById(<%= persistInstance %>.get<%= primaryKey.nameCapitalized%>());
            <%_ } _%>
        <%_ } else { _%>
        <%= returnPrefix %> <%= entityInstance %>Repository.save(<%= persistInstance %>);
        <%_ } _%>

    <%_ }
    if (searchEngine) { _%>
        <%= entityInstance %>SearchRepository.save(<%= resultEntity %>);
        <%_ if (returnDirectly) { _%>
        return result;
        <%_ } _%>
    <%_ } _%>
